Arduino WiFi Sheild - Aduino lockup

Hi all,
I'm using the Arduino Shield and working with the "wifichatserver" example to test out interactive control via TCP/IP.

I've updated the firmware to the latest version - in fact, from the posted beta from the Arduino software V1.5 beta release to see if that fixes the issues.

But the issue is after a connect/disconnect, the arduino locks up in 5-15 minutes (I have a counter displaying every second).

Also, when I don't connect at all, it doesn't lock up, but after 15 minutes or so it will no longer accept a connection request.

If there a good way to reset the stack or whatnot after a connect/disconnect or to reset it periodically to prevent lockup or no response?

I have a R3 Uno I'm running it on... I'm working on TCP-IP monitoring & contol examples for MakerPlot graphical SCADA software. Everything is working great except for the issues posted

Thanks,
Martin

The WiFi shield server firmware is not very stable. It has been known to do weird things, like send the wrong page to a client, or send corrupted files. This normally happens when multiple clients are trying to connect at the same time. The code below shows that the server uses only one socket, so subsequent requests use the same socket, and wrong or corrupted file transfers occur.

This show the status of the wifi sockets. It is the best I can up with for you.

#include <utility/server_drv.h>

void ShowSockStatus() {
  for(int x = 0; x < MAX_SOCK_NUM; x++) {
    Serial.print(WiFi._state[x]);    
    Serial.print("  ");
    Serial.print(WiFi._server_port[x]);    
    Serial.print("  s=");
    Serial.print(serverDrv.getServerState(x));    
    Serial.print("  c=");
    Serial.print(serverDrv.getClientState(x));    
    Serial.print("  d=");
    Serial.println(serverDrv.availData(x));    
 }
}

Thanks for the information Tim, I can deal with a single socket if the shield was stable enough to keep running and active for more than 15 minutes at a time, and especially prone to failing following a connect/disconnect. I may close and reopen the wifi/server periodically to see if that helps.

I'll be adapting my PC side software to act as a server soon and maybe that will help stability greatly.

Thanks!

The problem may be the connection state. If the connection is broken, that code will not close the connection. That normally causes problems for a server if the firmware is working ok. It is up to the server to close the connection.

In other words, there is no client.stop() call in that code.

Thanks, I have tried that in my numerous tests, but I'll give it another shot just in case.

Gave client.close another shot - the new beta release version seems much better at closing automatically than prior version, but still locking up within 15 minutes. Also tried closing the server and wifi and re-initializing after each session... Led to nearly immediate crashes. Ah well, guess this won't be stable enough for any actual use. Maybe when I get around to client or UDP uses it'll be better.

I don't have any problem with the server crashing/locking, but only wrong or corrupted files. The client code seems much more stable though.

A warning about UDP. If you are using the WiFi shield as the UDP "server", it will return the response packet to the UDP "client" (sender) from a different port than was sent to by the "client". It stumped me for a while.

Thanks, I really wish I could resolve this crash issue, it works great when it's up. I'm not serving out files, just sending raw data ASCII data back and forth between PC software and Arduino. After I close a session the Arduino crashes within 15 min. * shrugs *

Maybe you should post your code. It may be something I can help you correct.

Here is my latest test, slightly modified chat example. I also just tested web server example, it too causes my Arduino to lock up 5-15 minutes after a session.

The firmware is the latest from the 1.5.6 beta installer (similar results from v1.05 r2), and my setup is an Uno R3 with the WiFi Shield and nothing else. Help? Thanks much.

#include <SPI.h>
#include <WiFi.h>

char ssid[] = "xxxx"; //  your network SSID (name)
char pass[] = "xxxx";    // your network password (use for WPA, or use as key for WEP)

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

int status = WL_IDLE_STATUS;

WiFiServer server(9001);

boolean alreadyConnected = false; // whether or not the client was connected previously

void setup() {
  //Initialize serial and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }

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

  String fv = WiFi.firmwareVersion();
  if ( fv != "1.1.0" )
    Serial.println("Please upgrade the firmware");

  // attempt to connect to Wifi network:
  while ( status != WL_CONNECTED) {
    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);

    // wait 10 seconds for connection:
    delay(10000);
  }

  // start the server:
  server.begin();
  // you're connected now, so print out the status:
  printWifiStatus();
}

int i = 0;
unsigned long time = millis();

void loop() {
  // wait for a new client:
  WiFiClient client = server.available();

  // monitor up time by displaying count of seconds idle
  if (!client && (millis() > time + 1000)) {
    i++;
    time = millis();
    Serial.println(i);
  }

  // when the client sends the first byte, say hello:
  if (client) {
      i = 0;   // reset idle counter
      client.flush();
      Serial.println("We have a new client");
      client.println("Hello, client!");

      while(client.connected()){  
         if (client.available() > 0) {
             // read the bytes incoming from the client:
             char thisChar = client.read();
             // echo the bytes back to the client:
             server.write(thisChar);
             // echo the bytes to the server as well:
             Serial.write(thisChar);
         }         
      }
      client.stop();    
  }
}


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");
}

Sometimes the connection will break on a wireless connection. This should prevent the lockup. Normally the lockup happens in the "while(client.connected())" loop and it becomes an endless loop. The loopCount variable controls the timeout. If you see a "Timeout" message on the serial monitor, the sketch would have locked up there.

      int loopCount = 0;

      while(client.connected()){  
         while(client.available()) {
             // read the bytes incoming from the client:
             char thisChar = client.read();
             // echo the bytes back to the client:
             client.write(thisChar);
             // echo the bytes to the server as well:
             Serial.write(thisChar);
              loopCount = 0;
         }         
         delay(1);
         loopCount++;

         if(loopCount > 10000) {
         {
             client.stop();
             Serial.println();
             Serial.println(F("Timeout"));
         }
      }
      client.stop();

edit: I changed server.write() to client.write. You want that sent to only that client.

Thanks, I'll give it shot, but the Arduino lockup occurs when nothing is connected - after a connection closes it lasts < 15 min before locking up.

-Martin

Same result... It's interesting that if I run it and don't connect at all, the system stays up a LONG time. Recent tests show that after a connection/close, 305 seconds/counts later (with a little stall at a count of 300), the Arduino locks up. 3 times in a row now it has crashed right at that count. It's like something is causing a stack overflow or similar after a number of loop iterations, but only after a connect & close.

The only code it would be consistently running in those iterations would be: WiFiClient client = server.available();
But it runs that even before there's a connection at all and seems to stay up fine. It shouldn't be trying to create new instances?

Thoughts? Any chance you could run the code on your side and see if you get a lockup as well after a connect/close? Maybe there's a ghost in my machine...

Thanks.

If I am going to test code, it will be my code. I use basically the same code for the ethernet shield and the wifi shield.
Client: Arduino Playground - WebClient
Server: Arduino Playground - WebServerST

The only major change is the setup function to start whichever device I plan on using and the WiFiServer and .WiFiClient instead of the ethernet instances.

Thanks, but apparently your code isn't locking the system up. Maybe I'll get the Ethernet shield and try that.