works
This commit is contained in:
@ -282,8 +282,12 @@ void callback(char* topic, byte* payload, unsigned int length) {
|
|||||||
|
|
||||||
|
|
||||||
void reconnect() {
|
void reconnect() {
|
||||||
|
uint32_t currentMillis = millis();
|
||||||
|
static uint32_t lastMillis = 0;
|
||||||
|
|
||||||
// Loop until we're reconnected
|
// Loop until we're reconnected
|
||||||
while (!client.connected()) {
|
if (!client.connected() && (currentMillis > (lastMillis + RECONNECT_DELAY))) {
|
||||||
|
lastMillis = currentMillis;
|
||||||
#ifdef DEBUG
|
#ifdef DEBUG
|
||||||
Serial.print("Attempting MQTT connection...");
|
Serial.print("Attempting MQTT connection...");
|
||||||
#endif
|
#endif
|
||||||
@ -308,23 +312,11 @@ void reconnect() {
|
|||||||
Serial.print(client.state());
|
Serial.print(client.state());
|
||||||
Serial.println(" try again in 5 seconds");
|
Serial.println(" try again in 5 seconds");
|
||||||
#endif
|
#endif
|
||||||
// Wait 5 seconds before retrying
|
|
||||||
delay(5000);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
bool mqtt_connect() {
|
|
||||||
bool reconnected = false;
|
|
||||||
if (!client.connected()) {
|
|
||||||
reconnect();
|
|
||||||
reconnected = true;
|
|
||||||
}
|
|
||||||
client.loop();
|
|
||||||
return reconnected;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
void setupApplication() {
|
void setupApplication() {
|
||||||
client.setServer(configBlock.mqttBroker, configBlock.mqttPort);
|
client.setServer(configBlock.mqttBroker, configBlock.mqttPort);
|
||||||
@ -344,10 +336,10 @@ void setupApplication() {
|
|||||||
|
|
||||||
|
|
||||||
void loopApplication() {
|
void loopApplication() {
|
||||||
bool reconnected = mqtt_connect();
|
if (!client.connected()) {
|
||||||
static uint32_t reconnectTime = 0;
|
reconnect();
|
||||||
if (reconnected) {
|
} else {
|
||||||
reconnectTime = millis();
|
client.loop();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user