From 6654433ac34d45866e922f28096fefb4086365f5 Mon Sep 17 00:00:00 2001 From: Ben Jones Date: Sun, 26 Mar 2023 20:19:57 +1300 Subject: [PATCH] add 5m timeout to captive portal and reboot on timeout (#61) --- src/OXRS_Rack32.cpp | 18 +++++++++++++++--- src/OXRS_Rack32.h | 23 +++++++++++++---------- 2 files changed, 28 insertions(+), 13 deletions(-) diff --git a/src/OXRS_Rack32.cpp b/src/OXRS_Rack32.cpp index 9fae221..fec93cc 100644 --- a/src/OXRS_Rack32.cpp +++ b/src/OXRS_Rack32.cpp @@ -611,13 +611,25 @@ void OXRS_Rack32::_initialiseNetwork(byte * mac) // Ensure we are in the correct WiFi mode WiFi.mode(WIFI_STA); + // We use WifiManager library from /~https://github.com/tzapu/WiFiManager + // to handle WiFi connection and credential persistence etc. + WiFiManager wm; + + // If the captive portal is launched and nothing happens for a while then + // reboot - this helps if we come online before an AP (e.g. power outage) + // and prevents us sitting in captive portal mode indefinitely + wm.setConfigPortalTimeout(WM_CONFIG_PORTAL_TIMEOUT_S); + // Connect using saved creds, or start captive portal if none found // NOTE: Blocks until connected or the portal is closed - WiFiManager wm; - bool success = wm.autoConnect("OXRS_WiFi", "superhouse"); + if (!wm.autoConnect("OXRS_WiFi", "superhouse")) + { + _logger.println(F("[ra32] failed to connect to wifi access point, rebooting")); + ESP.restart(); + } _logger.print(F("[ra32] ip address: ")); - _logger.println(success ? WiFi.localIP() : IPAddress(0, 0, 0, 0)); + _logger.println(WiFi.localIP()); #else _logger.print(F("[ra32] ethernet mac address: ")); _logger.println(mac_display); diff --git a/src/OXRS_Rack32.h b/src/OXRS_Rack32.h index ebf4d30..121aa83 100644 --- a/src/OXRS_Rack32.h +++ b/src/OXRS_Rack32.h @@ -9,25 +9,28 @@ #include // For REST API #include // For LCD runtime displays +// WifiManager +#define WM_CONFIG_PORTAL_TIMEOUT_S 300 + // Ethernet -#define ETHERNET_CS_PIN 26 -#define WIZNET_RESET_PIN 13 -#define DHCP_TIMEOUT_MS 15000 -#define DHCP_RESPONSE_TIMEOUT_MS 4000 +#define ETHERNET_CS_PIN 26 +#define WIZNET_RESET_PIN 13 +#define DHCP_TIMEOUT_MS 15000 +#define DHCP_RESPONSE_TIMEOUT_MS 4000 // I2C -#define I2C_SDA 21 -#define I2C_SCL 22 +#define I2C_SDA 21 +#define I2C_SCL 22 // REST API -#define REST_API_PORT 80 +#define REST_API_PORT 80 // Temperature update internal -#define DEFAULT_TEMP_UPDATE_MS 60000L +#define DEFAULT_TEMP_UPDATE_MS 60000L // MCP9808 temperature sensor -#define MCP9808_I2C_ADDRESS 0x18 -#define MCP9808_MODE 0 +#define MCP9808_I2C_ADDRESS 0x18 +#define MCP9808_MODE 0 // Mode Resolution SampleTime // 0 0.5°C 30 ms // 1 0.25°C 65 ms