Hello again!
I'm trying to figure out why my ethershield appears to just give up polling an external site. My project is to poll a site at semi-regular intervals in order to decide if the Arduino should engage a solenoid. The issue I'm seeing is that after a while, the arduino no longer polls the site and stops responding to anything but the reset switch. I suspect it may be a timing issue, however I'm not certain.
void loop(){
int val = 0;
int bytesread = 0;
char code[10];
delay(1000);
//Turn on RFID reader, set LED to orange to "Neutral"
// If API says we are supposed to be unlocked, (returns 1), then
// unlocked we are.
if( ServerComms(stationID,code,1,server))
{
//LED to GREEN, Solenoid to HIGH
digitalWrite(solenoid,HIGH);
digitalWrite(redled,LOW);
digitalWrite(greenled,HIGH);
delay(5000);
}
//Else, we go through the card stuff.
else
{
//LED to ORANGE, Solenoid to LOW
digitalWrite(solenoid,LOW);
digitalWrite(rfid,LOW);
digitalWrite(redled,HIGH);
digitalWrite(greenled,HIGH);
if(Serial.available() > 0) // if data available from reader
{
if((val = Serial.read()) == 10)
{ // check for header
bytesread = 0;
digitalWrite(rfid,HIGH);
digitalWrite(redled,LOW);
digitalWrite(greenled,LOW);
while(bytesread<10)
{ // read 10 digit code
if( Serial.available() > 0)
{
val = Serial.read();
if((val == 10)||(val == 13)) // if header or stop bytes before the 10 digit reading
{
break; // stop reading
}
code[bytesread] = val; // add the digit
bytesread++; // ready to read next digit
}
}
if(bytesread == 10) // if 10 digit read is complete
{
Serial.println(code);
if( ServerComms(stationID,code,2,server))
{
Serial.println("Good Card - Unlocking goes here.");
digitalWrite(greenled,HIGH);
digitalWrite(solenoid,HIGH);
delay(2000);
digitalWrite(solenoid,LOW);
char code[10]=" ";
}
else
{
Serial.println("BAD CARD - Flashing red LED.");
for (val = 0; val < 6; val++ )
{
digitalWrite(greenled,LOW);
digitalWrite(redled,HIGH);
delay(500);
digitalWrite(redled,LOW);
delay(500);
}
}
}
bytesread = 0;
// deactivate the RFID reader for a moment so it will not flood
delay(2000); // wait for a bit
}
}
}
}
In english:
The code polls an API server, if "1", it unlocks, if 0 it checks the serial buffer for anything on the RFID reader. If nothing, then it sleeps for two seconds before checking the API server again. The ServerComms function returns either a 0 or a 1 depending on what the API returns on polling. If the Arduino is responding, swiping a "good" card results in an unlock, and swiping a "bad" card results in a flashing LED, then returns to normal polling state as expected.
I have a theory that I'm "flooding" the ethershield, however I'm still learning about how to do network communications with the ethershield that's long-term reliable. This is my first experiment with the Ethershield as a client.
debug output at time of failure:
Request:GET /ardurfid/commander.php?sta=2&mode=poll HTTP/1.0
API says YES
Request:GET /ardurfid/commander.php?sta=2&mode=poll HTTP/1.0
API says YES
Request:GET /ardurfid/commander.php?sta=2&mode=poll HTTP/1.0
API says YES
Request:GET /ardurfid/commander.php?sta=2&mode=poll HTTP/1.0
Request:GET /ardurfid/commander.php?sta=2&mode=poll HTTP/1.0
Any time I see the double REQUEST line above, I know the arduino has frozen up. How long should I wait in seconds between API polling?
What I'm trying to do is that if there is no card swiped, the device should check with the API to make sure it's supposed to be checking for cards (API=0) or supposed to be unlocked (API=1). If it's supposed to be unlocked, it should be checking the API at regular intervals (currently ~5sec) to check when it should go back to checking for cards again. If it's in checking for cards mode, it should be checking the serial buffer for a card to report to the API server(done as soon as it has a 10 character card ID) and checking for whether or not it should be unlocked (at regular intervals). The goal is to make a networked RFID door lock that uses a centralized API to manage card access.
If you need full-code or have suggestions, please let me know. I appreciate your help.