From fcdd6be291fc5c9996a6e9cb67f4f0ced0069246 Mon Sep 17 00:00:00 2001 From: Will Tatam Date: Sun, 20 Mar 2022 12:02:24 +0000 Subject: [PATCH] Fallback to AP mode if wifi client fails --- sensor/sensor.ino | 35 ++++++++++++++++++++++++++--------- 1 file changed, 26 insertions(+), 9 deletions(-) diff --git a/sensor/sensor.ino b/sensor/sensor.ino index 2bd6d93..c056b09 100644 --- a/sensor/sensor.ino +++ b/sensor/sensor.ino @@ -15,6 +15,7 @@ #include #include #include +#include #include "wifi.h" // Create file with the following @@ -82,7 +83,7 @@ void onBeforeSwitchStateChanged(bool state, HASwitch* s) boolean isConnected = false; void setup() { Serial.begin(115200); - delay(10); + delay(1000); // Make sure you're in station mode WiFi.mode(WIFI_STA); @@ -96,14 +97,30 @@ void setup() { else WiFi.begin(ssid); - while (WiFi.status() != WL_CONNECTED) { - delay(500); - Serial.print("."); + int sanity = 0; + while (sanity < 200) { + sanity++; + if(WiFi.status() == WL_CONNECTED) { + Serial.println(""); + Serial.print(F("Connected with IP: ")); + Serial.println(WiFi.localIP()); + break; + } + else { + delay(500); + Serial.print("."); + } } + + if(WiFi.status() != WL_CONNECTED) { + Serial.print("\n\nWifi failed, flip to fallback AP\n"); + WiFi.softAP("hottub", "Balboa"); + IPAddress myIP = WiFi.softAPIP(); + Serial.print("AP IP address: "); + Serial.println(myIP); + } + - Serial.println(""); - Serial.print(F("Connected with IP: ")); - Serial.println(WiFi.localIP()); #ifndef SERIAL_OVER_IP_ADDR #ifdef ESP32 @@ -121,7 +138,7 @@ void setup() { device.setName("Hottub"); - device.setSoftwareVersion("0.0.5"); + device.setSoftwareVersion("0.0.6"); device.setManufacturer("Balboa"); device.setModel("GL2000"); @@ -303,7 +320,7 @@ void handleBytes(uint8_t buf[], size_t len) { lightState = true; } - String newRaw = result.substring(4, 17) + " pump=" + pump + " light=" + light; + String newRaw = result.substring(13, 17) + " pump=" + pump + " light=" + light; if (lastRaw != newRaw) { newData = true; lastRaw = newRaw;