Official Wifi Shield Disconnect and Reconnect Problems

Hi there!

I am having problems with my Wifi Shields.

Setup:
All my Shields are plugged into Arduino Duo Rev.3, of course, one for each Wifi Shield. Nothing fancy here.
I do not power the Uno via USB, but with a 12V power supply. However, I tried both and it doesnt affect its behaviour or these problems.

Software:
The software running Server Side (on the Arduino) is just like the Chat Server example. It doesnt echo back the data, but makes a simple computation and then, sends back the data to my computer.
At client side, there is a software which connects to the server and sends random values, 1 per second.

Problems:
I have to deal with two problems:

  1. Reconnect Issues - Whenever I had to stop my software on the computer (happened a lot while debugging), the socket doesnt terminate correctly ( the server probably doesnt get any RST ). After that, I can not reconnect to the Arduino. After resetting the Arduino, connecting works normally.

  2. Disconnecting - After some time (like 3 hours) the Arduino just seems to disconnect from the network. I dont know why, but pinging the device still works. I cant reconnect, though. Here, I have to reset the whole Arduino again in order to reconnect.

  3. Random data - When connecting to the Arduino (with netcat or telnet) I get random data, first. If I dont send any data back, the connetion terminates and I have to reset the Arduino in order to reconnect again. Why is that? Of course, the server flushes the client buffer when I connect, but actually, I shouldnt get this random data.

All these problems occur not only once - They occur all the time. I am using 5 Uno - WifiShield setups so this is definitely not some hardware-specific failure.

I hope anyone can help me fixing these problems or, at least, provide some workaround. The server doesnt have to run 24/7 but 12 hours a day straight should be possible.
Thanks in advance!

kind regards,
kamo

Link to Chatserver Example:

I have a very similar problem. I am using the Webduino webserver library with the official wifi shield and official WiFi library. The arduino board is Mega 2560 rev 3. Everything works great and is rock solid as long as the router connection is maintained. If the LAN router goes down, the arduino/wifi shield reconnects the radio and reappears on the LAN when the LAN router is brought up. But when it comes back my sketch is hung up.

The arduino/wifi shield connot be reached from a client unless I do a hardware reset or power cycle the shield. I have two systems, and they both behave the same way.

I reflashed the firmware to the 1/19/2013 revision, and installed the 1/19/2013 library. These actions did not fix the problem. I think the previous poster is correct that something is not being reset that should be reset when the wifi shield loses connection.

I do not have the previous posters disconnecting problems (#2 and #3). I had those problems when I was using the example web servers from the arduino web site. Once I switched to the Webduino library those problems went away. I think the simple web server sketches on the arduino web site cannot handle connections coming from multiple clients in rapid succession. I was able to crash those servers easily by using two or three computers issuing client connections repeatedly. The Webduino library web server is robust enough to survive that.

When the arduino/wifi shield cannot survive a power failure of the LAN router without requiring a manual reset, it is not useful as a remote data server/controller. I am hoping enough people see the problem that a fix can be developed.

fpga6:
I reflashed the firmware to the 1/19/2013 revision, and installed the 1/19/2013 library. These actions did not fix the problem. I think the previous poster is correct that something is not being reset that should be reset when the wifi shield loses connection.

That didnt work for me, either.

fpga6:
I do not have the previous posters disconnecting problems (#2 and #3). I had those problems when I was using the example web servers from the arduino web site. Once I switched to the Webduino library those problems went away. I think the simple web server sketches on the arduino web site cannot handle connections coming from multiple clients in rapid succession. I was able to crash those servers easily by using two or three computers issuing client connections repeatedly. The Webduino library web server is robust enough to survive that.

Now, that is interesting. Did you have those problems only with multiple clients? Or was it exactly the same as in #2 ?
I will definitely try out the webduino library. My biggest problem here is reliability and I did not have any concurrent connections, just one. Thanks.

kind regards,
kamo

Both. Multiple clients and a single computer.

Using the simple arduino servers I was able to quickly and repeatably crash the arduino server using multiple computers on my LAN trying to become the client at the same time. I could also sometimes crash it by hitting it with refresh or GET from a single computer in rapid succession. If I hit it while the HTML page was reloading it would frequently fail.

Also, it would usually go down at least once a day by itself. That was because my Firefox browsers would issue page hits to the arduino occasionally on their own. I think Firefox does that to create the tiled page of previous web sites visited.

So then I switched to the webduino library. The webduino library has a more robust handling and timeout procedure that has been able to survive any traffic I have thrown at it. I am using the Webduino wifi library that was forked from the ethernet version:

At the moment I using webduino to run a 110v house light using an X10 firecracker controller. My webpage is on the internet and I am controlling the light reliably from 100 miles away. It works with iPad Safari, IE explorer, and Firefox. iPad is a somewhat slower, but mine is the old iPad2.

I still have trouble getting my arduino wifi shield to reconnect to my ATT Uverse 2Wire gateway after a power failure. If my arduino webserver powers up prior to the LAN being available, the radio in the shield connects, the wifi status is correct, but my webduino server does not process connections. I have to power down reset the arduino to reconnect. As long as the power on both stays up I do not have any problems with the arduino webduino server hanging up.

If you try webduino wifi , please post your results.

Regards also,
fpga6

An update.

I have two of these arduino/webduino/official wifi shield systems running.

Each ran successfully for about 6 1/2 days and then each stopped responding. The void loop is running fine, but the webserver is not responding to clients.

Since I am not at the location of these systems, I put one on a mechanical lamp timer so it powers down for one hour at midnight and reboots. That way the system is unlikely to be down for more than one day. I can live with that temporarily.

For the long term I am still searching for a new fix. I am now running the newly patched Webduino server at GitHub - cat101/Webduino at patch-2.

I had to change three lines to convert WebServer.h to WiFi from Ethernet. I will update this thread if I see improved reliability.

Can you post the code and list the changes you made? Or point out the two lines you changed?

How did you set the SSID and WPA/WEP Password?

Judgeless-

Make the following changes to your arduino library copy of webduino webserver.h with notepad so it will run with the wifi shield:

//remove:

#include <EthernetClient.h>
#include <EthernetServer.h>

//replace with

#include <WiFi.h>

//and farther down

// remove:

private:
  EthernetServer m_server;
  EthernetClient m_client;

//replace with:

private:
  WiFiServer m_server;
  WiFiClient m_client;

to program ssid and password use the examples in the arduino sketches:

// enter the user name and id in the arduino sketch definitions

char ssid[] = "2WIRE222";     // network SSID (name)
char pass[] = "5555555555";   // network password

// then in the void setup():

status = WiFi.begin(ssid, pass);

There are good examples of setting up the wifi shield ssid and pass in the arduino wifi shield documentation on the arduino web site.

You can use the example arduino sketches in the github library for webduino as a starting point for your own sketches. I used the sketch "Web_Demo.ino" as the starting point for mine.

I still have not achieved true bulletproof reliability with the wifi shield. My systems always fail after a number of days.

There are other references to m_server that gives errors.

m_server(port),

m_server.begin();

m_client = m_server.available();

return m_server.available();

What did you do with those? Can you post all your code? I am trying this on the Web_AjaxRGB sketch

Before diving too far into this, one question:

Did you replace in sketch AjaxRGB, line 5

#include "Ethernet.h"

instead with:

#include <WiFi.h>

You also have to do that to make this work with wifi shield.

I am so close to getting this to work. I added

int status = WL_IDLE_STATUS;
status = WiFi.begin(ssid, pass)

It compiles, links to my WiFi network, shows the web site. The bars on the web site do not change the LEDS connected to pins(5,3,6) . I can see the blue data led blink when I move the slider bars. After moving the bars a couple of times the data led does nothing. If I refresh the browser it fails to load a page. Here is all the code.

Web_AjaxRGB

/* Web_AjaxRGB.pde - example sketch for Webduino library */

#include "SPI.h"
#include <WiFi.h>
//#include "Ethernet.h"
#include "WebServer.h"

// CHANGE THIS TO YOUR OWN UNIQUE VALUE
//static uint8_t mac[6] = { 0x02, 0xAA, 0xBB, 0xCC, 0x00, 0x22 };

// CHANGE THIS TO MATCH YOUR HOST NETWORK
//static uint8_t ip[4] = { 192, 168, 1, 210 }; // area 51!

/* all URLs on this server will start with /rgb because of how we
 * define the PREFIX value.  We also will listen on port 80, the
 * standard HTTP service port */
#define PREFIX "/rgb"
WebServer webserver(PREFIX, 80);

#define RED_PIN 5
#define GREEN_PIN 3
#define BLUE_PIN 6

int red = 0;            //integer for red darkness
int blue = 0;           //integer for blue darkness
int green = 0;          //integer for green darkness

char ssid[] = "myhouse";     // network SSID (name)
char pass[] = "dietcoke";   // network password
int status = WL_IDLE_STATUS;


/* This command is set as the default command for the server.  It
 * handles both GET and POST requests.  For a GET, it returns a simple
 * page with some buttons.  For a POST, it saves the value posted to
 * the red/green/blue variable, affecting the output of the speaker */
void rgbCmd(WebServer &server, WebServer::ConnectionType type, char *, bool)
{
  if (type == WebServer::POST)
  {
    bool repeat;
    char name[16], value[16];
    do
    {
      /* readPOSTparam returns false when there are no more parameters
       * to read from the input.  We pass in buffers for it to store
       * the name and value strings along with the length of those
       * buffers. */
      repeat = server.readPOSTparam(name, 16, value, 16);

      /* this is a standard string comparison function.  It returns 0
       * when there's an exact match.  We're looking for a parameter
       * named red/green/blue here. */
      if (strcmp(name, "red") == 0)
      {
	/* use the STRing TO Unsigned Long function to turn the string
	 * version of the color strength value into our integer red/green/blue
	 * variable */
        red = strtoul(value, NULL, 10);
      }
      if (strcmp(name, "green") == 0)
      {
        green = strtoul(value, NULL, 10);
      }
      if (strcmp(name, "blue") == 0)
      {
        blue = strtoul(value, NULL, 10);
      }
    } while (repeat);
    
    // after procesing the POST data, tell the web browser to reload
    // the page using a GET method. 
    server.httpSeeOther(PREFIX);
//    Serial.print(name);
//    Serial.println(value);

    return;
  }

  /* for a GET or HEAD, send the standard "it's all OK headers" */
  server.httpSuccess();

  /* we don't output the body for a HEAD request */
  if (type == WebServer::GET)
  {
    /* store the HTML in program memory using the P macro */
    P(message) = 
"<!DOCTYPE html><html><head>"
  "<title>Webduino AJAX RGB Example</title>"
  "<link href='http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.16/themes/base/jquery-ui.css' rel=stylesheet />"
  "<script src='http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js'></script>"
  "<script src='http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.16/jquery-ui.min.js'></script>"
  "<style> body { background: black; } #red, #green, #blue { margin: 10px; } #red { background: #f00; } #green { background: #0f0; } #blue { background: #00f; } </style>"
  "<script>"

// change color on mouse up, not while sliding (causes much less traffic to the Arduino):
//    "function changeRGB(event, ui) { var id = $(this).attr('id'); if (id == 'red') $.post('/rgb', { red: ui.value } ); if (id == 'green') $.post('/rgb', { green: ui.value } ); if (id == 'blue') $.post('/rgb', { blue: ui.value } ); } "
//    "$(document).ready(function(){ $('#red, #green, #blue').slider({min: 0, max:255, change:changeRGB}); });"

// change color on slide and mouse up (causes more traffic to the Arduino):
    "function changeRGB(event, ui) { jQuery.ajaxSetup({timeout: 110}); /*not to DDoS the Arduino, you might have to change this to some threshold value that fits your setup*/ var id = $(this).attr('id'); if (id == 'red') $.post('/rgb', { red: ui.value } ); if (id == 'green') $.post('/rgb', { green: ui.value } ); if (id == 'blue') $.post('/rgb', { blue: ui.value } ); } "
    "$(document).ready(function(){ $('#red, #green, #blue').slider({min: 0, max:255, change:changeRGB, slide:changeRGB}); });"

  "</script>"
"</head>"
"<body style='font-size:62.5%;'>"
  "<div id=red></div>"
  "<div id=green></div>"
  "<div id=blue></div>"
"</body>"
"</html>";

    server.printP(message);
  }
}

void setup()
{
  pinMode(RED_PIN, OUTPUT);
  pinMode(GREEN_PIN, OUTPUT);
  pinMode(BLUE_PIN, OUTPUT);

//  Serial.begin(9600);

  // setup the Ehternet library to talk to the Wiznet board
//  Ethernet.begin(mac, ip);
status = WiFi.begin(ssid, pass);

  /* register our default command (activated with the request of
   * http://x.x.x.x/rgb */
  webserver.setDefaultCommand(&rgbCmd);

  /* start the server to wait for connections */
  webserver.begin();
}

void loop()
{
  // process incoming connections one at a time forever
  webserver.processConnection();
//  Serial.print(red);
//  Serial.print(" ");
//  Serial.print(green);
//  Serial.print(" ");
//  Serial.println(blue);
  analogWrite(RED_PIN, red);
  analogWrite(GREEN_PIN, green);
  analogWrite(BLUE_PIN, blue);
}

If try the exact demo on a wired ethernet shied it works pefect.

It appears your wifi shield is working because your page loads into your browser.

I would try alternate browsers to see if they display better. Firefox is the most forgiving browser that I have found.

For IE Explorer and Apple iPad Safari, I had to add a !DOCTYPE header to get them to work.

The header I use is:

  P(htmlHead) =
    "<!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.01 Transitional//EN'\n'http://www.w3.org/TR/html4/loose.dtd'>"
    "<html>"
    "<head>"
    "<title>Webduino Server</title>"
    "<style type=\"text/css\">"
    "BODY { font-family: sans-serif }"
    "H1 { font-size: 18pt; text-decoration: underline }"
    "P  { font-size: 14pt; }"
    "</style>"
    "</head>"
    "<body>"

You can run your code through one of the free HTML verifiers available on the web. Just go to "page>view source" in your browser, copy your HTML, and paste it into the verifier. You may find several things you can fix.

Different browsers display non industry standard HTML based upon their interpretation of what was intended. I think Mozilla does the best job of this.

Also, what arduino board are you running with your wifi shield?

Judgeless-

One other thing.

I think your sketch is trying to do an analog write to a digital pin. I believe the analog pins are numbered with an "A", i.e. A1, A2, etc.

Judgeless-

My comment about analog write to a digital pin is not correct. Doing analog write to those digital pins enables pulse width modulation. That is a much better way to control LED brightness than analog write to a true analog pin.

Sorry.

analogWrite() does PWM the pins that have a PWM function. The best way to drive an analog signal is with a DAC but the device does not have a DAC so they use a PWM.

I still cannot get it to work correct in any browser. I enabled all the SerialPrint() options in the code so I can see what is going on. When I change the sliders it sometimes changes the number by 1. It seems like it is a timing issue with the WebServer::Get function. The AJAX code is sending the info but the webserver is not bringing it in correctly.

I based my project on the example Web_Demo sketch because I wanted to use the radio buttons to control digital functions. I don't know anything about sliders.

Could you be overloading the computation capability of the arduino? I notice you chose the more traffic to arduino option in your sketch.

// change color on mouse up, not while sliding (causes much less traffic to the Arduino):
//    "function changeRGB(event, ui) { var id = $(this).attr('id'); if (id == 'red') $.post('/rgb', { red: ui.value } ); if (id == 'green') $.post('/rgb', { green: ui.value } ); if (id == 'blue') $.post('/rgb', { blue: ui.value } ); } "
//    "$(document).ready(function(){ $('#red, #green, #blue').slider({min: 0, max:255, change:changeRGB}); });"

// change color on slide and mouse up (causes more traffic to the Arduino):
    "function changeRGB(event, ui) { jQuery.ajaxSetup({timeout: 110}); /*not to DDoS the Arduino, you might have to change this to some threshold value that fits your setup*/ var id = $(this).attr('id'); if (id == 'red') $.post('/rgb', { red: ui.value } ); if (id == 'green') $.post('/rgb', { green: ui.value } ); if (id == 'blue') $.post('/rgb', { blue: ui.value } ); } "
    "$(document).ready(function(){ $('#red, #green, #blue').slider({min: 0, max:255, change:changeRGB, slide:changeRGB}); });"

Could it be that the arduino can't handle the wifi shield and some of your sketch functions? The arduino doesn't have any real time operating system properties that would prioritize time critical functions. However, the wifi shield has a relatively powerful microcontroller on board, so it seems that should take care of any time critical issues.

At this point I can't be of much help. My sketch either works well or totally freezes. When it is working, it works very well.