How to have arduino make http requests reliably for a long time

Hi,

I hope you are doing well!

Im using an Arduino Uno, a wifly shield and some of code taken from here( http://co2meters.com/Documentation/AppNotes/AN113-K33MicroComm.pdf : AN113 – Using the K33 ELG/BLG Sensor with a Microcontroller, written by Andrew Robinson) to make send co2 measurements to a site similar to pachube.com using http requests.

Everything seems to work smoothly but the system comes to a halt after a few hours (12-16hrs) of operation. I dont know if the sensor is doing something to the I2C ports on the microcontroller but something is happening that even the microcontroller’s reset function (watchdog) stops working and the entire system freezes. PLEASE take a look at the code and see if I am far from getting this right.

The system has to run reliably, without human help to turn power on-off or reset, for at least one month…

Any ideas on how to solve this problem will be highly appreciated. I have made my best effort and am still not there yet.

The code is a little long so i have attached it on a PDF.

here is the main function: (the rest of the code in PDF):

// Talks via I2C to K33?ELG/BLG Sensors for Host?Initiated Data Collection
// Submits collected data to myserver via HTTP POST

// Using the I2C hardware interface on the Arduino in
// combination with the built?in Wire library to interface.
//  Arduino analog input 5 ? I2C SCL  [el cable green]
//  Arduino analog input 4 ? I2C SDA  [el cable yellow]
//  basic read of the CO2 value and checksum verification.

#include <avr/wdt.h>
#include <Wire.h>
#include "WiFly.h"
#include "Credentials.h"

Client client("myserver.com", 80);         //Provide Server and Port to connect 

String feedtemp="3m793";//"3m793";//"3m749";                //PROVIDE THE FEED NAME like you would do on pachube.com 
String feedco2="3m78h";//"3m78h";//"3m748";                //PROVIDE THE FEED NAME pachube.com 
String feedrh="3m794";//"3m794";//"3m74i";                //PROVIDE THE FEED NAME

double actual = 107.43;
double tempValue = 0;
double rhValue = 0;
double co2Value = 0;

int co2Addr = 0x68;   // This is the default address of the CO2 sensor, 7bits shifted left.
int secondstowait = 0;
int cycletoreset = 0;

unsigned long time1=0;
unsigned long time2=0;
unsigned long reboot=10000;  //reboots arduino every REBOOT milli seconds (+8 seconds)
unsigned long start;
unsigned long duration;

void setup() {

  Serial.begin(9600);     
  Wire.begin (); delay(300);
  WiFly.begin(); delay(300);                   //START WIFLY
  WiFly.join(ssid, passphrase); delay(300);    //CONNECT TO WIFI USING NETWORK NAME AND PASSWORD FOUND IN CONFIG.H
//  wdt_enable(WDTO_8S);                         //Set up the watchdog timer. Si perro no pateado reset every 8 seconds 
//  wdt_reset();                                //Good practice to reset the watchdog in setup
  pinMode(13, OUTPUT);                        // We will use this pin as a read?indicator
  Serial.println("A RESET HAS BEEN MADE..........QUE PASO GASPAR? THE SERIAL,WIRE, AND WIFLY ARE TURNED (IN THEORY) ON!");
  start=millis();
}


void loop() {


//while (millis()-start<40000){
  

wdt_enable(WDTO_8S);
//-----------------  //READ DATA FROM SENSOR -------------------------------------------------
wakeSensor(); 
initPoll(); 
delaysinreset(16); //DELAY(16000);
wakeSensor();


double tempValue = readTemp();    delay(20); wakeSensor();


double rhValue = readRh();        delay(20);  wakeSensor();


double co2Value = readCo2();

//  if(co2Value >= 0) {                      //DISPLAY SENSOR VALUES TO THE SERIAL PORT (PARA DEBUGEAR)
        Serial.print("CO2: ");
        Serial.print(co2Value);
        Serial.print("ppm Temp: ");
        Serial.print(tempValue);
        Serial.print("C Rh: ");
        Serial.print(rhValue);
        Serial.println("%");
//  }     

// else {
//        Serial.println("Checksum failed / Communication failure");
//  }     

//-----------------  //POST DATA TO my server ----------------------------------------------------
      wdt_reset();
      char dummy[32] = {0};             //BUFFER TO STORE THE CO2 READING IN AN ARRAY OF CHARACTERS
      
      dtostrf(co2Value, 10, 2, dummy);  //converts doubles to chars : dtostrf(floatVar, minStringWidthIncDecimalPoint, numVarsAfterDecimal, charBuf);
      Serial.println("Valor a mandar, Co2:");
      Serial.println(dummy);
      postear(feedco2,dummy); wdt_reset(); 
      
      dtostrf(tempValue, 10, 2, dummy);
      Serial.println("Valor a mandar, Temperatura:");
      Serial.println(dummy);
      postear(feedtemp,dummy); wdt_reset();
      
      dtostrf(rhValue, 10, 2, dummy);
      Serial.println("Valor a mandar,Rh:");
      Serial.println(dummy);
      postear(feedrh,dummy);  wdt_reset();
      
      //NOTE POSSIBLE ERRRORS:
      // posting 00.xxx or 0x.xx  results in http 500 error. Cant start with two zeros !
      // posting the co2 value from the sensor someimes we get HTTP/1.1 503 Service Unavailable even when the format is correct

  
//}//END WHILE

//Serial.println("RESEEEETT");
//  Serial.println(millis()-start);
//  wdt_enable(WDTO_2S);
//  delay(3000);
  

  
}

co2 values to http.pdf (69.7 KB)

Can you post the code, not a Word document or whatever your attachment is?

Hi, Many thanks for your reply. The code is more than 9,500 characters so i put it in that word document because it does not fit here. I will PDF it also. Thank you !

Rather than attach a PDF or Word document, attach the code itself.