Веб-клиент
- Войдите на сайт для отправки комментариев
Пт, 25/11/2011 - 10:45
Не удается использовать ардуину + интернет шеилд как клиент...Стандартные примеры с гуглом не работают, нарыл один пример на забугорном ресурсе, залил скетч, порт говорит, что подключено...затем он читает что вернул ему сервер (рандомне число) и идет ре-коннект.
В рунете порядка десятка проектов, которые читают прогноз погоды, пробок и т.д., у меня они работать отказались, везде условие client.connect() возвращает отрицательный результат.
Пример скетча:
#include <SPI.h>
#include <Ethernet.h>
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192,168,1,7 };
byte server[] = { 64, 233, 187, 99 }; // Google
Client client(server, 80);
void setup()
{
Ethernet.begin(mac, ip);
Serial.begin(9600);
delay(1000);
Serial.println("connecting...");
if (client.connect()) {
Serial.println("connected");
client.println("GET /search?q=arduino HTTP/1.0");
client.println();
} else {
Serial.println("connection failed");
}
}
void loop()
{
if (client.available()) {
char c = client.read();
Serial.print( c );
}
if (!client.connected()) {
Serial.println();
Serial.println("disconnecting.");
client.stop();
for(;;)
;
}
}
Может, кто выложит рабочий пример обращения ардуина к тому же Яндексу, например.
Спасибо.
UPD: получилось подключиться, виновен был айпи сервера, проверил на своём серваке - все ок. Только есть одно НО: в терминал прут одни кракозябры, при открытии html файла, состоящего из символа "4". В чем проблемма?
Пример кода:
#include <Ethernet.h> #include <SPI.h> //////////////////////////////////////////////////////////////////////// //CONFIGURE //////////////////////////////////////////////////////////////////////// byte ip[] = { 192, 168, 1, 7 }; //ip address to assign the arduino byte server[] = {*,*,*,*}; //ip Address of the server you will connect to //The location to go to on the server //make sure to keep HTTP/1.0 at the end, this is telling it what type of file it is String location = "/test.html HTTP/1.0"; //Rarly need to change this byte subnet[] = { 255, 255, 255, 0 }; // if need to change the MAC address (Very Rare) byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; Client client(server, 80); // port 80 is typical www page //////////////////////////////////////////////////////////////////////// char inString[32]; // string for incoming serial data int stringPos = 0; // string index counter boolean startRead = false; // is reading? void setup(){ Ethernet.begin(mac, ip); Serial.begin(9600); } void loop(){ String pageValue = connectAndRead(); //connect to the server and read the output Serial.println(pageValue); //print out the findings. delay(5000); //wait 5 seconds before connecting again } String connectAndRead(){ //connect to the server Serial.println("connecting..."); if (client.connect()) { Serial.println("connected"); client.print("GET "); client.println(location); client.println(); while(true){ if (client.available()) { char c = client.read(); Serial.print(c); } else break; } } else{ return "connection failed"; } }