Wifi shield disconnect

Hello everyone,

I’ve some problems with my arduino wifi shield.

I created a simple code to turn on /off a led via my browser. I looked on internet for exemple and I found a code who use the GET methode. I encountered some issues:

  • After +/- 30 seconds/ 1minute i can’t connect to my server ( inaccessible web page on my browser
  • I read the HTTP request on the serial monitor and between the differents GET request when I press On(GET /?ledon HTTP/1.1) or Off(GET /?ledon HTTP/1.1) each time i press On/off or when I refresh my page my arduino send different header( GET/Host/Connection/Accepte/…)

So I don’t have time to change the state of my LED because he takes too long between each GET methode and because my server disconnect after a short time.

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


char ssid[] = "labowifi";      // your network SSID (name) 
char pass[] = "Wilfart2013";   // your network password
int keyIndex = 0;// your network key Index number (needed only for WEP)

String readString; 
WiFiServer server(80);
int status = WL_IDLE_STATUS;

int BLUE =6;
int GREEN=5;
int RED=3;
int brightness = 0;
int DELAY =5;

//////////////////////

void setup(){

  pinMode(6, OUTPUT); //pin selected to control
  //start Ethernet

  server.begin();
status = WiFi.begin(ssid, pass);
  //enable serial data print 
  Serial.begin(115200); 
  Serial.println("server LED test 1.0"); // so I can keep track of what is loaded
  printWifiStatus();
}

void loop(){
  // Create a client connection
  WiFiClient client = server.available();
  if (client) {
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();

        //read char by char HTTP request
        if (readString.length() < 100) {

          //store characters to string 
          readString += c; 
          Serial.print(c);
        } 

        //if HTTP request has ended
        if (c == '\n') {

          ///////////////
          Serial.println(readString); //print to serial monitor for debuging 

          //now output HTML data header
            if(readString.indexOf('?') >=0) { //don't send new page
               client.println("HTTP/1.1 204 Zoomkat");
               client.println();
              client.println();  
             }
             else {
          client.println("HTTP/1.1 200 OK"); //send new page
          client.println("Content-Type: text/html");
          client.println();

          client.println("<HTML>");
          client.println("<HEAD>");
          client.println("<TITLE>Projet Lamp-WiFi</TITLE>");
          client.println("</HEAD>");
          client.println("<BODY>");

          client.println("<H1>Projet Wifi-Lamp</H1>");
          
          client.println("<a href=\"/?ledon\" target=\"inlineframe\">ON</a>"); 
          client.println("<a href=\"/?ledoff\" target=\"inlineframe\">OFF</a>"); 

          
          client.println("<IFRAME name=inlineframe style=\"display:none\" >");          
          client.println("</IFRAME>");

          client.println("</BODY>");
          client.println("</HTML>");
             }

          delay(1);
          //stopping client
          client.stop();

          ///////////////////// control arduino pin
          if(readString.indexOf("ledon") >0)//checks for on
          {
           digitalWrite(6, HIGH);    // set pin 6 high
            Serial.println("Led On");
          }
          if(readString.indexOf("ledoff") >0)//checks for off
          {
            digitalWrite(6, LOW);    // set pin 6 low
            Serial.println("Led Off");
          }
          //clearing string for next read
          readString="";

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

I hope to have been clear.

Thank you .

The Wifi library/firmware server code is unreliable. It has been known to send corrupted files and incorrect files, normally when two requests are pending simultaneously. It is a socket problem. The WiFi firmware uses only one socket for the server, and that socket never quits listening for new requests, so a second request while a first request is still being processed causes a corruption problem.

Ok, so is it possible to change anything in the code to by pass this sockets issue ? And is the Ethernet shield more reliable, all the code I see is for Ethernet shield, which i change for my WiFi shield...

I have successfully been doing what you want to do, but have taken it a bit further. I am currently controlling three 8-bit PWM signals to Red, Green, and Blue elements on a RGB led. Therefore, I can control my RGB led to 16.7 million colors, and I do it using my wi-fi shield and the internet.

However, I use my wi-fi shield as a web CLIENT, not a server. As a client, I have my wifi shield/arduino go out to the xively website (xively.com where I have a free account.). I wrote my code so every 20 seconds, my arduino goes out to the xively site and looks for new LED commands. I have been playing with it successfully for months, and I recently added some software reset capability for when my arduino occasionally goes off to see Elvis.

One drawback of using this setup (cleint mode) is that there is a delay between my command that I put onto xively from my browser and me seeing the change in my RGB led (currently maximum of 20 seconds).

A couple of advantages to using the wi-fi shield in client mode with xively is that the arduino does not have to produce a web page (HTML code).

Another big advantage in client mode it that you don't have to PORT FORWARD with your router to get the commands to the arduino! It is a client and goes out to the interent to get commands rather than somebody loggin into the arduino's we b page.

Set yourself up with a free Xively.com account, read up on the xively documentation, and download the free library for arduino. You can probably also search on xively/arduino examples.

I could also probably help you with sample code (though just not right away).

I unplugged my system last night so it has not been updating today.

However, you can go here to view my xively "feed"

https://xively.com/feeds/151846204

You can click on graphs and see what my channels have been doing over the last several days. I have a RED value, GREEN value, BLUE value, and led COMMAND, number of GET errors since last reset, number of PUT errors since last reset, and RECORD number (number of successful xively data exchanges since last reset).

Also, make sure your wi-fi shield software is up to date!

Good luck!

dsmavis

Hi dnmavis, Thank you for your answer, I created my Xively account but I' in the waiting list :/. I'll already look at diffenrents exemple and look to make a code for arduino in "client mode". Do you know how many time do I have to wait for my account? I would probably have some questions in the near future. I'll keep you updated

Hi Sjaro,

Sorry, I didn’t realize that there was a waiting list for a Xively account. I have had my account for around 2 years and at that time there was no waiting.

I’d be happy to answer questions once you are in a position to get going with Xively.

All the best!

–dsmavis

I gave up on the WIFI shield and am using the ethernet shield. It was slow, the socket issues, and a few bugs but I did get it to interact with the arduino. I use AJAX to send info back and forth and have the html file on the SD card. It works pretty well.

Check you code and memory. Sometimes the arduino can crash and that's the cause of the freeze up. The String object eats up memory.

Maybe the following link to an excellent debugging report of the wifi shield will explain if not help. http://katrinaeg.com/arduino-debugging.html