24 lines
593 B
C++
24 lines
593 B
C++
// Function to connect and reconnect as necessary to the MQTT server.
|
|
// Should be called in the loop function and it will take care if connecting.
|
|
int MQTT_connect() {
|
|
int8_t ret;
|
|
|
|
// Stop if already connected.
|
|
if (mqtt.connected()) {
|
|
return 0;
|
|
}
|
|
|
|
uint8_t retries = 3;
|
|
while ((ret = mqtt.connect()) != 0) { // connect will return 0 for connected
|
|
Serial.println(mqtt.connectErrorString(ret));
|
|
Serial.println("Retrying MQTT connection ...");
|
|
mqtt.disconnect();
|
|
delay(100); // wait
|
|
retries--;
|
|
if (retries == 0) {
|
|
return -1;
|
|
}
|
|
}
|
|
return 0;
|
|
}
|