diff --git a/src/httpserver/http_fns.c b/src/httpserver/http_fns.c index 75536b8c6..f5e4b3bee 100644 --- a/src/httpserver/http_fns.c +++ b/src/httpserver/http_fns.c @@ -2263,7 +2263,7 @@ const char* g_obk_flagNames[] = { "[MQTT] In HA discovery, add relays as lights", "[HASS] Deactivate avty_t flag for sensor when publishing to HASS (permit to keep value)", "[DRV] Deactivate Autostart of all drivers", - "error", + "[WiFi] Quick connect to WiFi on reboot (TODO: check if it works for you and report on github)", "error", "error", "error", diff --git a/src/mqtt/new_mqtt.c b/src/mqtt/new_mqtt.c index b35db5112..8d14f31dd 100644 --- a/src/mqtt/new_mqtt.c +++ b/src/mqtt/new_mqtt.c @@ -212,7 +212,7 @@ static obk_mqtt_request_t g_mqtt_request; static obk_mqtt_request_t g_mqtt_request_cb; #define LOOPS_WITH_DISCONNECTED 15 -int loopsWithDisconnected = 0; +int mqtt_loopsWithDisconnected = 0; int mqtt_reconnect = 0; // set for the device to broadcast self state on start int g_bPublishAllStatesNow = 0; @@ -1854,7 +1854,12 @@ int MQTT_RunEverySecondUpdate() if (Main_HasWiFiConnected() == 0) { mqtt_reconnect = 0; - loopsWithDisconnected = LOOPS_WITH_DISCONNECTED - 2; + if (Main_HasFastConnect()) { + mqtt_loopsWithDisconnected = LOOPS_WITH_DISCONNECTED + 1; + } + else { + mqtt_loopsWithDisconnected = LOOPS_WITH_DISCONNECTED - 2; + } return 0; } @@ -1892,14 +1897,14 @@ int MQTT_RunEverySecondUpdate() if (mqtt_client && res) { MQTT_disconnect(mqtt_client); - loopsWithDisconnected = LOOPS_WITH_DISCONNECTED - 2; + mqtt_loopsWithDisconnected = LOOPS_WITH_DISCONNECTED - 2; } } } if (mqtt_client == 0 || res == 0) { - //addLogAdv(LOG_INFO,LOG_FEATURE_MAIN, "Timer discovers disconnected mqtt %i\n",loopsWithDisconnected); + //addLogAdv(LOG_INFO,LOG_FEATURE_MAIN, "Timer discovers disconnected mqtt %i\n",mqtt_loopsWithDisconnected); #if WINDOWS #elif PLATFORM_BL602 #elif PLATFORM_W600 || PLATFORM_W800 @@ -1908,8 +1913,8 @@ int MQTT_RunEverySecondUpdate() if (ota_progress() == -1) #endif { - loopsWithDisconnected++; - if (loopsWithDisconnected > LOOPS_WITH_DISCONNECTED) + mqtt_loopsWithDisconnected++; + if (mqtt_loopsWithDisconnected > LOOPS_WITH_DISCONNECTED) { if (mqtt_client == 0) { @@ -1928,7 +1933,7 @@ int MQTT_RunEverySecondUpdate() } MQTT_do_connect(mqtt_client); mqtt_connect_events++; - loopsWithDisconnected = 0; + mqtt_loopsWithDisconnected = 0; } } MQTT_Mutex_Free(); diff --git a/src/mqtt/new_mqtt.h b/src/mqtt/new_mqtt.h index 0ec121c64..e68f470a0 100644 --- a/src/mqtt/new_mqtt.h +++ b/src/mqtt/new_mqtt.h @@ -147,6 +147,7 @@ void MQTT_InvokeCommandAtEnd(PostPublishCommands command); bool MQTT_IsReady(); extern int g_mqtt_bBaseTopicDirty; extern int mqtt_reconnect; +extern int mqtt_loopsWithDisconnected; // TODO: hide it, internal usage only #define MQTT_STACK_BUFFER_SIZE 32 diff --git a/src/new_common.h b/src/new_common.h index 0a52c19f5..3d3d7aaee 100644 --- a/src/new_common.h +++ b/src/new_common.h @@ -389,6 +389,7 @@ void Main_ScheduleHomeAssistantDiscovery(int seconds); int Main_IsConnectedToWiFi(); int Main_IsOpenAccessPointMode(); void Main_Init(); +bool Main_HasFastConnect(); void Main_OnEverySecond(); int Main_HasMQTTConnected(); int Main_HasWiFiConnected(); diff --git a/src/new_pins.h b/src/new_pins.h index c0c967baa..d86848d16 100644 --- a/src/new_pins.h +++ b/src/new_pins.h @@ -261,8 +261,9 @@ typedef struct pinsState_s { #define OBK_FLAG_MQTT_HASS_ADD_RELAYS_AS_LIGHTS 34 #define OBK_FLAG_NOT_PUBLISH_AVAILABILITY_SENSOR 35 #define OBK_FLAG_DRV_DISABLE_AUTOSTART 36 +#define OBK_FLAG_WIFI_FAST_CONNECT 37 -#define OBK_TOTAL_FLAGS 37 +#define OBK_TOTAL_FLAGS 38 #define CGF_MQTT_CLIENT_ID_SIZE 64 diff --git a/src/user_main.c b/src/user_main.c index c5f5ad528..3bd633305 100644 --- a/src/user_main.c +++ b/src/user_main.c @@ -333,6 +333,31 @@ void Main_ScheduleHomeAssistantDiscovery(int seconds) { g_doHomeAssistantDiscoveryIn = seconds; } +void Main_ConnectToWiFiNow() { + const char* wifi_ssid, *wifi_pass; + + g_bOpenAccessPointMode = 0; + wifi_ssid = CFG_GetWiFiSSID(); + wifi_pass = CFG_GetWiFiPass(); + HAL_ConnectToWiFi(wifi_ssid, wifi_pass); + // register function to get callbacks about wifi changes. + HAL_WiFi_SetupStatusCallback(Main_OnWiFiStatusChange); + ADDLOGF_DEBUG("Registered for wifi changes\r\n"); + g_connectToWiFi = 0; +} +bool Main_HasFastConnect() { + if (g_bootFailures > 2) + return false; + if (CFG_HasFlag(OBK_FLAG_WIFI_FAST_CONNECT)) { + return true; + } + if ((PIN_FindPinIndexForRole(IOR_DoorSensorWithDeepSleep, -1) != -1) || + (PIN_FindPinIndexForRole(IOR_DoorSensorWithDeepSleep_NoPup, -1) != -1)) + { + return true; + } + return false; +} void Main_OnEverySecond() { int newMQTTState; @@ -561,15 +586,7 @@ void Main_OnEverySecond() g_connectToWiFi--; if (0 == g_connectToWiFi && g_bHasWiFiConnected == 0) { - const char* wifi_ssid, * wifi_pass; - - g_bOpenAccessPointMode = 0; - wifi_ssid = CFG_GetWiFiSSID(); - wifi_pass = CFG_GetWiFiPass(); - HAL_ConnectToWiFi(wifi_ssid, wifi_pass); - // register function to get callbacks about wifi changes. - HAL_WiFi_SetupStatusCallback(Main_OnWiFiStatusChange); - ADDLOGF_DEBUG("Registered for wifi changes\r\n"); + Main_ConnectToWiFiNow(); } } @@ -1049,7 +1066,13 @@ void Main_Init_After_Delay() g_openAP = 5; } else { - g_connectToWiFi = 5; + if (Main_HasFastConnect()) { + mqtt_loopsWithDisconnected = 9999; + Main_ConnectToWiFiNow(); + } + else { + g_connectToWiFi = 5; + } } }