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