Hello,
I'm using the Arduino with an Ethernet Shield in order to poll a web server for PWM values to put through to particular pins. I'm setting it up so that the server passes its response as a byte from 0-255 to the Arduino from a GET request. The arduino can request any number of different responses and put them out to different pins. In the case of my code below, the arduino requests values for device 1 & 2 and puts those values out on pins 5 & 6. I have it working so that it gets the first value, but for some reason, when I set the polling time to 60 seconds, it doesn't poll again. When I change the polling interval to 30 seconds, it works as expected. I've removed the server information for the code below, but hopefully someone might know what's going on by looking at the code.
Thanks,
Matt
#include <Ethernet.h>
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 10, 0, 1, 177 }; //change for different local networks
byte server[] = { IP HERE }; //change for different servers
Client client(server, 80);
const int numOfDevices = 2;
int value[] = {0, 0}; //value to display on meter for each meter/device
const int device[] = {1, 2}; //device identifiers (on server)
const int meterPin[] = {5, 6}; //pins to associate with each device
int secondsDelay = 60; //polling interval
void setup() {
Serial.begin(9600);
Ethernet.begin(mac, ip);
delay(1000);
}
void loop() {
for (int i = 0; i < numOfDevices; i++) //iterate for each device
{
if (client.connect()) {
client.print("GET /projects/pwm/?device=");
client.println(device[i]);
client.println();
delay(1000);
}
else
Serial.println("Connection failed.");
do {
if (client.available()) {
Serial.println("Reading data from server...");
value[i] = client.read();
client.flush();
}
} while ( client.connected() );
if (!client.connected()) {
client.stop();
}
analogWrite(meterPin[i], value[i]);
Serial.println(value[i]);
} //end for each device
delay(secondsDelay * 1000);
}