ESP32 Freezes at 4am everyday

Hi. I need some advise on what I could do to solve my issue I have with my project. I have an ESP32 board that collects the temperature from a locally connectd DS18B20 probe and publishes it to an MQTT broker. It works great...most of the time. However, without fail, everyday at 4am the ESP stops responding and I do not know how to debug this. Any guidance would be appreciated.

Thanks
Juan

#include <WiFi.h>
#include <PubSubClient.h>
#include <Wire.h>
#include <OneWire.h>
#include <DallasTemperature.h>

//define the onboard LED 
#define ONBOARD_LED  2

const char* ssid = "xxx";
const char* password =  "xxx";
const char* mqttServer = "xxx";
const int mqttPort = xxx;
const char* mqttUser = "xxx";
const char* mqttPassword = "xxx";

const byte SWITCH_PIN = 0;           // Pin to control the light with
const char *ID = "Example_Switch";  // Name of our device, must be unique
const char *TOPIC = "esp32/lora/devices/gateway";  // Topic to subcribe to

// GPIO where the DS18B20 is connected to
const int oneWireBus = 4;     

// Setup a oneWire instance to communicate with any OneWire devices
OneWire oneWire(oneWireBus);

// Pass our oneWire reference to Dallas Temperature sensor 
DallasTemperature sensors(&oneWire);

WiFiClient espClient;
PubSubClient client(espClient); // Setup MQTT client
bool state=0;

// Connect to WiFi network
void setup_wifi() {
  Serial.print("\nConnecting to ");
  Serial.println(ssid);

  WiFi.disconnect();
  WiFi.mode(WIFI_AP_STA);
  WiFi.begin(ssid, password); // Connect to network

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

  Serial.println();
  Serial.println("WiFi connected");
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());
}

// Reconnect to client
void reconnect() {
  // Loop until we're reconnected
  while (!client.connected()) {
    digitalWrite(ONBOARD_LED,HIGH);
    Serial.print("Attempting MQTT connection...");
    // Attempt to connect
    if (client.connect(ID, mqttUser, mqttPassword )) {
      Serial.println("connected");
      Serial.print("Publishing to: ");
      Serial.println(TOPIC);
      Serial.println('\n');

    } else {
      Serial.println(" try again in 5 seconds");
      // Wait 5 seconds before retrying
      delay(5000);
    }
  }
  
  digitalWrite(ONBOARD_LED,LOW);
  
}

void setup() {
  Serial.begin(115200); // Start serial communication at 115200 baud
  pinMode(ONBOARD_LED,OUTPUT);
  delay(1000);
  setup_wifi(); // Connect to network

  Serial.println("Connecting Temperature Sensor");
  // Start the DS18B20 sensor
  sensors.begin();
  delay(1000);
  Serial.println("Temperature Sensor Connected");
  
  client.setServer(mqttServer, mqttPort);
}

void loop() {

  if (WiFi.status() != WL_CONNECTED)
  {
  Serial.println("WiFi disconnected ");
  setup_wifi();
  }

  if (!client.connected())  // Reconnect if connection is lost
  {
    Serial.println("MQTT disconnected ");
    reconnect();
  }

  client.loop();

  // Local Sensor
  sensors.requestTemperatures(); 
  float temperatureC = sensors.getTempCByIndex(0);
  Serial.print(temperatureC);
  Serial.println("°C");

  char result[8]; // Buffer big enough for 7-character float
  dtostrf(temperatureC, 6, 2, result); // Leave room for too large numbers!

  client.publish("esp32/lora/devices/gateway/temperature", result);
  
  delay(1000);

  long locrssi = 0;
  locrssi = WiFi.RSSI();

  char result2[8]; // Buffer big enough for 7-character float
  dtostrf(locrssi, 6, 2, result2); // Leave room for too large numbers!
  client.publish("esp32/lora/devices/gateway/rssi", result2);

  delay(1000);
  
}

The delay functions in the loop can be a problem for ESP boards, but two seconds should be safe. How do you know it's the ESP that is stopping and not the Broker?

Can you have the ESP32 connected to a computer with Serial Monitor running during the evil time? You have some pretty good debug output in your sketch and this will give you some idea of what is happening.

I suspect there is some sort of timed event that happens on the broker, but you should be able to make your code resilient to this. It looks like you've already added some nice measures in your sketch to allow it to recover from network issues.

@SteveMann, I suppose my assumption was that it was the ESP32 as I simply toggle the power to the device and it corrects itself. I take your point on board though, it may very well be the broker (in my case Home Assistant).

@pert. I may have to go down this road and see what the output from the serial monitor is. 4am start for me then :wink:

Hi,
Welcome to the forum.

If you are using an ESP32 pcb assembly, it possibly has an LED onboard that is connected to one of the I/O pins.
Using "blink without delay" example, you could use this LED as a heartbeat for the ESP32.
If you have any hardware problems that cause your code to lock-up then the heatbeat LED will stop beating.

Tom... :slight_smile:

Did you get to the bottom of this? I'm using an ESP8266 which stops talking every day at 4:10am, irrespective of when I last rebooted it. I think I just need to tweak my reconnection code which I'll try today, but I also noticed my node-red MQTT client also gets disconnected at the same time then reconnects 10 seconds later, I'm thinking the broker (mosquitto on a Pi) is kicking everyone off at that time??