diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 64843909..d11aa5d4 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -29,7 +29,7 @@ Please ensure to specify the following: Arduino IDE version: 1.8.19 ESP8266 Core Version 3.0.2 OS: Ubuntu 20.04 LTS -Linux xy-Inspiron-3593 5.4.0-99-generic #112-Ubuntu SMP Thu Feb 3 13:50:55 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux +Linux xy-Inspiron-3593 5.13.0-35-generic #40~20.04.1-Ubuntu SMP Mon Mar 7 09:18:32 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux Context: I encountered an endless loop while trying to connect to Local WiFi. @@ -50,3 +50,4 @@ There are usually some outstanding feature requests in the [existing issues list ### Sending Pull Requests Pull Requests with changes and fixes are also welcome! + diff --git a/README.md b/README.md index 001151d8..01b8caac 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,8 @@ [![contributions welcome](https://img.shields.io/badge/contributions-welcome-brightgreen.svg?style=flat)](#Contributing) [![GitHub issues](https://img.shields.io/github/issues/khoih-prog/ESPAsync_WiFiManager.svg)](http://github.com/khoih-prog/ESPAsync_WiFiManager/issues) -Buy Me A Coffee +Donate to my libraries using BuyMeACoffee + --- --- @@ -115,11 +116,6 @@ * [High Complexity](#high-complexity) * [Async_ConfigOnDRD_FS_MQTT_Ptr_Complex](examples/Async_ConfigOnDRD_FS_MQTT_Ptr_Complex) * [Async_ConfigOnDRD_FS_MQTT_Ptr_Medium](examples/Async_ConfigOnDRD_FS_MQTT_Ptr_Medium) - * [Simplest Possible](#simplest-possible) - * [Async_AutoConnect_ESP32_minimal](examples/Async_AutoConnect_ESP32_minimal) - * [Async_AutoConnect_ESP8266_minimal](examples/Async_AutoConnect_ESP8266_minimal) - * [Async_ConfigOnDRD_ESP32_minimal](examples/Async_ConfigOnDRD_ESP32_minimal) - * [Async_ConfigOnDRD_ESP8266_minimal](examples/Async_ConfigOnDRD_ESP8266_minimal) * [Multiple-Definitions-Linker-Error demo](#Multiple-Definitions-Linker-Error-demo) * [Example Async_ConfigOnDRD_FS_MQTT_Ptr](#example-async_configondrd_fs_mqtt_ptr) * [Debug Terminal Output Samples](#debug-terminal-output-samples) @@ -271,7 +267,7 @@ This [**ESPAsync_WiFiManager** library](https://github.com/khoih-prog/ESPAsync_W 4. [`ESPAsyncWebServer v1.2.3+`](https://github.com/me-no-dev/ESPAsyncWebServer) for all ESP32/ESP8266-based boards. You have to use the latest [forked ESPAsyncWebServer](https://github.com/khoih-prog/ESPAsyncWebServer) if the PR [Fix compiler error for ESP32-C3 and mbed TLS v2.7.0+ #970](https://github.com/me-no-dev/ESPAsyncWebServer/pull/970) hasn't been merged. 5. [`ESPAsyncTCP v1.2.2+`](https://github.com/me-no-dev/ESPAsyncTCP) for ESP8266-based boards. 6. [`AsyncTCP v1.1.1+`](https://github.com/me-no-dev/AsyncTCP) for ESP32-based boards - 7. [`ESP_DoubleResetDetector v1.3.0+`](https://github.com/khoih-prog/ESP_DoubleResetDetector) if using DRD feature. To install, check [![arduino-library-badge](https://www.ardu-badge.com/badge/ESP_DoubleResetDetector.svg?)](https://www.ardu-badge.com/ESP_DoubleResetDetector). Use v1.1.0+ if using LittleFS for EP32. + 7. [`ESP_DoubleResetDetector v1.3.1+`](https://github.com/khoih-prog/ESP_DoubleResetDetector) if using DRD feature. To install, check [![arduino-library-badge](https://www.ardu-badge.com/badge/ESP_DoubleResetDetector.svg?)](https://www.ardu-badge.com/ESP_DoubleResetDetector). Use v1.1.0+ if using LittleFS for EP32. 8. [`LittleFS_esp32 v1.0.6+`](https://github.com/lorol/LITTLEFS) for ESP32-based boards using LittleFS with ESP32 core v1.0.4-. To install, check [![arduino-library-badge](https://www.ardu-badge.com/badge/LittleFS_esp32.svg?)](https://www.ardu-badge.com/LittleFS_esp32). **Notice**: This [`LittleFS_esp32 library`](https://github.com/lorol/LITTLEFS) has been integrated to Arduino [ESP32 core v1.0.6+](https://github.com/espressif/arduino-esp32/tree/master/libraries/LITTLEFS) and you don't need to install it if using ESP32 core v1.0.6+ --- @@ -2159,27 +2155,18 @@ ESPAsync_wifiManager.setRemoveDuplicateAPs(false); 9. [Async_AutoConnectWithFSParameters](examples/Async_AutoConnectWithFSParameters) 10. [Async_ConfigOnSwitchFS_MQTT_Ptr](examples/Async_ConfigOnSwitchFS_MQTT_Ptr) 11. [Async_AutoConnectWithFSParametersAndCustomIP](examples/Async_AutoConnectWithFSParametersAndCustomIP) -12. [ModelessConnect](examples/ModelessConnect) -13. [ModelessWithInterrupts](examples/ModelessWithInterrupts) -14. [Async_ESP32_FSWebServer](examples/Async_ESP32_FSWebServer) -15. [Async_ESP32_FSWebServer_DRD](examples/Async_ESP32_FSWebServer_DRD) -16. [Async_ESP_FSWebServer](examples/Async_ESP_FSWebServer) -17. [Async_ESP_FSWebServer_DRD](examples/Async_ESP_FSWebServer_DRD) -18. [Async_ConfigOnDRD_FS_MQTT_Ptr](examples/Async_ConfigOnDRD_FS_MQTT_Ptr) -19. [Async_ConfigOnDoubleReset_TZ](examples/Async_ConfigOnDoubleReset_TZ) (now support ArduinoJson 6.0.0+ as well as 5.13.5-) +12. [Async_ESP32_FSWebServer](examples/Async_ESP32_FSWebServer) +13. [Async_ESP32_FSWebServer_DRD](examples/Async_ESP32_FSWebServer_DRD) +14. [Async_ESP_FSWebServer](examples/Async_ESP_FSWebServer) +15. [Async_ESP_FSWebServer_DRD](examples/Async_ESP_FSWebServer_DRD) +16. [Async_ConfigOnDRD_FS_MQTT_Ptr](examples/Async_ConfigOnDRD_FS_MQTT_Ptr) +17. [Async_ConfigOnDoubleReset_TZ](examples/Async_ConfigOnDoubleReset_TZ) (now support ArduinoJson 6.0.0+ as well as 5.13.5-) #### High Complexity 1. [Async_ConfigOnDRD_FS_MQTT_Ptr_Complex](examples/Async_ConfigOnDRD_FS_MQTT_Ptr_Complex) 2. [Async_ConfigOnDRD_FS_MQTT_Ptr_Medium](examples/Async_ConfigOnDRD_FS_MQTT_Ptr_Medium) -#### Simplest Possible - - 1. [Async_AutoConnect_ESP32_minimal](examples/Async_AutoConnect_ESP32_minimal) - 2. [Async_AutoConnect_ESP8266_minimal](examples/Async_AutoConnect_ESP8266_minimal) - 3. [Async_ConfigOnDRD_ESP32_minimal](examples/Async_ConfigOnDRD_ESP32_minimal) - 4. [Async_ConfigOnDRD_ESP8266_minimal](examples/Async_ConfigOnDRD_ESP8266_minimal) - #### Multiple-Definitions-Linker-Error demo 1. [Async_ConfigOnDoubleReset_Multi](examples/Async_ConfigOnDoubleReset_Multi) @@ -2190,1369 +2177,8 @@ ESPAsync_wifiManager.setRemoveDuplicateAPs(false); ### Example [Async_ConfigOnDRD_FS_MQTT_Ptr](examples/Async_ConfigOnDRD_FS_MQTT_Ptr) -```cpp -#if !( defined(ESP8266) || defined(ESP32) ) - #error This code is intended to run on the ESP8266 or ESP32 platform! Please check your Tools->Board setting. -#endif - -#define ESP_ASYNC_WIFIMANAGER_VERSION_MIN_TARGET "ESPAsync_WiFiManager v1.12.1" -#define ESP_ASYNC_WIFIMANAGER_VERSION_MIN 1012000 - -// Use from 0 to 4. Higher number, more debugging messages and memory usage. -#define _ESPASYNC_WIFIMGR_LOGLEVEL_ 3 - -#include - -// Now support ArduinoJson 6.0.0+ ( tested with v6.15.2 to v6.16.1 ) -#include // get it from https://arduinojson.org/ or install via Arduino library manager - -//For ESP32, To use ESP32 Dev Module, QIO, Flash 4MB/80MHz, Upload 921600 -//Ported to ESP32 -#ifdef ESP32 - #include - #include - #include - - // From v1.1.1 - #include - WiFiMulti wifiMulti; - - // LittleFS has higher priority than SPIFFS - #if ( defined(ESP_ARDUINO_VERSION_MAJOR) && (ESP_ARDUINO_VERSION_MAJOR >= 2) ) - #define USE_LITTLEFS true - #define USE_SPIFFS false - #elif defined(ARDUINO_ESP32C3_DEV) - // For core v1.0.6-, ESP32-C3 only supporting SPIFFS and EEPROM. To use v2.0.0+ for LittleFS - #define USE_LITTLEFS false - #define USE_SPIFFS true - #endif - - #if USE_LITTLEFS - // Use LittleFS - #include "FS.h" - - // Check cores/esp32/esp_arduino_version.h and cores/esp32/core_version.h - //#if ( ESP_ARDUINO_VERSION >= ESP_ARDUINO_VERSION_VAL(2, 0, 0) ) //(ESP_ARDUINO_VERSION_MAJOR >= 2) - #if ( defined(ESP_ARDUINO_VERSION_MAJOR) && (ESP_ARDUINO_VERSION_MAJOR >= 2) ) - #warning Using ESP32 Core 1.0.6 or 2.0.0+ - // The library has been merged into esp32 core from release 1.0.6 - #include // https://github.com/espressif/arduino-esp32/tree/master/libraries/LittleFS - - FS* filesystem = &LittleFS; - #define FileFS LittleFS - #define FS_Name "LittleFS" - #else - #warning Using ESP32 Core 1.0.5-. You must install LITTLEFS library - // The library has been merged into esp32 core from release 1.0.6 - #include // https://github.com/lorol/LITTLEFS - - FS* filesystem = &LITTLEFS; - #define FileFS LITTLEFS - #define FS_Name "LittleFS" - #endif - - #elif USE_SPIFFS - #include - FS* filesystem = &SPIFFS; - #define FileFS SPIFFS - #define FS_Name "SPIFFS" - #else - // +Use FFat - #include - FS* filesystem = &FFat; - #define FileFS FFat - #define FS_Name "FFat" - #endif - ////// - - #define ESP_getChipId() ((uint32_t)ESP.getEfuseMac()) - - #define LED_BUILTIN 2 - #define LED_ON HIGH - #define LED_OFF LOW - -#else - - #include //https://github.com/esp8266/Arduino - //needed for library - #include - - // From v1.1.1 - #include - ESP8266WiFiMulti wifiMulti; - - #define USE_LITTLEFS true - - #if USE_LITTLEFS - #include - FS* filesystem = &LittleFS; - #define FileFS LittleFS - #define FS_Name "LittleFS" - #else - FS* filesystem = &SPIFFS; - #define FileFS SPIFFS - #define FS_Name "SPIFFS" - #endif - ////// - - #define ESP_getChipId() (ESP.getChipId()) - - #define LED_ON LOW - #define LED_OFF HIGH -#endif - -// These defines must be put before #include -// to select where to store DoubleResetDetector's variable. -// For ESP32, You must select one to be true (EEPROM or SPIFFS) -// For ESP8266, You must select one to be true (RTC, EEPROM, SPIFFS or LITTLEFS) -// Otherwise, library will use default EEPROM storage -#ifdef ESP32 - - // These defines must be put before #include - // to select where to store DoubleResetDetector's variable. - // For ESP32, You must select one to be true (EEPROM or SPIFFS) - // Otherwise, library will use default EEPROM storage - #if USE_LITTLEFS - #define ESP_DRD_USE_LITTLEFS true - #define ESP_DRD_USE_SPIFFS false - #define ESP_DRD_USE_EEPROM false - #elif USE_SPIFFS - #define ESP_DRD_USE_LITTLEFS false - #define ESP_DRD_USE_SPIFFS true - #define ESP_DRD_USE_EEPROM false - #else - #define ESP_DRD_USE_LITTLEFS false - #define ESP_DRD_USE_SPIFFS false - #define ESP_DRD_USE_EEPROM true - #endif - -#else //ESP8266 - - // For DRD - // These defines must be put before #include - // to select where to store DoubleResetDetector's variable. - // For ESP8266, You must select one to be true (RTC, EEPROM, SPIFFS or LITTLEFS) - // Otherwise, library will use default EEPROM storage - #if USE_LITTLEFS - #define ESP_DRD_USE_LITTLEFS true - #define ESP_DRD_USE_SPIFFS false - #else - #define ESP_DRD_USE_LITTLEFS false - #define ESP_DRD_USE_SPIFFS true - #endif - - #define ESP_DRD_USE_EEPROM false - #define ESP8266_DRD_USE_RTC false -#endif - -#define DOUBLERESETDETECTOR_DEBUG true //false - -#include //https://github.com/khoih-prog/ESP_DoubleResetDetector - -// Number of seconds after reset during which a -// subseqent reset will be considered a double reset. -#define DRD_TIMEOUT 10 - -// RTC Memory Address for the DoubleResetDetector to use -#define DRD_ADDRESS 0 - -DoubleResetDetector* drd = NULL; - -#include "Adafruit_MQTT.h" //https://github.com/adafruit/Adafruit_MQTT_Library -#include "Adafruit_MQTT_Client.h" //https://github.com/adafruit/Adafruit_MQTT_Library - -const char* CONFIG_FILE = "/ConfigMQTT.json"; - -// Default configuration values for Adafruit IO MQTT -// This actually works -#define AIO_SERVER "io.adafruit.com" -#define AIO_SERVERPORT "1883" //1883, or 8883 for SSL -#define AIO_USERNAME "private" //Adafruit IO -#define AIO_KEY "private" - -// Labels for custom parameters in WiFi manager -#define AIO_SERVER_Label "AIO_SERVER_Label" -#define AIO_SERVERPORT_Label "AIO_SERVERPORT_Label" -#define AIO_USERNAME_Label "AIO_USERNAME_Label" -#define AIO_KEY_Label "AIO_KEY_Label" - -// Just dummy topics. To be updated later when got valid data from FS or Config Portal -String MQTT_Pub_Topic = "private/feeds/Temperature"; - -// Variables to save custom parameters to... -// I would like to use these instead of #defines -#define custom_AIO_SERVER_LEN 20 -#define custom_AIO_PORT_LEN 5 -#define custom_AIO_USERNAME_LEN 20 -#define custom_AIO_KEY_LEN 40 +https://github.com/khoih-prog/ESPAsync_WiFiManager/blob/76ad904f63512b3a52f5fe00cc74c47d6ea6e49d/examples/Async_ConfigOnDRD_FS_MQTT_Ptr/Async_ConfigOnDRD_FS_MQTT_Ptr.ino#L17-L1408 -char custom_AIO_SERVER[custom_AIO_SERVER_LEN]; -char custom_AIO_SERVERPORT[custom_AIO_PORT_LEN]; -char custom_AIO_USERNAME[custom_AIO_USERNAME_LEN]; -char custom_AIO_KEY[custom_AIO_KEY_LEN]; - -// Function Prototypes -void MQTT_connect(); -bool readConfigFile(); -bool writeConfigFile(); - -// For Config Portal -// SSID and PW for Config Portal -String ssid = "ESP_" + String(ESP_getChipId(), HEX); -String password; - -// SSID and PW for your Router -String Router_SSID; -String Router_Pass; - -// From v1.1.1 -// You only need to format the filesystem once -//#define FORMAT_FILESYSTEM true -#define FORMAT_FILESYSTEM false - -#define MIN_AP_PASSWORD_SIZE 8 - -#define SSID_MAX_LEN 32 -//From v1.0.10, WPA2 passwords can be up to 63 characters long. -#define PASS_MAX_LEN 64 - -typedef struct -{ - char wifi_ssid[SSID_MAX_LEN]; - char wifi_pw [PASS_MAX_LEN]; -} WiFi_Credentials; - -typedef struct -{ - String wifi_ssid; - String wifi_pw; -} WiFi_Credentials_String; - -#define NUM_WIFI_CREDENTIALS 2 - -// Assuming max 49 chars -#define TZNAME_MAX_LEN 50 -#define TIMEZONE_MAX_LEN 50 - -typedef struct -{ - WiFi_Credentials WiFi_Creds [NUM_WIFI_CREDENTIALS]; - char TZ_Name[TZNAME_MAX_LEN]; // "America/Toronto" - char TZ[TIMEZONE_MAX_LEN]; // "EST5EDT,M3.2.0,M11.1.0" - uint16_t checksum; -} WM_Config; - -WM_Config WM_config; - -#define CONFIG_FILENAME F("/wifi_cred.dat") -////// - -// Indicates whether ESP has WiFi credentials saved from previous session, or double reset detected -bool initialConfig = false; - -// Use false if you don't like to display Available Pages in Information Page of Config Portal -// Comment out or use true to display Available Pages in Information Page of Config Portal -// Must be placed before #include -#define USE_AVAILABLE_PAGES false - -// From v1.0.10 to permit disable/enable StaticIP configuration in Config Portal from sketch. Valid only if DHCP is used. -// You'll loose the feature of dynamically changing from DHCP to static IP, or vice versa -// You have to explicitly specify false to disable the feature. -//#define USE_STATIC_IP_CONFIG_IN_CP false - -// Use false to disable NTP config. Advisable when using Cellphone, Tablet to access Config Portal. -// See Issue 23: On Android phone ConfigPortal is unresponsive (https://github.com/khoih-prog/ESP_WiFiManager/issues/23) -#define USE_ESP_WIFIMANAGER_NTP true - -// Just use enough to save memory. On ESP8266, can cause blank ConfigPortal screen -// if using too much memory -#define USING_AFRICA false -#define USING_AMERICA true -#define USING_ANTARCTICA false -#define USING_ASIA false -#define USING_ATLANTIC false -#define USING_AUSTRALIA false -#define USING_EUROPE false -#define USING_INDIAN false -#define USING_PACIFIC false -#define USING_ETC_GMT false - -// Use true to enable CloudFlare NTP service. System can hang if you don't have Internet access while accessing CloudFlare -// See Issue #21: CloudFlare link in the default portal (https://github.com/khoih-prog/ESP_WiFiManager/issues/21) -#define USE_CLOUDFLARE_NTP false - -// New in v1.0.11 -#define USING_CORS_FEATURE true -////// - -// Use USE_DHCP_IP == true for dynamic DHCP IP, false to use static IP which you have to change accordingly to your network -#if (defined(USE_STATIC_IP_CONFIG_IN_CP) && !USE_STATIC_IP_CONFIG_IN_CP) -// Force DHCP to be true -#if defined(USE_DHCP_IP) -#undef USE_DHCP_IP -#endif -#define USE_DHCP_IP true -#else -// You can select DHCP or Static IP here -//#define USE_DHCP_IP true -#define USE_DHCP_IP false -#endif - -#if ( USE_DHCP_IP ) -// Use DHCP -#warning Using DHCP IP -IPAddress stationIP = IPAddress(0, 0, 0, 0); -IPAddress gatewayIP = IPAddress(192, 168, 2, 1); -IPAddress netMask = IPAddress(255, 255, 255, 0); -#else -// Use static IP -#warning Using static IP -#ifdef ESP32 -IPAddress stationIP = IPAddress(192, 168, 2, 232); -#else -IPAddress stationIP = IPAddress(192, 168, 2, 186); -#endif - -IPAddress gatewayIP = IPAddress(192, 168, 2, 1); -IPAddress netMask = IPAddress(255, 255, 255, 0); -#endif - -#define USE_CONFIGURABLE_DNS true - -IPAddress dns1IP = gatewayIP; -IPAddress dns2IP = IPAddress(8, 8, 8, 8); - -#define USE_CUSTOM_AP_IP false - -IPAddress APStaticIP = IPAddress(192, 168, 100, 1); -IPAddress APStaticGW = IPAddress(192, 168, 100, 1); -IPAddress APStaticSN = IPAddress(255, 255, 255, 0); - -#include //https://github.com/khoih-prog/ESPAsync_WiFiManager - -// Redundant, for v1.10.0 only -//#include //https://github.com/khoih-prog/ESPAsync_WiFiManager - -#define HTTP_PORT 80 - -// Create an ESP32 WiFiClient class to connect to the MQTT server -WiFiClient *client = NULL; - -Adafruit_MQTT_Client *mqtt = NULL; -Adafruit_MQTT_Publish *Temperature = NULL; - -// Forward Declaration - -/////////////////////////////////////////// -// New in v1.4.0 -/****************************************** -typedef struct -{ - IPAddress _ap_static_ip; - IPAddress _ap_static_gw; - IPAddress _ap_static_sn; - -} WiFi_AP_IPConfig; - -typedef struct -{ - IPAddress _sta_static_ip; - IPAddress _sta_static_gw; - IPAddress _sta_static_sn; -#if USE_CONFIGURABLE_DNS - IPAddress _sta_static_dns1; - IPAddress _sta_static_dns2; -#endif -} WiFi_STA_IPConfig; -******************************************/ - -WiFi_AP_IPConfig WM_AP_IPconfig; -WiFi_STA_IPConfig WM_STA_IPconfig; - -void initAPIPConfigStruct(WiFi_AP_IPConfig &in_WM_AP_IPconfig) -{ - in_WM_AP_IPconfig._ap_static_ip = APStaticIP; - in_WM_AP_IPconfig._ap_static_gw = APStaticGW; - in_WM_AP_IPconfig._ap_static_sn = APStaticSN; -} - -void initSTAIPConfigStruct(WiFi_STA_IPConfig &in_WM_STA_IPconfig) -{ - in_WM_STA_IPconfig._sta_static_ip = stationIP; - in_WM_STA_IPconfig._sta_static_gw = gatewayIP; - in_WM_STA_IPconfig._sta_static_sn = netMask; -#if USE_CONFIGURABLE_DNS - in_WM_STA_IPconfig._sta_static_dns1 = dns1IP; - in_WM_STA_IPconfig._sta_static_dns2 = dns2IP; -#endif -} - -void displayIPConfigStruct(WiFi_STA_IPConfig in_WM_STA_IPconfig) -{ - LOGERROR3(F("stationIP ="), in_WM_STA_IPconfig._sta_static_ip, ", gatewayIP =", in_WM_STA_IPconfig._sta_static_gw); - LOGERROR1(F("netMask ="), in_WM_STA_IPconfig._sta_static_sn); -#if USE_CONFIGURABLE_DNS - LOGERROR3(F("dns1IP ="), in_WM_STA_IPconfig._sta_static_dns1, ", dns2IP =", in_WM_STA_IPconfig._sta_static_dns2); -#endif -} - -void configWiFi(WiFi_STA_IPConfig in_WM_STA_IPconfig) -{ - #if USE_CONFIGURABLE_DNS - // Set static IP, Gateway, Subnetmask, DNS1 and DNS2. New in v1.0.5 - WiFi.config(in_WM_STA_IPconfig._sta_static_ip, in_WM_STA_IPconfig._sta_static_gw, in_WM_STA_IPconfig._sta_static_sn, in_WM_STA_IPconfig._sta_static_dns1, in_WM_STA_IPconfig._sta_static_dns2); - #else - // Set static IP, Gateway, Subnetmask, Use auto DNS1 and DNS2. - WiFi.config(in_WM_STA_IPconfig._sta_static_ip, in_WM_STA_IPconfig._sta_static_gw, in_WM_STA_IPconfig._sta_static_sn); - #endif -} - -/////////////////////////////////////////// - -uint8_t connectMultiWiFi() -{ -#if ESP32 - // For ESP32, this better be 0 to shorten the connect time. - // For ESP32-S2/C3, must be > 500 - #if ( USING_ESP32_S2 || USING_ESP32_C3 ) - #define WIFI_MULTI_1ST_CONNECT_WAITING_MS 500L - #else - // For ESP32 core v1.0.6, must be >= 500 - #define WIFI_MULTI_1ST_CONNECT_WAITING_MS 800L - #endif -#else - // For ESP8266, this better be 2200 to enable connect the 1st time - #define WIFI_MULTI_1ST_CONNECT_WAITING_MS 2200L -#endif - -#define WIFI_MULTI_CONNECT_WAITING_MS 500L - - uint8_t status; - - //WiFi.mode(WIFI_STA); - - LOGERROR(F("ConnectMultiWiFi with :")); - - if ( (Router_SSID != "") && (Router_Pass != "") ) - { - LOGERROR3(F("* Flash-stored Router_SSID = "), Router_SSID, F(", Router_Pass = "), Router_Pass ); - LOGERROR3(F("* Add SSID = "), Router_SSID, F(", PW = "), Router_Pass ); - wifiMulti.addAP(Router_SSID.c_str(), Router_Pass.c_str()); - } - - for (uint8_t i = 0; i < NUM_WIFI_CREDENTIALS; i++) - { - // Don't permit NULL SSID and password len < MIN_AP_PASSWORD_SIZE (8) - if ( (String(WM_config.WiFi_Creds[i].wifi_ssid) != "") && (strlen(WM_config.WiFi_Creds[i].wifi_pw) >= MIN_AP_PASSWORD_SIZE) ) - { - LOGERROR3(F("* Additional SSID = "), WM_config.WiFi_Creds[i].wifi_ssid, F(", PW = "), WM_config.WiFi_Creds[i].wifi_pw ); - } - } - - LOGERROR(F("Connecting MultiWifi...")); - - //WiFi.mode(WIFI_STA); - -#if !USE_DHCP_IP - // New in v1.4.0 - configWiFi(WM_STA_IPconfig); - ////// -#endif - - int i = 0; - status = wifiMulti.run(); - delay(WIFI_MULTI_1ST_CONNECT_WAITING_MS); - - while ( ( i++ < 20 ) && ( status != WL_CONNECTED ) ) - { - status = WiFi.status(); - - if ( status == WL_CONNECTED ) - break; - else - delay(WIFI_MULTI_CONNECT_WAITING_MS); - } - - if ( status == WL_CONNECTED ) - { - LOGERROR1(F("WiFi connected after time: "), i); - LOGERROR3(F("SSID:"), WiFi.SSID(), F(",RSSI="), WiFi.RSSI()); - LOGERROR3(F("Channel:"), WiFi.channel(), F(",IP address:"), WiFi.localIP() ); - } - else - { - LOGERROR(F("WiFi not connected")); - - // To avoid unnecessary DRD - drd->loop(); - -#if ESP8266 - ESP.reset(); -#else - ESP.restart(); -#endif - } - - return status; -} - -void toggleLED() -{ - //toggle state - digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN)); -} - -#if USE_ESP_WIFIMANAGER_NTP - -void printLocalTime() -{ -#if ESP8266 - static time_t now; - - now = time(nullptr); - - if ( now > 1451602800 ) - { - Serial.print("Local Date/Time: "); - Serial.print(ctime(&now)); - } -#else - struct tm timeinfo; - - getLocalTime( &timeinfo ); - - // Valid only if year > 2000. - // You can get from timeinfo : tm_year, tm_mon, tm_mday, tm_hour, tm_min, tm_sec - if (timeinfo.tm_year > 100 ) - { - Serial.print("Local Date/Time: "); - Serial.print( asctime( &timeinfo ) ); - } -#endif -} - -#endif - -void heartBeatPrint() -{ -#if USE_ESP_WIFIMANAGER_NTP - printLocalTime(); -#else - static int num = 1; - - if (WiFi.status() == WL_CONNECTED) - Serial.print(F("H")); // H means connected to WiFi - else - Serial.print(F("F")); // F means not connected to WiFi - - if (num == 80) - { - Serial.println(); - num = 1; - } - else if (num++ % 10 == 0) - { - Serial.print(F(" ")); - } -#endif -} - -void publishMQTT() -{ - float some_number = 25.0 + (float) ( millis() % 100 ) / 100; - - // For debug only - //Serial.print(F("Published Temp = ")); - //Serial.println(some_number); - - MQTT_connect(); - - if (Temperature->publish(some_number)) - { - Serial.print(F("T")); // T means publishing OK - } - else - { - Serial.print(F("F")); // F means publishing failure - } -} - -void check_WiFi() -{ - if ( (WiFi.status() != WL_CONNECTED) ) - { - Serial.println(F("\nWiFi lost. Call connectMultiWiFi in loop")); - connectMultiWiFi(); - } -} - -void check_status() -{ - static ulong checkstatus_timeout = 0; - static ulong LEDstatus_timeout = 0; - static ulong checkwifi_timeout = 0; - static ulong mqtt_publish_timeout = 0; - - ulong current_millis = millis(); - -#define WIFICHECK_INTERVAL 1000L - -#if USE_ESP_WIFIMANAGER_NTP - #define HEARTBEAT_INTERVAL 60000L -#else - #define HEARTBEAT_INTERVAL 10000L -#endif - -#define LED_INTERVAL 2000L -#define PUBLISH_INTERVAL 70000L - - // Check WiFi every WIFICHECK_INTERVAL (1) seconds. - if ((current_millis > checkwifi_timeout) || (checkwifi_timeout == 0)) - { - check_WiFi(); - checkwifi_timeout = current_millis + WIFICHECK_INTERVAL; - } - - if ((current_millis > LEDstatus_timeout) || (LEDstatus_timeout == 0)) - { - // Toggle LED at LED_INTERVAL = 2s - toggleLED(); - LEDstatus_timeout = current_millis + LED_INTERVAL; - } - - // Print hearbeat every HEARTBEAT_INTERVAL (10) seconds. - if ((current_millis > checkstatus_timeout) || (checkstatus_timeout == 0)) - { - heartBeatPrint(); - checkstatus_timeout = current_millis + HEARTBEAT_INTERVAL; - } - - // Check every PUBLISH_INTERVAL (60) seconds. - if ((current_millis > mqtt_publish_timeout) || (mqtt_publish_timeout == 0)) - { - if (WiFi.status() == WL_CONNECTED) - { - publishMQTT(); - } - - mqtt_publish_timeout = current_millis + PUBLISH_INTERVAL; - } -} - -int calcChecksum(uint8_t* address, uint16_t sizeToCalc) -{ - uint16_t checkSum = 0; - - for (uint16_t index = 0; index < sizeToCalc; index++) - { - checkSum += * ( ( (byte*) address ) + index); - } - - return checkSum; -} - -bool loadConfigData() -{ - File file = FileFS.open(CONFIG_FILENAME, "r"); - LOGERROR(F("LoadWiFiCfgFile ")); - - memset((void *) &WM_config, 0, sizeof(WM_config)); - - // New in v1.4.0 - memset((void *) &WM_STA_IPconfig, 0, sizeof(WM_STA_IPconfig)); - ////// - - if (file) - { - file.readBytes((char *) &WM_config, sizeof(WM_config)); - - // New in v1.4.0 - file.readBytes((char *) &WM_STA_IPconfig, sizeof(WM_STA_IPconfig)); - ////// - - file.close(); - LOGERROR(F("OK")); - - if ( WM_config.checksum != calcChecksum( (uint8_t*) &WM_config, sizeof(WM_config) - sizeof(WM_config.checksum) ) ) - { - LOGERROR(F("WM_config checksum wrong")); - - return false; - } - - // New in v1.4.0 - displayIPConfigStruct(WM_STA_IPconfig); - ////// - - return true; - } - else - { - LOGERROR(F("failed")); - - return false; - } -} - -void saveConfigData() -{ - File file = FileFS.open(CONFIG_FILENAME, "w"); - LOGERROR(F("SaveWiFiCfgFile ")); - - if (file) - { - WM_config.checksum = calcChecksum( (uint8_t*) &WM_config, sizeof(WM_config) - sizeof(WM_config.checksum) ); - - file.write((uint8_t*) &WM_config, sizeof(WM_config)); - - displayIPConfigStruct(WM_STA_IPconfig); - - // New in v1.4.0 - file.write((uint8_t*) &WM_STA_IPconfig, sizeof(WM_STA_IPconfig)); - ////// - - file.close(); - LOGERROR(F("OK")); - } - else - { - LOGERROR(F("failed")); - } -} - -void deleteOldInstances() -{ - // Delete previous instances - if (mqtt) - { - delete mqtt; - mqtt = NULL; - - Serial.println(F("Deleting old MQTT object")); - } - - if (Temperature) - { - delete Temperature; - Temperature = NULL; - - Serial.println(F("Deleting old Temperature object")); - } -} - -void createNewInstances() -{ - if (!client) - { - client = new WiFiClient; - - Serial.print(F("\nCreating new WiFi client object : ")); - Serial.println(client? F("OK") : F("failed")); - } - - // Create new instances from new data - if (!mqtt) - { - // Setup the MQTT client class by passing in the WiFi client and MQTT server and login details. - mqtt = new Adafruit_MQTT_Client(client, custom_AIO_SERVER, atoi(custom_AIO_SERVERPORT), custom_AIO_USERNAME, custom_AIO_KEY); - - Serial.print(F("Creating new MQTT object : ")); - - if (mqtt) - { - Serial.println(F("OK")); - Serial.println(String("AIO_SERVER = ") + custom_AIO_SERVER + ", AIO_SERVERPORT = " + custom_AIO_SERVERPORT); - Serial.println(String("AIO_USERNAME = ") + custom_AIO_USERNAME + ", AIO_KEY = " + custom_AIO_KEY); - } - else - Serial.println(F("Failed")); - } - - if (!Temperature) - { - Serial.print(F("Creating new MQTT_Pub_Topic, Temperature = ")); - Serial.println(MQTT_Pub_Topic); - - Temperature = new Adafruit_MQTT_Publish(mqtt, MQTT_Pub_Topic.c_str()); - - Serial.print(F("Creating new Temperature object : ")); - - if (Temperature) - { - Serial.println(F("OK")); - Serial.println(String("Temperature MQTT_Pub_Topic = ") + MQTT_Pub_Topic); - } - else - Serial.println(F("Failed")); - } -} - -void wifi_manager() -{ - Serial.println(F("\nConfig Portal requested.")); - digitalWrite(LED_BUILTIN, LED_ON); // turn the LED on by making the voltage LOW to tell us we are in configuration mode. - - //Local intialization. Once its business is done, there is no need to keep it around - // Use this to default DHCP hostname to ESP8266-XXXXXX or ESP32-XXXXXX - //ESPAsync_WiFiManager ESPAsync_wifiManager(&webServer, &dnsServer); - // Use this to personalize DHCP hostname (RFC952 conformed) - AsyncWebServer webServer(HTTP_PORT); - -#if ( USING_ESP32_S2 || USING_ESP32_C3 ) - ESPAsync_WiFiManager ESPAsync_wifiManager(&webServer, NULL, "ConfigOnDRD-FS-MQTT"); -#else - DNSServer dnsServer; - - ESPAsync_WiFiManager ESPAsync_wifiManager(&webServer, &dnsServer, "ConfigOnDRD-FS-MQTT"); -#endif - - //Check if there is stored WiFi router/password credentials. - //If not found, device will remain in configuration mode until switched off via webserver. - Serial.print(F("Opening Configuration Portal. ")); - - Router_SSID = ESPAsync_wifiManager.WiFi_SSID(); - Router_Pass = ESPAsync_wifiManager.WiFi_Pass(); - - // From v1.1.1, Don't permit NULL password - if ( !initialConfig && (Router_SSID != "") && (Router_Pass != "") ) - { - //If valid AP credential and not DRD, set timeout 120s. - ESPAsync_wifiManager.setConfigPortalTimeout(120); - Serial.println("Got stored Credentials. Timeout 120s"); - } - else - { - ESPAsync_wifiManager.setConfigPortalTimeout(0); - - Serial.print(F("No timeout : ")); - - if (initialConfig) - { - Serial.println(F("DRD or No stored Credentials..")); - } - else - { - Serial.println(F("No stored Credentials.")); - } - } - - // Extra parameters to be configured - // After connecting, parameter.getValue() will get you the configured value - // Format: