Skip to content

Instantly share code, notes, and snippets.

@JorinL
Last active June 20, 2019 06:31
Show Gist options
  • Save JorinL/d97f3f2d95b93de946832c38409356ef to your computer and use it in GitHub Desktop.
Save JorinL/d97f3f2d95b93de946832c38409356ef to your computer and use it in GitHub Desktop.
//global vars needed
unsigned long WiFifix = 0;
unsigned long problemDetected = 0;
int problemCount = 0;
String problemCause;
void fixWiFi() {
// Posts every 10 seconds the state of WiFi.status(), Homie.getMqttClient().connected() and Homie.isConfigured()
// Within this interval the connectivity is checked and logged if a problem is detected
// Then it disconnects Wifi, if Wifi or MQTT is not connected for 1 Minute (but only if Homie is configured)
if ( WiFifix == 0 || ((millis() - WiFifix) > 10000)) {
if (Homie.isConfigured() == 1) {
float rssi = WiFi.RSSI();
Homie.getLogger() << endl;
relayNode.setProperty("quality").send(String(rssi));
Homie.getLogger() << "Wifi-state:" << WiFi.status() << " | Wi-Fi signal quality:" << rssi << " | MQTT-state:" << Homie.getMqttClient().connected() << " | HomieConfig-state:" << Homie.isConfigured() << endl;
if (!Homie.getMqttClient().connected() || WiFi.status() != 3) {
if (0 == problemDetected) {
if (WiFi.status() != 3) {
problemCause = "WiFi: Disconnected ";
}
if (!Homie.getMqttClient().connected()) {
problemCause += "MQTT: Disconnected";
}
Homie.getLogger() << "Connectivity in problematic state --> " << problemCause << endl;
problemDetected = millis();
}
else if ((millis() - problemDetected) > 120000 && (problemCount >= 5)) {
Homie.getLogger() << "Connectivity in problematic state --> This remained for 10 minutes. Rebooting!" << endl;
Homie.reboot();
}
else if ((millis() - problemDetected) > 120000 && problemCount < 5) {
problemCount = (problemCount + 1);
Homie.getLogger() << "Connectivity in problematic state --> " << problemCause << "/n This remained for 2 minutes. Disconnecting WiFi to start over." << endl;
problemDetected = 0;
problemCause = "";
WiFi.disconnect();
}
}
else if (problemCount != 0 && Homie.getMqttClient().connected() || WiFi.status() == 3) {
problemCount = 0;
ArduinoOTA.setHostname(Homie.getConfiguration().deviceId);
ArduinoOTA.begin();
}
}
WiFifix = millis();
}
}
//use fixWiFi(); inside the arduino loop
@JorinL
Copy link
Author

JorinL commented Jan 22, 2019

With the greatest help and contribution from @kleini

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment