Improving the WiFi temperature logger

Hi everyone,
I’m basing on a sketch from http://www.instructables.com/id/DIY-Smart-Plant-pot/ to build a WiFi thermostat which measures temperature and remotely switches an electric infrared heater in my home (while logging temperature on www.Thingspeak.com).

Everything is basically working, with 2 minor problems:

  1. It stops sending data to thingspeak after ~5 minutes, although temperature appears on the serial monitor. I solved this in a dirty way by restarting ESP8266 (setting Pin 13 to low then high) and reconnecting. I’ve uploaded my sketch below.
    Is this happening to anyone else?

  2. I’m using a TMP36 sensor which outputs 10mV/C, connected to the Cactus Micro A1 pin. By default, the A2D resolution is 3600/1024 = 3.5 mV which is 0.35 C. This is not good enough for a sensitive PID loop and I’m looking for ways to improve this. I want to try several ways:
    a. How do I change the analog reference voltage AREF on the cactus micro?**
    b. Should I try amplifying the signal from the TMP36? If so, how?
    c. I’ve got a few more TMP36 sensors around. Can I connect them in serial in some way to sum their signal?

Thanks!
Yuval

Sketch:


char str_temp2[6];
char str_PID_Input[6];

int temp2_integer=0;

float temp2_float = 0;

int status;
int wifiConnected = 0;

define DEBUG 0  // Please set DEBUG = 0 when USB not connected

define _baudrate 9600

// Used pins (Excl. LCD):
define wifi_pin 13
define wifiLED 9

define sensor_temp2 19 // TMP36
define calibration_offset 0.56 // offset for TMP36 (deg C offset * 0.01 V/C)

define SSID "XXXXX"  //  YOUR WIFI NETWORK NAME
define PASS "XXXXX"  //  YOUR WIFI PASSWORD
define IP "184.106.153.149"

// Periodically reset the ESP8266 module to try keep it running
const long interval = 120000;           // interval at which to restart ESP8266 (milliseconds)
unsigned long previousMillis = 0;        // will store last time ESP8266 was reset


String GET = "GET /update?key=XXXXX";  // YOUR THINSPEAK.COM ACCOUNT API KEY

void setup() {
// WiFi Stuff 
  pinMode (wifi_pin, OUTPUT);
  digitalWrite (wifi_pin, HIGH);
  delay(1000);

  pinMode (wifiLED, OUTPUT);
  digitalWrite(wifiLED, HIGH);

  Serial1.begin( _baudrate );

  if(DEBUG) { 
    Serial.begin( _baudrate );
    while(!Serial);
  }

  //set mode needed for new boards
  Serial1.println("AT+RST");
  delay(3000);//delay after mode change       
  Serial1.println("AT+CWMODE=1");
  delay(300);
  Serial1.println("AT+RST");
  delay(500);

}

void loop() {

  if(!wifiConnected) {
    Serial1.println("AT");
    digitalWrite(wifiLED,HIGH);
    delay(2000);
    if(Serial1.find("OK")){
      if(DEBUG) { Serial.println("Module Test: OK");}
      connectWifi();

      if (wifiConnected) {
          String cmd = "AT+CIPMUX=0";
          sendDebug( cmd );
          if( Serial1.find( "Error") )
          {
               if(DEBUG) { Serial.print( "RECEIVED: Error" );}
               digitalWrite(wifiLED,HIGH);
               return;
          }
      }
    } 
  }

  if(!wifiConnected) {
    delay(500);
    digitalWrite(wifiLED,HIGH);
    return;
  }

// --- Now WiFi is connected, continue
temp2_integer = analogRead(sensor_temp2);

temp2_float = (double(temp2_integer) / 1023 * 3.6 - calibration_offset) * 100; // Added calibration offset to the TMP36

//TMP36
  dtostrf(temp2_float, 4, 2, str_temp2); 

  if(DEBUG) {   
  Serial.print("TMP36 ");
  Serial.println(str_temp2);
  }

  updateTS(str_temp2); //In case of a single channel
  delay(5000); //added delay here

// Now reset the ESP8266
unsigned long currentMillis = millis();

  if (currentMillis - previousMillis >= interval) {
    // save the last time you reset the WiFi
    previousMillis = currentMillis;

      // Reset the WiFi
    digitalWrite(wifi_pin, LOW);
    delay(500);
    digitalWrite(wifi_pin, HIGH);
    digitalWrite(wifiLED, HIGH);
    wifiConnected = 0;
    connectWifi();
  }

}

//----- update the Thingspeak string with 1 value
void updateTS( String T)
{
  String cmd = "AT+CIPSTART=\"TCP\",\"";
  cmd += IP;
  cmd += "\",80";
  sendDebug(cmd);
  delay(2000);
  if( Serial1.find( "Error" ) )
  {
    Serial1.print( "RECEIVED: Error\nExit1" );
    return;
  }

  cmd = GET + "&field1=" + T +"\r\n";
  Serial1.print( "AT+CIPSEND=" );
  Serial1.println( cmd.length() );

  //display command in serial monitor
  if(DEBUG) { Serial.print("AT+CIPSEND=");
  Serial.println( cmd.length() );}

  if(Serial1.find(">")) {
    // The line is useless
    //Serial1.print(">");
    Serial1.print(cmd);
    if(DEBUG) { Serial.print(cmd);}
    delay(1000);
    sendDebug( "AT+CIPCLOSE" );
  } else {
    sendDebug( "AT+CIPCLOSE" );
    return;
  }


  if( Serial1.find("OK") )
  {
    Serial1.println( "RECEIVED: OK" );
  }
  else
  {
    Serial1.println( "RECEIVED: Error\nExit2" );
  }
}

void sendDebug(String cmd)
{
  if(DEBUG) { Serial.print("SEND: ");
  Serial.println(cmd);}
  Serial1.println(cmd);

}

boolean connectWifi() {     
 String cmd="AT+CWJAP=\"";
 cmd+=SSID;
 cmd+="\",\"";
 cmd+=PASS;
 cmd+="\"";
 if(DEBUG) { Serial.println(cmd);}
 Serial1.println(cmd);

 for(int i = 0; i < 20; i++) {
   if(DEBUG) { Serial.print(".");}
   if(Serial1.find("OK"))
   {
     wifiConnected = 1;
     ;

     break;
   }

   delay(50);
 }

 if(DEBUG) { Serial.println(
   wifiConnected ? 
   "OK, Connected to WiFi." :
   "Can not connect to the WiFi."
 );}

 digitalWrite(wifiLED,wifiConnected ? 
   LOW :
   HIGH
 );

 return wifiConnected;
}