Hello!
I'm new to Arduino, and want to do the following.
I want simple board with a button, when i press the button i want it to do a http-request, and display contents on the mac using serial connection.
This shouldn't be too hard..
I thought I'd best approach this problem one sub-problem at the time.
My first step was to create the http-request and serial display.
I found the code below, and understand what it does..
I inserted the delay(500); pieces myself.
Problem with code below is that sometimes it can't connect to the server, I don't understand why cause it really is up. When I increase the amount of delay it seems to go better. But I want to be able to push the button (in the final) project multpile times per second....
This should be possible, shouldn't is? Could the problem be serverside? If anyone can help, it would help me a lot.
Thanks!
Code:#include <Ethernet.h>
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 10,0,0,7 };
byte server[] = {10,0,0,1};
Client client(server, 80);
enum CLIENT_STATE
{
STEP1, STEP2, STEP3, STEP4
};
static CLIENT_STATE client_state;
void setup()
{
Ethernet.begin(mac, ip);
Serial.begin(9600);
delay(1000);
}
void loop()
{
if (client_state==STEP1)
{
Serial.println("connecting...");
if (client.connect()) {
Serial.println("connected");
client.println("GET /index.html HTTP/1.0");
client.println("Host: localhost");
client.println("User-Agent: AVR ethernet");
client.println("Accept: text/html");
client.println();
client_state=STEP2;
delay(500);
} else {
Serial.println("connection failed");
delay(500);
client_state=STEP1;
}
}
if (client_state==STEP2)
{
if (client.available()) {
char c = client.read();
Serial.print(c);
client_state=STEP2;
}
else if (!client.connected())
{ Serial.println();
Serial.println("disconnecting.");
client.stop();
delay(500);
client_state=STEP1;
}
}
}