Fixed error blinks and improved WiFi reconnect

This commit is contained in:
Gunnar Skjold
2022-10-12 19:25:48 +02:00
parent 867ab9d6c2
commit ce3a47a7e6
3 changed files with 33 additions and 18 deletions

View File

@@ -1,7 +1,7 @@
#ifndef _AMSTOMQTTBRIDGE_H #ifndef _AMSTOMQTTBRIDGE_H
#define _AMSTOMQTTBRIDGE_H #define _AMSTOMQTTBRIDGE_H
#define WIFI_CONNECTION_TIMEOUT 30000; #define WIFI_CONNECTION_TIMEOUT 30000
#define INVALID_BUTTON_PIN 0xFFFFFFFF #define INVALID_BUTTON_PIN 0xFFFFFFFF

View File

@@ -436,6 +436,10 @@ void loop() {
} }
} }
if(now > 10000 && now - lastErrorBlink > 3000) {
errorBlink();
}
// Only do normal stuff if we're not booted as AP // Only do normal stuff if we're not booted as AP
if (WiFi.getMode() != WIFI_AP) { if (WiFi.getMode() != WIFI_AP) {
if (WiFi.status() != WL_CONNECTED) { if (WiFi.status() != WL_CONNECTED) {
@@ -506,10 +510,6 @@ void loop() {
MDNS.update(); MDNS.update();
#endif #endif
if(now > 10000 && now - lastErrorBlink > 3000) {
errorBlink();
}
if (mqttEnabled || config.isMqttChanged()) { if (mqttEnabled || config.isMqttChanged()) {
if(mqtt == NULL || !mqtt->connected() || config.isMqttChanged()) { if(mqtt == NULL || !mqtt->connected() || config.isMqttChanged()) {
MQTT_connect(); MQTT_connect();
@@ -725,22 +725,25 @@ void errorBlink() {
if(lastError == 3) if(lastError == 3)
lastError = 0; lastError = 0;
lastErrorBlink = millis(); lastErrorBlink = millis();
for(;lastError < 3;lastError++) { while(lastError < 3) {
switch(lastError) { switch(lastError++) {
case 0: case 0:
if(lastErrorBlink - meterState.getLastUpdateMillis() > 30000) { if(lastErrorBlink - meterState.getLastUpdateMillis() > 30000) {
debugW("No HAN data received last 30s, single blink");
hw.ledBlink(LED_RED, 1); // If no message received from AMS in 30 sec, blink once hw.ledBlink(LED_RED, 1); // If no message received from AMS in 30 sec, blink once
return; return;
} }
break; break;
case 1: case 1:
if(mqttEnabled && mqtt != NULL && mqtt->lastError() != 0) { if(mqttEnabled && mqtt != NULL && mqtt->lastError() != 0) {
debugW("MQTT connection not available, double blink");
hw.ledBlink(LED_RED, 2); // If MQTT error, blink twice hw.ledBlink(LED_RED, 2); // If MQTT error, blink twice
return; return;
} }
break; break;
case 2: case 2:
if(WiFi.getMode() != WIFI_AP && WiFi.status() != WL_CONNECTED) { if(WiFi.getMode() != WIFI_AP && WiFi.status() != WL_CONNECTED) {
debugW("WiFi not connected, tripe blink");
hw.ledBlink(LED_RED, 3); // If WiFi not connected, blink three times hw.ledBlink(LED_RED, 3); // If WiFi not connected, blink three times
return; return;
} }
@@ -985,17 +988,29 @@ void debugPrint(byte *buffer, int start, int length) {
Debug.println(""); Debug.println("");
} }
unsigned long wifiTimeout = WIFI_CONNECTION_TIMEOUT;
unsigned long lastWifiRetry = -WIFI_CONNECTION_TIMEOUT; unsigned long lastWifiRetry = -WIFI_CONNECTION_TIMEOUT;
void WiFi_connect() { void WiFi_connect() {
if(millis() - lastWifiRetry < wifiTimeout) {
delay(50);
return;
}
lastWifiRetry = millis();
if (WiFi.status() != WL_CONNECTED) { if (WiFi.status() != WL_CONNECTED) {
if(WiFi.status() == WL_DISCONNECTED) {
if(millis() - lastWifiRetry < WIFI_CONNECTION_TIMEOUT) {
return;
}
}
if(WiFi.getMode() != WIFI_OFF) { if(WiFi.getMode() != WIFI_OFF) {
switch(WiFi.status()) {
case WL_NO_SSID_AVAIL:
debugE("WiFi error, no SSID available");
break;
case WL_CONNECT_FAILED:
debugE("WiFi error, connection failed");
break;
case WL_CONNECTION_LOST:
debugE("WiFi error, connection lost");
break;
case WL_WRONG_PASSWORD:
debugE("WiFi error, wrong password");
break;
}
if(wifiReconnectCount > 3) { if(wifiReconnectCount > 3) {
ESP.restart(); ESP.restart();
return; return;
@@ -1029,11 +1044,11 @@ void WiFi_connect() {
WiFi.softAPdisconnect(true); WiFi.softAPdisconnect(true);
WiFi.enableAP(false); WiFi.enableAP(false);
WiFi.mode(WIFI_OFF); WiFi.mode(WIFI_OFF);
WiFi.forceSleepBegin();
yield(); yield();
wifiTimeout = 5000;
return; return;
} }
wifiTimeout = WIFI_CONNECTION_TIMEOUT; lastWifiRetry = millis();
WiFiConfig wifi; WiFiConfig wifi;
if(!config.getWiFiConfig(wifi) || strlen(wifi.ssid) == 0) { if(!config.getWiFiConfig(wifi) || strlen(wifi.ssid) == 0) {

View File

@@ -373,8 +373,8 @@ bool HwTools::ledBlink(uint8_t color, uint8_t blink) {
if(!ledOn(color)) return false; if(!ledOn(color)) return false;
delay(50); delay(50);
ledOff(color); ledOff(color);
if(i != blink) if(i != blink-1)
delay(50); delay(200);
} }
return true; return true;
} }