Strange crashes + Wifly shield + RFID reader

I’m building a RFID card system for my high school, where students swipe ID cards (with RFID tags implanted) at Arduinos hooked up to Parallax RFID readers. Using a Wifly shield, the Arduino then notifies the central server that the tag has been swiped (using a HTTP GET request to a php page). The system works fine when the Arduino is plugged into a computer. When it’s running on external power, however (or even a USB cord plugged into the wall), the system initially works fine, but after an hour or so (it’s not always the exact same time period) the Arduino seems to crash: the Wifly shield dissasociates from the wireless network, and the Arduino won’t communicate with the RFID reader (over a serial connection). I’ve tried having the Arduino reset itself at certain intervals, but the problem persists. This has happened on multiple boards, so I don’t think mine is defective. Any help would be greatly appreciated, and the source code is copied below.

Thanks so much.

#include <Client.h>
#include <Configuration.h>
#include <Debug.h>
#include <ParsedStream.h>
#include <Server.h>
#include <SpiUart.h>
#include <WiFly.h>
#include <WiFlyDevice.h>
#include <_Spi.h>
byte server[] = { 10, 100, 0, 36 }; 
Client client(server, 80);
int  val = 0; 
char code[10]; 
int bytesread = 0;
unsigned long time=0;

void software_Reset()
{
asm volatile ("  jmp 0");  
} 

void setup() {
  Serial.begin(2400); // RFID reader SOUT pin connected to Serial RX pin at 2400bps 
  pinMode(2,OUTPUT);   // Set digital pin 2 as OUTPUT to connect it to the RFID /ENABLE pin 
  digitalWrite(2, LOW);                  // Activate the RFID reader
  //Start up the Wifly shield
  WiFly.begin();
  SpiSerial.print("$$"); delay(100); //readFromWifly();
  SpiSerial.println(""); delay(100);
  SpiSerial.println("set wlan ssid SSIDHERE"); delay(500);
  SpiSerial.println("set wlna phrase PASSPHRASEHERE"); delay(500);
  SpiSerial.println("join SSIDHERE"); delay(500);
}

 void loop() { 
   //Every 5 minutes
  time = millis() % 300000L;
  if(time >= 0 && time <= 500) {
    software_Reset();
    delay(2000);
  }
  if(Serial.available() > 0) {          // if data available from reader 
    if((val = Serial.read()) == 10) {   // check for header 
      bytesread = 0; 
      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  
        } 
      } 
      digitalWrite(2, HIGH);             //Deactivate the RFID reader
      if(bytesread == 10) {              // if 10 digit read is complete 
        //Tell the server
        client.connect();
        client.print("GET http://10.100.0.36/rfid/toggle.php?studentid=+");
        client.print(code);
        client.print("&Submit=Submit");
        client.println();
        bytesread = 0; 
        client.stop();
        delay(3000);                       // wait for a bit 
        digitalWrite(2, LOW);                  // Activate the RFID reader
        code[0]='0'; code[1]='0'; code[2]='0'; code[3]='0'; code[4]='0'; code[5]='0'; code[6]='0'; code[7]='0'; code[8]='0'; code[9]='0';
      } 
    } 
  } 
}

cybergeek11:

code[0]='0'; code[1]='0'; code[2]='0'; code[3]='0'; code[4]='0'; code[5]='0'; code[6]='0'; code[7]='0'; code[8]='0'; code[9]='0';

Try change that part of code for this:

for (i=0; i<10; i++) 
{
   code[i] = '\0';          
}

It’s will clear the array properly! Let us know about the result!

The problem lies in the WiFly library. It expects everything to be transmitted correctly without any bit error. If there's one single bit transmitted wrongly it will hang endlessly. I used an RN-XV (which is from the same family) with an older version of the library and had similar problems. I patched it to my needs and inserted timeouts for every loop that else may be endless. After that patch my sketch worked reliably.

Your software reset doesn't work because the Arduino will not get there if it really hangs in an endless loop inside the library.

The only strange thing in your case is that it works reliably if connected to the computer. Sounds a bit like the power supply inserts some electrical noise to the system.

Get rid of this:

   //Every 5 minutes
  time = millis() % 300000L;
  if(time >= 0 && time <= 500) {
    software_Reset();
    delay(2000);
  }

Replace this:

char code[10];

with this:

const int CODE_LENGTH=10;
char code[CODE_LENGTH+1]; // +1 provides space for the terminating character

Replace this:

          code[bytesread] = val;         // add the digit           
          bytesread++;                   // ready to read next digit

with this:

          code[bytesread++] = val;         // add the digit           
          code[bytesread] = 0;                // terminate the string

In all the places in your code where you use the magic number 10 as the buffer length, replace it with CODE_LENGTH.

PeterH:
In all the places in your code where you use the magic number 10 as the buffer length, replace it with CODE_LENGTH.

Just a newbie question.... Why ? It's just to make the code clean right ?

It's just to make the code clean right ?

Suppose that you decide that the buffer needs to be 20 characters, instead. How many places do you need to change in your code?

How many after implementing PeterH's suggestions?