Go Down

Topic: Wifi Shield stops sending after sometime. (Read 4182 times) previous topic - next topic

surendhar

Hi everyone,

I have been using Arduino WiFi shield to log sensor values to google spreadsheet. It logs perfectly.

But when I try to run it for a longer period it just stops sending. sometimes it stops after 20 minutes and sometimes it stops after 4-5 hours. The code is below. By the way the firmware version of Arduino WiFi shield is 1.1.0. I usually connect it the laptop to run the code, so I can see the serial monitor. There were no errors shown. For the last two tests I connected it to a 12V power supply.

Code: [Select]


#include <dht.h>
#include <SPI.h>
#include <WiFi.h>
#define dht_dpin A0

dht DHT;


char ssid[] = "mywifi";     
int status = WL_IDLE_STATUS;     
char server[] = "api.pushingbox.com";
WiFiClient client; //define 'client' as object
boolean DEBUG= true;

void setup()
{
 
  Serial.begin(9600);
  while (!Serial) {
    ;
  }

  // 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 open SSID: ");
    Serial.println(ssid);
    status = WiFi.begin(ssid);


    delay(10000);
  }


  Serial.print("You're connected to the network");

}

void loop()
{
  DHT.read11(dht_dpin);
  double temp = DHT.temperature;
  double hum = DHT.humidity;

  delay(5000);

  Serial.println("connecting...");
 
  if(temp>=30)
  {
    if (client.connect(server, 80))
  {
      logData();
      delay(70); 
      DEBUG = true;
   }
   else
   {
     Serial.println("connection failed");
   }
  // loop
  while(DEBUG)
  {
   if (client.available())
    {
    char c = client.read();
    Serial.print(c);
    }
    if (!client.connected())
    {
    Serial.println();
    Serial.println("disconnecting.");
    Serial.print("Current temperature = ");
    Serial.println(DHT.temperature);
    Serial.print("Current humidity = ");
    Serial.println(DHT.humidity);
    client.stop();
    DEBUG=false;     
    }
 if (client.connect(server, 80))
  {
      sendtemptoPushBullet();
      delay(70);
      DEBUG=true;
   }
 while(DEBUG)
 {
   if (client.available())
 {
    char c = client.read();
    Serial.print(c);
 }
   if(!client.connected())
 {
    Serial.println("disconnecting from pushbullet");
    client.stop();
    DEBUG=false;
   }
  }
  }
  }
  else
  {
     if (client.connect(server, 80))
  {
      logData();
      delay(70); 
      DEBUG = true;
   }
   else
   {
     Serial.println("connection failed");
   }
  // loop
  while(DEBUG)
  {
   if (client.available())
    {
    char c = client.read();
    Serial.print(c);
    }
    if (!client.connected())
    {
    Serial.println();
    Serial.println("disconnecting.");
    Serial.print("Current temperature = ");
    Serial.println(DHT.temperature);
    Serial.print("Current humidity = ");
    Serial.println(DHT.humidity);
    client.stop();
    DEBUG=false;     
    }
    }
  }

if(hum>=90)
  {
 if(client.connect(server, 80))
  {
      sendhumtoPushBullet();
      delay(70);
      DEBUG=true;
   }
 
   while(DEBUG)
   {
   if (client.available())
   {
    char c = client.read();
    Serial.print(c);
   }
   if(!client.connected())
   {
    Serial.println("disconnecting from pushbullet");
    client.stop();
    DEBUG=false;
   }
  }
  }
  delay(120000);
}


void logData()
{
 double temp= DHT.temperature;
 double hum = DHT.humidity;
 delay(1000);
   
  Serial.println("connected");
 
  client.print("GET /pushingbox?devid=");
  client.print("mydeviceid");
  client.print("&tempData=");
  client.print(temp);
  client.print("&humData=");
  client.print(hum);
  client.println(" HTTP/1.1");
  client.print("Host: ");
  client.println(server);
  client.println("User-Agent: Arduino");
  client.println();
}

void sendtemptoPushBullet()
{
  Serial.print("Sending notification to pushbullet");
   
  client.print("GET /pushingbox?devid=");
  client.print("mydeviceid");
  client.println(" HTTP/1.1");
  client.print("Host: ");
  client.println(server);
  client.println("User-Agent: Arduino");
  client.println();
}

void sendhumtoPushBullet()
{
  Serial.print("Sending notification to pushbullet");
   
  client.print("GET /pushingbox?devid=");
  client.print("mydeviceid");
  client.println(" HTTP/1.1");
  client.print("Host: ");
  client.println(server);
  client.println("User-Agent: Arduino");
  client.println();
}



Please give some suggestions.

Thank you

barakzai786

Hi

that is the nature of arduino wifi shield the solution that works for me is a software reset.

elliott954

barakzai78, could you provide the details on what you mean by a "software reset"?

elliott954


In trying to find the root cause of this problem I ran a network trace, and the trace suggests the problem lies either in the WiFi101 driver, the WiFi Shield firmware, the Arduino Mega 2560 firmware, or any combination of the three.  I say that because it is the Arduino that is issuing retry's to the client. 

Summary of what I can tell so far:
  • The "freeze" occurs whether use the  WiFi101 card as a Web Server or Web Client.
  • The "freeze" happens if I access the card using either of the following methods:
  • Chrome
  • Microsoft Powershell
  • The serial port spews garbage data when the "freeze" occurs
  • Sometimes the sketch stops running, but not always
  • Sometimes the serial port stops running, but otherwise the sketch seems to continue running


Here is what a network trace shows when the failure occurs:
Set up:
  • Arduino Mega 2560 with a WiFi 101 card installed
  • WiFi 101 configured as a server
  • My client was a Windows 2012 Server, running power shell script as admin (script is given further below)
  • I ran NetMon on the server where I was doing the request (For those of you who do a lot of network trace captures, I know running the sniffer on the same machine as the client is not the best practice, but in my case, the server was not busy doing anything else, it has plenty of memory and CPU speed, and under those circumstances I think it was not a bad approach to do the capture)
  • What you see:
  • The trace looks normal for a while,
  • After a few minutes, the WiFi101 card issues a retry (for more information on this, see MSDN explanation)
  • The server resends,
  • The WiFi101 issues a second retry
  • The server responds again.
  • After that, the WiFi 101 goes "silent"








My Powershell Script (client):

$a = 1;
$b = 0;
$web= New-Object Net.Webclient
do
{
        $b = $b+1;
        $b;
        $webstr = $web.downloadString("http://192.168.12.147");
       
        $lines = $webstr;
       
 
  # the lines from here down have nothing to do with the aruduino "freeze"
  # I have just included them for sake of completeness
  # this script "scrapes" the web page served up by the arduino
     
        $dex = $lines.indexof("<p>");
        $lines = $lines.substring($dex);
        #$lines;
        $dex = $lines.indexof("</html");
        $lines = $lines.substring(0,$dex);
        #$lines;
        $lines = $lines.replace("</p>","");
        #$lines;
        $lines = $lines.replace("<p>","");
        #$lines;
       
        $split = $lines.split(",");
 
 
        [regex]$r="[^a-zA-Z]"
        $split | ForEach-Object {$_ -Replace "$r",""}
        $split;
        [regex]$regex = 'RDAQ_ID';
        $nResults = $regex.matches($Split).count;
       
       
        $lastItem = $split.length - 1;
        $strLen = $split[$lastItem].length-2;
        $g = $split[$lastItem];we
        $g = $g.substring(0,$strLen);
        $split[$lastItem] = $g;
       
        $split;     
        start-sleep -s 10;
 
} while ($a -eq 1)
 


mephala

Hi,
I'm a owner of a MKR1000, and i think we got same problem.
I was trying to control a home device through a webpage on my wifi board but, after a few hours working fine, it stops responding http requests from a webrowser.
This issue was explained in this post:

http://forum.arduino.cc/index.php?topic=401107.0

After a lot of issues and debugs, i concluded it's because a problem in the wifi part of the sketch, so
i extracted all unnecessary code to detect the problem.
Finally, and to be sure, i loaded the example included with the WiFi101 library, named 'AP_SimpleWebserver', and cleaned the unnecessary sentences (basically, deleted all the 'Serial.println()' ones).
So the code lasts this way:
Code: [Select]
/*
  WiFi Web Server LED Blink

 A simple web server that lets you blink an LED via the web.
 This sketch will print the IP address of your WiFi Shield (once connected)
 to the Serial monitor. From there, you can open that address in a web browser
 to turn on and off the LED on pin 9.

 If the IP address of your shield is yourAddress:
 http://yourAddress/H turns the LED on
 http://yourAddress/L turns it off

 created 25 Nov 2012
 by Tom Igoe
 */
#include <SPI.h>
#include <WiFi101.h>

int led =  LED_BUILTIN;

char ssid[] = "wifi101-network"; // created AP name
char pass[] = "1234567890";      // AP password (needed only for WEP, must be exactly 10 or 26 characters in length)
int keyIndex = 0;                // your network key Index number (needed only for WEP)


int status = WL_IDLE_STATUS;
WiFiServer server(80);

void setup() {
  pinMode(led, OUTPUT);      // set the LED pin mode
  // attempt to connect to Wifi network:
  while ( status != WL_CONNECTED) {
    // 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);
  }
  server.begin();                           // start the web server on port 80
}


void loop() {
  WiFiClient client = server.available();   // listen for incoming clients

  if (client) {                             // if you get a client,
    String currentLine = "";                // make a String to hold incoming data from the client
    while (client.connected()) {            // loop while the client's connected
      if (client.available()) {             // if there's bytes to read from the client,
        char c = client.read();             // read a byte, then
        if (c == '\n') {                    // if the byte is a newline character

          // if the current line is blank, you got two newline characters in a row.
          // that's the end of the client HTTP request, so send a response:
          if (currentLine.length() == 0) {
            // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
            // and a content-type so the client knows what's coming, then a blank line:
            client.println("HTTP/1.1 200 OK");
            client.println("Content-type:text/html");
            client.println();

            // the content of the HTTP response follows the header:
            client.print("Click <a href=\"/H\">here</a> turn the LED on<br>");
            client.print("Click <a href=\"/L\">here</a> turn the LED off<br>");

            // The HTTP response ends with another blank line:
            client.println();
            // break out of the while loop:
            break;
          }
          else {      // if you got a newline, then clear currentLine:
            currentLine = "";
          }
        }
        else if (c != '\r') {    // if you got anything else but a carriage return character,
          currentLine += c;      // add it to the end of the currentLine
        }

        // Check to see if the client request was "GET /H" or "GET /L":
        if (currentLine.endsWith("GET /H")) {
          digitalWrite(led, HIGH);               // GET /H turns the LED on
        }
        if (currentLine.endsWith("GET /L")) {
          digitalWrite(led, LOW);                // GET /L turns the LED off
        }
      }
    }
    // close the connection:
    client.stop();
  }
}

Due to the absence of Serial outputs, you wont be informed about your board ip address, but this could be done asking your router's DHCP client's list.
This code starts working perfect but, after a variable time, stops responding the webbrowser.
I asked Arduino Support, but got still no response.
It would be very helpfull if someone could also verify this behaviour, and find some explanation.
Thanks!

mephala

slosa

Hello, I also have this problem with a MKR1000 and SimpleWebserver, the board responds to the ping, but the web server does not respond within a few hours or as much a couple of days.

mbanzi

@slosa

Arduino Support pointed out to you this github issue (https://github.com/arduino-libraries/WiFi101/issues/72 )  where we are helping another user with a similar problem. You can speed up the resolution of this issue by providing our engineer with the code so he can figue out where the bug is.

@mephala see the above github issue. please help out by providing your code

People we're here to help but the only way to determine if the problem is in our code, Atmel's code or your code  is by providing us with something that consistently reproduces the bug.


ballscrewbob

#7
Jul 05, 2016, 05:57 am Last Edit: Jul 05, 2016, 06:07 am by Ballscrewbob
The MKR issue is well known to me as i have tried a multitude of methods to get around it and identify it.

See here

I hadnt thought of checking the 101 section for MKR issues but glad I did.

My code is on the web IDE here

The MKR is as glitchy as h3ll and I am reasonably sure its not a code issue.

I suspect its an Arduino or Atmel issue. as people are seeing it with a variety of sketches from reading here and the dropout times vary wildly from a couple of hours to a couple of days.

It  could I suppose be some sort of buffer issue (best guess)
It may not be the answer you were looking for but its the one I am giving based on either experience, educated guess, google or the fact that you gave nothing to go with in the first place so I used my wonky crystal ball.

Thomas499

#8
Jul 11, 2016, 08:15 pm Last Edit: Jul 11, 2016, 08:22 pm by Thomas499
Quote
You can speed up the resolution of this issue by providing our engineer with the code so he can figue out where the bug is.
You want code that helps diagnost the wifi crash, this may be of interest to you I included the code below that does not use Strings as people have speculated that Strings may have been the cause. It turns out it wasn't Strings. 
Code: [Select]
/*
 WiFi Web Server LED Blink

A simple web server that lets you blink an LED via the web.
This sketch will print the IP address of your WiFi Shield (once connected)
to the Serial monitor. From there, you can open that address in a web browser
to turn on and off the LED on pin 9.

If the IP address of your shield is yourAddress:
http://yourAddress/H turns the LED on
http://yourAddress/L turns it off

This example is written for a network using WPA encryption. For
WEP or WPA, change the Wifi.begin() call accordingly.

Circuit:
* WiFi shield attached
* LED attached to pin 9

created 25 Nov 2012
by Tom Igoe
*/
#include <SPI.h>
#include <WiFi.h>

char ssid[] = "";      //  your network SSID (name)
char pass[] = "";   // your network password
int keyIndex = 0;                 // your network key Index number (needed only for WEP)
unsigned long previousMillis=0;
int interval=30000;
unsigned long PrintCount=0;
unsigned long LoopCount=0;
int status = WL_IDLE_STATUS;
WiFiServer server(80);

void setup() {
 Serial.begin(9600);      // initialize serial communication
 pinMode(9, OUTPUT);      // set the LED pin mode

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

 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 Network named: ");
   Serial.println(ssid);                   // print the network name (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);
 }
 server.begin();                           // start the web server on port 80
 printWifiStatus();                        // you're connected now, so print out the status
}


void loop()
{
 ListenForController();
 unsigned long currentMillis = millis();
 if(currentMillis - previousMillis >= interval) // print this every 2 seconds
 { previousMillis = currentMillis;   
   Serial.print(PrintCount);
   Serial.print(F("   "));
   Serial.print(LoopCount);
   Serial.print(F("   "));
   Serial.print(millis());
   Serial.print(F("   "));
   Serial.print(freeRam());
   Serial.print(F("   "));
   Serial.println(F("slave arduino // freezes"));
   PrintCount++;
 }
 LoopCount++;
}

void ListenForController()
{
 WiFiClient client = server.available();   // listen for incoming clients

 if (client) {                             // if you get a client,
   Serial.println("new client");           // print a message out the serial port
   //String currentLine = "";                // make a String to hold incoming data from the client
   boolean lenght=1;
   while (client.connected()) {            // loop while the client's connected
     if (client.available()) {             // if there's bytes to read from the client,
       char c = client.read();             // read a byte, then
       Serial.write(c);                    // print it out the serial monitor
       if (c == '\n') {                    // if the byte is a newline character

         // if the current line is blank, you got two newline characters in a row.
         // that's the end of the client HTTP request, so send a response:
         if (lenght == 0) {
           // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
           // and a content-type so the client knows what's coming, then a blank line:
           client.println("HTTP/1.1 200 OK");
           client.println("Content-type:text/html");
           client.println();

           // the content of the HTTP response follows the header:
           client.print("Click <a href=\"/H\">here</a> turn the LED on pin 9 on<br>");
           client.print("Click <a href=\"/L\">here</a> turn the LED on pin 9 off<br>");

           // The HTTP response ends with another blank line:
           client.println();
           // break out of the while loop:
           break;
         }
         else {      // if you got a newline, then clear currentLine:
          // currentLine = "";
          lenght=0;
         }
       }
       else if (c != '\r') {    // if you got anything else but a carriage return character,
       //  currentLine += c;      // add it to the end of the currentLine
       lenght=1;
       }

       // Check to see if the client request was "GET /H" or "GET /L":
    /*   if (currentLine.endsWith("GET /H")) {
         digitalWrite(9, HIGH);               // GET /H turns the LED on
       }
       if (currentLine.endsWith("GET /L")) {
         digitalWrite(9, LOW);                // GET /L turns the LED off
       }*/
     }
   }
   // close the connection:
   client.stop();
   Serial.println("client disonnected");
 }
}

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");
 // print where to go in a browser:
 Serial.print("To see this page in action, open a browser to http://");
 Serial.println(ip);
}
 int freeRam() {
 extern int __heap_start,*__brkval;
 int v;
 return (int)&v - (__brkval == 0 ? (int)&__heap_start : (int) __brkval);
}

Serial printout, what you will see here is that once the server is called it will run and then freeze. Below it froze and has been like that for 10 minutes.
Quote
Attempting to connect to Network named: ProbablyYours
SSID: ProbablyYours
IP Address: 192.168.0.176
signal strength (RSSI):-57 dBm
To see this page in action, open a browser to http://192.168.0.176
0   19935   30000   6846   slave arduino // freezes
new client
GET /H HTTP/1.1
Host: 192.168.0.176
User-Agent: Mozilla/5.0 (Windows NT 6.2; WOW64; rv:41.0) Gecko/20100101 Firefox/41.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
DNT: 1
Referer: http://192.168.0.176/
Connection: keep-alive

client disonnected
1   82426   60000   6846   slave arduino // freezes
2   145563   90000   6846   slave arduino // freezes
3   208691   120000   6846   slave arduino // freezes
4   271798   150000   6846   slave arduino // freezes
5   334930   180000   6846   slave arduino // freezes
6   398067   210000   6846   slave arduino // freezes
7   461199   240000   6846   slave arduino // freezes
8   524327   270000   6846   slave arduino // freezes
9   587460   300000   6846   slave arduino // freezes
10   650595   330000   6846   slave arduino // freezes

Thomas499

#9
Jul 11, 2016, 08:18 pm Last Edit: Jul 11, 2016, 08:21 pm by Thomas499
To sum up what I found during a week of trying to debug the WiFi problem, if you use server code, expect it to run for 6 minutes up to 2 hours once the server is called to the point that it becomes unresponsive.

I found putting and using booleans in certain places when certain code is written increases the time until the crash by a little bit, which suggest an overflow problem. This overflow problem was not from the code I wrote, and seems to be caused in the WiFi firmware.

Interestingly, if you use the server it causes the crash relatively quickly, if you have server code in the program but it is never called, it will not freeze. Even more interesting, is that server almost always crashes the arduino once called within 20 minutes, client code crashes the arduino once called as well, but it usually takes a few hours and is not nearly as quick to crash as the server code. Either way, your arduino will not run two days without crashing if client or server code is called.

Post 30 of the link I provided on the post above provides even more diagnostics from another person.

Quote
Sorry to break this to you but you are playing with inherently broken hardware.

This thread has the most complete summary of the many issues affecting the WiFi shields TCP communication.
http://forum.arduino.cc/index.php?topic=128424.0

I also did some experimenting myself.
http://mssystems.emscom.net/helpdesk/knowledgebase.php?article=51
I think at one point I tried calling the server code, then unattached the wifi shield before the crash accorded. Interestingly, that didn't stop the crash. I am confident that the problem is in the wifi firmware, which no one ever really looks into.

For convience, the url links he mentioned are provided here

http://forum.arduino.cc/index.php?topic=128424.0

http://mssystems.emscom.net/helpdesk/knowledgebase.php?article=51


Poej

Hi,
When looking for a solution to the same problems in my temp logging setup, i kept finding the presented solutions to the described problems rather complicated.
Starting from the view that simplicity often beats complexity i looked at the most often used code to build some kind of webserver on any arduino-like platform.
It looked to me that a very likely candidate to cause the freezes was the whileclient() statement mostly used to detect the presence of a webclient and to start the delivery of the required html code.
I replaced the while(client.available()) with if(client.available()) and PRESTO since then my Wemos D1 based templogger works as stable as a rock.
It seems that once in awhile the client fails to properly close the connection, causing the server to lock itself into an endless serving loop hence rendering the whole setup locked.
I hope this solution works for you as it worked for me: Like a miracle!

CowboySkippy

I also found that my MKR1000 device would stop sending data and the sketch would stop running. I am using the "B" version of the MKR1000 device, Arduino 1.8.0, and Arduino 0.13.0.

After a series of experiments, I found a solution.  I observed that my sketch would run and the sketch sent sensor data through PushingBox to my Google Sheet for a few minutes.  But the sketch would stop executing.  I know this because I was sending a character to the connected OLED screen and screen updates stopped.

Refer to the project, "Send MKR1000 Data to Google Sheets", by Stephen Borsay on the Arduino Project Hub at the link, https://create.arduino.cc/projecthub/detox/send-mkr1000-data-to-google-sheets-1175ca.

In Stephen's loop(), I see that he exemplifies the following pseudocode:

Code: [Select]
loop(){
// Get sensor data
if (client.connect(WEBSITE,80)){
  // Send sensor data to PushingBox
  }
}


In the script, Stephen's comment "//for MKR1000, unlike esp8266, do not close connection" triggered me to challenge the statement.  I am glad I did!
So I modified the function by adding a stop command to the client:

Code: [Select]
loop(){
// Get sensor data
if (client.connect(WEBSITE,80)){
  // Send sensor data to PushingBox
  }
client.stop();
}


After the addition of the stop command, I found that my MKR1000 script continually sent sensor data to PushingBox and my Google Sheet.  In my case, my timer is set to 1 hour and I have been continually sending data for 50 hours and running.

So now I have a robust, reliable sketch that sends sensor data through PushingBox to my Google Sheet.  Try this suggestion in your own sketch and post your results.  Cheers! :smiley-grin:

gnusso

also me ... I've this answer ...

A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.

means that the client is not connected anymore and completely disconnected ...

means that is unusable

Nick_Nack

#13
Apr 06, 2017, 01:44 pm Last Edit: Apr 06, 2017, 02:17 pm by Nick_Nack
@cowboyskippy

I also am using a MKR1000 version B running Stephen Borsay's code.  However, even if I do add the client.stop() command, my sketch updates my google sheet for only 15 seconds or so, then the WiFi cuts out, but my sketch continues to run fine.  

I don't know if I configured my router incorrectly or if there is an error in my code.  Have you run into this problem?

Go Up