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:
parent
491b87c0b3
commit
754f1591f7
1 changed files with 16 additions and 4 deletions
|
@ -137,10 +137,21 @@ void connect_mqtt() {
|
||||||
Serial.println("ok");
|
Serial.println("ok");
|
||||||
}
|
}
|
||||||
|
|
||||||
void check_mqtt() {
|
void check_mqtt(bool reset = false) {
|
||||||
if(mqtt.connected() == 0) {
|
if(mqtt.connected() == 0) {
|
||||||
|
if(reset) {
|
||||||
Serial.print("MQTT reconnect: ");
|
Serial.print("MQTT reconnect: ");
|
||||||
connect_mqtt();
|
connect_mqtt();
|
||||||
|
delay(2000);
|
||||||
|
if(mqtt.connected() == 0) {
|
||||||
|
Serial.println("failed! rebooting.\n\n");
|
||||||
|
ESP.restart();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
Serial.print("MQTT reconnect: ");
|
||||||
|
connect_mqtt();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
Serial.println("MQTT: connection ok");
|
Serial.println("MQTT: connection ok");
|
||||||
|
@ -159,7 +170,8 @@ bool send_mqtt(String id, String sensor, float value) {
|
||||||
// char* payload = new char[length]();
|
// char* payload = new char[length]();
|
||||||
// serializeJson(data, payload, length);
|
// serializeJson(data, payload, length);
|
||||||
|
|
||||||
check_mqtt();
|
check_mqtt(false);
|
||||||
|
check_mqtt(true);
|
||||||
bool result = mqtt.publish("sensors/data", payload.c_str());
|
bool result = mqtt.publish("sensors/data", payload.c_str());
|
||||||
// delete[] payload;
|
// delete[] payload;
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue