From 5e407bd41f6742dddb4b00a5a0fbbe943abd6df7 Mon Sep 17 00:00:00 2001 From: Khoi Hoang <57012152+khoih-prog@users.noreply.github.com> Date: Mon, 9 Mar 2020 02:58:12 -0400 Subject: [PATCH] v1.0.0 #### New in v1.0.0 - This is a Credentials / WiFi Connection Manager with fallback web configuration portal. Completely new to support ***Teensy, SAM DUE, SAMD, STM32, etc. boards running ESP8266 AT-command shields.*** --- README.md | 216 +++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 206 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 70e71a53..0d57a3a9 100644 --- a/README.md +++ b/README.md @@ -185,9 +185,155 @@ When the time passes, the startConfigPortal function will return and continue th #### On Demand Configuration Portal -Example usage +Sample Code ```cpp +#define _ESP_AT_LOGLEVEL_ 0 +#define DEBUG_WIFIMGR true //false + +#if ( defined(STM32F0) || defined(STM32F1) || defined(STM32F2) || defined(STM32F3) ||defined(STM32F4) || defined(STM32F7) ) + #if defined(ESP8266_AT_USE_STM32) + #undef ESP8266_AT_USE_STM32 + #endif + #define ESP8266_AT_USE_STM32 true +#endif + +#if ( defined(ARDUINO_SAM_DUE) || defined(__SAM3X8E__) ) + #if defined(ESP8266_AT_USE_SAM_DUE) + #undef ESP8266_AT_USE_SAM_DUE + #endif + #define ESP8266_AT_USE_SAM_DUE true +#endif + +#if ( defined(ARDUINO_SAMD_ZERO) || defined(ARDUINO_SAMD_MKR1000) || defined(ARDUINO_SAMD_MKRWIFI1010) \ + || defined(ARDUINO_SAMD_NANO_33_IOT) || defined(ARDUINO_SAMD_MKRFox1200) || defined(ARDUINO_SAMD_MKRWAN1300) || defined(ARDUINO_SAMD_MKRWAN1310) \ + || defined(ARDUINO_SAMD_MKRGSM1400) || defined(ARDUINO_SAMD_MKRNB1500) || defined(ARDUINO_SAMD_MKRVIDOR4000) || defined(__SAMD21G18A__) \ + || defined(ARDUINO_SAMD_CIRCUITPLAYGROUND_EXPRESS) ) + #if defined(ESP8266_AT_USE_SAMD) + #undef ESP8266_AT_USE_SAMD + #endif + #define ESP8266_AT_USE_SAMD true +#endif + +#ifdef CORE_TEENSY + // For Teensy 4.0 + #define EspSerial Serial2 //Serial2, Pin RX2 : 7, TX2 : 8 + #if defined(__IMXRT1062__) + #define BOARD_TYPE "TEENSY 4.0" + #elif ( defined(__MKL26Z64__) || defined(ARDUINO_ARCH_AVR) ) + #define BOARD_TYPE "TEENSY LC or 2.0" + #else + #define BOARD_TYPE "TEENSY 3.X" + #endif + +#elif (ESP8266_AT_USE_SAMD) +// For SAMD + #define EspSerial Serial1 + + #if defined(ARDUINO_SAMD_ZERO) + #define BOARD_TYPE "SAMD Zero" + #elif defined(ARDUINO_SAMD_MKR1000) + #define BOARD_TYPE "SAMD MKR1000" + #elif defined(ARDUINO_SAMD_MKRWIFI1010) + #define BOARD_TYPE "SAMD MKRWIFI1010" + #elif defined(ARDUINO_SAMD_NANO_33_IOT) + #define BOARD_TYPE "SAMD NANO_33_IOT" + #elif defined(ARDUINO_SAMD_MKRFox1200) + #define BOARD_TYPE "SAMD MKRFox1200" + #elif ( defined(ARDUINO_SAMD_MKRWAN1300) || defined(ARDUINO_SAMD_MKRWAN1310) ) + #define BOARD_TYPE "SAMD MKRWAN13X0" + #elif defined(ARDUINO_SAMD_MKRGSM1400) + #define BOARD_TYPE "SAMD MKRGSM1400" + #elif defined(ARDUINO_SAMD_MKRNB1500) + #define BOARD_TYPE "SAMD MKRNB1500" + #elif defined(ARDUINO_SAMD_MKRVIDOR4000) + #define BOARD_TYPE "SAMD MKRVIDOR4000" + #elif defined(ARDUINO_SAMD_CIRCUITPLAYGROUND_EXPRESS) + #define BOARD_TYPE "SAMD ARDUINO_SAMD_CIRCUITPLAYGROUND_EXPRESS" + #elif ( defined(__SAM3X8E__) || (__SAM3X8E__) || (__CPU_ARC__) ) + #define BOARD_TYPE "SAMD Board" + #else + #define BOARD_TYPE "SAMD Unknown" + #endif + +#elif (ESP8266_AT_USE_SAM_DUE) +// For SAM DUE + #define EspSerial Serial1 + #define BOARD_TYPE "SAM DUE" + +#elif (ESP8266_AT_USE_STM32) +// For STM32F + #define EspSerial Serial1 + #define BOARD_TYPE "STM32F" +#else +// For other boards. Change Serial as necessary +#define EspSerial Serial3 +#define BOARD_TYPE "Unknown" +#endif + +// Must be before #include +#define EEPROM_START 0 + +#include //https://github.com/khoih-prog/ESP_AT_WiFiManager + +// Your Mega <-> ESP8266 baud rate: +#define ESP8266_BAUD 115200 + +// SSID and PW for Config Portal +#ifdef CORE_TEENSY +String ssid = "ESP_AT_" + String(0x1ABCDEF, HEX); +#else +String ssid = "ESP_AT_" + String(0xABCDEF, HEX); +#endif + +const char* password = "ESP_AT_PW"; + +IPAddress staticAP_IP = IPAddress(192,168,100,1); + +// SSID and PW for your Router +String Router_SSID; +String Router_Pass; + +// Onboard LED I/O pin on board +const int LOCAL_PIN_LED = 13; // Pin 13, Controls the onboard LED. + +#define LED_ON HIGH +#define LED_OFF LOW + +void heartBeatPrint(void) +{ + static int num = 1; + + if (WiFi.status() == WL_CONNECTED) + Serial.print("H"); // H means connected to WiFi + else + Serial.print("F"); // F means not connected to WiFi + + if (num == 80) + { + Serial.println(); + num = 1; + } + else if (num++ % 10 == 0) + { + Serial.print(" "); + } +} + +void check_status() +{ + static unsigned long checkstatus_timeout = 0; + + //KH + #define HEARTBEAT_INTERVAL 10000L + // Print hearbeat every HEARTBEAT_INTERVAL (10) seconds. + if ((millis() > checkstatus_timeout) || (checkstatus_timeout == 0)) + { + heartBeatPrint(); + checkstatus_timeout = millis() + HEARTBEAT_INTERVAL; + } +} + void enterConfigPortal(void) { //Local intialization. Once its business is done, there is no need to keep it around @@ -203,7 +349,7 @@ void enterConfigPortal(void) ESP_AT_wiFiManager.setAPStaticIPConfig(staticAP_IP); // Set static STA IP - ESP_AT_wiFiManager.setSTAStaticIPConfig(IPAddress(192,168,2,114)); + ESP_AT_wiFiManager.setSTAStaticIPConfig(IPAddress(192,168,2,114)); //Check if there is stored WiFi router/password credentials. //If not found, device will remain in configuration mode until switched off via webserver. @@ -221,7 +367,7 @@ void enterConfigPortal(void) Serial.println(F("No stored Credentials. No timeout")); // SSID to uppercase - ssid.toUpperCase(); + ssid.toUpperCase(); //Starts an AP and goes into a blocking loop awaiting configuration Serial.println("Start Config Portal, SSID = " + ssid + ", Pass = " + password); @@ -236,18 +382,68 @@ void enterConfigPortal(void) digitalWrite(LOCAL_PIN_LED, LED_OFF); // Turn led off as we exit Config Portal } -void loop() +void setup() { - // is configuration portal requested? - if ((digitalRead(TRIGGER_PIN) == LOW) || (digitalRead(TRIGGER_PIN2) == LOW)) + // put your setup code here, to run once: + // initialize the LED digital pin as an output. + pinMode(LOCAL_PIN_LED, OUTPUT); + digitalWrite(LOCAL_PIN_LED, LED_ON); // turn the LED on by making the voltage LOW to tell us we are in configuration mode. + + Serial.begin(115200); + + unsigned long startedAt = millis(); + delay(200); + Serial.println("\nStart ConfigOnStartup on " + String(BOARD_TYPE)); + + // initialize serial for ESP module + EspSerial.begin(115200); + + // initialize ESP module + WiFi.init(&EspSerial); + + // check for the presence of the shield + if (WiFi.status() == WL_NO_SHIELD) { - Serial.println("\nConfig Portal requested."); - enterConfigPortal(); + Serial.println(F("WiFi shield not present")); + // don't continue + while (true); } - + + enterConfigPortal(); + + // For some unknown reason webserver can only be started once per boot up + // so webserver can not be used again in the sketch. + #define WIFI_CONNECT_TIMEOUT 30000L + #define WHILE_LOOP_DELAY 200L + #define WHILE_LOOP_STEPS (WIFI_CONNECT_TIMEOUT / ( 3 * WHILE_LOOP_DELAY )) + + startedAt = millis(); + + while ( (WiFi.status() != WL_CONNECTED) && (millis() - startedAt < WIFI_CONNECT_TIMEOUT ) ) + { + int i = 0; + while((!WiFi.status() || WiFi.status() >= WL_DISCONNECTED) && i++ < WHILE_LOOP_STEPS) + { + delay(WHILE_LOOP_DELAY); + } + } + + Serial.print(F("After waiting ")); + Serial.print((millis()- startedAt) / 1000); + Serial.print(F(" secs in setup(), connect result is ")); + + if (WiFi.status() == WL_CONNECTED) + { + Serial.print(F("connected. Local IP: ")); + Serial.println(WiFi.localIP()); + } +} + + +void loop() +{ // put your main code here, to run repeatedly check_status(); - } ```