Good afternoon, I want to make a continuously operable gateway that measures temperature with esp32. The device sends data via MQTT every 1.5 seconds. Esp freezes after 12 - 24 hours. I read that some users operate the device every 6-7 months without resetting, how can I operate the device for a long time without resetting it?
Note1: As a workaround in the code I reset the device with MQTT, Ethernet and ESP.restart () every 12 hours before it can freeze.
Note2: LEDs indicate that the system is working.
- Red lights when ethernet or MQTT cannot be connected.
- Blue indicates trying to connect to MQTT or ethernet.
- Green indicates that the system is active. When the device was frozen, the green led was still on.
Versions of libraries I use:
- Arduino IDE == 1.8.14
- ESP Library Version == 1.0.6
- MQTT Library Version == 2.5.0
- ESP Library Version == 2.0.9
ESP Link: https://www.direnc.net/esp32-wroom-32u-wifi-bluetooth-gelistirme-modulu-en
The code:
#include <UIPEthernet.h>
#include <ArduinoJson.h>
#include <MQTT.h>
#include <DHT.h>
//////////////////////// ETHERNET CONFİG //////////////////////////
byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
//////////////////////// ETHERNET CONFİG //////////////////////////
//////////////////////// LED & DHT22 CONFİG //////////////////////////
byte red = 2;
byte green = 0;
byte blue = 4;
byte temp = 15;
#define DHTTYPE DHT22
DHT dht = DHT(temp, DHTTYPE);
//////////////////////// LED & DHT22 CONFİG //////////////////////////
//////////////////////// MQTT CONFİG //////////////////////////
#define mqttServer IPAddress(xxx,xxx,xx,xxx)
const char* mqttUser = "test";
const char* mqttPassword = "test";
const char* deviceId = "Ai-Sens 1";
//////////////////////// MQTT CONFİG //////////////////////////
unsigned long lastMillis = 0;
byte x;
byte y;
EthernetClient net;
MQTTClient client;
//Receives an external reset command every 12 hours
void callback(String &topic, String &payload) {
if (payload == "1")
{
ESP.restart(); // This is just a workaround to avoid the freezing issue
}
}
void setup_eth() {
delay(10);
digitalWrite(blue, HIGH); // turn the LED on
//Serial.print("connecting ethernet...");
if (Ethernet.begin(mac) == 0) {
// Serial.println("Failed to configure Ethernet using DHCP");
for (;;)
;
}
Ethernet.begin(mac, Ethernet.localIP());
digitalWrite(blue, LOW);
}
void connect() {
//Serial.print("connecting mqtt...");
digitalWrite(blue, HIGH);
while (!client.connected()) {
if (client.connect(deviceId, mqttUser, mqttPassword)) {
// Subscribe
//client.subscribe("esp/data1");
} else {
delay(500);
}
}
//Serial.println("\nconnected!");
digitalWrite(blue, LOW);
//client.subscribe("esp/data1");
}
void setup() {
//Serial.begin(115200);
pinMode(red, OUTPUT);
pinMode(green, OUTPUT);
pinMode(blue, OUTPUT);
digitalWrite(red, LOW);
digitalWrite(green, LOW);
digitalWrite(blue, LOW);
dht.begin();
setup_eth();
client.setHost(mqttServer, 1883);
client.setKeepAlive(90);
client.begin(mqttServer, net);
connect();
}
void loop() {
client.loop();
delay(10);
client.subscribe("esp/rst");
client.onMessage(callback);
StaticJsonBuffer<300> JSON;
JsonObject& JSONencoder = JSON.createObject();
if (millis() - lastMillis > 1500) {
digitalWrite(green, LOW);
digitalWrite(blue, LOW);
digitalWrite(red, LOW);
if (!client.connected()) {
digitalWrite(red, HIGH);
if (y == 5) {
//Serial.println("Reboot System Now....");
ESP.restart();
y = 0;
}
y++;
connect();
}
if (Ethernet.linkStatus() == LinkON) {
// Serial.println("On");
digitalWrite(green, HIGH);
}
else {
digitalWrite(red, HIGH);
if (x == 5) {
//Serial.println("Reboot System Now....");
ESP.restart();
x = 0;
}
x++;
}
lastMillis = millis();
//Serial.println(Ethernet.maintain ());
JSONencoder["DEVICE = "] = deviceId;
JSONencoder["USER = "] = mqttUser;
JSONencoder["TEMP = "] = dht.readTemperature();
JSONencoder["HUMD = "] = dht.readHumidity();
JSONencoder["STATE = "] = true;
char JSONmessageBuffer[100];
JSONencoder.printTo(JSONmessageBuffer, sizeof(JSONmessageBuffer));
client.publish("esp/data1", JSONmessageBuffer);
}
}