WEB-Server with new ARDUINO wifi shield loses connection after a while

Hi guys,

I test the new wifi-shield of Arduino. The web server loses connection to the router for several hours and is no longer accessible.
After I inserted the line if (WiFi.begin(ssid, pass) != WL_CONNECTED) connectToWifi()
I can not connect to wifi Shield

void loop() {
  //if (WiFi.begin(ssid, pass) != WL_CONNECTED) connectToWifi(); //lose the the connection
  Serial.print(status);
  Serial.println();
  if (mustReadRFID) readRFID();          //isr Read RFID         
  if (mustSwitchDoorBuzzer) do_door_buzzer();    // Web request
  
  // listen for incoming clients
  WiFiClient client = server.available();

  if (client) {
    Serial.println("if (client)");
    
    while (client.connected()) {
      if (client.available()) {

        if( client.find("GET /")) {  // search for 'GET'
          char ch = client.read(); //read one byte this ?
          memset(action, 0, 11);
          for (int i=0; i<12; i++) {
            action[i] = client.read();
          }
          action[12] = 0;

          printHeader(client);

          if ( memcmp(who_i_am, action, 11) == 0) {
            client.print("door_buzzer,door_opener");
          }
          else if (memcmp(door_buzzer, action, 11) == 0){
            mustSwitchDoorBuzzer = true;
            client.print("Action door buzzer");
          }
          else if (memcmp(door_opener, action, 11) == 0){
            client.print("Action door opener");
          }
          else client.print("Unknown Action");        
          client.println("
");

          break;
        }
      }
    }
    // give the web browser time to receive the data
    delay(1);
    // close the connection:
    client.stop();
    //Serial.println("client disonnected");
  }
}

Thanks to all. :-))

  //if (WiFi.begin(ssid, pass) != WL_CONNECTED) connectToWifi(); //lose the the connection

You really don't want to call WiFi.begin() on every pass through loop().

What, exactly, is WiFi an instance of? Snippets-are-us is down the road a ways. Here, we want to see ALL of the code.

#include <SD.h>
#include <avr/interrupt.h>
#include <SoftwareSerial.h>
#include <SPI.h>
#include <WiFi.h>


#define rxPin 2
#define txPin 3
#define ledPin 7
#define buzzerTime 3000

// for SD-Card
char* keyFileName="tags.txt";                   //File with RFID Tags from sd card
File keyFile;                 
char* wifiFileName="wifi.txt";                  //File with wifi data
File wifiFile;                 

char** rfidKeys;                                 //Keys auf der SD Card two dimensional array
int  keyCount;                                   //Anzahl der gespeicherten Karten auf der SDCard
char action[11];                                  //Action from htm String 
char who_i_am[12]    = "who_i_am___";
char door_buzzer[12] = "door_buzzer";
char door_opener[12] = "door_opener";


// for RFID
SoftwareSerial mySerial(rxPin, txPin); 
volatile boolean mustReadRFID = false;

int  val = 0; 
char code[10]; 
//char myString[] = "A92824A9AC";

int bytesread = 0;

//for Web-Server
volatile boolean mustSwitchDoorBuzzer = false;  
char ssid[] = "HARIBO3";      //  your network SSID (name) 
char pass[] = "20106628632011";   // your network password
//char ssid[] = "aha";      //  your network SSID (name) 
//char pass[] = "11111111";   // your network password

int keyIndex = 0;                 // your network key Index number (needed only for WEP)

int status = WL_IDLE_STATUS;

WiFiServer server(80);


void setup()
{
  Serial.begin(9600);

  //Serial.println("Initializing SD card...");
  pinMode(10, OUTPUT);

  if (!SD.begin(4)) {
    //Serial.println("initialization failed!");
    return;
  }
  else {
    keyFile = SD.open(keyFileName,FILE_READ);

    readkey();  // read   

    keyFile.close();
    //Serial.print("Anzahl tags:");
    //Serial.println(keyCount);
    /*
    wifiFile = SD.open(wifiFileName,FILE_READ);
     if (wifiFile) {
     Serial.println("in Wifi-File");
     //readWifiData();
     wifiFile.close();
     }
     //else Serial.println("nix Wifi-File"); 
     */
  }   



  // define pin modes for tx, rx, led pins:
  pinMode(rxPin, INPUT);    // Set rxPin as INPUT to accept SOUT from RFID pin
  pinMode(txPin, OUTPUT);   // Set txPin as OUTPUT to connect it to the RFID /ENABLE pin
  pinMode(ledPin, OUTPUT);  // Let the user know whats up

  // set the data rate for the serial ports
  mySerial.begin(2400);                 // RFID reader SOUT pin connected to Serial RX pin at 2400bps

  attachInterrupt(0, isr, CHANGE);      // Setup interrupt for SOUT on RFID reader
  digitalWrite(txPin, LOW);             // Activate the RFID reader  


  // check for the presence of the shield:
  if (WiFi.status() == WL_NO_SHIELD) {
    //Serial.println("WiFi shield not present"); 
    // don't continue:
    while(true);
  } 
  
  connectToWifi(); //connect to Wifi

}

void printWifiStatus() {
  // print the SSID of the network you're attached to:
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());

  // print your WiFi shield's IP address:
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);

  /*
  // print the received signal strength:
   long rssi = WiFi.RSSI();
   Serial.print("signal strength (RSSI):");
   Serial.print(rssi);
   Serial.println(" dBm");
   */
}

void printHeader(WiFiClient client){
  client.println("HTTP/1.1 200 OK");
  client.println("Content-Type: text/html");
  client.println();  
}

void connectToWifi(){
  // attempt to connect to Wifi network:
  //while ( status != WL_CONNECTED) { 
  for(int i=0;i<10;i++){  
    Serial.print("Attempting to connect to SSID: ");
    Serial.println(ssid);
    // Connect to WPA/WPA2 network. Change this line if using open or WEP network:    
    status = WiFi.begin(ssid, pass);
    Serial.print("Status :");
    Serial.print(status);
    Serial.println();
    if (status==WL_CONNECTED) break; 
    // wait 10 seconds for connection:
    delay(10000);
  } 
  server.begin();
  // you're connected now, so print out the status:
  printWifiStatus();  
}

void loop() {
  //if (WiFi.begin(ssid, pass) != WL_CONNECTED) connectToWifi(); //lose the the connection attempting reconnect 
  Serial.print(status);
  Serial.println();
  if (mustReadRFID) readRFID();          //isr Read RFID         
  if (mustSwitchDoorBuzzer) do_door_buzzer();    // Web request
  
  // listen for incoming clients
  WiFiClient client = server.available();

  if (client) {
    Serial.println("if (client)");
    
    while (client.connected()) {
      if (client.available()) {

        if( client.find("GET /")) {  // search for 'GET'
          char ch = client.read(); //read one byte this ?
          memset(action, 0, 11);
          for (int i=0; i<12; i++) {
            action[i] = client.read();
          }
          action[12] = 0;

          printHeader(client);

          if ( memcmp(who_i_am, action, 11) == 0) {
            client.print("door_buzzer,door_opener");
          }
          else if (memcmp(door_buzzer, action, 11) == 0){
            mustSwitchDoorBuzzer = true;
            client.print("Action door buzzer");
          }
          else if (memcmp(door_opener, action, 11) == 0){
            client.print("Action door opener");
          }
          else client.print("Unknown Action");        
          client.println("
");

          break;
        }
      }
    }
    // give the web browser time to receive the data
    delay(1);
    // close the connection:
    client.stop();
    //Serial.println("client disonnected");
  }
}

void isr () {
  mustReadRFID = true;
}

void readRFID() {
  //Serial.println("in Read");
  digitalWrite(ledPin, LOW);          // Turn off debug LED – Event triggered!
  if((val = mySerial.read()) == 10) { // check for header 
    bytesread = 0; 


    while(bytesread<10) {             // read 10 digit code 

      val = mySerial.read(); 

      if((val == 10)||(val == 13)) {  // if header or stop bytes before the 10 digit reading 
        break;                        // stop reading 
      } 
      delay(15);                       //probably not a very pure millisecond 
      code[bytesread] = val;          // add the digit           
      bytesread++;                    // ready to read next digit  
    } 

    if(bytesread == 10) {             // If 10 digit read is complete 
      //Serial.print("TAG code is: ");  // possibly a good TAG 
      //Serial.println(code);           // print the TAG code 

      digitalWrite(txPin, HIGH);      // deactivate RFID reader 
      for (int i=0; i<keyCount; i++) {
        if ( memcmp(rfidKeys[i], code, 9) == 0){
          //Serial.println("Vergleich erfolgreich hurra");
          switch_door_buzzer_on();
          break;
        }
      }
    } 
    bytesread = 0; 
    delay(500);
    digitalWrite(txPin, LOW);           // Activate the RFID reader

    mySerial.flush();
    memset(code, 0, 10);    //init the char array with 0
  }
  mustReadRFID = false;
}

void do_door_buzzer() {
  mustSwitchDoorBuzzer = false;
  switch_door_buzzer_on();
}
void switch_door_buzzer_on(){
  digitalWrite(ledPin, HIGH);     // activate door buzzer
  delay(buzzerTime);
  digitalWrite(ledPin, LOW);

}

void readkey() {
  int index = 0;
  char strValue[12];

  while (keyFile.available()) {  

    char ch = keyFile.read();      // run through file, 1 char at a time

    if( index == 10) {
      index++;
      keyCount++;
      index=0;
    }
    else index++;
  }
  // close the file:
  keyFile.close();
  //Array dimensionieren
  rfidKeys = (char**)malloc(keyCount * sizeof(char *));  //count of rfid keys

  for (int i=0;i<keyCount;i++) {
    rfidKeys[i] = ( char*) malloc( 12 );                 // 12 bytes per row
  } 
  keyFile = SD.open(keyFileName);
  keyCount = 0;
  while (keyFile.available()) {  

    char ch = keyFile.read();      // run through file, 1 char at a time
    strValue[index] = ch;
    if( index == 10) {
      index++;
      strValue[index] = 0;
      strcpy(rfidKeys[keyCount],strValue);
      keyCount++;
      memset(strValue, 0, 12);    //init the char array with 0
      index=0;
    }
    else index++;
  }  
}
  //Serial.println("Initializing SD card...");
  pinMode(10, OUTPUT);

The code has nothing to do with the statement in the Serial.println() function...

  pinMode(rxPin, INPUT);    // Set rxPin as INPUT to accept SOUT from RFID pin
  pinMode(txPin, OUTPUT);   // Set txPin as OUTPUT to connect it to the RFID /ENABLE pin

You told the SoftwareSerial instance that these were it's pins. Why are you diddling with them?

    if( index == 10) {
      index++;
      keyCount++;
      index=0;
    }

If index is 10, increment it, then set it to 0. Why?

  //Array dimensionieren
  rfidKeys = (char**)malloc(keyCount * sizeof(char *));  //count of rfid keys

  for (int i=0;i<keyCount;i++) {
    rfidKeys[i] = ( char*) malloc( 12 );                 // 12 bytes per row
  }

You are potentially mallocing more space than you have available. You never check that malloc() succeeded.

  char strValue[12];

Is this big enough to read a whole record, including the terminating NULL?

Between the SD memory allocation, the allocation here, the WiFi library memory usage, I'm wondering if you are running out of space.

The web server loses connection to the router for several hours and is no longer accessible.

For several hours? Or, after several hours?

Is your router kicking the Arduino off after some idle time? Is it expecting the Arduino to be able to reconnect?

How do you know that this has happened?

  1. //Serial.println("Initializing SD card..."); is the comment for
    Serial.begin(9600);

  2. pinMode(rxPin, INPUT); // Set rxPin as INPUT to accept SOUT from RFID pin
    pinMode(txPin, OUTPUT); // Set txPin as OUTPUT to connect it to the RFID /ENABLE pin
    these are the pins to communicate with the RFID reader, that works

  3. if( index == 10) {
    index++;
    keyCount++;
    index=0;
    }
    correct index++ is rubbish

  4. //Array dimensionieren
    rfidKeys = (char**)malloc(keyCount * sizeof(char *)); //count of rfid keys

for (int i=0;i<keyCount;i++) {
rfidKeys = ( char*) malloc( 12 ); // 12 bytes per row

  • }*
    correct, I must ask for success -> this works, i have only 5 rfid keys

5) The Webserver can not be reached after several hours/Once only after one day. Why, no idea, the router was not switched off
6) I need a new connection after a connection is terminated