If we can't reconnect after 2 attempts and a delay fo 2s, then restart

Thankfully, the ESP32 has an inbuilt function to reboot it :D
This commit is contained in:
Starbeamrainbowlabs 2022-07-13 02:01:31 +01:00
parent 491b87c0b3
commit 754f1591f7
Signed by: sbrl
GPG key ID: 1BE5172E637709C2

View file

@ -137,10 +137,21 @@ void connect_mqtt() {
Serial.println("ok");
}
void check_mqtt() {
void check_mqtt(bool reset = false) {
if(mqtt.connected() == 0) {
if(reset) {
Serial.print("MQTT reconnect: ");
connect_mqtt();
delay(2000);
if(mqtt.connected() == 0) {
Serial.println("failed! rebooting.\n\n");
ESP.restart();
}
}
else {
Serial.print("MQTT reconnect: ");
connect_mqtt();
}
}
else {
Serial.println("MQTT: connection ok");
@ -159,7 +170,8 @@ bool send_mqtt(String id, String sensor, float value) {
// char* payload = new char[length]();
// serializeJson(data, payload, length);
check_mqtt();
check_mqtt(false);
check_mqtt(true);
bool result = mqtt.publish("sensors/data", payload.c_str());
// delete[] payload;