You may want to try the below code to see if it works for you (modify the arduino ip address for your LAN). Your code runs in a continous loop which may cause issues.
//zoomkat 11-13-10
//simple ethernet client test code
//for use with IDE 0021 and W5100 ethernet shield
//modify the arduino lan ip address as needed
//open serial monitor to see what the arduino receives
//push the shield reset button to run client again
#include <SPI.h>
#include <Ethernet.h>
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192, 168, 1, 102 }; // Arduino IP address
byte server[] = { 208, 104, 2, 86 }; // zoomkat's web site
Client client(server, 80);
void setup()
{
Ethernet.begin(mac, ip);
Serial.begin(9600);
Serial.println("starting simple arduino client test");
Serial.println();
delay(1000);
Serial.println("connecting...");
if (client.connect()) {
Serial.println("connected");
client.println("GET /~shb/arduino.txt 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.");
Serial.println("==================================");
Serial.println("");
client.stop();
for(;;);
}
}
Ethernet.available() won't immediately return true after you send the get, so the loop will exit with the connection closed. You need to put in a delay or wait for available bytes somehow.
I suggest you put client code inside a conditional statement so that it will run only when you want it to run. Servers have to run in continous loops to lisen for client request, but running a client in a tight loop can create a lot network churn that might not be desired. Just depends on what you are actually trying to accomplish.
I suggest you put client code inside a conditional statement so that it will run only when you want it to run. Servers have to run in continous loops to lisen for client request, but running a client in a tight loop can create a lot network churn that might not be desired. Just depends on what you are actually trying to accomplish.
Instead of
if (client.available()) {
char c = client.read();
Serial.print(c);
}
I put:
delay(2000);
while (client.available()) {
char c = client.read();
Serial.print(c);
}
One is to start the connection at the beginning of loop(), and do all the processing before you exit it. Most (all??) of the Client examples do this.
If you have other things to do that are time-sensitive, though, you need to take a different approach to avoid delays while talking with the server. It's a little more complicated, but "no pain, no gain".
What you need to do is declare a global buffer, fill it up by checking available() every pass through loop(), and acting when it fills up. You also need to track the state of the connection: whether it's been initiated, is sending or receiving, etc. I don't know whether there's an example available online, but you should try using the phrase "state machine" along with Ethernet-related terms to hunt for it.