A few days ago i tried to get the WebClient example in Arduino working. After some hours of frustration I finally stopped working and googled for a solution. Soon I realized that I was not the only person having this problem. The example code does not work behind a router.
(UPDATE: It does also has issues with Lighttpd – Fix in the second example)
The solution was easy. I found it in the german book „Arduino – Physical Computing für Bastler, Designer & Geeks“. The Ethernet.beginn() method takes 2 more optional parameters:
Ethernet.begin(mac, ip, gateway, subnet);
That was not mentioned in the example code and obviously i was not the only person that had troubles because of this little mistake.
I propose to add a hint to the example or to change it, since a router is the normal case for an Arduino.
Here is my slightly modified version of the example code that finally worked 🙂
[cpp]
#include <Ethernet.h>
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192, 168, 178, 25 };
byte gateway[] = { 192, 168, 178, 1 };
byte server[] = { 74, 125, 77, 104 }; // Google
Client client(server, 80);
void setup()
{
Ethernet.begin(mac, ip, gateway);
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(;;)
;
}
}
[/cpp]
UPDATE:
The Ethernet Library does not work with Lighttpd. You can however easily circumvent this if you use a HTTP-proxy server.
You have to set server variable to the IP of the proxy and send something like the following example to the proxy. The rest works pretty much the same then..
[cpp]
client.println("GET http://www.google.com/search?q=arduino HTTP/1.0");
client.println("host: www.google.com"); // You have to send a host-header for many domains
client.println();
[/cpp]
Wenn du nun noch meinen ENC28J60 zum Laufen bekommst, gehört das Internetz bald uns 🙂
Thanks for this post, it really helped me!
🙂
J
Thank you so much for posting this solution! I was stuck for hours on this. Then found this and now it is working. I wish arduino.cc would update their code!
Danke!
Hey Paul 🙂 I’m happy that I could help. I can feel the pain 🙂 Hope that someone fixes it.
das war der Bringer !!
Vielen Dank für das Posting.
Werner