ESP8266 freeze over time

Hi!

I'm using a Huzzah ESP8266 to send some data over MQTT with PubSub client, it used to work well but since I've change a bit of code now it freeze after some random time (usualy after 10-20min) I've tried to Ping it without any result, I have an old ESP8266 with old library, this one is working so I don't want to flash it, the other one that's the one who freeze, is there anybody having the same issues with theses boards?

Can you get any error message on the Serial console?

either it is the code that is at fault or the board

eliminate the code by flashing the new board with the old code
if it works then you know it is the code that is at fault

No error message from the console, I will try to flash with the old code but my concern is that it was working with an old pubsub library, since I've upgraded it, when compiling it give me error message, so I've taken a exemple from the new library and paste back all my code without any error... and I don't remember which version the library was.

Ok I've flashed my old board and it still freeze but now maybe after 1 day...

I'm using pin 2,4,12 for sensors are theses pins connected to a sleep timer?

Then start putting in lots of print statements so you know exactly what's happening in your code, what values various variables have, and where the freeze happens. Make sure to include a heartbeat as well (a message that simply goes out every few seconds to show the thing is still alive). Standard debugging, so to say.

You could also consider to post your code, circuit diagram, and any other info that may be relevant.

Here is my code.

/*
Labo Sensors
Connected to Huzzah ESP8266

Contains theses sensors:
-Temperature and humidity
  
*/

#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include "DHT.h"

// DHT22 Temperature sensor settings
#define DHTPIN 2     // what digital pin we're connected to temperature sensor
#define DHTTYPE DHT22   // DHT 22  (AM2302), AM2321

// Initialize DHT sensor.

DHT dht(DHTPIN, DHTTYPE);

// Light Sensor
const int light = 4; // Sensor is connected to ESP8266 pin #4
int lightState = 0; // Reset the light sensor
char* lightOnOff[]={"OFF","ON"};

// Water leak sensor
const int flood = 12; // the flood sensor that is connected to pin 12
int floodState = 0; //Reset the flood detector

//Network value
const char* ssid = "Network";
const char* password ="Password";
const char* mqtt_server = "10.0.1.101";

WiFiClient espClient;
PubSubClient client(espClient);
char message_buff[50];

void setup() {
Serial.begin(115200); // Starts the serial communication
setup_wifi();
client.setServer(mqtt_server, 1883);
client.setCallback(callback);
Serial.println("DHTxx test!");
dht.begin();

}

void loop() {
  if (!client.connected()){
  reconnect();
}
client.loop();
Serial.println("");
Serial.println("Sending Value by MQTT");
sendMessage(); //Transmit by MQTT different sensors

}

//sendMessage Function
void sendMessage(){
  //Temperature and Humidity sensor
  delay(2000);
  float h = dht.readHumidity();// Humidity
  float t = dht.readTemperature();// Temperature in Celsius
  String pubString = String(t);
  pubString.toCharArray(message_buff, pubString.length()+1);
  client.publish("Boubounouche/Labo/Temperature", message_buff);
  pubString = String(h);
  pubString.toCharArray(message_buff, pubString.length()+1);
  client.publish("Boubounouche/Labo/Humidity", message_buff);  

  //Publishing Light Sensor
  lightState = digitalRead(light);
  Serial.print(F("\nSending light sensor val "));
  Serial.println(lightOnOff[lightState]);
  pubString = String(lightOnOff[lightState]);
  pubString.toCharArray(message_buff, pubString.length()+1);
  client.publish("Boubounouche/Labo/LightSensor", message_buff);

  // Publishing Flood detector hot water tank
  floodState = digitalRead(flood);
  Serial.println(floodState);
  pubString = String(floodState);
  pubString.toCharArray(message_buff, pubString.length()+1);
  client.publish("Boubounouche/Labo/Flood", message_buff); 
  
  delay(8000);
}

// Establishing connection to Wifi
void setup_wifi(){
 delay(10);
 Serial.println();
 Serial.print("Connecting to ");
 Serial.println(ssid);

 WiFi.begin(ssid, password);

 while (WiFi.status() != WL_CONNECTED){
  delay(500);
  Serial.print(".");
 }

 Serial.println("");
 Serial.println("WiFi Connected");
 Serial.println("IP address: ");
 Serial.println(WiFi.localIP());
}
void callback(char* topic, byte* payload, unsigned int length) {
}

void reconnect(){
//Loop until we're reconnected
while (!client.connected()){
  Serial.println("");
  Serial.print("Attempting MQTT connection...");
  //Attempe to reconnect
  if (client.connect("ESP8266Client")){
    Serial.println("connected");
    
  }
  else{
    Serial.print("failed reconnection");
    Serial.print(client.state());
    Serial.println("try again in 5 seconds");
    //Wait 5 seconds before retrying
    delay(5000);
    }
  }
}

Here is the schematic

Homer v3.pdf (15.6 KB)

So when it hangs, what is the point it hangs? You got quite some Serial.print statements there.

Also I notice this odd construction, coming back a few times:

  pubString = String(floodState);
  pubString.toCharArray(message_buff, pubString.length()+1);

Why not using sprintf or ftoa for a direct coversion? The String class may be causing the hanging