Arduino randomly hangs during HTTP Request!

Hello, i try to code a web client for arduino that sents data from my sensors to a database using GET http request. The loop after some successful requests hangs. (sometimes at the first 10 loops sometimes after a hundrer or more)

I have tried with both linux(Raspberry PI - Rasbian) and windows. On linux the loop hangs infinitely and on Windows the loop continues after about two minuites but not always, sometimes it hangs forever too.

I really have reached a stalemate...

Here is the full project: https://dl.dropboxusercontent.com/u/15854314/ver7.ino

Here is part of the code:

 if(!getPage(server,serverPort,pageAdd)) Serial.print(F("Fail "));
  if(client.connect(ipBuf,thisPort))
  {
    Serial.println(F("connected"));
    client.print("GET /write3.php?value0=");  //GET to php file which handle data (value0 and value1) into a database
    //Temperature
    client.print(value0); 
    //Humidity
    client.print("&value1=");
    client.print(dht_humidity); 
    //bmp085 values
    client.print("&value2=");
    client.print(temperature);
    
    client.print("&value3=");
    client.print(pressure);
    
    client.print("&value4=");
    client.print(altitude);
    
    //Weather Meters (Wind speed, direction, rain)
    client.print("&value5=");
    client.print(gust); //Wind speed every 20 seconds
    
    client.print("&value6=");
    client.print(dir);
    
    client.print("&value7=");
    client.print(rain); 
    
    client.print("&value8=");
    client.print(knots); //Wind speed every 60 seconds

    client.println(" HTTP/1.0");
    //client.println("Host: 192.168.1.8");
    client.println();

Thanks in advance !!!!

What model Arduino are you running this code on?

Arduino Mega 2560 r3 and w5100 Ethernet shield

You might try the HTTP/1.1 approach like below.

    client.println(" HTTP/1.1");
    //client.println("Host: 192.168.1.8");
    client.println("Connection: close");
    client.println();

What generates the “DEBUG 1” message? Is that the PHP server response? Is that normal?

I would try assembling the entire GET request string into the outBuf array and send it in one packet.

client.write(outBuf);

SurferTim: What generates the "DEBUG 1" message? Is that the PHP server response? Is that normal?

I would try assembling the entire GET request string into the outBuf array and send it in one packet.

client.write(outBuf);

I also tried that way using sprintf and but i get the same problem. In addition i can't pass double/float values using sprintf function. :(

The screenshot is from another part of code that i tried:

      sprintf(pageAdd,"/write3.php?value0=%d&value1=%d&value2=%f&value3=%lu&value4=%f&value5=%f&value6=%f&value7=%f&value8=%f", value0, dht_humidity, temperature, pressure, altitude, gust, dir, rain, knots);
    if(!getPage(server,serverPort,pageAdd)) Serial.print(F("Fail "));
  if(client.connect(ipBuf,thisPort))
  {
    Serial.println(F("connected"));
    sprintf(outBuf,"GET %s HTTP/1.0",page);
    Serial.println(F("DEBUG 1"));
    client.println(outBuf);
    Serial.println(F("DEBUG 2"));
    sprintf(outBuf,"Host: %s\r\n",serverName);
    Serial.println(F("DEBUG 3"));
    client.println(outBuf);

  }

zoomkat: You might try the HTTP/1.1 approach like below.

    client.println(" HTTP/1.1");
    //client.println("Host: 192.168.1.8");
    client.println("Connection: close");
    client.println();

I tried using http 1.1 - no difference

Is there any chance that the problem is not on the http request side but on any other part of the code?

Tried that code

    client.print("GET /write3.php?value0=");  //GET to php file which handle data (value0 and value1) into a database
    //Temperature
    client.print(value0); 
    //Humidity
    client.print("&value1=");
    client.print(dht_humidity); 
    //bmp085 values
    client.print("&value2=");
    client.print(temperature);
    
    client.print("&value3=");
    client.print(pressure);
    
    client.print("&value4=");
    client.print(altitude);
    
    //Weather Meters (Wind speed, direction, rain)
    client.print("&value5=");
    client.print(gust); //Wind speed every 20 seconds
    
    client.print("&value6=");
    client.print(dir);
    
    client.print("&value7=");
    client.print(rain); 
    
    client.print("&value8=");
    client.print(knots); //Wind speed every 60 seconds

    client.println(" HTTP/1.1");
    client.println("Host: 192.168.1.3");
    client.println("Connection: close");
    client.println();

Hanged after 197 succesful http requests.... That sucks i really dont know what to do i am stuck for a month

The client.print and client.println functions send the data one byte per packet. A request that long may be causing problems on the server end. Use the client.write function instead. Each call to client.write send the string in one packet.

The float data type is not supported by sprintf, so use the dtostrf function for float and double types to strings (character arrays). http://www.atmel.no/webdoc/AVRLibcReferenceManual/group__avr__stdlib_1ga060c998e77fb5fc0d3168b3ce8771d42.html

char outBuf[128];
char tBuf[16];

strcpy(outBuf,"GET /write3.php?value0=");
itoa(value0,tBuf,10);
strcat(outBuf,tBuf);

strcat(outBuf,"&value1=");
itoa(dht_humidity,tBuf,10);
strcat(outBuf,tBuf);

// and so on, then send as one packet
client.write(outBuf);

SurferTim: The client.print and client.println functions send the data one byte per packet. A request that long may be causing problems on the server end. Use the client.write function instead. Each call to client.write send the string in one packet.

The float data type is not supported by sprintf, so use the dtostrf function for float and double types to strings (character arrays). http://www.atmel.no/webdoc/AVRLibcReferenceManual/group__avr__stdlib_1ga060c998e77fb5fc0d3168b3ce8771d42.html

char outBuf[128];
char tBuf[16];

strcpy(outBuf,"GET /write3.php?value0="); itoa(value0,tBuf,10); strcat(outBuf,tBuf);

strcat(outBuf,"&value1="); itoa(dht_humidity,tBuf,10); strcat(outBuf,tBuf);

// and so on, then send as one packet client.write(outBuf);

Thanks for the reply, i will try it out, but if i convert everything to string then how will i use the data to draw charts as i want ? :(

EDIT: It hanged again after 125 successful requests on linux......

If it fails without a timeout, then you must be running out of SRAM, overflowing an array, or having a power problem.

It appears you have a servo connected. How are you powering the servo? Are you using the Arduino's 5v bus?

SurferTim: If it fails without a timeout, then you must be running out of SRAM, overflowing an array, or having a power problem.

It appears you have a servo connected. How are you powering the servo? Are you using the Arduino's 5v bus?

Thanks for the reply SurferTim... I really appreciate your will to help.!!!

I dont use servos. I might will in the future but i must get through this problem first. I also tried powering the arduino with external power supply, apart the USB, nothing changed expect the voltage at the sensors that raised from ~4.75 to 4.99.

Arduino loops now for ~16 hours without any problem on the WINDOWS server. 2400 succesful requests(It someties hangs for about two minuites then it recovers), but the idea is to get it running on the raspberry PI server.

Isnt it weired that it runs ok on the windows but hangs on linux ? Is there any chance that the problem is somewhere else beyond the arduino code?

Shall i use your .free method to check the available SRAM or is there any other way?

I updated the code a little bit. The new version is HERE:

https://github.com/mKontakis/Arduino/blob/master/WeatherMeters/ver8/ver8.ino

Thank you very very much !!!

I tested my client sketch on a RPi server, and it works ok. I used the first (GET method) of the two sketches here: http://playground.arduino.cc/Code/WebClient

If the connection fails or the server stalls, the Arduino code should show a "Timeout" message after 10 seconds and continue to attempt the connection every 30 seconds. Mine does anyway. I am using a R2 version Mega 2560 and a R2 version ethernet shield. I have tested it with Windows IIS, Linux and RPi servers.

SurferTim: I tested my client sketch on a RPi server, and it works ok. I used the first (GET method) of the two sketches here: http://playground.arduino.cc/Code/WebClient

If the connection fails or the server stalls, the Arduino code should show a "Timeout" message after 10 seconds and continue to attempt the connection every 30 seconds. Mine does anyway. I am using a R2 version Mega 2560 and a R2 version ethernet shield. I have tested it with Windows IIS, Linux and RPi servers.

I know, i use the same code for the web client part but i never get "Timeout" when it hangs...!

Then I suspect you are running out of SRAM. Add this function to your sketch. Call it in setup to check the available memory before starting the loop, then call it again just before attempting the connection.

int freeRam() {
  extern int __heap_start,*__brkval;
  int v;
  return (int)&v - (__brkval == 0 ? (int)&__heap_start : (int) __brkval);  
}

This code does not show zero when no memory is available. It will show a negative value, or an unrealistic large value.

You should use the F() function on your static strings. That will save you a bunch of SRAM.

Then I suspect you are running out of SRAM. Add this function to your sketch. Call it in setup to check the available memory before starting the loop, then call it again just before attempting the connection.

Again it hanged with 6199 free memory....

    Serial.println("DEBUG___2");
    client.write(outBuf);
    Serial.println("DEBUG___3");
    client.println(" HTTP/1.1");
    Serial.println("DEBUG___4");
    client.println("Host: 192.168.1.3");
    Serial.println("DEBUG___5");
    client.println("Connection: close");
    client.println();

If the SRAM is ok, then you must be overflowing an array. Are you certain you are not overflowing the outBuf array?

I've tested my client sketch for thousands of connections. I do not baby my ethernet shield. I torture it intentionally. I've tried everything I know to get it to fail. I broke the connection by pulling out the CAT5 cable in the middle of a download, rebooted the router and the server. If it doesn't finish the download, I get a "Timeout" message and it resumes as if nothing went wrong.

Have you tried only my client sketch from the playground? See how long it will run without any of your other libraries.

I think your code for the web client is the best out there. With just the dht11 sensor i tested ur code and it was ok for ~20 hours.
Can you share any technique for checking if an array overflows? But to be honest i dont think this is the problem since the code performs well on windows.

Can you think anything that makes the RPI server hangs?

If my code was ok for 20 hours, and yours won't run for a couple hours, then you have a problem with your code, not the server.

I have not had any problems with mu RPi Apache/PHP/MySQL server. I tested it for 2 days with my client code without a lockup.

Use this to check the length of outBuf before sending it to the server. If it reports a length more than 127 characters, you have an overflow condition.

Serial.print(F("outBuf length = "));
Serial.println(strlen(outBuf),DEC);

edit: Also insure you have no illegal characters in that array. That will also cause serious problems with a server. No spaces, additional question marks, etc.

SurferTim: If my code was ok for 20 hours, and yours won't run for a couple hours, then you have a problem with your code, not the server.

I have not had any problems with mu RPi Apache/PHP/MySQL server. I tested it for 2 days with my client code without a lockup.

Yes but how do you explain the fact that on windows the same code performs well?