I have written some code that gets the arduino to check a MYSQL db and return a value. It then turns on/off an LED depending on the value it receives. The code seems to work but it doesnt loop, ie if I change a value in the MYSQL, I have to reset the arduino before it picks up the new value. Eventually I would put in a delay so it only checks every five mins or so.
I am very new to Arduino and the code I have written uses bits of code from examples I have found so I dont fully understand all of the functions.
Could anyone tell me what I am doing wrong?
Here is my code:
void setup()
{
Ethernet.begin(mac, ip);
Serial.begin(9600);
pinMode (LED, OUTPUT);
}
void loop()
{
if (client.connect(server, 80));
{
client.println("GET /arduino/info.php?name=BedRoom");
client.println();
}
if (client.available())
{
char c = client.read();
status = c - '0';
Serial.println(status);
if (status != 0)
{
digitalWrite(LED, HIGH);
}
else
{
digitalWrite(LED, LOW);
}
}
}
It seems unlikely that what is returned to the client is just a single character. There should be some status (200, 404, 403, 500, etc. as well as the data that the php script returns.
There is also the possibility that the PC has cached the request from the client, and is returning the same page, not actually executing the script again.
If I run it and look at the serial monitor it shows a '0' and the LED is off. If I change the MYSQL value to a 1 and reset the arduino, the serial monitor shows a '1' and the LED is on.
If I run it and look at the serial monitor it shows a '0' and the LED is off. If I change the MYSQL value to a 1 and reset the arduino, the serial monitor shows a '1' and the LED is on.
And, if you don't reset the Arduino, what happens?
Add Serial.print() statements in meaningful places to find out what is happening. For instance, in the if(client.connect()) block, print that a connection was made.
In the if(client.available()) block, print c.
You may be using up all of your connections, very quickly. When the client says that there is no more data available, you should close the connection, using client.stop().
The if(client.available()) block should really be a while(client.available()) block, and should be inside the if(client.connect()) block, as should the client.stop() call.