arduino web client random freeze

I have implemented a web client for arduino to sent data from my sensors to a database throw a GET Http request. The loop randomly freezes on DEBUG 4

This is the part of the code and here https://dl.dropboxusercontent.com/u/15854314/ver4.ino is the full sketch link.

You can also ask at stackoverflow if you like

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 "));
else Serial.print(F("Pass "));



byte getPage(IPAddress ipBuf,int thisPort, char *page)
{
  int inChar;
  char outBuf[128];

  Serial.print(F("connecting..."));

  if(client.connect(ipBuf,thisPort))
  {

     Serial.println(F("connected"));
    sprintf(outBuf,"GET %s HTTP/1.1",page);
    Serial.println(F("DEBUG 1"));
    client.println(outBuf);
    Serial.println(F("DEBUG 2"));
    sprintf(outBuf,"Host: %s",serverName);
    Serial.println(F("DEBUG 3"));
    client.println(outBuf);
    Serial.println(F("DEBUG 4"));
    client.println(F("Connection: close\r\n"));

  } 
  else
  {
    Serial.println(F("failed"));
    return 0;
  }

Thanks for your time!

When it gets hung does it stay hung for a long time? Have you tried waiting an hour or more?

I'd suggest trying this change:

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

When you drop to HTTP/1.0 you can skip the "Connection: close" part; persistent connections are not part of the HTTP/1.0 spec so you don't have to tell it to close. You can also skip the "Host: 192.168.1.3" part too since you're using what is assuredly the default hostname configured on your web server. The "Host:" header is still usually required with public web sites or with web servers using name-based virtualhosting so keep that in mind.

Chagrin:
When it gets hung does it stay hung for a long time? Have you tried waiting an hour or more?

I'd suggest trying this change:

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




When you drop to HTTP/1.0 you can skip the "Connection: close" part; persistent connections are not part of the HTTP/1.0 spec so you don't have to tell it to close. You can also skip the "Host: 192.168.1.3" part too since you're using what is assuredly the default hostname configured on your web server. The "Host:" header is still usually required with public web sites or with web servers using name-based virtualhosting so keep that in mind.

Two days ago same code it recovered after two minuites, when i posted the code it remained hung for hours.
I try now with ur changes

That is partly my client code. If it doesn't receive a packet for 10 seconds, it should timeout and continue. Where in the code is it freezing? What is the last message on the serial monitor when it freezes?

You should have posted this in the networking section. I would have seen this thread sooner.

SurferTim:
That is partly my client code. If it doesn't receive a packet for 10 seconds, it should timeout and continue. Where in the code is it freezing? What is the last message on the serial monitor when it freezes?

You should have posted this in the networking section. I would have seen this thread sooner.

Just froze and the last printed message was DEBUG 1.... I am in despair guys.... I try to fix this one week now !

Full project link: https://dl.dropboxusercontent.com/u/15854314/ver4.ino

Hang image:

This will cause you problems:

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

sprintf does not support the float data type. It will print a question mark instead of the float value. That would be an invalid character for a url.

SurferTim:
This will cause you problems:

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

sprintf does not support the float data type. It will print a question mark instead of the float value. That would be an invalid character for a url.

It prints 0 so it is not the problem. I also tried like that, still hangs....

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

Which Arduino are you using? I'm testing on a Mega 2560.

:.

mega 2560 r3, w5100 are you running my code ?

No, but I just tested the sprintf function, and it prints a question mark if you try to display a float data type.

Which IDE version are you using?

1.0.5 At phpmyadmin it logs 0 to database

With client.print() method, it just hanged and unhanged two minuites later. With sprintf method it always remained hanged.

Check the screenshot substraction (not substraction_2) value in milliseconds.

I ran this code...

void setup() {
  Serial.begin(115200);
  
  float testf = 123.4;
  char tBuf[32];
  sprintf(tBuf,"Float %f",testf);
  Serial.println(tBuf);
}

void loop() {
}

..and it prints this:

Float ?

A question mark is not allowed unless immediately following the filename.

SurferTim:
I ran this code...

void setup() {

Serial.begin(115200);
 
 float testf = 123.4;
 char tBuf[32];
 sprintf(tBuf,"Float %f",testf);
 Serial.println(tBuf);
}

void loop() {
}



..and it prints this:


> Float ?


A question mark is not allowed unless immediately following the filename.

This is not the problem anyway still hangs with the other way..!

Are you certain you are not overflowing the pageAdd array?

This method does not use pageAdd

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

It is running for 30 hours now without problem but sometimes it spikes on "Connected" and continues after two minuites.
Any ideas?

the code is this:

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

up!

up up !!