Im using Timer1 and Ethernet shield. I want to connect to a server every 5 seconds and do http GET.
Code below works. But is it the correct way of doing it? Do I need to call client.stop() and close connection every call of 'check' method?
thanks..
#include <TimerOne.h>
#include <SPI.h>
#include <Ethernet.h>
...
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192,168,1,7);
EthernetClient client;
char server[] = "www.example.com";
String url;
void setup() {
Serial.begin(9600);
...
// start the Ethernet connection:
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
Ethernet.begin(mac, ip);
}
...
Timer1.initialize(5000000);
Timer1.attachInterrupt(check);
}
void loop() {
...
}
void check() {
...
//if there's a successful connection:
if (client.connect(server, 80)) {
client.println(...);
client.println("Host: www.example.com");
client.println("User-Agent: arduino-ethernet");
client.println("Connection: close");
client.println();
client.stop();
}
else {
Serial.println("connection to server failed...");
client.stop();
}
...
}