diff --git a/README.md b/README.md index a5f6a03c..141075c5 100644 --- a/README.md +++ b/README.md @@ -6,6 +6,16 @@ [![contributions welcome](https://img.shields.io/badge/contributions-welcome-brightgreen.svg?style=flat)](#Contributing) [![GitHub issues](https://img.shields.io/github/issues/khoih-prog/WiFiManager_NINA_Lite.svg)](http://github.com/khoih-prog/WiFiManager_NINA_Lite/issues) +#### Major Release v1.0.4 + +1. Configurable ***Config Portal Title*** to be either HostName, BoardName or default undistinguishable names. +2. Optional default ***Credentials as well as Dynamic parameters to be optionally autoloaded into Config Portal*** to use or change instead of manually input. +3. ***DoubleDetectDetector*** feature to force Config Portal when double reset is detected within predetermined time, default 10s. +4. Examples are redesigned to separate Credentials / Defines / Dynamic Params / Code so that you can change Credentials / Dynamic Params quickly for each device. +5. Add Board Name + +Thanks to [thorathome in GitHub](https://github.com/thorathome) to test, suggest and encourage to add those new features in [Blynk_WM](https://github.com/khoih-prog/Blynk_WM), such as Default Credentials/Dynamic Params, Configurable Config Portal Title, DRD. Now those features are speading fast into libraries having similar functionalities. + #### Major Release v1.0.3 1. ***Multiple WiFi Credentials (SSID, Password)*** and system will autoconnect to the available WiFi AP. It will then autoreconnect to the remaining WiFi AP if the current connection is lost. @@ -32,7 +42,7 @@ ## Features -This library is a Light Weight Credentials / WiFi Manager for WiFiNINA modules/shields, specially designed to support ***Teensy, SAM DUE, SAMD (Nano-33 IoT, etc), STM32, nRF52, etc. boards running WiFiNINA modules/shields.*** with smaller memory (64+K bytes) +This library is a Light Weight Credentials / WiFi Manager for WiFiNINA modules/shields, specially designed to support ***Teensy, SAM DUE, SAMD21 (Nano-33 IoT, etc), SAMD51 (Itsy-Bitsy M4, Metro M4, Grand Central M4, Feather M4 Express, etc.), STM32 (F1, F2, F3, F4, F7, etc.), nRF52 (Adafruit NRF52840_FEATHER, NRF52832_FEATHER, NRF52840_FEATHER_SENSE, NRF52840_ITSYBITSY, NRF52840_CIRCUITPLAY, NRF52840_CLUE, NRF52840_METRO, NRF52840_PCA10056, PARTICLE_XENON, NINA_B302_ublox, etc.), etc. boards running WiFiNINA modules/shields.*** with smaller memory (64+K bytes) The AVR-family boards (UNO, Nano, etc.) are ***not supported*** as they don't have enough memory to run Config Portal WebServer. @@ -57,11 +67,14 @@ The web configuration portal, served from the `WiFiNINA modules/shields` is oper 5. [`Arduino SAMD core 1.8.5 or later`](https://www.arduino.cc/en/Guide/ArduinoM0) for SAMD ARM Cortex-M0+ boards 6. [`Adafruit SAMD core 1.5.11 or later`](https://www.adafruit.com/) for SAMD ARM Cortex-M0+ and M4 boards (Nano 33 IoT, etc.) 7. [`Arduino Core for STM32 v1.8.0 or later`](https://github.com/khoih-prog/Arduino_Core_STM32) for STM32 boards - 8. [`Functional-VLPP library`](https://github.com/khoih-prog/functional-vlpp) to use server's lambda function. To install. check [![arduino-library-badge](https://www.ardu-badge.com/badge/Functional-Vlpp.svg?)](https://www.ardu-badge.com/Functional-Vlpp) - 9. [`WiFiNINA_Generic library v1.5.1-final or later`](https://github.com/khoih-prog/WiFiNINA_Generic) -10. [`WiFiWebServer library v1.0.1 or later`](https://github.com/khoih-prog/WiFiWebServer) -11. [`FlashStorage_SAMD library v1.0.0`](https://github.com/khoih-prog/FlashStorage_SAMD) for SAMD21 and SAMD51 boards (ZERO, MKR, NANO_33_IOT, M0, M0 Pro, AdaFruit Itsy-Bitsy M4, etc.) -12. [`DueFlashStorage library`](https://github.com/sebnil/DueFlashStorage) for SAM DUE + 8. [`Adafruit nRF52 v0.20.1 or later`](www.adafruit.com) for nRF52 boards such as Adafruit NRF52840_FEATHER, NRF52832_FEATHER, NRF52840_FEATHER_SENSE, NRF52840_ITSYBITSY, NRF52840_CIRCUITPLAY, NRF52840_CLUE, NRF52840_METRO, NRF52840_PCA10056, PARTICLE_XENON, ***NINA_B302_ublox***, etc. + 9. [`Functional-VLPP library`](https://github.com/khoih-prog/functional-vlpp) to use server's lambda function. To install. check [![arduino-library-badge](https://www.ardu-badge.com/badge/Functional-Vlpp.svg?)](https://www.ardu-badge.com/Functional-Vlpp) +10. [`WiFiNINA_Generic library v1.5.1-final or later`](https://github.com/khoih-prog/WiFiNINA_Generic). To install. check [![arduino-library-badge](https://www.ardu-badge.com/badge/WiFiNINA_Generic.svg?)](https://www.ardu-badge.com/WiFiNINA_Generic) +11. [`WiFiWebServer library v1.0.1 or later`](https://github.com/khoih-prog/WiFiWebServer). To install. check [![arduino-library-badge](https://www.ardu-badge.com/badge/WiFiWebServer.svg?)](https://www.ardu-badge.com/WiFiWebServer) +12. [`FlashStorage_SAMD library v1.0.0`](https://github.com/khoih-prog/FlashStorage_SAMD) for SAMD21 and SAMD51 boards (ZERO, MKR, NANO_33_IOT, M0, M0 Pro, AdaFruit Itsy-Bitsy M4, etc.) +13. [`DueFlashStorage library`](https://github.com/sebnil/DueFlashStorage) for SAM DUE +14. [`Adafruit's LittleFS/InternalFS`](www.adafruit.com)) for nRF52 +15. [`DoubleResetDetector_Generic`](https://github.com/khoih-prog/DoubleResetDetector_Generic). To install. check [![arduino-library-badge](https://www.ardu-badge.com/badge/DoubleResetDetector_Generic.svg?)](https://www.ardu-badge.com/DoubleResetDetector_Generic) ## How It Works @@ -244,86 +257,96 @@ The WiFi Credentials will be saved and the board connect to the selected WiFi AP If you're already connected to a listed WiFi AP and don't want to change anything, just select `Exit` from the `Main` page to reboot the board and connect to the previously-stored AP. The WiFi Credentials are still intact. -#### On Demand Configuration Portal +### Important notes +1. Now you can use special chars such as ***~, !, @, #, $, %, ^, &, _, -, space,etc.*** thanks to [brondolin](https://github.com/brondolin) to provide the amazing fix in [Blynk_WM](https://github.com/khoih-prog/Blynk_WM) to permit input special chars such as ***%*** and ***#*** into data fields. See [Issue 3](https://github.com/khoih-prog/Blynk_WM/issues/3). +2. The SSIDs, Passwords must be input (or to make them different from ***blank***). Otherwise, the Config Portal will re-open until those fields have been changed. If you don't need any field, just input anything or use duplicated data from similar field. +3. WiFi password max length now is 63 chars according to WPA2 standard. + +### How to use default Credentials and have them pre-loaded onto Config Portal + +See this example and modify as necessary + +1. To load [Default Credentials](examples//Credentials.h) +``` +bool LOAD_DEFAULT_CONFIG_DATA = true; +``` + +2. To use system default to load "blank" when there is no valid Credentials +``` +bool LOAD_DEFAULT_CONFIG_DATA = false; +``` -Sample Code +3. Example of [Default Credentials](examples/SAMD_WiFiNINA/Credentials.h) ```cpp -/* Comment this out to disable prints and save space */ -#define DEBUG_WIFI_WEBSERVER_PORT Serial -#define WIFININA_DEBUG_OUTPUT Serial +/// Start Default Config Data ////////////////// -#define WIFININA_DEBUG true +/* +#define SSID_MAX_LEN 32 +//From v1.0.3, WPA2 passwords can be up to 63 characters long. +#define PASS_MAX_LEN 64 -#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) || defined(__SAMD21E18A__) || defined(__SAMD51__) || defined(__SAMD51J20A__) || defined(__SAMD51J19A__) \ - || defined(__SAMD51G19A__) || defined(__SAMD21G18A__) ) -#if defined(WIFININA_USE_SAMD) -#undef WIFININA_USE_SAMD -#undef WIFI_USE_SAMD -#endif -#define WIFININA_USE_SAMD true -#define WIFI_USE_SAMD true -#else -#error This code is intended to run only on the SAMD boards ! Please check your Tools->Board setting. -#endif +typedef struct +{ + char wifi_ssid[SSID_MAX_LEN]; + char wifi_pw [PASS_MAX_LEN]; +} WiFi_Credentials; -#if defined(WIFININA_USE_SAMD) +#define NUM_WIFI_CREDENTIALS 2 + +// Configurable items besides fixed Header, just add board_name +#define NUM_CONFIGURABLE_ITEMS ( ( 2 * NUM_WIFI_CREDENTIALS ) + 1 ) +//////////////// + +typedef struct Configuration +{ + char header [16]; + WiFi_Credentials WiFi_Creds [NUM_WIFI_CREDENTIALS]; + char board_name [24]; + int checkSum; +} WiFiNINA_Configuration; +*/ + +#define TO_LOAD_DEFAULT_CONFIG_DATA true + +#if TO_LOAD_DEFAULT_CONFIG_DATA + +bool LOAD_DEFAULT_CONFIG_DATA = true; + +WiFiNINA_Configuration defaultConfig = +{ + //char header[16], dummy, not used + "WIFININA", + // WiFi_Credentials WiFi_Creds [NUM_WIFI_CREDENTIALS]; + // WiFi_Credentials.wifi_ssid and WiFi_Credentials.wifi_pw + "SSID1", "password1", + "SSID2", "password2", + //char board_name [24]; + "Air-Control", + // terminate the list + //int checkSum, dummy, not used + 0 + /////////// End Default Config Data ///////////// +}; -#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(ADAFRUIT_ITSYBITSY_M4_EXPRESS) -#define BOARD_TYPE "SAMD ADAFRUIT_ITSYBITSY_M4_EXPRESS" -#elif defined(__SAMD21E18A__) -#define BOARD_TYPE "SAMD21E18A" -#elif defined(__SAMD21G18A__) -#define BOARD_TYPE "SAMD21G18A" -#elif defined(__SAMD51G19A__) -#define BOARD_TYPE "SAMD51G19A" -#elif defined(__SAMD51J19A__) -#define BOARD_TYPE "SAMD51J19A" -#elif defined(__SAMD51J20A__) -#define BOARD_TYPE "SAMD51J20A" -#elif defined(__SAM3X8E__) -#define BOARD_TYPE "SAM3X8E" -#elif defined(__CPU_ARC__) -#define BOARD_TYPE "CPU_ARC" -#elif defined(__SAMD51__) -#define BOARD_TYPE "SAMD51" #else -#define BOARD_TYPE "SAMD Unknown" -#endif -#endif +bool LOAD_DEFAULT_CONFIG_DATA = false; -// Start location in EEPROM to store config data. Default 0 -// Config data Size currently is 128 bytes) -#define EEPROM_START 0 +WiFiNINA_Configuration defaultConfig; -#include "WiFiNINA_Pinout_Generic.h" +#endif // TO_LOAD_DEFAULT_CONFIG_DATA -#include +/////////// End Default Config Data ///////////// +``` + +### How to add dynamic parameters from sketch + +Example of [Default dynamicParams](examples/SAMD_WiFiNINA/dynamicParams.h) + +- To add custom parameters, just modify the example below +``` #define USE_DYNAMIC_PARAMETERS true /////////////// Start dynamic Credentials /////////////// @@ -347,17 +370,17 @@ Sample Code #define MAX_BLYNK_SERVER_LEN 34 #define MAX_BLYNK_TOKEN_LEN 34 -char Blynk_Server1 [MAX_BLYNK_SERVER_LEN + 1] = ""; -char Blynk_Token1 [MAX_BLYNK_TOKEN_LEN + 1] = ""; +char Blynk_Server1 [MAX_BLYNK_SERVER_LEN + 1] = "account.duckdns.org"; +char Blynk_Token1 [MAX_BLYNK_TOKEN_LEN + 1] = "token1"; -char Blynk_Server2 [MAX_BLYNK_SERVER_LEN + 1] = ""; -char Blynk_Token2 [MAX_BLYNK_TOKEN_LEN + 1] = ""; +char Blynk_Server2 [MAX_BLYNK_SERVER_LEN + 1] = "account.ddns.net"; +char Blynk_Token2 [MAX_BLYNK_TOKEN_LEN + 1] = "token2"; #define MAX_BLYNK_PORT_LEN 6 -char Blynk_Port [MAX_BLYNK_PORT_LEN + 1] = ""; +char Blynk_Port [MAX_BLYNK_PORT_LEN + 1] = "8080"; #define MAX_MQTT_SERVER_LEN 34 -char MQTT_Server [MAX_MQTT_SERVER_LEN + 1] = ""; +char MQTT_Server [MAX_MQTT_SERVER_LEN + 1] = "mqtt.duckdns.org"; MenuItem myMenuItems [] = { @@ -379,6 +402,50 @@ uint16_t NUM_MENU_ITEMS = 0; #endif //USE_DYNAMIC_PARAMETERS +``` +- If you don't need to add dynamic parameters, use the following in sketch + +``` +#define USE_DYNAMIC_PARAMETERS false +``` + +or + +``` +/////////////// Start dynamic Credentials /////////////// + +MenuItem myMenuItems [] = {}; + +uint16_t NUM_MENU_ITEMS = 0; +/////// // End dynamic Credentials /////////// + +``` + +### Important Notes for using Dynamic Parameters' ids + +1. These ids (such as "mqtt" in example) must be ***unique***. + +Please be noted that the following ***reserved names are already used in library***: + +``` +"id" for WiFi SSID +"pw" for WiFi PW +"id1" for WiFi1 SSID +"pw1" for WiFi1 PW +"nm" for Board Name +``` + +## Example [SAMD_WiFiNINA](examples/SAMD_WiFiNINA) + +Please take a look at other examples, as well. + +1. File [SAMD_WiFiNINA.ino](examples/SAMD_WiFiNINA/SAMD_WiFiNINA.ino) + +```cpp +#include "defines.h" +#include "Credentials.h" +#include "dynamicParams.h" + void heartBeatPrint(void) { static int num = 1; @@ -431,7 +498,7 @@ void setup() //WiFiManager_NINA->setConfigPortalChannel(1); // Set customized DHCP HostName - WiFiManager_NINA->begin("SAMD-WiFiNINA-ABCDEF"); + WiFiManager_NINA->begin(HOST_NAME); //Or use default Hostname "SAMD-WiFiNINA-XXXXXX" //WiFiManager_NINA->begin(); @@ -477,6 +544,235 @@ void loop() } ``` +2. File [defines.h](examples/SAMD_WiFiNINA/defines.h) + +```cpp +#ifndef defines_h +#define defines_h + +/* Comment this out to disable prints and save space */ +#define DEBUG_WIFI_WEBSERVER_PORT Serial +#define WIFININA_DEBUG_OUTPUT Serial + +#define WIFININA_DEBUG false //true + +#define DRD_GENERIC_DEBUG false //true + +#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) || defined(__SAMD21E18A__) || defined(__SAMD51__) || defined(__SAMD51J20A__) || defined(__SAMD51J19A__) \ + || defined(__SAMD51G19A__) || defined(__SAMD21G18A__) ) +#if defined(WIFININA_USE_SAMD) +#undef WIFININA_USE_SAMD +#undef WIFI_USE_SAMD +#endif +#define WIFININA_USE_SAMD true +#define WIFI_USE_SAMD true +#else +#error This code is intended to run only on the SAMD boards ! Please check your Tools->Board setting. +#endif + +#if defined(WIFININA_USE_SAMD) + +#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(ADAFRUIT_ITSYBITSY_M4_EXPRESS) +#define BOARD_TYPE "SAMD ADAFRUIT_ITSYBITSY_M4_EXPRESS" +#elif defined(__SAMD21E18A__) +#define BOARD_TYPE "SAMD21E18A" +#elif defined(__SAMD21G18A__) +#define BOARD_TYPE "SAMD21G18A" +#elif defined(__SAMD51G19A__) +#define BOARD_TYPE "SAMD51G19A" +#elif defined(__SAMD51J19A__) +#define BOARD_TYPE "SAMD51J19A" +#elif defined(__SAMD51J20A__) +#define BOARD_TYPE "SAMD51J20A" +#elif defined(__SAM3X8E__) +#define BOARD_TYPE "SAM3X8E" +#elif defined(__CPU_ARC__) +#define BOARD_TYPE "CPU_ARC" +#elif defined(__SAMD51__) +#define BOARD_TYPE "SAMD51" +#else +#define BOARD_TYPE "SAMD Unknown" +#endif + +#endif + +// Start location in EEPROM to store config data. Default 0 +// Config data Size currently is 128 bytes) +#define EEPROM_START 0 +#define EEPROM_SIZE (2 * 1024) + +#include "WiFiNINA_Pinout_Generic.h" + +#include + +#define HOST_NAME "SAMD-Master-Controller" + +#endif //defines_h +``` + +3. File [Credentials.h](examples/SAMD_WiFiNINA/Credentials.h) + +```cpp +#ifndef Credentials_h +#define Credentials_h + +#include "defines.h" + +/// Start Default Config Data ////////////////// + +/* +#define SSID_MAX_LEN 32 +//From v1.0.3, 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; + +#define NUM_WIFI_CREDENTIALS 2 + +// Configurable items besides fixed Header, just add board_name +#define NUM_CONFIGURABLE_ITEMS ( ( 2 * NUM_WIFI_CREDENTIALS ) + 1 ) +//////////////// + +typedef struct Configuration +{ + char header [16]; + WiFi_Credentials WiFi_Creds [NUM_WIFI_CREDENTIALS]; + char board_name [24]; + int checkSum; +} WiFiNINA_Configuration; +*/ + +#define TO_LOAD_DEFAULT_CONFIG_DATA true + +#if TO_LOAD_DEFAULT_CONFIG_DATA + +bool LOAD_DEFAULT_CONFIG_DATA = true; + +WiFiNINA_Configuration defaultConfig = +{ + //char header[16], dummy, not used + "WIFININA", + // WiFi_Credentials WiFi_Creds [NUM_WIFI_CREDENTIALS]; + // WiFi_Credentials.wifi_ssid and WiFi_Credentials.wifi_pw + "SSID1", "password1", + "SSID2", "password2", + //char board_name [24]; + "Air-Control", + // terminate the list + //int checkSum, dummy, not used + 0 + /////////// End Default Config Data ///////////// +}; + +#else + +bool LOAD_DEFAULT_CONFIG_DATA = false; + +WiFiNINA_Configuration defaultConfig; + +#endif // TO_LOAD_DEFAULT_CONFIG_DATA + +/////////// End Default Config Data ///////////// + + +#endif //Credentials_h +``` + + +4. File [dynamicParams.h](examples/SAMD_WiFiNINA/dynamicParams.h) + +```cpp +#ifndef dynamicParams_h +#define dynamicParams_h + +#include "defines.h" + +#define USE_DYNAMIC_PARAMETERS true + +/////////////// Start dynamic Credentials /////////////// + +//Defined in +/************************************** + #define MAX_ID_LEN 5 + #define MAX_DISPLAY_NAME_LEN 16 + + typedef struct + { + char id [MAX_ID_LEN + 1]; + char displayName [MAX_DISPLAY_NAME_LEN + 1]; + char *pdata; + uint8_t maxlen; + } MenuItem; +**************************************/ + +#if USE_DYNAMIC_PARAMETERS + +#define MAX_BLYNK_SERVER_LEN 34 +#define MAX_BLYNK_TOKEN_LEN 34 + +char Blynk_Server1 [MAX_BLYNK_SERVER_LEN + 1] = "account.duckdns.org"; +char Blynk_Token1 [MAX_BLYNK_TOKEN_LEN + 1] = "token1"; + +char Blynk_Server2 [MAX_BLYNK_SERVER_LEN + 1] = "account.ddns.net"; +char Blynk_Token2 [MAX_BLYNK_TOKEN_LEN + 1] = "token2"; + +#define MAX_BLYNK_PORT_LEN 6 +char Blynk_Port [MAX_BLYNK_PORT_LEN + 1] = "8080"; + +#define MAX_MQTT_SERVER_LEN 34 +char MQTT_Server [MAX_MQTT_SERVER_LEN + 1] = "mqtt.duckdns.org"; + +MenuItem myMenuItems [] = +{ + { "sv1", "Blynk Server1", Blynk_Server1, MAX_BLYNK_SERVER_LEN }, + { "tk1", "Token1", Blynk_Token1, MAX_BLYNK_TOKEN_LEN }, + { "sv2", "Blynk Server2", Blynk_Server2, MAX_BLYNK_SERVER_LEN }, + { "tk2", "Token2", Blynk_Token2, MAX_BLYNK_TOKEN_LEN }, + { "pt", "Port", Blynk_Port, MAX_BLYNK_PORT_LEN }, + { "mq", "MQTT Server", MQTT_Server, MAX_MQTT_SERVER_LEN }, +}; + +uint16_t NUM_MENU_ITEMS = sizeof(myMenuItems) / sizeof(MenuItem); //MenuItemSize; + +#else + +MenuItem myMenuItems [] = {}; + +uint16_t NUM_MENU_ITEMS = 0; + +#endif //USE_DYNAMIC_PARAMETERS + + +#endif //dynamicParams_h +``` + This is the terminal output when running [SAMD_WiFiNINA](examples/SAMD_WiFiNINA) example on ***Nano-33 IoT***: 1. Open Config Portal @@ -664,6 +960,129 @@ Port = 8080 MQTT Server = mqtt-server ``` +6. DRD Not Detected: + +``` + +Start SAMD_WiFiNINA on SAMD NANO_33_IOT +*NN: Hostname=SAMD-Master-Controller +Flag read = 0xd0d04321 +No doubleResetDetected <==== DRD not detected, run normally +SetFlag write = 0xd0d01234 +*NN: ======= Start Default Config Data ======= +*NN: Hdr=WIFININA,SSID=SSID1,PW=password1 +*NN: SSID1=SSID2,PW1=password2 +*NN: BName=Air-Control +*NN: i=0,id=sv1,data=account.duckdns.org +*NN: i=1,id=tk1,data=token1 +*NN: i=2,id=sv2,data=account.ddns.net +*NN: i=3,id=tk2,data=token2 +*NN: i=4,id=pt,data=8080 +*NN: i=5,id=mq,data=mqtt.duckdns.org +*NN: ChkCrR:CrCCsum=20d4,CrRCsum=20d4 +*NN: Valid Stored Dynamic Data +*NN: CrCCSum=8404,CrRCSum=8404 +*NN: ======= Start Stored Config Data ======= +*NN: Hdr=WIFININA,SSID=HueNet1,PW=**** +*NN: SSID1=HueNet2,PW1=**** +*NN: BName=Air-Control-Nano33IoT +*NN: i=0,id=sv1,data=new-account.duckdns.org +*NN: i=1,id=tk1,data=new-token1 +*NN: i=2,id=sv2,data=new-account.ddns.net +*NN: i=3,id=tk2,data=new-token2 +*NN: i=4,id=pt,data=8080 +*NN: i=5,id=mq,data=new-mqtt.duckdns.org +*NN: CCSum=0x1571,RCSum=0x1571 +*NN: Hdr=WIFININA,SSID=HueNet1,PW=**** +*NN: SSID1=HueNet2,PW1=**** +*NN: BName=Air-Control-Nano33IoT +*NN: i=0,id=sv1,data=new-account.duckdns.org +*NN: i=1,id=tk1,data=new-token1 +*NN: i=2,id=sv2,data=new-account.ddns.net +*NN: i=3,id=tk2,data=new-token2 +*NN: i=4,id=pt,data=8080 +*NN: i=5,id=mq,data=new-mqtt.duckdns.org +*NN: bg: noConfigPortal = true +*NN: Connecting MultiWifi... +*NN: con2WF:spentMsec=0 +WiFi-begin: return1 = 3 +WiFi-begin: return2 = 3 +*NN: con2WF:OK +*NN: SSID=HueNet1,RSSI=-26 +*NN: IP=192.168.2.93 +*NN: b:WOK +H +Your stored Credentials : +Blynk Server1 = new-account.duckdns.org +Token1 = new-token1 +Blynk Server2 = new-account.ddns.net +Token2 = new-token2 +Port = 8080 +MQTT Server = new-mqtt.duckdns.org +Stop doubleResetDetecting +ClearFlag write = 0xd0d04321 + +``` + +7.DRD detected and Config Portal is forcefully opened + +``` +Start SAMD_WiFiNINA on SAMD NANO_33_IOT +*NN: Hostname=SAMD-Master-Controller +Flag read = 0xd0d01234 +doubleResetDetected <==== DRD detected, to open Config Portal +ClearFlag write = 0xd0d04321 +*NN: Double Reset Detected +*NN: ======= Start Default Config Data ======= +*NN: Hdr=WIFININA,SSID=SSID1,PW=password1 +*NN: SSID1=SSID2,PW1=password2 +*NN: BName=Air-Control +*NN: i=0,id=sv1,data=account.duckdns.org +*NN: i=1,id=tk1,data=token1 +*NN: i=2,id=sv2,data=account.ddns.net +*NN: i=3,id=tk2,data=token2 +*NN: i=4,id=pt,data=8080 +*NN: i=5,id=mq,data=mqtt.duckdns.org +*NN: ChkCrR:CrCCsum=20d4,CrRCsum=20d4 +*NN: Valid Stored Dynamic Data +*NN: CrCCSum=8404,CrRCSum=8404 +*NN: ======= Start Stored Config Data ======= +*NN: Hdr=WIFININA,SSID=HueNet1,PW=**** +*NN: SSID1=HueNet2,PW1=**** +*NN: BName=Air-Control-Nano33IoT +*NN: i=0,id=sv1,data=new-account.duckdns.org +*NN: i=1,id=tk1,data=new-token1 +*NN: i=2,id=sv2,data=new-account.ddns.net +*NN: i=3,id=tk2,data=new-token2 +*NN: i=4,id=pt,data=8080 +*NN: i=5,id=mq,data=new-mqtt.duckdns.org +*NN: CCSum=0x1571,RCSum=0x1571 +*NN: Hdr=WIFININA,SSID=HueNet1,PW=**** +*NN: SSID1=HueNet2,PW1=**** +*NN: BName=Air-Control-Nano33IoT +*NN: i=0,id=sv1,data=new-account.duckdns.org +*NN: i=1,id=tk1,data=new-token1 +*NN: i=2,id=sv2,data=new-account.ddns.net +*NN: i=3,id=tk2,data=new-token2 +*NN: i=4,id=pt,data=8080 +*NN: i=5,id=mq,data=new-mqtt.duckdns.org +*NN: bg: noConfigPortal = false +*NN: b:OpenPortal <==== Config Portal opened +*NN: SSID=WIFININA_51F485,PW=MyWIFININA_51F485 +*NN: IP=192.168.4.1,CH=10 +WiFi-beginAP3: return1 = 7 +WiFi-beginAP3: return2 = 7 +F +Your stored Credentials : +Blynk Server1 = new-account.duckdns.org +Token1 = new-token1 +Blynk Server2 = new-account.ddns.net +Token2 = new-token2 +Port = 8080 +MQTT Server = new-mqtt.duckdns.org + +``` + #### Debug Debug is enabled by default on Serial. To disable, add at the beginning of sketch @@ -672,6 +1091,10 @@ Debug is enabled by default on Serial. To disable, add at the beginning of sketc #define DEBUG_WIFI_WEBSERVER_PORT Serial #define WIFININA_DEBUG_OUTPUT false + +#define WIFININA_DEBUG false //true + +#define DRD_GENERIC_DEBUG false //true ``` ## Troubleshooting @@ -681,15 +1104,29 @@ Sometimes, the library will only work if you update the `WiFiNINA module/shield` ### TO DO -1. Too many things to list, EEPROM, SPIFFS/FS/FAT FS (if available) -2. Support more boards +1. Support more boards ### DONE -1. Adding dynamic custom parameters -2. Add MultiWiFi and Auto(Re)Connect feature -3. Add support to SAMD21, SAMD51, nRF52, etc. -4. Many more to list ( DHCP Hostname, WPA2 password length, special chars, etc.) + 1. Adding dynamic custom parameters + 2. Add MultiWiFi and Auto(Re)Connect feature + 3. Add support to SAMD21, SAMD51, nRF52, etc. + 4. Many more to list ( DHCP Hostname, WPA2 password length, special chars, etc.) + 5. Add DRD + 6. Add default Credentials + 7. Add Dynamic parameters + 8. Add Configurable Config Portal Title + 9. Spilt each example into several manageable files. + +#### Major Release v1.0.4 + +1. Configurable ***Config Portal Title*** to be either HostName, BoardName or default undistinguishable names. +2. Optional default ***Credentials as well as Dynamic parameters to be optionally autoloaded into Config Portal*** to use or change instead of manually input. +3. ***DoubleDetectDetector*** feature to force Config Portal when double reset is detected within predetermined time, default 10s. +4. Examples are redesigned to separate Credentials / Defines / Dynamic Params / Code so that you can change Credentials / Dynamic Params quickly for each device. +5. Add Board Name + +Thanks to [thorathome in GitHub](https://github.com/thorathome) to test, suggest and encourage to add those new features in [Blynk_WM](https://github.com/khoih-prog/Blynk_WM), such as Default Credentials/Dynamic Params, Configurable Config Portal Title, DRD. Now those features are speading fast into libraries having similar functionalities. #### Major Release v1.0.3 @@ -720,6 +1157,7 @@ Sometimes, the library will only work if you update the `WiFiNINA module/shield` ### Contributions and thanks 1. Thanks to [Darvesh7](https://github.com/Darvesh7) to report [When Saved Network is unavailable or credential changed bug](https://github.com/khoih-prog/WiFiManager_NINA_Lite/issues/1) leading to the major release v1.0.3 +2. Thanks to [thorathome in GitHub](https://github.com/thorathome) to test, suggest and encourage to add those new features in [Blynk_WM](https://github.com/khoih-prog/Blynk_WM), such as Default Credentials/Dynamic Params, Configurable Config Portal Title, DRD. Now those features are speading fast into libraries having similar functionalities. ### Contributing diff --git a/examples/Mega_WiFiNINA/Credentials.h b/examples/Mega_WiFiNINA/Credentials.h new file mode 100644 index 00000000..fc9a519a --- /dev/null +++ b/examples/Mega_WiFiNINA/Credentials.h @@ -0,0 +1,89 @@ +/**************************************************************************************************************************** + Credentials.h + For nRF52 boards using WiFiNINA modules/shields, using much less code to support boards with smaller memory + + WiFiManager_NINA_WM_Lite is a library for the Mega, Teensy, SAM DUE, SAMD and STM32 boards + (https://github.com/khoih-prog/WiFiManager_NINA_Lite) to enable store Credentials in EEPROM/LittleFS for easy + configuration/reconfiguration and autoconnect/autoreconnect of WiFi and other services without Hardcoding. + + Built by Khoi Hoang https://github.com/khoih-prog/WiFiManager_NINA_Lite + Licensed under MIT license + Version: 1.0.4 + + Version Modified By Date Comments + ------- ----------- ---------- ----------- + 1.0.0 K Hoang 26/03/2020 Initial coding + 1.0.1 K Hoang 27/03/2020 Fix SAMD soft-reset bug. Add support to remaining boards + 1.0.2 K Hoang 15/04/2020 Fix bug. Add SAMD51 support. + 1.0.3 K Hoang 24/04/2020 Fix bug. Add nRF5 (Adafruit, NINA_B302_ublox, etc.) support. Add MultiWiFi, HostName capability. + SSID password maxlen is 63 now. Permit special chars # and % in input data. + 1.0.4 K Hoang 04/05/2020 Add Configurable Config Portal Title, Default Config Data and DRD. Update examples. + *****************************************************************************************************************************/ + +#ifndef Credentials_h +#define Credentials_h + +#include "defines.h" + +/// Start Default Config Data ////////////////// + +/* +#define SSID_MAX_LEN 32 +//From v1.0.3, 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; + +#define NUM_WIFI_CREDENTIALS 2 + +// Configurable items besides fixed Header, just add board_name +#define NUM_CONFIGURABLE_ITEMS ( ( 2 * NUM_WIFI_CREDENTIALS ) + 1 ) +//////////////// + +typedef struct Configuration +{ + char header [16]; + WiFi_Credentials WiFi_Creds [NUM_WIFI_CREDENTIALS]; + char board_name [24]; + int checkSum; +} WiFiNINA_Configuration; +*/ + +#define TO_LOAD_DEFAULT_CONFIG_DATA true + +#if TO_LOAD_DEFAULT_CONFIG_DATA + +bool LOAD_DEFAULT_CONFIG_DATA = true; + +WiFiNINA_Configuration defaultConfig = +{ + //char header[16], dummy, not used + "WIFININA", + // WiFi_Credentials WiFi_Creds [NUM_WIFI_CREDENTIALS]; + // WiFi_Credentials.wifi_ssid and WiFi_Credentials.wifi_pw + "SSID1", "password1", + "SSID2", "password2", + //char board_name [24]; + "Air-Control", + // terminate the list + //int checkSum, dummy, not used + 0 + /////////// End Default Config Data ///////////// +}; + +#else + +bool LOAD_DEFAULT_CONFIG_DATA = false; + +WiFiNINA_Configuration defaultConfig; + +#endif // TO_LOAD_DEFAULT_CONFIG_DATA + +/////////// End Default Config Data ///////////// + + +#endif //Credentials_h diff --git a/examples/Mega_WiFiNINA/Mega_WiFiNINA.ino b/examples/Mega_WiFiNINA/Mega_WiFiNINA.ino index 3e998f50..d6b5f05a 100644 --- a/examples/Mega_WiFiNINA/Mega_WiFiNINA.ino +++ b/examples/Mega_WiFiNINA/Mega_WiFiNINA.ino @@ -2,13 +2,13 @@ Mega_WiFiNINA.ino For AVR or Generic boards using WiFiNINA Modules/Shields, using much less code to support boards with smaller memory - WiFiManager_NINA_WM_Lite is a library for the Mega, Teensy, SAM DUE, SAMD and STM32 boards (https://github.com/khoih-prog/WiFiManager_NINA_Lite) - to enable store Credentials in EEPROM to easy configuration/reconfiguration and autoconnect/autoreconnect of WiFi and other services - without Hardcoding. + WiFiManager_NINA_WM_Lite is a library for the Mega, Teensy, SAM DUE, SAMD and STM32 boards + (https://github.com/khoih-prog/WiFiManager_NINA_Lite) to enable store Credentials in EEPROM/LittleFS for easy + configuration/reconfiguration and autoconnect/autoreconnect of WiFi and other services without Hardcoding. Built by Khoi Hoang https://github.com/khoih-prog/WiFiManager_NINA_Lite Licensed under MIT license - Version: 1.0.3 + Version: 1.0.4 Version Modified By Date Comments ------- ----------- ---------- ----------- @@ -17,83 +17,11 @@ 1.0.2 K Hoang 15/04/2020 Fix bug. Add SAMD51 support. 1.0.3 K Hoang 24/04/2020 Fix bug. Add nRF5 (Adafruit, NINA_B302_ublox, etc.) support. Add MultiWiFi, HostName capability. SSID password maxlen is 63 now. Permit special chars # and % in input data. + 1.0.4 K Hoang 04/05/2020 Add Configurable Config Portal Title, Default Config Data and DRD. Update examples. *****************************************************************************************************************************/ - -/* Comment this out to disable prints and save space */ -#define DEBUG_WIFI_WEBSERVER_PORT Serial -#define WIFININA_DEBUG_OUTPUT Serial - -#if !( defined(ARDUINO_AVR_MEGA) || defined(ARDUINO_AVR_MEGA2560) ) -#error This code is intended to run only on the Arduino Mega 1280/2560 boards ! Please check your Tools->Board setting. -#endif - -#if defined(ARDUINO_AVR_MEGA2560) -#define BOARD_TYPE "AVR Mega2560" -#else -#define BOARD_TYPE "AVR Mega" -#endif - -// Start location in EEPROM to store config data. Default 0 -// Config data Size currently is 128 bytes) -#define EEPROM_START 0 - -#include - -// Mega might have not enough memory to run dynamic params -#define USE_DYNAMIC_PARAMETERS false //true - -/////////////// Start dynamic Credentials /////////////// - -//Defined in -/************************************** - #define MAX_ID_LEN 5 - #define MAX_DISPLAY_NAME_LEN 16 - - typedef struct - { - char id [MAX_ID_LEN + 1]; - char displayName [MAX_DISPLAY_NAME_LEN + 1]; - char *pdata; - uint8_t maxlen; - } MenuItem; -**************************************/ - -#if USE_DYNAMIC_PARAMETERS - -#define MAX_BLYNK_SERVER_LEN 34 -#define MAX_BLYNK_TOKEN_LEN 34 - -char Blynk_Server1 [MAX_BLYNK_SERVER_LEN + 1] = ""; -char Blynk_Token1 [MAX_BLYNK_TOKEN_LEN + 1] = ""; - -char Blynk_Server2 [MAX_BLYNK_SERVER_LEN + 1] = ""; -char Blynk_Token2 [MAX_BLYNK_TOKEN_LEN + 1] = ""; - -#define MAX_BLYNK_PORT_LEN 6 -char Blynk_Port [MAX_BLYNK_PORT_LEN + 1] = ""; - -#define MAX_MQTT_SERVER_LEN 34 -char MQTT_Server [MAX_MQTT_SERVER_LEN + 1] = ""; - -MenuItem myMenuItems [] = -{ - { "sv1", "Blynk Server1", Blynk_Server1, MAX_BLYNK_SERVER_LEN }, - { "tk1", "Token1", Blynk_Token1, MAX_BLYNK_TOKEN_LEN }, - { "sv2", "Blynk Server2", Blynk_Server2, MAX_BLYNK_SERVER_LEN }, - { "tk2", "Token2", Blynk_Token2, MAX_BLYNK_TOKEN_LEN }, - { "pt", "Port", Blynk_Port, MAX_BLYNK_PORT_LEN }, - { "mq", "MQTT Server", MQTT_Server, MAX_MQTT_SERVER_LEN }, -}; - -uint16_t NUM_MENU_ITEMS = sizeof(myMenuItems) / sizeof(MenuItem); //MenuItemSize; - -#else - -MenuItem myMenuItems [] = {}; - -uint16_t NUM_MENU_ITEMS = 0; - -#endif //USE_DYNAMIC_PARAMETERS +#include "defines.h" +#include "Credentials.h" +#include "dynamicParams.h" void heartBeatPrint(void) { @@ -146,7 +74,7 @@ void setup() //WiFiManager_NINA->setConfigPortalChannel(1); // Set customized DHCP HostName - WiFiManager_NINA->begin("Mega-WiFiNINA-ABCDEF"); + WiFiManager_NINA->begin(HOST_NAME); //Or use default Hostname "Mega-WiFiNINA-XXXXXX" //WiFiManager_NINA->begin(); } diff --git a/examples/Mega_WiFiNINA/defines.h b/examples/Mega_WiFiNINA/defines.h new file mode 100644 index 00000000..cc756d00 --- /dev/null +++ b/examples/Mega_WiFiNINA/defines.h @@ -0,0 +1,52 @@ +/**************************************************************************************************************************** + defines.h for ESP32WM_Config.ino + For nRF52 boards using WiFiNINA modules/shields, using much less code to support boards with smaller memory + + WiFiManager_NINA_WM_Lite is a library for the Mega, Teensy, SAM DUE, SAMD and STM32 boards + (https://github.com/khoih-prog/WiFiManager_NINA_Lite) to enable store Credentials in EEPROM/LittleFS for easy + configuration/reconfiguration and autoconnect/autoreconnect of WiFi and other services without Hardcoding. + + Built by Khoi Hoang https://github.com/khoih-prog/WiFiManager_NINA_Lite + Licensed under MIT license + Version: 1.0.4 + + Version Modified By Date Comments + ------- ----------- ---------- ----------- + 1.0.0 K Hoang 26/03/2020 Initial coding + 1.0.1 K Hoang 27/03/2020 Fix SAMD soft-reset bug. Add support to remaining boards + 1.0.2 K Hoang 15/04/2020 Fix bug. Add SAMD51 support. + 1.0.3 K Hoang 24/04/2020 Fix bug. Add nRF5 (Adafruit, NINA_B302_ublox, etc.) support. Add MultiWiFi, HostName capability. + SSID password maxlen is 63 now. Permit special chars # and % in input data. + 1.0.4 K Hoang 04/05/2020 Add Configurable Config Portal Title, Default Config Data and DRD. Update examples. + *****************************************************************************************************************************/ + +#ifndef defines_h +#define defines_h + +/* Comment this out to disable prints and save space */ +#define DEBUG_WIFI_WEBSERVER_PORT Serial +#define WIFININA_DEBUG_OUTPUT Serial + +#define WIFININA_DEBUG false //true + +#define DRD_GENERIC_DEBUG false //true + +#if !( defined(ARDUINO_AVR_MEGA) || defined(ARDUINO_AVR_MEGA2560) ) +#error This code is intended to run only on the Arduino Mega 1280/2560 boards ! Please check your Tools->Board setting. +#endif + +#if defined(ARDUINO_AVR_MEGA2560) +#define BOARD_TYPE "AVR Mega2560" +#else +#define BOARD_TYPE "AVR Mega" +#endif + +// Start location in EEPROM to store config data. Default 0 +// Config data Size currently is 128 bytes) +#define EEPROM_START 0 + +#include + +#define HOST_NAME "AVR-Master-Controller" + +#endif //defines_h diff --git a/examples/Mega_WiFiNINA/dynamicParams.h b/examples/Mega_WiFiNINA/dynamicParams.h new file mode 100644 index 00000000..c4644cac --- /dev/null +++ b/examples/Mega_WiFiNINA/dynamicParams.h @@ -0,0 +1,84 @@ +/**************************************************************************************************************************** + dynamicParams.h + For nRF52 boards using WiFiNINA modules/shields, using much less code to support boards with smaller memory + + WiFiManager_NINA_WM_Lite is a library for the Mega, Teensy, SAM DUE, SAMD and STM32 boards + (https://github.com/khoih-prog/WiFiManager_NINA_Lite) to enable store Credentials in EEPROM/LittleFS for easy + configuration/reconfiguration and autoconnect/autoreconnect of WiFi and other services without Hardcoding. + + Built by Khoi Hoang https://github.com/khoih-prog/WiFiManager_NINA_Lite + Licensed under MIT license + Version: 1.0.4 + + Version Modified By Date Comments + ------- ----------- ---------- ----------- + 1.0.0 K Hoang 26/03/2020 Initial coding + 1.0.1 K Hoang 27/03/2020 Fix SAMD soft-reset bug. Add support to remaining boards + 1.0.2 K Hoang 15/04/2020 Fix bug. Add SAMD51 support. + 1.0.3 K Hoang 24/04/2020 Fix bug. Add nRF5 (Adafruit, NINA_B302_ublox, etc.) support. Add MultiWiFi, HostName capability. + SSID password maxlen is 63 now. Permit special chars # and % in input data. + 1.0.4 K Hoang 04/05/2020 Add Configurable Config Portal Title, Default Config Data and DRD. Update examples. + *****************************************************************************************************************************/ + +#ifndef dynamicParams_h +#define dynamicParams_h + +#include "defines.h" + +#define USE_DYNAMIC_PARAMETERS true + +/////////////// Start dynamic Credentials /////////////// + +//Defined in +/************************************** + #define MAX_ID_LEN 5 + #define MAX_DISPLAY_NAME_LEN 16 + + typedef struct + { + char id [MAX_ID_LEN + 1]; + char displayName [MAX_DISPLAY_NAME_LEN + 1]; + char *pdata; + uint8_t maxlen; + } MenuItem; +**************************************/ + +#if USE_DYNAMIC_PARAMETERS + +#define MAX_BLYNK_SERVER_LEN 34 +#define MAX_BLYNK_TOKEN_LEN 34 + +char Blynk_Server1 [MAX_BLYNK_SERVER_LEN + 1] = "account.duckdns.org"; +char Blynk_Token1 [MAX_BLYNK_TOKEN_LEN + 1] = "token1"; + +char Blynk_Server2 [MAX_BLYNK_SERVER_LEN + 1] = "account.ddns.net"; +char Blynk_Token2 [MAX_BLYNK_TOKEN_LEN + 1] = "token2"; + +#define MAX_BLYNK_PORT_LEN 6 +char Blynk_Port [MAX_BLYNK_PORT_LEN + 1] = "8080"; + +#define MAX_MQTT_SERVER_LEN 34 +char MQTT_Server [MAX_MQTT_SERVER_LEN + 1] = "mqtt.duckdns.org"; + +MenuItem myMenuItems [] = +{ + { "sv1", "Blynk Server1", Blynk_Server1, MAX_BLYNK_SERVER_LEN }, + { "tk1", "Token1", Blynk_Token1, MAX_BLYNK_TOKEN_LEN }, + { "sv2", "Blynk Server2", Blynk_Server2, MAX_BLYNK_SERVER_LEN }, + { "tk2", "Token2", Blynk_Token2, MAX_BLYNK_TOKEN_LEN }, + { "pt", "Port", Blynk_Port, MAX_BLYNK_PORT_LEN }, + { "mq", "MQTT Server", MQTT_Server, MAX_MQTT_SERVER_LEN }, +}; + +uint16_t NUM_MENU_ITEMS = sizeof(myMenuItems) / sizeof(MenuItem); //MenuItemSize; + +#else + +MenuItem myMenuItems [] = {}; + +uint16_t NUM_MENU_ITEMS = 0; + +#endif //USE_DYNAMIC_PARAMETERS + + +#endif //dynamicParams_h diff --git a/examples/SAMD_WiFiNINA/Credentials.h b/examples/SAMD_WiFiNINA/Credentials.h new file mode 100644 index 00000000..fc9a519a --- /dev/null +++ b/examples/SAMD_WiFiNINA/Credentials.h @@ -0,0 +1,89 @@ +/**************************************************************************************************************************** + Credentials.h + For nRF52 boards using WiFiNINA modules/shields, using much less code to support boards with smaller memory + + WiFiManager_NINA_WM_Lite is a library for the Mega, Teensy, SAM DUE, SAMD and STM32 boards + (https://github.com/khoih-prog/WiFiManager_NINA_Lite) to enable store Credentials in EEPROM/LittleFS for easy + configuration/reconfiguration and autoconnect/autoreconnect of WiFi and other services without Hardcoding. + + Built by Khoi Hoang https://github.com/khoih-prog/WiFiManager_NINA_Lite + Licensed under MIT license + Version: 1.0.4 + + Version Modified By Date Comments + ------- ----------- ---------- ----------- + 1.0.0 K Hoang 26/03/2020 Initial coding + 1.0.1 K Hoang 27/03/2020 Fix SAMD soft-reset bug. Add support to remaining boards + 1.0.2 K Hoang 15/04/2020 Fix bug. Add SAMD51 support. + 1.0.3 K Hoang 24/04/2020 Fix bug. Add nRF5 (Adafruit, NINA_B302_ublox, etc.) support. Add MultiWiFi, HostName capability. + SSID password maxlen is 63 now. Permit special chars # and % in input data. + 1.0.4 K Hoang 04/05/2020 Add Configurable Config Portal Title, Default Config Data and DRD. Update examples. + *****************************************************************************************************************************/ + +#ifndef Credentials_h +#define Credentials_h + +#include "defines.h" + +/// Start Default Config Data ////////////////// + +/* +#define SSID_MAX_LEN 32 +//From v1.0.3, 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; + +#define NUM_WIFI_CREDENTIALS 2 + +// Configurable items besides fixed Header, just add board_name +#define NUM_CONFIGURABLE_ITEMS ( ( 2 * NUM_WIFI_CREDENTIALS ) + 1 ) +//////////////// + +typedef struct Configuration +{ + char header [16]; + WiFi_Credentials WiFi_Creds [NUM_WIFI_CREDENTIALS]; + char board_name [24]; + int checkSum; +} WiFiNINA_Configuration; +*/ + +#define TO_LOAD_DEFAULT_CONFIG_DATA true + +#if TO_LOAD_DEFAULT_CONFIG_DATA + +bool LOAD_DEFAULT_CONFIG_DATA = true; + +WiFiNINA_Configuration defaultConfig = +{ + //char header[16], dummy, not used + "WIFININA", + // WiFi_Credentials WiFi_Creds [NUM_WIFI_CREDENTIALS]; + // WiFi_Credentials.wifi_ssid and WiFi_Credentials.wifi_pw + "SSID1", "password1", + "SSID2", "password2", + //char board_name [24]; + "Air-Control", + // terminate the list + //int checkSum, dummy, not used + 0 + /////////// End Default Config Data ///////////// +}; + +#else + +bool LOAD_DEFAULT_CONFIG_DATA = false; + +WiFiNINA_Configuration defaultConfig; + +#endif // TO_LOAD_DEFAULT_CONFIG_DATA + +/////////// End Default Config Data ///////////// + + +#endif //Credentials_h diff --git a/examples/SAMD_WiFiNINA/SAMD_WiFiNINA.ino b/examples/SAMD_WiFiNINA/SAMD_WiFiNINA.ino index 8e53bb38..fe55b8d8 100644 --- a/examples/SAMD_WiFiNINA/SAMD_WiFiNINA.ino +++ b/examples/SAMD_WiFiNINA/SAMD_WiFiNINA.ino @@ -2,13 +2,13 @@ SAMD_WiFiNINA.ino For SAMD boards using WiFiNINA Modules/Shields, using much less code to support boards with smaller memory - WiFiManager_NINA_WM_Lite is a library for the Mega, Teensy, SAM DUE, SAMD and STM32 boards (https://github.com/khoih-prog/WiFiManager_NINA_Lite) - to enable store Credentials in EEPROM to easy configuration/reconfiguration and autoconnect/autoreconnect of WiFi and other services - without Hardcoding. + WiFiManager_NINA_WM_Lite is a library for the Mega, Teensy, SAM DUE, SAMD and STM32 boards + (https://github.com/khoih-prog/WiFiManager_NINA_Lite) to enable store Credentials in EEPROM/LittleFS for easy + configuration/reconfiguration and autoconnect/autoreconnect of WiFi and other services without Hardcoding. Built by Khoi Hoang https://github.com/khoih-prog/WiFiManager_NINA_Lite Licensed under MIT license - Version: 1.0.3 + Version: 1.0.4 Version Modified By Date Comments ------- ----------- ---------- ----------- @@ -17,137 +17,11 @@ 1.0.2 K Hoang 15/04/2020 Fix bug. Add SAMD51 support. 1.0.3 K Hoang 24/04/2020 Fix bug. Add nRF5 (Adafruit, NINA_B302_ublox, etc.) support. Add MultiWiFi, HostName capability. SSID password maxlen is 63 now. Permit special chars # and % in input data. + 1.0.4 K Hoang 04/05/2020 Add Configurable Config Portal Title, Default Config Data and DRD. Update examples. *****************************************************************************************************************************/ - -/* Comment this out to disable prints and save space */ -#define DEBUG_WIFI_WEBSERVER_PORT Serial -#define WIFININA_DEBUG_OUTPUT Serial - -#define WIFININA_DEBUG true - -#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) || defined(__SAMD21E18A__) || defined(__SAMD51__) || defined(__SAMD51J20A__) || defined(__SAMD51J19A__) \ - || defined(__SAMD51G19A__) || defined(__SAMD21G18A__) ) -#if defined(WIFININA_USE_SAMD) -#undef WIFININA_USE_SAMD -#undef WIFI_USE_SAMD -#endif -#define WIFININA_USE_SAMD true -#define WIFI_USE_SAMD true -#else -#error This code is intended to run only on the SAMD boards ! Please check your Tools->Board setting. -#endif - -#if defined(WIFININA_USE_SAMD) - -#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(ADAFRUIT_ITSYBITSY_M4_EXPRESS) -#define BOARD_TYPE "SAMD ADAFRUIT_ITSYBITSY_M4_EXPRESS" -#elif defined(__SAMD21E18A__) -#define BOARD_TYPE "SAMD21E18A" -#elif defined(__SAMD21G18A__) -#define BOARD_TYPE "SAMD21G18A" -#elif defined(__SAMD51G19A__) -#define BOARD_TYPE "SAMD51G19A" -#elif defined(__SAMD51J19A__) -#define BOARD_TYPE "SAMD51J19A" -#elif defined(__SAMD51J20A__) -#define BOARD_TYPE "SAMD51J20A" -#elif defined(__SAM3X8E__) -#define BOARD_TYPE "SAM3X8E" -#elif defined(__CPU_ARC__) -#define BOARD_TYPE "CPU_ARC" -#elif defined(__SAMD51__) -#define BOARD_TYPE "SAMD51" -#else -#define BOARD_TYPE "SAMD Unknown" -#endif - -#endif - -// Start location in EEPROM to store config data. Default 0 -// Config data Size currently is 128 bytes) -#define EEPROM_START 0 - -#include "WiFiNINA_Pinout_Generic.h" - -#include - -#define USE_DYNAMIC_PARAMETERS true - -/////////////// Start dynamic Credentials /////////////// - -//Defined in -/************************************** - #define MAX_ID_LEN 5 - #define MAX_DISPLAY_NAME_LEN 16 - - typedef struct - { - char id [MAX_ID_LEN + 1]; - char displayName [MAX_DISPLAY_NAME_LEN + 1]; - char *pdata; - uint8_t maxlen; - } MenuItem; -**************************************/ - -#if USE_DYNAMIC_PARAMETERS - -#define MAX_BLYNK_SERVER_LEN 34 -#define MAX_BLYNK_TOKEN_LEN 34 - -char Blynk_Server1 [MAX_BLYNK_SERVER_LEN + 1] = ""; -char Blynk_Token1 [MAX_BLYNK_TOKEN_LEN + 1] = ""; - -char Blynk_Server2 [MAX_BLYNK_SERVER_LEN + 1] = ""; -char Blynk_Token2 [MAX_BLYNK_TOKEN_LEN + 1] = ""; - -#define MAX_BLYNK_PORT_LEN 6 -char Blynk_Port [MAX_BLYNK_PORT_LEN + 1] = ""; - -#define MAX_MQTT_SERVER_LEN 34 -char MQTT_Server [MAX_MQTT_SERVER_LEN + 1] = ""; - -MenuItem myMenuItems [] = -{ - { "sv1", "Blynk Server1", Blynk_Server1, MAX_BLYNK_SERVER_LEN }, - { "tk1", "Token1", Blynk_Token1, MAX_BLYNK_TOKEN_LEN }, - { "sv2", "Blynk Server2", Blynk_Server2, MAX_BLYNK_SERVER_LEN }, - { "tk2", "Token2", Blynk_Token2, MAX_BLYNK_TOKEN_LEN }, - { "pt", "Port", Blynk_Port, MAX_BLYNK_PORT_LEN }, - { "mq", "MQTT Server", MQTT_Server, MAX_MQTT_SERVER_LEN }, -}; - -uint16_t NUM_MENU_ITEMS = sizeof(myMenuItems) / sizeof(MenuItem); //MenuItemSize; - -#else - -MenuItem myMenuItems [] = {}; - -uint16_t NUM_MENU_ITEMS = 0; - -#endif //USE_DYNAMIC_PARAMETERS +#include "defines.h" +#include "Credentials.h" +#include "dynamicParams.h" void heartBeatPrint(void) { @@ -201,7 +75,7 @@ void setup() //WiFiManager_NINA->setConfigPortalChannel(1); // Set customized DHCP HostName - WiFiManager_NINA->begin("SAMD-WiFiNINA-ABCDEF"); + WiFiManager_NINA->begin(HOST_NAME); //Or use default Hostname "SAMD-WiFiNINA-XXXXXX" //WiFiManager_NINA->begin(); diff --git a/examples/SAMD_WiFiNINA/defines.h b/examples/SAMD_WiFiNINA/defines.h new file mode 100644 index 00000000..236ba60e --- /dev/null +++ b/examples/SAMD_WiFiNINA/defines.h @@ -0,0 +1,106 @@ +/**************************************************************************************************************************** + defines.h for ESP32WM_Config.ino + For nRF52 boards using WiFiNINA modules/shields, using much less code to support boards with smaller memory + + WiFiManager_NINA_WM_Lite is a library for the Mega, Teensy, SAM DUE, SAMD and STM32 boards + (https://github.com/khoih-prog/WiFiManager_NINA_Lite) to enable store Credentials in EEPROM/LittleFS for easy + configuration/reconfiguration and autoconnect/autoreconnect of WiFi and other services without Hardcoding. + + Built by Khoi Hoang https://github.com/khoih-prog/WiFiManager_NINA_Lite + Licensed under MIT license + Version: 1.0.4 + + Version Modified By Date Comments + ------- ----------- ---------- ----------- + 1.0.0 K Hoang 26/03/2020 Initial coding + 1.0.1 K Hoang 27/03/2020 Fix SAMD soft-reset bug. Add support to remaining boards + 1.0.2 K Hoang 15/04/2020 Fix bug. Add SAMD51 support. + 1.0.3 K Hoang 24/04/2020 Fix bug. Add nRF5 (Adafruit, NINA_B302_ublox, etc.) support. Add MultiWiFi, HostName capability. + SSID password maxlen is 63 now. Permit special chars # and % in input data. + 1.0.4 K Hoang 04/05/2020 Add Configurable Config Portal Title, Default Config Data and DRD. Update examples. + *****************************************************************************************************************************/ + +#ifndef defines_h +#define defines_h + +/* Comment this out to disable prints and save space */ +#define DEBUG_WIFI_WEBSERVER_PORT Serial +#define WIFININA_DEBUG_OUTPUT Serial + +#define WIFININA_DEBUG false //true + +#define DRD_GENERIC_DEBUG false //true + +#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) || defined(__SAMD21E18A__) || defined(__SAMD51__) || defined(__SAMD51J20A__) || defined(__SAMD51J19A__) \ + || defined(__SAMD51G19A__) || defined(__SAMD21G18A__) ) +#if defined(WIFININA_USE_SAMD) +#undef WIFININA_USE_SAMD +#undef WIFI_USE_SAMD +#endif +#define WIFININA_USE_SAMD true +#define WIFI_USE_SAMD true +#else +#error This code is intended to run only on the SAMD boards ! Please check your Tools->Board setting. +#endif + +#if defined(WIFININA_USE_SAMD) + +#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(ADAFRUIT_ITSYBITSY_M4_EXPRESS) +#define BOARD_TYPE "SAMD ADAFRUIT_ITSYBITSY_M4_EXPRESS" +#elif defined(__SAMD21E18A__) +#define BOARD_TYPE "SAMD21E18A" +#elif defined(__SAMD21G18A__) +#define BOARD_TYPE "SAMD21G18A" +#elif defined(__SAMD51G19A__) +#define BOARD_TYPE "SAMD51G19A" +#elif defined(__SAMD51J19A__) +#define BOARD_TYPE "SAMD51J19A" +#elif defined(__SAMD51J20A__) +#define BOARD_TYPE "SAMD51J20A" +#elif defined(__SAM3X8E__) +#define BOARD_TYPE "SAM3X8E" +#elif defined(__CPU_ARC__) +#define BOARD_TYPE "CPU_ARC" +#elif defined(__SAMD51__) +#define BOARD_TYPE "SAMD51" +#else +#define BOARD_TYPE "SAMD Unknown" +#endif + +#endif + +// Start location in EEPROM to store config data. Default 0 +// Config data Size currently is 128 bytes) +#define EEPROM_START 0 +#define EEPROM_SIZE (2 * 1024) + +#include "WiFiNINA_Pinout_Generic.h" + +#include + +#define HOST_NAME "SAMD-Master-Controller" + +#endif //defines_h diff --git a/examples/SAMD_WiFiNINA/dynamicParams.h b/examples/SAMD_WiFiNINA/dynamicParams.h new file mode 100644 index 00000000..c4644cac --- /dev/null +++ b/examples/SAMD_WiFiNINA/dynamicParams.h @@ -0,0 +1,84 @@ +/**************************************************************************************************************************** + dynamicParams.h + For nRF52 boards using WiFiNINA modules/shields, using much less code to support boards with smaller memory + + WiFiManager_NINA_WM_Lite is a library for the Mega, Teensy, SAM DUE, SAMD and STM32 boards + (https://github.com/khoih-prog/WiFiManager_NINA_Lite) to enable store Credentials in EEPROM/LittleFS for easy + configuration/reconfiguration and autoconnect/autoreconnect of WiFi and other services without Hardcoding. + + Built by Khoi Hoang https://github.com/khoih-prog/WiFiManager_NINA_Lite + Licensed under MIT license + Version: 1.0.4 + + Version Modified By Date Comments + ------- ----------- ---------- ----------- + 1.0.0 K Hoang 26/03/2020 Initial coding + 1.0.1 K Hoang 27/03/2020 Fix SAMD soft-reset bug. Add support to remaining boards + 1.0.2 K Hoang 15/04/2020 Fix bug. Add SAMD51 support. + 1.0.3 K Hoang 24/04/2020 Fix bug. Add nRF5 (Adafruit, NINA_B302_ublox, etc.) support. Add MultiWiFi, HostName capability. + SSID password maxlen is 63 now. Permit special chars # and % in input data. + 1.0.4 K Hoang 04/05/2020 Add Configurable Config Portal Title, Default Config Data and DRD. Update examples. + *****************************************************************************************************************************/ + +#ifndef dynamicParams_h +#define dynamicParams_h + +#include "defines.h" + +#define USE_DYNAMIC_PARAMETERS true + +/////////////// Start dynamic Credentials /////////////// + +//Defined in +/************************************** + #define MAX_ID_LEN 5 + #define MAX_DISPLAY_NAME_LEN 16 + + typedef struct + { + char id [MAX_ID_LEN + 1]; + char displayName [MAX_DISPLAY_NAME_LEN + 1]; + char *pdata; + uint8_t maxlen; + } MenuItem; +**************************************/ + +#if USE_DYNAMIC_PARAMETERS + +#define MAX_BLYNK_SERVER_LEN 34 +#define MAX_BLYNK_TOKEN_LEN 34 + +char Blynk_Server1 [MAX_BLYNK_SERVER_LEN + 1] = "account.duckdns.org"; +char Blynk_Token1 [MAX_BLYNK_TOKEN_LEN + 1] = "token1"; + +char Blynk_Server2 [MAX_BLYNK_SERVER_LEN + 1] = "account.ddns.net"; +char Blynk_Token2 [MAX_BLYNK_TOKEN_LEN + 1] = "token2"; + +#define MAX_BLYNK_PORT_LEN 6 +char Blynk_Port [MAX_BLYNK_PORT_LEN + 1] = "8080"; + +#define MAX_MQTT_SERVER_LEN 34 +char MQTT_Server [MAX_MQTT_SERVER_LEN + 1] = "mqtt.duckdns.org"; + +MenuItem myMenuItems [] = +{ + { "sv1", "Blynk Server1", Blynk_Server1, MAX_BLYNK_SERVER_LEN }, + { "tk1", "Token1", Blynk_Token1, MAX_BLYNK_TOKEN_LEN }, + { "sv2", "Blynk Server2", Blynk_Server2, MAX_BLYNK_SERVER_LEN }, + { "tk2", "Token2", Blynk_Token2, MAX_BLYNK_TOKEN_LEN }, + { "pt", "Port", Blynk_Port, MAX_BLYNK_PORT_LEN }, + { "mq", "MQTT Server", MQTT_Server, MAX_MQTT_SERVER_LEN }, +}; + +uint16_t NUM_MENU_ITEMS = sizeof(myMenuItems) / sizeof(MenuItem); //MenuItemSize; + +#else + +MenuItem myMenuItems [] = {}; + +uint16_t NUM_MENU_ITEMS = 0; + +#endif //USE_DYNAMIC_PARAMETERS + + +#endif //dynamicParams_h diff --git a/examples/SAM_DUE_WiFiNINA/Credentials.h b/examples/SAM_DUE_WiFiNINA/Credentials.h new file mode 100644 index 00000000..fc9a519a --- /dev/null +++ b/examples/SAM_DUE_WiFiNINA/Credentials.h @@ -0,0 +1,89 @@ +/**************************************************************************************************************************** + Credentials.h + For nRF52 boards using WiFiNINA modules/shields, using much less code to support boards with smaller memory + + WiFiManager_NINA_WM_Lite is a library for the Mega, Teensy, SAM DUE, SAMD and STM32 boards + (https://github.com/khoih-prog/WiFiManager_NINA_Lite) to enable store Credentials in EEPROM/LittleFS for easy + configuration/reconfiguration and autoconnect/autoreconnect of WiFi and other services without Hardcoding. + + Built by Khoi Hoang https://github.com/khoih-prog/WiFiManager_NINA_Lite + Licensed under MIT license + Version: 1.0.4 + + Version Modified By Date Comments + ------- ----------- ---------- ----------- + 1.0.0 K Hoang 26/03/2020 Initial coding + 1.0.1 K Hoang 27/03/2020 Fix SAMD soft-reset bug. Add support to remaining boards + 1.0.2 K Hoang 15/04/2020 Fix bug. Add SAMD51 support. + 1.0.3 K Hoang 24/04/2020 Fix bug. Add nRF5 (Adafruit, NINA_B302_ublox, etc.) support. Add MultiWiFi, HostName capability. + SSID password maxlen is 63 now. Permit special chars # and % in input data. + 1.0.4 K Hoang 04/05/2020 Add Configurable Config Portal Title, Default Config Data and DRD. Update examples. + *****************************************************************************************************************************/ + +#ifndef Credentials_h +#define Credentials_h + +#include "defines.h" + +/// Start Default Config Data ////////////////// + +/* +#define SSID_MAX_LEN 32 +//From v1.0.3, 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; + +#define NUM_WIFI_CREDENTIALS 2 + +// Configurable items besides fixed Header, just add board_name +#define NUM_CONFIGURABLE_ITEMS ( ( 2 * NUM_WIFI_CREDENTIALS ) + 1 ) +//////////////// + +typedef struct Configuration +{ + char header [16]; + WiFi_Credentials WiFi_Creds [NUM_WIFI_CREDENTIALS]; + char board_name [24]; + int checkSum; +} WiFiNINA_Configuration; +*/ + +#define TO_LOAD_DEFAULT_CONFIG_DATA true + +#if TO_LOAD_DEFAULT_CONFIG_DATA + +bool LOAD_DEFAULT_CONFIG_DATA = true; + +WiFiNINA_Configuration defaultConfig = +{ + //char header[16], dummy, not used + "WIFININA", + // WiFi_Credentials WiFi_Creds [NUM_WIFI_CREDENTIALS]; + // WiFi_Credentials.wifi_ssid and WiFi_Credentials.wifi_pw + "SSID1", "password1", + "SSID2", "password2", + //char board_name [24]; + "Air-Control", + // terminate the list + //int checkSum, dummy, not used + 0 + /////////// End Default Config Data ///////////// +}; + +#else + +bool LOAD_DEFAULT_CONFIG_DATA = false; + +WiFiNINA_Configuration defaultConfig; + +#endif // TO_LOAD_DEFAULT_CONFIG_DATA + +/////////// End Default Config Data ///////////// + + +#endif //Credentials_h diff --git a/examples/SAM_DUE_WiFiNINA/SAM_DUE_WiFiNINA.ino b/examples/SAM_DUE_WiFiNINA/SAM_DUE_WiFiNINA.ino index 34b343e5..c190eb0e 100644 --- a/examples/SAM_DUE_WiFiNINA/SAM_DUE_WiFiNINA.ino +++ b/examples/SAM_DUE_WiFiNINA/SAM_DUE_WiFiNINA.ino @@ -2,13 +2,13 @@ SAM_DUE_WiFiNINA.ino For SAM DUE boards using WiFiNINA Modules/Shields, using much less code to support boards with smaller memory - WiFiManager_NINA_WM_Lite is a library for the Mega, Teensy, SAM DUE, SAMD and STM32 boards (https://github.com/khoih-prog/WiFiManager_NINA_Lite) - to enable store Credentials in EEPROM to easy configuration/reconfiguration and autoconnect/autoreconnect of WiFi and other services - without Hardcoding. + WiFiManager_NINA_WM_Lite is a library for the Mega, Teensy, SAM DUE, SAMD and STM32 boards + (https://github.com/khoih-prog/WiFiManager_NINA_Lite) to enable store Credentials in EEPROM/LittleFS for easy + configuration/reconfiguration and autoconnect/autoreconnect of WiFi and other services without Hardcoding. Built by Khoi Hoang https://github.com/khoih-prog/WiFiManager_NINA_Lite Licensed under MIT license - Version: 1.0.3 + Version: 1.0.4 Version Modified By Date Comments ------- ----------- ---------- ----------- @@ -17,96 +17,11 @@ 1.0.2 K Hoang 15/04/2020 Fix bug. Add SAMD51 support. 1.0.3 K Hoang 24/04/2020 Fix bug. Add nRF5 (Adafruit, NINA_B302_ublox, etc.) support. Add MultiWiFi, HostName capability. SSID password maxlen is 63 now. Permit special chars # and % in input data. - *****************************************************************************************************************************/ - -/* Comment this out to disable prints and save space */ -#define DEBUG_WIFI_WEBSERVER_PORT Serial -#define WIFININA_DEBUG_OUTPUT Serial - -#if ( defined(ARDUINO_SAM_DUE) || defined(__SAM3X8E__) ) -#if defined(WIFININA_USE_SAM_DUE) -#undef WIFININA_USE_SAM_DUE -#endif -#define WIFININA_USE_SAM_DUE true -#warning Use SAM_DUE architecture -#endif - -#if ( defined(ESP8266) || defined(ESP32) || defined(ARDUINO_AVR_MEGA2560) || defined(ARDUINO_AVR_MEGA) || \ - defined(CORE_TEENSY) || defined(CORE_TEENSY) || !(WIFININA_USE_SAM_DUE) ) -#error This code is intended to run on the SAM DUE platform! Please check your Tools->Board setting. -#endif - -#if defined(WIFININA_USE_SAM_DUE) -// For SAM DUE -#if defined(ARDUINO_SAM_DUE) -#define BOARD_TYPE "SAM DUE" -#elif defined(__SAM3X8E__) -#define BOARD_TYPE "SAM SAM3X8E" -#else -#define BOARD_TYPE "SAM Unknown" -#endif -#endif - -// Start location in EEPROM to store config data. Default 0 -// Config data Size currently is 128 bytes) -#define EEPROM_START 0 - -#include - -#define USE_DYNAMIC_PARAMETERS true - -/////////////// Start dynamic Credentials /////////////// - -//Defined in -/************************************** - #define MAX_ID_LEN 5 - #define MAX_DISPLAY_NAME_LEN 16 - - typedef struct - { - char id [MAX_ID_LEN + 1]; - char displayName [MAX_DISPLAY_NAME_LEN + 1]; - char *pdata; - uint8_t maxlen; - } MenuItem; -**************************************/ - -#if USE_DYNAMIC_PARAMETERS - -#define MAX_BLYNK_SERVER_LEN 34 -#define MAX_BLYNK_TOKEN_LEN 34 - -char Blynk_Server1 [MAX_BLYNK_SERVER_LEN + 1] = ""; -char Blynk_Token1 [MAX_BLYNK_TOKEN_LEN + 1] = ""; - -char Blynk_Server2 [MAX_BLYNK_SERVER_LEN + 1] = ""; -char Blynk_Token2 [MAX_BLYNK_TOKEN_LEN + 1] = ""; - -#define MAX_BLYNK_PORT_LEN 6 -char Blynk_Port [MAX_BLYNK_PORT_LEN + 1] = ""; - -#define MAX_MQTT_SERVER_LEN 34 -char MQTT_Server [MAX_MQTT_SERVER_LEN + 1] = ""; - -MenuItem myMenuItems [] = -{ - { "sv1", "Blynk Server1", Blynk_Server1, MAX_BLYNK_SERVER_LEN }, - { "tk1", "Token1", Blynk_Token1, MAX_BLYNK_TOKEN_LEN }, - { "sv2", "Blynk Server2", Blynk_Server2, MAX_BLYNK_SERVER_LEN }, - { "tk2", "Token2", Blynk_Token2, MAX_BLYNK_TOKEN_LEN }, - { "pt", "Port", Blynk_Port, MAX_BLYNK_PORT_LEN }, - { "mq", "MQTT Server", MQTT_Server, MAX_MQTT_SERVER_LEN }, -}; - -uint16_t NUM_MENU_ITEMS = sizeof(myMenuItems) / sizeof(MenuItem); //MenuItemSize; - -#else - -MenuItem myMenuItems [] = {}; - -uint16_t NUM_MENU_ITEMS = 0; - -#endif //USE_DYNAMIC_PARAMETERS + 1.0.4 K Hoang 04/05/2020 Add Configurable Config Portal Title, Default Config Data and DRD. Update examples. + *****************************************************************************************************************************/ +#include "defines.h" +#include "Credentials.h" +#include "dynamicParams.h" void heartBeatPrint(void) { diff --git a/examples/SAM_DUE_WiFiNINA/defines.h b/examples/SAM_DUE_WiFiNINA/defines.h new file mode 100644 index 00000000..aae2bcaf --- /dev/null +++ b/examples/SAM_DUE_WiFiNINA/defines.h @@ -0,0 +1,70 @@ +/**************************************************************************************************************************** + defines.h for ESP32WM_Config.ino + For nRF52 boards using WiFiNINA modules/shields, using much less code to support boards with smaller memory + + WiFiManager_NINA_WM_Lite is a library for the Mega, Teensy, SAM DUE, SAMD and STM32 boards + (https://github.com/khoih-prog/WiFiManager_NINA_Lite) to enable store Credentials in EEPROM/LittleFS for easy + configuration/reconfiguration and autoconnect/autoreconnect of WiFi and other services without Hardcoding. + + Built by Khoi Hoang https://github.com/khoih-prog/WiFiManager_NINA_Lite + Licensed under MIT license + Version: 1.0.4 + + Version Modified By Date Comments + ------- ----------- ---------- ----------- + 1.0.0 K Hoang 26/03/2020 Initial coding + 1.0.1 K Hoang 27/03/2020 Fix SAMD soft-reset bug. Add support to remaining boards + 1.0.2 K Hoang 15/04/2020 Fix bug. Add SAMD51 support. + 1.0.3 K Hoang 24/04/2020 Fix bug. Add nRF5 (Adafruit, NINA_B302_ublox, etc.) support. Add MultiWiFi, HostName capability. + SSID password maxlen is 63 now. Permit special chars # and % in input data. + 1.0.4 K Hoang 04/05/2020 Add Configurable Config Portal Title, Default Config Data and DRD. Update examples. + *****************************************************************************************************************************/ + +#ifndef defines_h +#define defines_h + +/* Comment this out to disable prints and save space */ +#define DEBUG_WIFI_WEBSERVER_PORT Serial +#define WIFININA_DEBUG_OUTPUT Serial + +#define WIFININA_DEBUG false //true + +#define DRD_GENERIC_DEBUG false //true + +/* Comment this out to disable prints and save space */ +#define DEBUG_WIFI_WEBSERVER_PORT Serial +#define WIFININA_DEBUG_OUTPUT Serial + +#if ( defined(ARDUINO_SAM_DUE) || defined(__SAM3X8E__) ) +#if defined(WIFININA_USE_SAM_DUE) +#undef WIFININA_USE_SAM_DUE +#endif +#define WIFININA_USE_SAM_DUE true +#warning Use SAM_DUE architecture +#endif + +#if ( defined(ESP8266) || defined(ESP32) || defined(ARDUINO_AVR_MEGA2560) || defined(ARDUINO_AVR_MEGA) || \ + defined(CORE_TEENSY) || defined(CORE_TEENSY) || !(WIFININA_USE_SAM_DUE) ) +#error This code is intended to run on the SAM DUE platform! Please check your Tools->Board setting. +#endif + +#if defined(WIFININA_USE_SAM_DUE) +// For SAM DUE +#if defined(ARDUINO_SAM_DUE) +#define BOARD_TYPE "SAM DUE" +#elif defined(__SAM3X8E__) +#define BOARD_TYPE "SAM SAM3X8E" +#else +#define BOARD_TYPE "SAM Unknown" +#endif +#endif + +// Start location in EEPROM to store config data. Default 0 +// Config data Size currently is 128 bytes) +#define EEPROM_START 0 + +#include + +#define HOST_NAME "DUE-Master-Controller" + +#endif //defines_h diff --git a/examples/SAM_DUE_WiFiNINA/dynamicParams.h b/examples/SAM_DUE_WiFiNINA/dynamicParams.h new file mode 100644 index 00000000..c4644cac --- /dev/null +++ b/examples/SAM_DUE_WiFiNINA/dynamicParams.h @@ -0,0 +1,84 @@ +/**************************************************************************************************************************** + dynamicParams.h + For nRF52 boards using WiFiNINA modules/shields, using much less code to support boards with smaller memory + + WiFiManager_NINA_WM_Lite is a library for the Mega, Teensy, SAM DUE, SAMD and STM32 boards + (https://github.com/khoih-prog/WiFiManager_NINA_Lite) to enable store Credentials in EEPROM/LittleFS for easy + configuration/reconfiguration and autoconnect/autoreconnect of WiFi and other services without Hardcoding. + + Built by Khoi Hoang https://github.com/khoih-prog/WiFiManager_NINA_Lite + Licensed under MIT license + Version: 1.0.4 + + Version Modified By Date Comments + ------- ----------- ---------- ----------- + 1.0.0 K Hoang 26/03/2020 Initial coding + 1.0.1 K Hoang 27/03/2020 Fix SAMD soft-reset bug. Add support to remaining boards + 1.0.2 K Hoang 15/04/2020 Fix bug. Add SAMD51 support. + 1.0.3 K Hoang 24/04/2020 Fix bug. Add nRF5 (Adafruit, NINA_B302_ublox, etc.) support. Add MultiWiFi, HostName capability. + SSID password maxlen is 63 now. Permit special chars # and % in input data. + 1.0.4 K Hoang 04/05/2020 Add Configurable Config Portal Title, Default Config Data and DRD. Update examples. + *****************************************************************************************************************************/ + +#ifndef dynamicParams_h +#define dynamicParams_h + +#include "defines.h" + +#define USE_DYNAMIC_PARAMETERS true + +/////////////// Start dynamic Credentials /////////////// + +//Defined in +/************************************** + #define MAX_ID_LEN 5 + #define MAX_DISPLAY_NAME_LEN 16 + + typedef struct + { + char id [MAX_ID_LEN + 1]; + char displayName [MAX_DISPLAY_NAME_LEN + 1]; + char *pdata; + uint8_t maxlen; + } MenuItem; +**************************************/ + +#if USE_DYNAMIC_PARAMETERS + +#define MAX_BLYNK_SERVER_LEN 34 +#define MAX_BLYNK_TOKEN_LEN 34 + +char Blynk_Server1 [MAX_BLYNK_SERVER_LEN + 1] = "account.duckdns.org"; +char Blynk_Token1 [MAX_BLYNK_TOKEN_LEN + 1] = "token1"; + +char Blynk_Server2 [MAX_BLYNK_SERVER_LEN + 1] = "account.ddns.net"; +char Blynk_Token2 [MAX_BLYNK_TOKEN_LEN + 1] = "token2"; + +#define MAX_BLYNK_PORT_LEN 6 +char Blynk_Port [MAX_BLYNK_PORT_LEN + 1] = "8080"; + +#define MAX_MQTT_SERVER_LEN 34 +char MQTT_Server [MAX_MQTT_SERVER_LEN + 1] = "mqtt.duckdns.org"; + +MenuItem myMenuItems [] = +{ + { "sv1", "Blynk Server1", Blynk_Server1, MAX_BLYNK_SERVER_LEN }, + { "tk1", "Token1", Blynk_Token1, MAX_BLYNK_TOKEN_LEN }, + { "sv2", "Blynk Server2", Blynk_Server2, MAX_BLYNK_SERVER_LEN }, + { "tk2", "Token2", Blynk_Token2, MAX_BLYNK_TOKEN_LEN }, + { "pt", "Port", Blynk_Port, MAX_BLYNK_PORT_LEN }, + { "mq", "MQTT Server", MQTT_Server, MAX_MQTT_SERVER_LEN }, +}; + +uint16_t NUM_MENU_ITEMS = sizeof(myMenuItems) / sizeof(MenuItem); //MenuItemSize; + +#else + +MenuItem myMenuItems [] = {}; + +uint16_t NUM_MENU_ITEMS = 0; + +#endif //USE_DYNAMIC_PARAMETERS + + +#endif //dynamicParams_h diff --git a/examples/STM32_WiFiNINA/Credentials.h b/examples/STM32_WiFiNINA/Credentials.h new file mode 100644 index 00000000..fc9a519a --- /dev/null +++ b/examples/STM32_WiFiNINA/Credentials.h @@ -0,0 +1,89 @@ +/**************************************************************************************************************************** + Credentials.h + For nRF52 boards using WiFiNINA modules/shields, using much less code to support boards with smaller memory + + WiFiManager_NINA_WM_Lite is a library for the Mega, Teensy, SAM DUE, SAMD and STM32 boards + (https://github.com/khoih-prog/WiFiManager_NINA_Lite) to enable store Credentials in EEPROM/LittleFS for easy + configuration/reconfiguration and autoconnect/autoreconnect of WiFi and other services without Hardcoding. + + Built by Khoi Hoang https://github.com/khoih-prog/WiFiManager_NINA_Lite + Licensed under MIT license + Version: 1.0.4 + + Version Modified By Date Comments + ------- ----------- ---------- ----------- + 1.0.0 K Hoang 26/03/2020 Initial coding + 1.0.1 K Hoang 27/03/2020 Fix SAMD soft-reset bug. Add support to remaining boards + 1.0.2 K Hoang 15/04/2020 Fix bug. Add SAMD51 support. + 1.0.3 K Hoang 24/04/2020 Fix bug. Add nRF5 (Adafruit, NINA_B302_ublox, etc.) support. Add MultiWiFi, HostName capability. + SSID password maxlen is 63 now. Permit special chars # and % in input data. + 1.0.4 K Hoang 04/05/2020 Add Configurable Config Portal Title, Default Config Data and DRD. Update examples. + *****************************************************************************************************************************/ + +#ifndef Credentials_h +#define Credentials_h + +#include "defines.h" + +/// Start Default Config Data ////////////////// + +/* +#define SSID_MAX_LEN 32 +//From v1.0.3, 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; + +#define NUM_WIFI_CREDENTIALS 2 + +// Configurable items besides fixed Header, just add board_name +#define NUM_CONFIGURABLE_ITEMS ( ( 2 * NUM_WIFI_CREDENTIALS ) + 1 ) +//////////////// + +typedef struct Configuration +{ + char header [16]; + WiFi_Credentials WiFi_Creds [NUM_WIFI_CREDENTIALS]; + char board_name [24]; + int checkSum; +} WiFiNINA_Configuration; +*/ + +#define TO_LOAD_DEFAULT_CONFIG_DATA true + +#if TO_LOAD_DEFAULT_CONFIG_DATA + +bool LOAD_DEFAULT_CONFIG_DATA = true; + +WiFiNINA_Configuration defaultConfig = +{ + //char header[16], dummy, not used + "WIFININA", + // WiFi_Credentials WiFi_Creds [NUM_WIFI_CREDENTIALS]; + // WiFi_Credentials.wifi_ssid and WiFi_Credentials.wifi_pw + "SSID1", "password1", + "SSID2", "password2", + //char board_name [24]; + "Air-Control", + // terminate the list + //int checkSum, dummy, not used + 0 + /////////// End Default Config Data ///////////// +}; + +#else + +bool LOAD_DEFAULT_CONFIG_DATA = false; + +WiFiNINA_Configuration defaultConfig; + +#endif // TO_LOAD_DEFAULT_CONFIG_DATA + +/////////// End Default Config Data ///////////// + + +#endif //Credentials_h diff --git a/examples/STM32_WiFiNINA/STM32_WiFiNINA.ino b/examples/STM32_WiFiNINA/STM32_WiFiNINA.ino index 4968d503..2c82b476 100644 --- a/examples/STM32_WiFiNINA/STM32_WiFiNINA.ino +++ b/examples/STM32_WiFiNINA/STM32_WiFiNINA.ino @@ -2,13 +2,13 @@ STM32_WiFiNINA.ino For STM32 boards using WiFiNINA Modules/Shields, using much less code to support boards with smaller memory - WiFiManager_NINA_WM_Lite is a library for the Mega, Teensy, SAM DUE, SAMD and STM32 boards (https://github.com/khoih-prog/WiFiManager_NINA_Lite) - to enable store Credentials in EEPROM to easy configuration/reconfiguration and autoconnect/autoreconnect of WiFi and other services - without Hardcoding. + WiFiManager_NINA_WM_Lite is a library for the Mega, Teensy, SAM DUE, SAMD and STM32 boards + (https://github.com/khoih-prog/WiFiManager_NINA_Lite) to enable store Credentials in EEPROM/LittleFS for easy + configuration/reconfiguration and autoconnect/autoreconnect of WiFi and other services without Hardcoding. Built by Khoi Hoang https://github.com/khoih-prog/WiFiManager_NINA_Lite Licensed under MIT license - Version: 1.0.3 + Version: 1.0.4 Version Modified By Date Comments ------- ----------- ---------- ----------- @@ -17,110 +17,12 @@ 1.0.2 K Hoang 15/04/2020 Fix bug. Add SAMD51 support. 1.0.3 K Hoang 24/04/2020 Fix bug. Add nRF5 (Adafruit, NINA_B302_ublox, etc.) support. Add MultiWiFi, HostName capability. SSID password maxlen is 63 now. Permit special chars # and % in input data. + 1.0.4 K Hoang 04/05/2020 Add Configurable Config Portal Title, Default Config Data and DRD. Update examples. *****************************************************************************************************************************/ +#include "defines.h" +#include "Credentials.h" +#include "dynamicParams.h" -/* Comment this out to disable prints and save space */ -#define DEBUG_WIFI_WEBSERVER_PORT Serial -#define WIFININA_DEBUG_OUTPUT Serial - -#if ( defined(STM32F0) || defined(STM32F1) || defined(STM32F2) || defined(STM32F3) ||defined(STM32F4) || defined(STM32F7) ) -#if defined(WIFININA_USE_STM32) -#undef WIFININA_USE_STM32 -#undef WIFI_USE_STM32 -#endif -#define WIFININA_USE_STM32 true -#define WIFI_USE_STM32 true -#endif - -#if ( defined(ESP8266) || defined(ESP32) || defined(ARDUINO_AVR_MEGA) || defined(ARDUINO_AVR_MEGA2560) || defined(CORE_TEENSY) || !(WIFININA_USE_STM32) ) -//#error This code is intended to run on STM32 platform! Please check your Tools->Board setting. -#endif - -#if WIFININA_USE_STM32 -#if defined(STM32F0) -#define BOARD_TYPE "STM32F0" -#error Board STM32F0 not supported -#elif defined(STM32F1) -#define BOARD_TYPE "STM32F1" -#elif defined(STM32F2) -#define BOARD_TYPE "STM32F2" -#elif defined(STM32F3) -#define BOARD_TYPE "STM32F3" -#elif defined(STM32F4) -#define BOARD_TYPE "STM32F4" -#elif defined(STM32F7) -#define BOARD_TYPE "STM32F7" -#else -#warning STM32 unknown board selected -#define BOARD_TYPE "STM32 Unknown" -#endif -#else -// For Mega -#define BOARD_TYPE "AVR Mega" -#endif - -// Start location in EEPROM to store config data. Default 0 -// Config data Size currently is 128 bytes) -#define EEPROM_START 0 - -#include "WiFiNINA_Pinout_Generic.h" - -#include - -#define USE_DYNAMIC_PARAMETERS true - -/////////////// Start dynamic Credentials /////////////// - -//Defined in -/************************************** - #define MAX_ID_LEN 5 - #define MAX_DISPLAY_NAME_LEN 16 - - typedef struct - { - char id [MAX_ID_LEN + 1]; - char displayName [MAX_DISPLAY_NAME_LEN + 1]; - char *pdata; - uint8_t maxlen; - } MenuItem; -**************************************/ - -#if USE_DYNAMIC_PARAMETERS - -#define MAX_BLYNK_SERVER_LEN 34 -#define MAX_BLYNK_TOKEN_LEN 34 - -char Blynk_Server1 [MAX_BLYNK_SERVER_LEN + 1] = ""; -char Blynk_Token1 [MAX_BLYNK_TOKEN_LEN + 1] = ""; - -char Blynk_Server2 [MAX_BLYNK_SERVER_LEN + 1] = ""; -char Blynk_Token2 [MAX_BLYNK_TOKEN_LEN + 1] = ""; - -#define MAX_BLYNK_PORT_LEN 6 -char Blynk_Port [MAX_BLYNK_PORT_LEN + 1] = ""; - -#define MAX_MQTT_SERVER_LEN 34 -char MQTT_Server [MAX_MQTT_SERVER_LEN + 1] = ""; - -MenuItem myMenuItems [] = -{ - { "sv1", "Blynk Server1", Blynk_Server1, MAX_BLYNK_SERVER_LEN }, - { "tk1", "Token1", Blynk_Token1, MAX_BLYNK_TOKEN_LEN }, - { "sv2", "Blynk Server2", Blynk_Server2, MAX_BLYNK_SERVER_LEN }, - { "tk2", "Token2", Blynk_Token2, MAX_BLYNK_TOKEN_LEN }, - { "pt", "Port", Blynk_Port, MAX_BLYNK_PORT_LEN }, - { "mq", "MQTT Server", MQTT_Server, MAX_MQTT_SERVER_LEN }, -}; - -uint16_t NUM_MENU_ITEMS = sizeof(myMenuItems) / sizeof(MenuItem); //MenuItemSize; - -#else - -MenuItem myMenuItems [] = {}; - -uint16_t NUM_MENU_ITEMS = 0; - -#endif //USE_DYNAMIC_PARAMETERS void heartBeatPrint(void) { static int num = 1; diff --git a/examples/STM32_WiFiNINA/defines.h b/examples/STM32_WiFiNINA/defines.h new file mode 100644 index 00000000..09da5d89 --- /dev/null +++ b/examples/STM32_WiFiNINA/defines.h @@ -0,0 +1,80 @@ +/**************************************************************************************************************************** + defines.h for ESP32WM_Config.ino + For nRF52 boards using WiFiNINA modules/shields, using much less code to support boards with smaller memory + + WiFiManager_NINA_WM_Lite is a library for the Mega, Teensy, SAM DUE, SAMD and STM32 boards + (https://github.com/khoih-prog/WiFiManager_NINA_Lite) to enable store Credentials in EEPROM/LittleFS for easy + configuration/reconfiguration and autoconnect/autoreconnect of WiFi and other services without Hardcoding. + + Built by Khoi Hoang https://github.com/khoih-prog/WiFiManager_NINA_Lite + Licensed under MIT license + Version: 1.0.4 + + Version Modified By Date Comments + ------- ----------- ---------- ----------- + 1.0.0 K Hoang 26/03/2020 Initial coding + 1.0.1 K Hoang 27/03/2020 Fix SAMD soft-reset bug. Add support to remaining boards + 1.0.2 K Hoang 15/04/2020 Fix bug. Add SAMD51 support. + 1.0.3 K Hoang 24/04/2020 Fix bug. Add nRF5 (Adafruit, NINA_B302_ublox, etc.) support. Add MultiWiFi, HostName capability. + SSID password maxlen is 63 now. Permit special chars # and % in input data. + 1.0.4 K Hoang 04/05/2020 Add Configurable Config Portal Title, Default Config Data and DRD. Update examples. + *****************************************************************************************************************************/ + +#ifndef defines_h +#define defines_h + +/* Comment this out to disable prints and save space */ +#define DEBUG_WIFI_WEBSERVER_PORT Serial +#define WIFININA_DEBUG_OUTPUT Serial + +#define WIFININA_DEBUG false //true + +#define DRD_GENERIC_DEBUG false //true + +#if ( defined(STM32F0) || defined(STM32F1) || defined(STM32F2) || defined(STM32F3) ||defined(STM32F4) || defined(STM32F7) ) +#if defined(WIFININA_USE_STM32) +#undef WIFININA_USE_STM32 +#undef WIFI_USE_STM32 +#endif +#define WIFININA_USE_STM32 true +#define WIFI_USE_STM32 true +#endif + +#if ( defined(ESP8266) || defined(ESP32) || defined(ARDUINO_AVR_MEGA) || defined(ARDUINO_AVR_MEGA2560) || defined(CORE_TEENSY) || !(WIFININA_USE_STM32) ) +//#error This code is intended to run on STM32 platform! Please check your Tools->Board setting. +#endif + +#if WIFININA_USE_STM32 +#if defined(STM32F0) +#define BOARD_TYPE "STM32F0" +#error Board STM32F0 not supported +#elif defined(STM32F1) +#define BOARD_TYPE "STM32F1" +#elif defined(STM32F2) +#define BOARD_TYPE "STM32F2" +#elif defined(STM32F3) +#define BOARD_TYPE "STM32F3" +#elif defined(STM32F4) +#define BOARD_TYPE "STM32F4" +#elif defined(STM32F7) +#define BOARD_TYPE "STM32F7" +#else +#warning STM32 unknown board selected +#define BOARD_TYPE "STM32 Unknown" +#endif +#else +// For Mega +#define BOARD_TYPE "AVR Mega" +#endif + +// Start location in EEPROM to store config data. Default 0 +// Config data Size currently is 128 bytes) +#define EEPROM_START 0 + +#include "WiFiNINA_Pinout_Generic.h" + +#include + +#define HOST_NAME "STM32-Master-Controller" + +#endif //defines_h diff --git a/examples/STM32_WiFiNINA/dynamicParams.h b/examples/STM32_WiFiNINA/dynamicParams.h new file mode 100644 index 00000000..c4644cac --- /dev/null +++ b/examples/STM32_WiFiNINA/dynamicParams.h @@ -0,0 +1,84 @@ +/**************************************************************************************************************************** + dynamicParams.h + For nRF52 boards using WiFiNINA modules/shields, using much less code to support boards with smaller memory + + WiFiManager_NINA_WM_Lite is a library for the Mega, Teensy, SAM DUE, SAMD and STM32 boards + (https://github.com/khoih-prog/WiFiManager_NINA_Lite) to enable store Credentials in EEPROM/LittleFS for easy + configuration/reconfiguration and autoconnect/autoreconnect of WiFi and other services without Hardcoding. + + Built by Khoi Hoang https://github.com/khoih-prog/WiFiManager_NINA_Lite + Licensed under MIT license + Version: 1.0.4 + + Version Modified By Date Comments + ------- ----------- ---------- ----------- + 1.0.0 K Hoang 26/03/2020 Initial coding + 1.0.1 K Hoang 27/03/2020 Fix SAMD soft-reset bug. Add support to remaining boards + 1.0.2 K Hoang 15/04/2020 Fix bug. Add SAMD51 support. + 1.0.3 K Hoang 24/04/2020 Fix bug. Add nRF5 (Adafruit, NINA_B302_ublox, etc.) support. Add MultiWiFi, HostName capability. + SSID password maxlen is 63 now. Permit special chars # and % in input data. + 1.0.4 K Hoang 04/05/2020 Add Configurable Config Portal Title, Default Config Data and DRD. Update examples. + *****************************************************************************************************************************/ + +#ifndef dynamicParams_h +#define dynamicParams_h + +#include "defines.h" + +#define USE_DYNAMIC_PARAMETERS true + +/////////////// Start dynamic Credentials /////////////// + +//Defined in +/************************************** + #define MAX_ID_LEN 5 + #define MAX_DISPLAY_NAME_LEN 16 + + typedef struct + { + char id [MAX_ID_LEN + 1]; + char displayName [MAX_DISPLAY_NAME_LEN + 1]; + char *pdata; + uint8_t maxlen; + } MenuItem; +**************************************/ + +#if USE_DYNAMIC_PARAMETERS + +#define MAX_BLYNK_SERVER_LEN 34 +#define MAX_BLYNK_TOKEN_LEN 34 + +char Blynk_Server1 [MAX_BLYNK_SERVER_LEN + 1] = "account.duckdns.org"; +char Blynk_Token1 [MAX_BLYNK_TOKEN_LEN + 1] = "token1"; + +char Blynk_Server2 [MAX_BLYNK_SERVER_LEN + 1] = "account.ddns.net"; +char Blynk_Token2 [MAX_BLYNK_TOKEN_LEN + 1] = "token2"; + +#define MAX_BLYNK_PORT_LEN 6 +char Blynk_Port [MAX_BLYNK_PORT_LEN + 1] = "8080"; + +#define MAX_MQTT_SERVER_LEN 34 +char MQTT_Server [MAX_MQTT_SERVER_LEN + 1] = "mqtt.duckdns.org"; + +MenuItem myMenuItems [] = +{ + { "sv1", "Blynk Server1", Blynk_Server1, MAX_BLYNK_SERVER_LEN }, + { "tk1", "Token1", Blynk_Token1, MAX_BLYNK_TOKEN_LEN }, + { "sv2", "Blynk Server2", Blynk_Server2, MAX_BLYNK_SERVER_LEN }, + { "tk2", "Token2", Blynk_Token2, MAX_BLYNK_TOKEN_LEN }, + { "pt", "Port", Blynk_Port, MAX_BLYNK_PORT_LEN }, + { "mq", "MQTT Server", MQTT_Server, MAX_MQTT_SERVER_LEN }, +}; + +uint16_t NUM_MENU_ITEMS = sizeof(myMenuItems) / sizeof(MenuItem); //MenuItemSize; + +#else + +MenuItem myMenuItems [] = {}; + +uint16_t NUM_MENU_ITEMS = 0; + +#endif //USE_DYNAMIC_PARAMETERS + + +#endif //dynamicParams_h diff --git a/examples/Teensy40_WiFiNINA/Credentials.h b/examples/Teensy40_WiFiNINA/Credentials.h new file mode 100644 index 00000000..fc9a519a --- /dev/null +++ b/examples/Teensy40_WiFiNINA/Credentials.h @@ -0,0 +1,89 @@ +/**************************************************************************************************************************** + Credentials.h + For nRF52 boards using WiFiNINA modules/shields, using much less code to support boards with smaller memory + + WiFiManager_NINA_WM_Lite is a library for the Mega, Teensy, SAM DUE, SAMD and STM32 boards + (https://github.com/khoih-prog/WiFiManager_NINA_Lite) to enable store Credentials in EEPROM/LittleFS for easy + configuration/reconfiguration and autoconnect/autoreconnect of WiFi and other services without Hardcoding. + + Built by Khoi Hoang https://github.com/khoih-prog/WiFiManager_NINA_Lite + Licensed under MIT license + Version: 1.0.4 + + Version Modified By Date Comments + ------- ----------- ---------- ----------- + 1.0.0 K Hoang 26/03/2020 Initial coding + 1.0.1 K Hoang 27/03/2020 Fix SAMD soft-reset bug. Add support to remaining boards + 1.0.2 K Hoang 15/04/2020 Fix bug. Add SAMD51 support. + 1.0.3 K Hoang 24/04/2020 Fix bug. Add nRF5 (Adafruit, NINA_B302_ublox, etc.) support. Add MultiWiFi, HostName capability. + SSID password maxlen is 63 now. Permit special chars # and % in input data. + 1.0.4 K Hoang 04/05/2020 Add Configurable Config Portal Title, Default Config Data and DRD. Update examples. + *****************************************************************************************************************************/ + +#ifndef Credentials_h +#define Credentials_h + +#include "defines.h" + +/// Start Default Config Data ////////////////// + +/* +#define SSID_MAX_LEN 32 +//From v1.0.3, 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; + +#define NUM_WIFI_CREDENTIALS 2 + +// Configurable items besides fixed Header, just add board_name +#define NUM_CONFIGURABLE_ITEMS ( ( 2 * NUM_WIFI_CREDENTIALS ) + 1 ) +//////////////// + +typedef struct Configuration +{ + char header [16]; + WiFi_Credentials WiFi_Creds [NUM_WIFI_CREDENTIALS]; + char board_name [24]; + int checkSum; +} WiFiNINA_Configuration; +*/ + +#define TO_LOAD_DEFAULT_CONFIG_DATA true + +#if TO_LOAD_DEFAULT_CONFIG_DATA + +bool LOAD_DEFAULT_CONFIG_DATA = true; + +WiFiNINA_Configuration defaultConfig = +{ + //char header[16], dummy, not used + "WIFININA", + // WiFi_Credentials WiFi_Creds [NUM_WIFI_CREDENTIALS]; + // WiFi_Credentials.wifi_ssid and WiFi_Credentials.wifi_pw + "SSID1", "password1", + "SSID2", "password2", + //char board_name [24]; + "Air-Control", + // terminate the list + //int checkSum, dummy, not used + 0 + /////////// End Default Config Data ///////////// +}; + +#else + +bool LOAD_DEFAULT_CONFIG_DATA = false; + +WiFiNINA_Configuration defaultConfig; + +#endif // TO_LOAD_DEFAULT_CONFIG_DATA + +/////////// End Default Config Data ///////////// + + +#endif //Credentials_h diff --git a/examples/Teensy40_WiFiNINA/Teensy40_WiFiNINA.ino b/examples/Teensy40_WiFiNINA/Teensy40_WiFiNINA.ino index e1c8fe40..daf5f0ec 100644 --- a/examples/Teensy40_WiFiNINA/Teensy40_WiFiNINA.ino +++ b/examples/Teensy40_WiFiNINA/Teensy40_WiFiNINA.ino @@ -2,13 +2,13 @@ Teensy40_WiFiNINA.ino For Teensy 4.0 using WiFiNINA Modules/Shields, using much less code to support boards with smaller memory - WiFiManager_NINA_WM_Lite is a library for the Mega, Teensy, SAM DUE, SAMD and STM32 boards (https://github.com/khoih-prog/WiFiManager_NINA_Lite) - to enable store Credentials in EEPROM to easy configuration/reconfiguration and autoconnect/autoreconnect of WiFi and other services - without Hardcoding. + WiFiManager_NINA_WM_Lite is a library for the Mega, Teensy, SAM DUE, SAMD and STM32 boards + (https://github.com/khoih-prog/WiFiManager_NINA_Lite) to enable store Credentials in EEPROM/LittleFS for easy + configuration/reconfiguration and autoconnect/autoreconnect of WiFi and other services without Hardcoding. Built by Khoi Hoang https://github.com/khoih-prog/WiFiManager_NINA_Lite Licensed under MIT license - Version: 1.0.3 + Version: 1.0.4 Version Modified By Date Comments ------- ----------- ---------- ----------- @@ -17,103 +17,11 @@ 1.0.2 K Hoang 15/04/2020 Fix bug. Add SAMD51 support. 1.0.3 K Hoang 24/04/2020 Fix bug. Add nRF5 (Adafruit, NINA_B302_ublox, etc.) support. Add MultiWiFi, HostName capability. SSID password maxlen is 63 now. Permit special chars # and % in input data. + 1.0.4 K Hoang 04/05/2020 Add Configurable Config Portal Title, Default Config Data and DRD. Update examples. *****************************************************************************************************************************/ - -/* Comment this out to disable prints and save space */ -#define DEBUG_WIFI_WEBSERVER_PORT Serial -#define WIFININA_DEBUG_OUTPUT Serial - -#if ( defined(ESP8266) || defined(ESP32) || defined(ARDUINO_AVR_MEGA) || defined(ARDUINO_AVR_MEGA2560) || !defined(CORE_TEENSY) ) -#error This code is intended to run on Teensy platform! Please check your Tools->Board setting. -#endif - -#ifdef CORE_TEENSY - -#if defined(__IMXRT1062__) -// For Teensy 4.0 -#define BOARD_TYPE "TEENSY 4.0" -#elif defined(__MK66FX1M0__) -#define BOARD_TYPE "Teensy 3.6" -#elif defined(__MK64FX512__) -#define BOARD_TYPE "Teensy 3.5" -#elif defined(__MKL26Z64__) -#define BOARD_TYPE "Teensy LC" -#elif defined(__MK20DX256__) -#define BOARD_TYPE "Teensy 3.2" // and Teensy 3.1 (obsolete) -#elif defined(__MK20DX128__) -#define BOARD_TYPE "Teensy 3.0" -#elif defined(__AVR_AT90USB1286__) -#error Teensy 2.0++ not supported yet -#elif defined(__AVR_ATmega32U4__) -#error Teensy 2.0 not supported yet -#else -// For Other Boards -#define BOARD_TYPE "Unknown Teensy Board" -#endif -#endif - -// Start location in EEPROM to store config data. Default 0 -// Config data Size currently is 128 bytes) -#define EEPROM_START 0 - -#include "WiFiNINA_Pinout_Generic.h" - -#include - -#define USE_DYNAMIC_PARAMETERS true - -/////////////// Start dynamic Credentials /////////////// - -//Defined in -/************************************** - #define MAX_ID_LEN 5 - #define MAX_DISPLAY_NAME_LEN 16 - - typedef struct - { - char id [MAX_ID_LEN + 1]; - char displayName [MAX_DISPLAY_NAME_LEN + 1]; - char *pdata; - uint8_t maxlen; - } MenuItem; -**************************************/ - -#if USE_DYNAMIC_PARAMETERS - -#define MAX_BLYNK_SERVER_LEN 34 -#define MAX_BLYNK_TOKEN_LEN 34 - -char Blynk_Server1 [MAX_BLYNK_SERVER_LEN + 1] = ""; -char Blynk_Token1 [MAX_BLYNK_TOKEN_LEN + 1] = ""; - -char Blynk_Server2 [MAX_BLYNK_SERVER_LEN + 1] = ""; -char Blynk_Token2 [MAX_BLYNK_TOKEN_LEN + 1] = ""; - -#define MAX_BLYNK_PORT_LEN 6 -char Blynk_Port [MAX_BLYNK_PORT_LEN + 1] = ""; - -#define MAX_MQTT_SERVER_LEN 34 -char MQTT_Server [MAX_MQTT_SERVER_LEN + 1] = ""; - -MenuItem myMenuItems [] = -{ - { "sv1", "Blynk Server1", Blynk_Server1, MAX_BLYNK_SERVER_LEN }, - { "tk1", "Token1", Blynk_Token1, MAX_BLYNK_TOKEN_LEN }, - { "sv2", "Blynk Server2", Blynk_Server2, MAX_BLYNK_SERVER_LEN }, - { "tk2", "Token2", Blynk_Token2, MAX_BLYNK_TOKEN_LEN }, - { "pt", "Port", Blynk_Port, MAX_BLYNK_PORT_LEN }, - { "mq", "MQTT Server", MQTT_Server, MAX_MQTT_SERVER_LEN }, -}; - -uint16_t NUM_MENU_ITEMS = sizeof(myMenuItems) / sizeof(MenuItem); //MenuItemSize; - -#else - -MenuItem myMenuItems [] = {}; - -uint16_t NUM_MENU_ITEMS = 0; - -#endif //USE_DYNAMIC_PARAMETERS +#include "defines.h" +#include "Credentials.h" +#include "dynamicParams.h" void heartBeatPrint(void) { diff --git a/examples/Teensy40_WiFiNINA/defines.h b/examples/Teensy40_WiFiNINA/defines.h new file mode 100644 index 00000000..78390e8a --- /dev/null +++ b/examples/Teensy40_WiFiNINA/defines.h @@ -0,0 +1,73 @@ +/**************************************************************************************************************************** + defines.h for ESP32WM_Config.ino + For nRF52 boards using WiFiNINA modules/shields, using much less code to support boards with smaller memory + + WiFiManager_NINA_WM_Lite is a library for the Mega, Teensy, SAM DUE, SAMD and STM32 boards + (https://github.com/khoih-prog/WiFiManager_NINA_Lite) to enable store Credentials in EEPROM/LittleFS for easy + configuration/reconfiguration and autoconnect/autoreconnect of WiFi and other services without Hardcoding. + + Built by Khoi Hoang https://github.com/khoih-prog/WiFiManager_NINA_Lite + Licensed under MIT license + Version: 1.0.4 + + Version Modified By Date Comments + ------- ----------- ---------- ----------- + 1.0.0 K Hoang 26/03/2020 Initial coding + 1.0.1 K Hoang 27/03/2020 Fix SAMD soft-reset bug. Add support to remaining boards + 1.0.2 K Hoang 15/04/2020 Fix bug. Add SAMD51 support. + 1.0.3 K Hoang 24/04/2020 Fix bug. Add nRF5 (Adafruit, NINA_B302_ublox, etc.) support. Add MultiWiFi, HostName capability. + SSID password maxlen is 63 now. Permit special chars # and % in input data. + 1.0.4 K Hoang 04/05/2020 Add Configurable Config Portal Title, Default Config Data and DRD. Update examples. + *****************************************************************************************************************************/ + +#ifndef defines_h +#define defines_h + +/* Comment this out to disable prints and save space */ +#define DEBUG_WIFI_WEBSERVER_PORT Serial +#define WIFININA_DEBUG_OUTPUT Serial + +#define WIFININA_DEBUG false //true + +#define DRD_GENERIC_DEBUG false //true + +#if ( defined(ESP8266) || defined(ESP32) || defined(ARDUINO_AVR_MEGA) || defined(ARDUINO_AVR_MEGA2560) || !defined(CORE_TEENSY) ) +#error This code is intended to run on Teensy platform! Please check your Tools->Board setting. +#endif + +#ifdef CORE_TEENSY + +#if defined(__IMXRT1062__) +// For Teensy 4.0 +#define BOARD_TYPE "TEENSY 4.0" +#elif defined(__MK66FX1M0__) +#define BOARD_TYPE "Teensy 3.6" +#elif defined(__MK64FX512__) +#define BOARD_TYPE "Teensy 3.5" +#elif defined(__MKL26Z64__) +#define BOARD_TYPE "Teensy LC" +#elif defined(__MK20DX256__) +#define BOARD_TYPE "Teensy 3.2" // and Teensy 3.1 (obsolete) +#elif defined(__MK20DX128__) +#define BOARD_TYPE "Teensy 3.0" +#elif defined(__AVR_AT90USB1286__) +#error Teensy 2.0++ not supported yet +#elif defined(__AVR_ATmega32U4__) +#error Teensy 2.0 not supported yet +#else +// For Other Boards +#define BOARD_TYPE "Unknown Teensy Board" +#endif +#endif + +// Start location in EEPROM to store config data. Default 0 +// Config data Size currently is 128 bytes) +#define EEPROM_START 0 + +#include "WiFiNINA_Pinout_Generic.h" + +#include + +#define HOST_NAME "Teensy-Master-Controller" + +#endif //defines_h diff --git a/examples/Teensy40_WiFiNINA/dynamicParams.h b/examples/Teensy40_WiFiNINA/dynamicParams.h new file mode 100644 index 00000000..c4644cac --- /dev/null +++ b/examples/Teensy40_WiFiNINA/dynamicParams.h @@ -0,0 +1,84 @@ +/**************************************************************************************************************************** + dynamicParams.h + For nRF52 boards using WiFiNINA modules/shields, using much less code to support boards with smaller memory + + WiFiManager_NINA_WM_Lite is a library for the Mega, Teensy, SAM DUE, SAMD and STM32 boards + (https://github.com/khoih-prog/WiFiManager_NINA_Lite) to enable store Credentials in EEPROM/LittleFS for easy + configuration/reconfiguration and autoconnect/autoreconnect of WiFi and other services without Hardcoding. + + Built by Khoi Hoang https://github.com/khoih-prog/WiFiManager_NINA_Lite + Licensed under MIT license + Version: 1.0.4 + + Version Modified By Date Comments + ------- ----------- ---------- ----------- + 1.0.0 K Hoang 26/03/2020 Initial coding + 1.0.1 K Hoang 27/03/2020 Fix SAMD soft-reset bug. Add support to remaining boards + 1.0.2 K Hoang 15/04/2020 Fix bug. Add SAMD51 support. + 1.0.3 K Hoang 24/04/2020 Fix bug. Add nRF5 (Adafruit, NINA_B302_ublox, etc.) support. Add MultiWiFi, HostName capability. + SSID password maxlen is 63 now. Permit special chars # and % in input data. + 1.0.4 K Hoang 04/05/2020 Add Configurable Config Portal Title, Default Config Data and DRD. Update examples. + *****************************************************************************************************************************/ + +#ifndef dynamicParams_h +#define dynamicParams_h + +#include "defines.h" + +#define USE_DYNAMIC_PARAMETERS true + +/////////////// Start dynamic Credentials /////////////// + +//Defined in +/************************************** + #define MAX_ID_LEN 5 + #define MAX_DISPLAY_NAME_LEN 16 + + typedef struct + { + char id [MAX_ID_LEN + 1]; + char displayName [MAX_DISPLAY_NAME_LEN + 1]; + char *pdata; + uint8_t maxlen; + } MenuItem; +**************************************/ + +#if USE_DYNAMIC_PARAMETERS + +#define MAX_BLYNK_SERVER_LEN 34 +#define MAX_BLYNK_TOKEN_LEN 34 + +char Blynk_Server1 [MAX_BLYNK_SERVER_LEN + 1] = "account.duckdns.org"; +char Blynk_Token1 [MAX_BLYNK_TOKEN_LEN + 1] = "token1"; + +char Blynk_Server2 [MAX_BLYNK_SERVER_LEN + 1] = "account.ddns.net"; +char Blynk_Token2 [MAX_BLYNK_TOKEN_LEN + 1] = "token2"; + +#define MAX_BLYNK_PORT_LEN 6 +char Blynk_Port [MAX_BLYNK_PORT_LEN + 1] = "8080"; + +#define MAX_MQTT_SERVER_LEN 34 +char MQTT_Server [MAX_MQTT_SERVER_LEN + 1] = "mqtt.duckdns.org"; + +MenuItem myMenuItems [] = +{ + { "sv1", "Blynk Server1", Blynk_Server1, MAX_BLYNK_SERVER_LEN }, + { "tk1", "Token1", Blynk_Token1, MAX_BLYNK_TOKEN_LEN }, + { "sv2", "Blynk Server2", Blynk_Server2, MAX_BLYNK_SERVER_LEN }, + { "tk2", "Token2", Blynk_Token2, MAX_BLYNK_TOKEN_LEN }, + { "pt", "Port", Blynk_Port, MAX_BLYNK_PORT_LEN }, + { "mq", "MQTT Server", MQTT_Server, MAX_MQTT_SERVER_LEN }, +}; + +uint16_t NUM_MENU_ITEMS = sizeof(myMenuItems) / sizeof(MenuItem); //MenuItemSize; + +#else + +MenuItem myMenuItems [] = {}; + +uint16_t NUM_MENU_ITEMS = 0; + +#endif //USE_DYNAMIC_PARAMETERS + + +#endif //dynamicParams_h diff --git a/examples/nRF52840_WiFiNINA/Credentials.h b/examples/nRF52840_WiFiNINA/Credentials.h new file mode 100644 index 00000000..fc9a519a --- /dev/null +++ b/examples/nRF52840_WiFiNINA/Credentials.h @@ -0,0 +1,89 @@ +/**************************************************************************************************************************** + Credentials.h + For nRF52 boards using WiFiNINA modules/shields, using much less code to support boards with smaller memory + + WiFiManager_NINA_WM_Lite is a library for the Mega, Teensy, SAM DUE, SAMD and STM32 boards + (https://github.com/khoih-prog/WiFiManager_NINA_Lite) to enable store Credentials in EEPROM/LittleFS for easy + configuration/reconfiguration and autoconnect/autoreconnect of WiFi and other services without Hardcoding. + + Built by Khoi Hoang https://github.com/khoih-prog/WiFiManager_NINA_Lite + Licensed under MIT license + Version: 1.0.4 + + Version Modified By Date Comments + ------- ----------- ---------- ----------- + 1.0.0 K Hoang 26/03/2020 Initial coding + 1.0.1 K Hoang 27/03/2020 Fix SAMD soft-reset bug. Add support to remaining boards + 1.0.2 K Hoang 15/04/2020 Fix bug. Add SAMD51 support. + 1.0.3 K Hoang 24/04/2020 Fix bug. Add nRF5 (Adafruit, NINA_B302_ublox, etc.) support. Add MultiWiFi, HostName capability. + SSID password maxlen is 63 now. Permit special chars # and % in input data. + 1.0.4 K Hoang 04/05/2020 Add Configurable Config Portal Title, Default Config Data and DRD. Update examples. + *****************************************************************************************************************************/ + +#ifndef Credentials_h +#define Credentials_h + +#include "defines.h" + +/// Start Default Config Data ////////////////// + +/* +#define SSID_MAX_LEN 32 +//From v1.0.3, 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; + +#define NUM_WIFI_CREDENTIALS 2 + +// Configurable items besides fixed Header, just add board_name +#define NUM_CONFIGURABLE_ITEMS ( ( 2 * NUM_WIFI_CREDENTIALS ) + 1 ) +//////////////// + +typedef struct Configuration +{ + char header [16]; + WiFi_Credentials WiFi_Creds [NUM_WIFI_CREDENTIALS]; + char board_name [24]; + int checkSum; +} WiFiNINA_Configuration; +*/ + +#define TO_LOAD_DEFAULT_CONFIG_DATA true + +#if TO_LOAD_DEFAULT_CONFIG_DATA + +bool LOAD_DEFAULT_CONFIG_DATA = true; + +WiFiNINA_Configuration defaultConfig = +{ + //char header[16], dummy, not used + "WIFININA", + // WiFi_Credentials WiFi_Creds [NUM_WIFI_CREDENTIALS]; + // WiFi_Credentials.wifi_ssid and WiFi_Credentials.wifi_pw + "SSID1", "password1", + "SSID2", "password2", + //char board_name [24]; + "Air-Control", + // terminate the list + //int checkSum, dummy, not used + 0 + /////////// End Default Config Data ///////////// +}; + +#else + +bool LOAD_DEFAULT_CONFIG_DATA = false; + +WiFiNINA_Configuration defaultConfig; + +#endif // TO_LOAD_DEFAULT_CONFIG_DATA + +/////////// End Default Config Data ///////////// + + +#endif //Credentials_h diff --git a/examples/nRF52840_WiFiNINA/defines.h b/examples/nRF52840_WiFiNINA/defines.h new file mode 100644 index 00000000..79662a76 --- /dev/null +++ b/examples/nRF52840_WiFiNINA/defines.h @@ -0,0 +1,92 @@ +/**************************************************************************************************************************** + defines.h for ESP32WM_Config.ino + For nRF52 boards using WiFiNINA modules/shields, using much less code to support boards with smaller memory + + WiFiManager_NINA_WM_Lite is a library for the Mega, Teensy, SAM DUE, SAMD and STM32 boards + (https://github.com/khoih-prog/WiFiManager_NINA_Lite) to enable store Credentials in EEPROM/LittleFS for easy + configuration/reconfiguration and autoconnect/autoreconnect of WiFi and other services without Hardcoding. + + Built by Khoi Hoang https://github.com/khoih-prog/WiFiManager_NINA_Lite + Licensed under MIT license + Version: 1.0.4 + + Version Modified By Date Comments + ------- ----------- ---------- ----------- + 1.0.0 K Hoang 26/03/2020 Initial coding + 1.0.1 K Hoang 27/03/2020 Fix SAMD soft-reset bug. Add support to remaining boards + 1.0.2 K Hoang 15/04/2020 Fix bug. Add SAMD51 support. + 1.0.3 K Hoang 24/04/2020 Fix bug. Add nRF5 (Adafruit, NINA_B302_ublox, etc.) support. Add MultiWiFi, HostName capability. + SSID password maxlen is 63 now. Permit special chars # and % in input data. + 1.0.4 K Hoang 04/05/2020 Add Configurable Config Portal Title, Default Config Data and DRD. Update examples. + *****************************************************************************************************************************/ + +#ifndef defines_h +#define defines_h + +/* Comment this out to disable prints and save space */ +#define DEBUG_WIFI_WEBSERVER_PORT Serial +#define WIFININA_DEBUG_OUTPUT Serial + +#define WIFININA_DEBUG true + +#define DRD_GENERIC_DEBUG true + +#if ( defined(NRF52840_FEATHER) || defined(NRF52832_FEATHER) || defined(NRF52_SERIES) || defined(ARDUINO_NRF52_ADAFRUIT) || \ + defined(NRF52840_FEATHER_SENSE) || defined(NRF52840_ITSYBITSY) || defined(NRF52840_CIRCUITPLAY) || defined(NRF52840_CLUE) || \ + defined(NRF52840_METRO) || defined(NRF52840_PCA10056) || defined(PARTICLE_XENON) || defined(NINA_B302_ublox) ) + #if defined(WIFININA_USE_NRF528XX) + #undef WIFININA_USE_NRF528XX + #undef WIFI_USE_NRF528XX + #endif + #define WIFININA_USE_NRF528XX true + #define WIFI_USE_NRF528XX true +#else + #error This code is intended to run only on the NRF528XX boards ! Please check your Tools->Board setting. +#endif + + +#if defined(WIFININA_USE_NRF528XX) + +#if defined(NRF52840_FEATHER) +#define BOARD_TYPE "NRF52840_FEATHER" +#elif defined(NRF52832_FEATHER) +#define BOARD_TYPE "NRF52832_FEATHER" +#elif defined(NRF52840_FEATHER_SENSE) +#define BOARD_TYPE "NRF52840_FEATHER_SENSE" +#elif defined(NRF52840_ITSYBITSY) +#define BOARD_TYPE "NRF52840_ITSYBITSY" +#elif defined(NRF52840_CIRCUITPLAY) +#define BOARD_TYPE "NRF52840_CIRCUITPLAY" +#elif defined(NRF52840_CLUE) +#define BOARD_TYPE "NRF52840_CLUE" +#elif defined(NRF52840_METRO) +#define BOARD_TYPE "NRF52840_METRO" +#elif defined(NRF52840_PCA10056) +#define BOARD_TYPE "NRF52840_PCA10056" +#elif defined(PARTICLE_XENON) +#define BOARD_TYPE "PARTICLE_XENON" +#elif defined(NINA_B302_ublox) +#define BOARD_TYPE "NINA_B302_ublox" +#elif defined(ARDUINO_NRF52_ADAFRUIT) +#define BOARD_TYPE "ARDUINO_NRF52_ADAFRUIT" +#elif defined(NRF52_SERIES) +#define BOARD_TYPE "NRF52_SERIES" +#else +#define BOARD_TYPE "NRF52 Unknown" +#endif + +#endif + +// Start location in EEPROM to store config data. Default 0 +// Config data Size currently is 128 bytes) +#define EEPROM_START + +#include "WiFiNINA_Pinout_Generic.h" + +#include + + + +#define HOST_NAME "nRF52-Master-Controller" + +#endif //defines_h diff --git a/examples/nRF52840_WiFiNINA/dynamicParams.h b/examples/nRF52840_WiFiNINA/dynamicParams.h new file mode 100644 index 00000000..c4644cac --- /dev/null +++ b/examples/nRF52840_WiFiNINA/dynamicParams.h @@ -0,0 +1,84 @@ +/**************************************************************************************************************************** + dynamicParams.h + For nRF52 boards using WiFiNINA modules/shields, using much less code to support boards with smaller memory + + WiFiManager_NINA_WM_Lite is a library for the Mega, Teensy, SAM DUE, SAMD and STM32 boards + (https://github.com/khoih-prog/WiFiManager_NINA_Lite) to enable store Credentials in EEPROM/LittleFS for easy + configuration/reconfiguration and autoconnect/autoreconnect of WiFi and other services without Hardcoding. + + Built by Khoi Hoang https://github.com/khoih-prog/WiFiManager_NINA_Lite + Licensed under MIT license + Version: 1.0.4 + + Version Modified By Date Comments + ------- ----------- ---------- ----------- + 1.0.0 K Hoang 26/03/2020 Initial coding + 1.0.1 K Hoang 27/03/2020 Fix SAMD soft-reset bug. Add support to remaining boards + 1.0.2 K Hoang 15/04/2020 Fix bug. Add SAMD51 support. + 1.0.3 K Hoang 24/04/2020 Fix bug. Add nRF5 (Adafruit, NINA_B302_ublox, etc.) support. Add MultiWiFi, HostName capability. + SSID password maxlen is 63 now. Permit special chars # and % in input data. + 1.0.4 K Hoang 04/05/2020 Add Configurable Config Portal Title, Default Config Data and DRD. Update examples. + *****************************************************************************************************************************/ + +#ifndef dynamicParams_h +#define dynamicParams_h + +#include "defines.h" + +#define USE_DYNAMIC_PARAMETERS true + +/////////////// Start dynamic Credentials /////////////// + +//Defined in +/************************************** + #define MAX_ID_LEN 5 + #define MAX_DISPLAY_NAME_LEN 16 + + typedef struct + { + char id [MAX_ID_LEN + 1]; + char displayName [MAX_DISPLAY_NAME_LEN + 1]; + char *pdata; + uint8_t maxlen; + } MenuItem; +**************************************/ + +#if USE_DYNAMIC_PARAMETERS + +#define MAX_BLYNK_SERVER_LEN 34 +#define MAX_BLYNK_TOKEN_LEN 34 + +char Blynk_Server1 [MAX_BLYNK_SERVER_LEN + 1] = "account.duckdns.org"; +char Blynk_Token1 [MAX_BLYNK_TOKEN_LEN + 1] = "token1"; + +char Blynk_Server2 [MAX_BLYNK_SERVER_LEN + 1] = "account.ddns.net"; +char Blynk_Token2 [MAX_BLYNK_TOKEN_LEN + 1] = "token2"; + +#define MAX_BLYNK_PORT_LEN 6 +char Blynk_Port [MAX_BLYNK_PORT_LEN + 1] = "8080"; + +#define MAX_MQTT_SERVER_LEN 34 +char MQTT_Server [MAX_MQTT_SERVER_LEN + 1] = "mqtt.duckdns.org"; + +MenuItem myMenuItems [] = +{ + { "sv1", "Blynk Server1", Blynk_Server1, MAX_BLYNK_SERVER_LEN }, + { "tk1", "Token1", Blynk_Token1, MAX_BLYNK_TOKEN_LEN }, + { "sv2", "Blynk Server2", Blynk_Server2, MAX_BLYNK_SERVER_LEN }, + { "tk2", "Token2", Blynk_Token2, MAX_BLYNK_TOKEN_LEN }, + { "pt", "Port", Blynk_Port, MAX_BLYNK_PORT_LEN }, + { "mq", "MQTT Server", MQTT_Server, MAX_MQTT_SERVER_LEN }, +}; + +uint16_t NUM_MENU_ITEMS = sizeof(myMenuItems) / sizeof(MenuItem); //MenuItemSize; + +#else + +MenuItem myMenuItems [] = {}; + +uint16_t NUM_MENU_ITEMS = 0; + +#endif //USE_DYNAMIC_PARAMETERS + + +#endif //dynamicParams_h diff --git a/examples/nRF52840_WiFiNINA/nRF52840_WiFiNINA.ino b/examples/nRF52840_WiFiNINA/nRF52840_WiFiNINA.ino index e348d2da..0e4a41de 100644 --- a/examples/nRF52840_WiFiNINA/nRF52840_WiFiNINA.ino +++ b/examples/nRF52840_WiFiNINA/nRF52840_WiFiNINA.ino @@ -1,14 +1,14 @@ /**************************************************************************************************************************** - Nano33BLE_WiFiNINA.ino - For SAMD boards using WiFiNINA Modules/Shields, using much less code to support boards with smaller memory + nRF52840_WiFiNINA.ino + For nRF52 boards using WiFiNINA modules/shields, using much less code to support boards with smaller memory - WiFiManager_NINA_WM_Lite is a library for the Mega, Teensy, SAM DUE, SAMD and STM32 boards (https://github.com/khoih-prog/WiFiManager_NINA_Lite) - to enable store Credentials in EEPROM to easy configuration/reconfiguration and autoconnect/autoreconnect of WiFi and other services - without Hardcoding. + WiFiManager_NINA_WM_Lite is a library for the Mega, Teensy, SAM DUE, SAMD and STM32 boards + (https://github.com/khoih-prog/WiFiManager_NINA_Lite) to enable store Credentials in EEPROM/LittleFS for easy + configuration/reconfiguration and autoconnect/autoreconnect of WiFi and other services without Hardcoding. Built by Khoi Hoang https://github.com/khoih-prog/WiFiManager_NINA_Lite Licensed under MIT license - Version: 1.0.3 + Version: 1.0.4 Version Modified By Date Comments ------- ----------- ---------- ----------- @@ -17,122 +17,11 @@ 1.0.2 K Hoang 15/04/2020 Fix bug. Add SAMD51 support. 1.0.3 K Hoang 24/04/2020 Fix bug. Add nRF5 (Adafruit, NINA_B302_ublox, etc.) support. Add MultiWiFi, HostName capability. SSID password maxlen is 63 now. Permit special chars # and % in input data. + 1.0.4 K Hoang 04/05/2020 Add Configurable Config Portal Title, Default Config Data and DRD. Update examples. *****************************************************************************************************************************/ - -/* Comment this out to disable prints and save space */ -#define DEBUG_WIFI_WEBSERVER_PORT Serial -#define WIFININA_DEBUG_OUTPUT Serial - -#define WIFININA_DEBUG false //true - -#if ( defined(NRF52840_FEATHER) || defined(NRF52832_FEATHER) || defined(NRF52_SERIES) || defined(ARDUINO_NRF52_ADAFRUIT) || \ - defined(NRF52840_FEATHER_SENSE) || defined(NRF52840_ITSYBITSY) || defined(NRF52840_CIRCUITPLAY) || defined(NRF52840_CLUE) || \ - defined(NRF52840_METRO) || defined(NRF52840_PCA10056) || defined(PARTICLE_XENON) || defined(NINA_B302_ublox) ) - #if defined(WIFININA_USE_NRF528XX) - #undef WIFININA_USE_NRF528XX - #undef WIFI_USE_NRF528XX - #endif - #define WIFININA_USE_NRF528XX true - #define WIFI_USE_NRF528XX true -#else - #error This code is intended to run only on the NRF528XX boards ! Please check your Tools->Board setting. -#endif - - -#if defined(WIFININA_USE_NRF528XX) - -#if defined(NRF52840_FEATHER) -#define BOARD_TYPE "NRF52840_FEATHER" -#elif defined(NRF52832_FEATHER) -#define BOARD_TYPE "NRF52832_FEATHER" -#elif defined(NRF52840_FEATHER_SENSE) -#define BOARD_TYPE "NRF52840_FEATHER_SENSE" -#elif defined(NRF52840_ITSYBITSY) -#define BOARD_TYPE "NRF52840_ITSYBITSY" -elif defined(NRF52840_CIRCUITPLAY) -#define BOARD_TYPE "NRF52840_CIRCUITPLAY" -#elif defined(NRF52840_CLUE) -#define BOARD_TYPE "NRF52840_CLUE" -#elif defined(NRF52840_METRO) -#define BOARD_TYPE "NRF52840_METRO" -elif defined(NRF52840_PCA10056) -#define BOARD_TYPE "NRF52840_PCA10056" -#elif defined(PARTICLE_XENON) -#define BOARD_TYPE "PARTICLE_XENON" -#elif defined(NINA_B302_ublox) -#define BOARD_TYPE "NINA_B302_ublox" -#elif defined(ARDUINO_NRF52_ADAFRUIT) -#define BOARD_TYPE "ARDUINO_NRF52_ADAFRUIT" -#elif defined(NRF52_SERIES) -#define BOARD_TYPE "NRF52_SERIES" -#else -#define BOARD_TYPE "nRF52 Unknown" -#endif - -#endif - -// Start location in EEPROM to store config data. Default 0 -// Config data Size currently is 128 bytes) -#define EEPROM_START - -#include "WiFiNINA_Pinout_Generic.h" - -#include - -#define USE_DYNAMIC_PARAMETERS true - -/////////////// Start dynamic Credentials /////////////// - -//Defined in -/************************************** - #define MAX_ID_LEN 5 - #define MAX_DISPLAY_NAME_LEN 16 - - typedef struct - { - char id [MAX_ID_LEN + 1]; - char displayName [MAX_DISPLAY_NAME_LEN + 1]; - char *pdata; - uint8_t maxlen; - } MenuItem; -**************************************/ - -#if USE_DYNAMIC_PARAMETERS - -#define MAX_BLYNK_SERVER_LEN 34 -#define MAX_BLYNK_TOKEN_LEN 34 - -char Blynk_Server1 [MAX_BLYNK_SERVER_LEN + 1] = ""; -char Blynk_Token1 [MAX_BLYNK_TOKEN_LEN + 1] = ""; - -char Blynk_Server2 [MAX_BLYNK_SERVER_LEN + 1] = ""; -char Blynk_Token2 [MAX_BLYNK_TOKEN_LEN + 1] = ""; - -#define MAX_BLYNK_PORT_LEN 6 -char Blynk_Port [MAX_BLYNK_PORT_LEN + 1] = ""; - -#define MAX_MQTT_SERVER_LEN 34 -char MQTT_Server [MAX_MQTT_SERVER_LEN + 1] = ""; - -MenuItem myMenuItems [] = -{ - { "sv1", "Blynk Server1", Blynk_Server1, MAX_BLYNK_SERVER_LEN }, - { "tk1", "Token1", Blynk_Token1, MAX_BLYNK_TOKEN_LEN }, - { "sv2", "Blynk Server2", Blynk_Server2, MAX_BLYNK_SERVER_LEN }, - { "tk2", "Token2", Blynk_Token2, MAX_BLYNK_TOKEN_LEN }, - { "pt", "Port", Blynk_Port, MAX_BLYNK_PORT_LEN }, - { "mq", "MQTT Server", MQTT_Server, MAX_MQTT_SERVER_LEN }, -}; - -uint16_t NUM_MENU_ITEMS = sizeof(myMenuItems) / sizeof(MenuItem); //MenuItemSize; - -#else - -MenuItem myMenuItems [] = {}; - -uint16_t NUM_MENU_ITEMS = 0; - -#endif //USE_DYNAMIC_PARAMETERS +#include "defines.h" +#include "Credentials.h" +#include "dynamicParams.h" void heartBeatPrint(void) { @@ -176,8 +65,7 @@ void setup() Serial.begin(115200); while (!Serial); - Serial.println("\nStart SAMD_WiFiNINA on " + String(BOARD_TYPE)); - + Serial.println("\nStart nRF52_WiFiNINA on " + String(BOARD_TYPE)); WiFiManager_NINA = new WiFiManager_NINA_Lite(); @@ -186,7 +74,7 @@ void setup() //WiFiManager_NINA->setConfigPortalChannel(1); // Set customized DHCP HostName - WiFiManager_NINA->begin("NRF52-WiFiNINA-ABCDEF"); + WiFiManager_NINA->begin(HOST_NAME); //Or use default Hostname "NRF52-WiFiNINA-XXXXXX" //WiFiManager_NINA->begin(); } diff --git a/library.json b/library.json index ce46a5fa..2b78c526 100644 --- a/library.json +++ b/library.json @@ -1,8 +1,8 @@ { "name": "WiFiManager_NINA_Lite", - "version": "1.0.3", - "keywords": "wifi, wi-fi, MutiWiFi, WiFiNINA, U-Blox, W101, W102, shield, Mega, Teensy, SAM DUE, SAMD, STM32, nRF52, Credentials, Manager", - "description": "Library to configure MultiWiFi/Credentials at runtime for AVR Mega, Teensy, SAM DUE, SAMD, STM32nRF52, etc. boards running U-Blox WiFiNINA modules/shields. You can also specify DHCP HostName, static AP and STA IP. Use much less memory compared to full-fledge WiFiManager. Config Portal will be auto-adjusted to match the number of dynamic custom parameters. Credentials are saved in EEPROM, FlashStorage or DueFlashStorage.", + "version": "1.0.4", + "keywords": "wifi, wi-fi, MutiWiFi, WiFiNINA, U-Blox, W101, W102, shield, Mega, Teensy, SAM DUE, SAMD21, SAMD51, STM32, nRF52, Credentials, Manager, Config Portal, Double, Reset, Detector", + "description": "Library to configure MultiWiFi/Credentials at runtime for AVR Mega, Teensy, SAM DUE, SAMD21, SAMD51, STM32, nRF52, etc. boards running U-Blox WiFiNINA modules/shields. You can also specify DHCP HostName, static AP and STA IP. Use much less memory compared to full-fledge WiFiManager. Config Portal will be auto-adjusted to match the number of dynamic custom parameters. Optional default Credentials to be autoloaded into Config Portal to use or change instead of manually input. Credentials are saved in LittleFS/InternalFS, EEPROM, FlashStorage or DueFlashStorage. DoubleDetectDetector feature permits entering Config Portal as requested.", "authors": { "name": "Khoi Hoang", diff --git a/library.properties b/library.properties index 3d8a72de..9e613dc7 100644 --- a/library.properties +++ b/library.properties @@ -1,11 +1,11 @@ name=WiFiManager_NINA_Lite -version=1.0.3 +version=1.0.4 author=Khoi Hoang maintainer=Khoi Hoang license=MIT -sentence=Light-Weight MultiWiFi/Credentials Manager for AVR Mega, Teensy, SAM DUE, SAMD, STM32, nRF52, etc. boards running U-Blox WiFiNINA modules/shields. Powerful-yet-simple-to-use feature to enable adding dynamic custom parameters. -paragraph=Library to configure MultiWiFi/Credentials at runtime for AVR Mega, Teensy, SAM DUE, SAMD, STM32nRF52, etc. boards running U-Blox WiFiNINA modules/shields. You can also specify DHCP HostName, static AP and STA IP. Use much less memory compared to full-fledge WiFiManager. Config Portal will be auto-adjusted to match the number of dynamic custom parameters. Credentials are saved in EEPROM, FlashStorage or DueFlashStorage. +sentence=Light-Weight MultiWiFi/Credentials Manager for AVR Mega, Teensy, SAM DUE, SAMD21, SAMD51, STM32, nRF52, etc. boards running U-Blox WiFiNINA modules/shields. Powerful-yet-simple-to-use feature to enable adding dynamic custom parameters. +paragraph=Library to configure MultiWiFi/Credentials at runtime for AVR Mega, Teensy, SAM DUE, SAMD21, SAMD51, STM32, nRF52, etc. boards running U-Blox WiFiNINA modules/shields. You can also specify DHCP HostName, static AP and STA IP. Use much less memory compared to full-fledge WiFiManager. Config Portal will be auto-adjusted to match the number of dynamic custom parameters. Optional default Credentials to be autoloaded into Config Portal to use or change instead of manually input. Credentials are saved in LittleFS/InternalFS, EEPROM, FlashStorage or DueFlashStorage. DoubleDetectDetector feature permits entering Config Portal as requested. category=Communication url=https://github.com/khoih-prog/WiFiManager_NINA_Lite architectures=* -depends=Functional-VLPP,WiFiNINA_Generic,WiFiWebServer +depends=Functional-VLPP,WiFiNINA_Generic,WiFiWebServer,DoubleResetDetector_Generic diff --git a/pics/Input.png b/pics/Input.png index 2dfb3490..63b2d977 100644 Binary files a/pics/Input.png and b/pics/Input.png differ diff --git a/pics/Main.png b/pics/Main.png index ad2ad7ca..7c255946 100644 Binary files a/pics/Main.png and b/pics/Main.png differ diff --git a/pics/Save.png b/pics/Save.png index 1b47140b..f0bb6511 100644 Binary files a/pics/Save.png and b/pics/Save.png differ diff --git a/src/WiFiManager_NINA_Lite.h b/src/WiFiManager_NINA_Lite.h index b8c54f0c..1cec09fc 100644 --- a/src/WiFiManager_NINA_Lite.h +++ b/src/WiFiManager_NINA_Lite.h @@ -2,13 +2,13 @@ WiFiManager_NINA_Lite.h For AVR or Generic boards using using WiFiNINA modules/shields, using much less code to support boards with smaller memory - WiFiManager_NINA_WM_Lite is a library for the Mega, Teensy, SAM DUE, SAMD and STM32 boards (https://github.com/khoih-prog/WiFiManager_NINA_Lite) - to enable store Credentials in EEPROM to easy configuration/reconfiguration and autoconnect/autoreconnect of WiFi and other services - without Hardcoding. + WiFiManager_NINA_WM_Lite is a library for the Mega, Teensy, SAM DUE, SAMD and STM32 boards + (https://github.com/khoih-prog/WiFiManager_NINA_Lite) to enable store Credentials in EEPROM/LittleFS for easy + configuration/reconfiguration and autoconnect/autoreconnect of WiFi and other services without Hardcoding. Built by Khoi Hoang https://github.com/khoih-prog/WiFiManager_NINA_Lite Licensed under MIT license - Version: 1.0.3 + Version: 1.0.4 Version Modified By Date Comments ------- ----------- ---------- ----------- @@ -17,13 +17,14 @@ 1.0.2 K Hoang 15/04/2020 Fix bug. Add SAMD51 support. 1.0.3 K Hoang 24/04/2020 Fix bug. Add nRF5 (Adafruit, NINA_B302_ublox, etc.) support. Add MultiWiFi, HostName capability. SSID password maxlen is 63 now. Permit special chars # and % in input data. + 1.0.4 K Hoang 04/05/2020 Add Configurable Config Portal Title, Default Config Data and DRD. Update examples. *****************************************************************************************************************************/ #ifndef WiFiManager_NINA_Lite_h #define WiFiManager_NINA_Lite_h -#if ( defined(ESP8266) || defined(ESP32) || defined(CORE_TEENSY) ) -#error This code is not intended to run on the ESP8266, ESP32 nor Teensy platform! Please check your Tools->Board setting. +#if ( defined(ESP8266) || defined(ESP32) || defined(CORE_TEENSY) || defined(NRF52_SERIES) ) +#error This code is not intended to run on the ESP8266, ESP32, nRF52 nor Teensy platform! Please check your Tools->Board setting. #endif #if !( defined(ARDUINO_AVR_MEGA2560) || defined(ARDUINO_AVR_MEGA) ) @@ -34,6 +35,30 @@ #include #include +///////// NEW for DRD ///////////// +// These defines must be put before #include +// to select where to store DoubleResetDetector_Generic's variable. +// Otherwise, library will use default EEPROM storage +#define DRD_FLAG_DATA_SIZE 4 + +#ifndef DRD_GENERIC_DEBUG +#define DRD_GENERIC_DEBUG false +#endif + +#include //https://github.com/khoih-prog/DoubleResetDetector_Generic + +// 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_Generic to use +#define DRD_ADDRESS 0 + +DoubleResetDetector_Generic* drd; + +///////// NEW for DRD ///////////// + + //NEW #define MAX_ID_LEN 5 #define MAX_DISPLAY_NAME_LEN 16 @@ -66,19 +91,23 @@ typedef struct #define NUM_WIFI_CREDENTIALS 2 // Configurable items besides fixed Header -#define NUM_CONFIGURABLE_ITEMS ( 2 * NUM_WIFI_CREDENTIALS ) +#define NUM_CONFIGURABLE_ITEMS ( ( 2 * NUM_WIFI_CREDENTIALS ) + 1 ) //////////////// typedef struct Configuration { char header [16]; WiFi_Credentials WiFi_Creds [NUM_WIFI_CREDENTIALS]; + char board_name [24]; int checkSum; } WiFiNINA_Configuration; -// Currently CONFIG_DATA_SIZE = 212 = (16 + 96 * 2 + 4) +// Currently CONFIG_DATA_SIZE = 236 = (16 + 96 * 2 + 4 + 24) uint16_t CONFIG_DATA_SIZE = sizeof(WiFiNINA_Configuration); +///New from v1.0.4 +extern bool LOAD_DEFAULT_CONFIG_DATA; +extern WiFiNINA_Configuration defaultConfig; String IPAddressToString(IPAddress _address) { @@ -168,7 +197,28 @@ class WiFiManager_NINA_Lite DEBUG_WM2(F("Hostname="), RFC952_hostname); ////// - if (getConfigData()) + //// New DRD //// + drd = new DoubleResetDetector_Generic(DRD_TIMEOUT, DRD_ADDRESS); + bool noConfigPortal = true; + + if (drd->detectDoubleReset()) + { + DEBUG_WM1(F("Double Reset Detected")); + + noConfigPortal = false; + } + //// New DRD //// + DEBUG_WM1(F("======= Start Default Config Data =======")); + displayConfigData(defaultConfig); + + hadConfigData = getConfigData(); + + DEBUG_WM1(noConfigPortal? F("bg: noConfigPortal = true") : F("bg: noConfigPortal = false")); + + //// New DRD //// + // noConfigPortal when getConfigData() OK and no DRD'ed + if (hadConfigData && noConfigPortal) + //// New DRD //// { hadConfigData = true; @@ -235,6 +285,14 @@ class WiFiManager_NINA_Lite static unsigned long checkstatus_timeout = 0; #define WIFI_STATUS_CHECK_INTERVAL 2000L + //// New DRD //// + // Call the double reset detector loop method every so often, + // so that it can recognise when the timeout expires. + // You can also call drd.stop() when you wish to no longer + // consider the next reset as a double reset. + drd->loop(); + //// New DRD //// + if ((millis() > checkstatus_timeout) || (checkstatus_timeout == 0)) { if (WiFi.status() == WL_CONNECTED) @@ -381,7 +439,8 @@ class WiFiManager_NINA_Lite memset(myMenuItems[i].pdata, 0, myMenuItems[i].maxlen + 1); } - EEPROM_put(); + //EEPROM_put(); + saveConfigData(); } bool isConfigDataValid(void) @@ -444,11 +503,12 @@ class WiFiManager_NINA_Lite return RFC952_hostname; } - void displayConfigData(void) + void displayConfigData(WiFiNINA_Configuration configData) { - DEBUG_WM6(F("Hdr="), WiFiNINA_config.header, F(",SSID="), WiFiNINA_config.WiFi_Creds[0].wifi_ssid, - F(",PW="), WiFiNINA_config.WiFi_Creds[0].wifi_pw); - DEBUG_WM4(F("SSID1="), WiFiNINA_config.WiFi_Creds[1].wifi_ssid, F(",PW1="), WiFiNINA_config.WiFi_Creds[1].wifi_pw); + DEBUG_WM6(F("Hdr="), configData.header, F(",SSID="), configData.WiFi_Creds[0].wifi_ssid, + F(",PW="), configData.WiFi_Creds[0].wifi_pw); + DEBUG_WM4(F("SSID1="), configData.WiFi_Creds[1].wifi_ssid, F(",PW1="), configData.WiFi_Creds[1].wifi_pw); + DEBUG_WM2(F("BName="), configData.board_name); for (int i = 0; i < NUM_MENU_ITEMS; i++) { @@ -463,7 +523,7 @@ class WiFiManager_NINA_Lite } #define WIFININA_BOARD_TYPE "WIFININA" -#define NO_CONFIG "blank" +#define WM_NO_CONFIG "blank" #ifndef EEPROM_SIZE #define EEPROM_SIZE 4096 @@ -481,13 +541,17 @@ class WiFiManager_NINA_Lite #endif #ifndef EEPROM_START -#define EEPROM_START 0 +#define EEPROM_START 0 //define 256 in DRD +#warning EEPROM_START not defined. Set to 0 #else -#if (EEPROM_START + CONFIG_DATA_SIZE > EEPROM_SIZE) -#error EPROM_START + CONFIG_DATA_SIZE > EEPROM_SIZE. Please adjust. +#if (EEPROM_START + DRD_FLAG_DATA_SIZE + CONFIG_DATA_SIZE > EEPROM_SIZE) +#error EPROM_START + DRD_FLAG_DATA_SIZE + CONFIG_DATA_SIZE > EEPROM_SIZE. Please adjust. #endif #endif +// Stating positon to store Blynk8266_WM_config +#define NINA_EEPROM_START (EEPROM_START + DRD_FLAG_DATA_SIZE) + int calcChecksum() { int checkSum = 0; @@ -499,19 +563,72 @@ class WiFiManager_NINA_Lite return checkSum; } - bool EEPROM_get() + bool checkDynamicData(void) { - uint16_t offset = EEPROM_START; + int checkSum = 0; + int readCheckSum; - EEPROM.get(offset, WiFiNINA_config); + #define BUFFER_LEN 128 + char readBuffer[BUFFER_LEN + 1]; - offset += sizeof(WiFiNINA_config); + uint16_t offset = NINA_EEPROM_START + sizeof(WiFiNINA_config); + + // Find the longest pdata, then dynamically allocate buffer. Remember to free when done + // This is used to store tempo data to calculate checksum to see of data is valid + // We dont like to destroy myMenuItems[i].pdata with invalid data - int checkSum = 0; - int readCheckSum; + for (int i = 0; i < NUM_MENU_ITEMS; i++) + { + if (myMenuItems[i].maxlen > BUFFER_LEN) + { + // Size too large, abort and flag false + DEBUG_WM1(F("ChkCrR: Error Small Buffer.")); + return false; + } + } + + for (int i = 0; i < NUM_MENU_ITEMS; i++) + { + char* _pointer = readBuffer; + + // Prepare buffer, more than enough + memset(readBuffer, 0, sizeof(readBuffer)); + + // Read more than necessary, but OK and easier to code + EEPROM.get(offset, readBuffer); + // NULL terminated + readBuffer[myMenuItems[i].maxlen] = 0; + + DEBUG_WM4(F("ChkCrR:pdata="), readBuffer, F(",len="), myMenuItems[i].maxlen); + + for (uint16_t j = 0; j < myMenuItems[i].maxlen; j++,_pointer++) + { + checkSum += *_pointer; + } + + offset += myMenuItems[i].maxlen; + } + EEPROM.get(offset, readCheckSum); + + DEBUG_WM4(F("ChkCrR:CrCCsum="), String(checkSum, HEX), F(",CrRCsum="), String(readCheckSum, HEX)); + + if ( checkSum != readCheckSum) + { + return false; + } + + return true; + } + + bool EEPROM_getDynamicData(void) + { + int readCheckSum; + int checkSum = 0; + uint16_t offset = NINA_EEPROM_START + sizeof(WiFiNINA_config); + totalDataSize = sizeof(WiFiNINA_config) + sizeof(readCheckSum); - + for (int i = 0; i < NUM_MENU_ITEMS; i++) { char* _pointer = myMenuItems[i].pdata; @@ -538,24 +655,19 @@ class WiFiManager_NINA_Lite } return true; - } - - void EEPROM_put() + } + + void EEPROM_putDynamicData(void) { - uint16_t offset = EEPROM_START; - - EEPROM.put(offset, WiFiNINA_config); - - offset += sizeof(WiFiNINA_config); - int checkSum = 0; - + uint16_t offset = NINA_EEPROM_START + sizeof(WiFiNINA_config); + for (int i = 0; i < NUM_MENU_ITEMS; i++) { char* _pointer = myMenuItems[i].pdata; - DEBUG_WM4(F("pdata="), myMenuItems[i].pdata, F(",len="), myMenuItems[i].maxlen); - + //DEBUG_WM4(F("pdata="), myMenuItems[i].pdata, F(",len="), myMenuItems[i].maxlen); + for (uint16_t j = 0; j < myMenuItems[i].maxlen; j++,_pointer++,offset++) { EEPROM.write(offset, *_pointer); @@ -565,57 +677,101 @@ class WiFiManager_NINA_Lite } EEPROM.put(offset, checkSum); + //EEPROM.commit(); - DEBUG_WM2(F("CrCCsum="), checkSum); - } + DEBUG_WM2(F("CrCCSum="), checkSum); + } bool getConfigData() { - bool credDataValid; - - credDataValid = EEPROM_get(); + bool dynamicDataValid; + + hadConfigData = false; + + EEPROM.begin(); + DEBUG_WM2(F("EEPROMsz:"), EEPROM_SIZE); + EEPROM.get(NINA_EEPROM_START, WiFiNINA_config); + + DEBUG_WM1(F("======= Start Stored Config Data =======")); + displayConfigData(WiFiNINA_config); int calChecksum = calcChecksum(); - DEBUG_WM4(F("CCsum="), calChecksum, F(",RCsum="), WiFiNINA_config.checkSum); - - if ( (strncmp(WiFiNINA_config.header, WIFININA_BOARD_TYPE, strlen(WIFININA_BOARD_TYPE)) != 0) || - (calChecksum != WiFiNINA_config.checkSum) || !credDataValid ) + DEBUG_WM4(F("CCSum=0x"), String(calChecksum, HEX), + F(",RCSum=0x"), String(WiFiNINA_config.checkSum, HEX)); + + if (LOAD_DEFAULT_CONFIG_DATA) { - memset(&WiFiNINA_config, 0, sizeof(WiFiNINA_config)); - - for (int i = 0; i < NUM_MENU_ITEMS; i++) + // Load default dynamicData, if checkSum OK => valid data => load + // otherwise, use default in sketch and just assume it's OK + if (checkDynamicData()) { - // Actual size of pdata is [maxlen + 1] - memset(myMenuItems[i].pdata, 0, myMenuItems[i].maxlen + 1); + DEBUG_WM1(F("Valid Stored Dynamic Data")); + EEPROM_getDynamicData(); } - + else + { + DEBUG_WM1(F("Ignore invalid Stored Dynamic Data")); + } + + dynamicDataValid = true; + } + else + { + dynamicDataValid = EEPROM_getDynamicData(); + } + + if ( (strncmp(WiFiNINA_config.header, WIFININA_BOARD_TYPE, strlen(WIFININA_BOARD_TYPE)) != 0) || + (calChecksum != WiFiNINA_config.checkSum) || !dynamicDataValid ) + { // Including Credentials CSum - DEBUG_WM4(F("InitEEPROM,sz="), EEPROM.length(), F(",Datasz="), totalDataSize); - - // doesn't have any configuration + DEBUG_WM2(F("InitCfgFile,sz="), sizeof(WiFiNINA_config)); + + // doesn't have any configuration + if (LOAD_DEFAULT_CONFIG_DATA) + { + memcpy(&WiFiNINA_config, &defaultConfig, sizeof(WiFiNINA_config)); + } + else + { + memset(&WiFiNINA_config, 0, sizeof(WiFiNINA_config)); + + for (int i = 0; i < NUM_MENU_ITEMS; i++) + { + // Actual size of pdata is [maxlen + 1] + memset(myMenuItems[i].pdata, 0, myMenuItems[i].maxlen + 1); + } + + strcpy(WiFiNINA_config.WiFi_Creds[0].wifi_ssid, WM_NO_CONFIG); + strcpy(WiFiNINA_config.WiFi_Creds[0].wifi_pw, WM_NO_CONFIG); + strcpy(WiFiNINA_config.WiFi_Creds[1].wifi_ssid, WM_NO_CONFIG); + strcpy(WiFiNINA_config.WiFi_Creds[1].wifi_pw, WM_NO_CONFIG); + strcpy(WiFiNINA_config.board_name, WM_NO_CONFIG); + + for (int i = 0; i < NUM_MENU_ITEMS; i++) + { + strncpy(myMenuItems[i].pdata, WM_NO_CONFIG, myMenuItems[i].maxlen); + } + } + strcpy(WiFiNINA_config.header, WIFININA_BOARD_TYPE); - strcpy(WiFiNINA_config.WiFi_Creds[0].wifi_ssid, NO_CONFIG); - strcpy(WiFiNINA_config.WiFi_Creds[0].wifi_pw, NO_CONFIG); - strcpy(WiFiNINA_config.WiFi_Creds[1].wifi_ssid, NO_CONFIG); - strcpy(WiFiNINA_config.WiFi_Creds[1].wifi_pw, NO_CONFIG); for (int i = 0; i < NUM_MENU_ITEMS; i++) { - strncpy(myMenuItems[i].pdata, NO_CONFIG, myMenuItems[i].maxlen); + DEBUG_WM4(F("g:myMenuItems["), i, F("]="), myMenuItems[i].pdata ); } - + // Don't need WiFiNINA_config.checkSum = 0; - EEPROM_put(); - - return false; + saveConfigData(); + + return false; } - else if ( !strncmp(WiFiNINA_config.WiFi_Creds[0].wifi_ssid, NO_CONFIG, strlen(NO_CONFIG) ) || - !strncmp(WiFiNINA_config.WiFi_Creds[0].wifi_pw, NO_CONFIG, strlen(NO_CONFIG) ) || - !strncmp(WiFiNINA_config.WiFi_Creds[1].wifi_ssid, NO_CONFIG, strlen(NO_CONFIG) ) || - !strncmp(WiFiNINA_config.WiFi_Creds[1].wifi_pw, NO_CONFIG, strlen(NO_CONFIG) ) || + else if ( !strncmp(WiFiNINA_config.WiFi_Creds[0].wifi_ssid, WM_NO_CONFIG, strlen(WM_NO_CONFIG) ) || + !strncmp(WiFiNINA_config.WiFi_Creds[0].wifi_pw, WM_NO_CONFIG, strlen(WM_NO_CONFIG) ) || + !strncmp(WiFiNINA_config.WiFi_Creds[1].wifi_ssid, WM_NO_CONFIG, strlen(WM_NO_CONFIG) ) || + !strncmp(WiFiNINA_config.WiFi_Creds[1].wifi_pw, WM_NO_CONFIG, strlen(WM_NO_CONFIG) ) || !strlen(WiFiNINA_config.WiFi_Creds[0].wifi_ssid) || !strlen(WiFiNINA_config.WiFi_Creds[1].wifi_ssid) || !strlen(WiFiNINA_config.WiFi_Creds[0].wifi_pw) || @@ -626,7 +782,7 @@ class WiFiManager_NINA_Lite } else { - displayConfigData(); + displayConfigData(WiFiNINA_config); } return true; @@ -639,7 +795,9 @@ class WiFiManager_NINA_Lite DEBUG_WM6(F("SaveEEPROM,sz="), EEPROM.length(), F(",Datasz="), totalDataSize, F(",CSum="), calChecksum); - EEPROM_put(); + //EEPROM_put(); + EEPROM.put(NINA_EEPROM_START, WiFiNINA_config); + EEPROM_putDynamicData(); } bool connectMultiWiFi(int timeout) @@ -699,7 +857,7 @@ class WiFiManager_NINA_Lite void createHTML(String& root_html_template) { // -- HTML page fragments - root_html_template = "SAMD_WM_NINA_Lite
"; + root_html_template = "AVR_WM_NINA_Lite
"; String WIFININA_FLDSET_START = "
"; String WIFININA_FLDSET_END = "
"; @@ -708,7 +866,8 @@ class WiFiManager_NINA_Lite String WIFININA_HTML_SCRIPT = ""; const char WIFININA_HTML_END[] /*PROGMEM*/ = ""; /// + String IPAddressToString(IPAddress _address) { String str = String(_address[0]); @@ -197,7 +230,28 @@ class WiFiManager_NINA_Lite DEBUG_WM2(F("Hostname="), RFC952_hostname); ////// - if (getConfigData()) + //// New DRD //// + drd = new DoubleResetDetector_Generic(DRD_TIMEOUT, DRD_ADDRESS); + bool noConfigPortal = true; + + if (drd->detectDoubleReset()) + { + DEBUG_WM1(F("Double Reset Detected")); + + noConfigPortal = false; + } + //// New DRD //// + DEBUG_WM1(F("======= Start Default Config Data =======")); + displayConfigData(defaultConfig); + + hadConfigData = getConfigData(); + + DEBUG_WM1(noConfigPortal? F("bg: noConfigPortal = true") : F("bg: noConfigPortal = false")); + + //// New DRD //// + // noConfigPortal when getConfigData() OK and no DRD'ed + if (hadConfigData && noConfigPortal) + //// New DRD //// { hadConfigData = true; @@ -264,6 +318,14 @@ class WiFiManager_NINA_Lite static unsigned long checkstatus_timeout = 0; #define WIFI_STATUS_CHECK_INTERVAL 2000L + //// New DRD //// + // Call the double reset detector loop method every so often, + // so that it can recognise when the timeout expires. + // You can also call drd.stop() when you wish to no longer + // consider the next reset as a double reset. + drd->loop(); + //// New DRD //// + if ((millis() > checkstatus_timeout) || (checkstatus_timeout == 0)) { if (WiFi.status() == WL_CONNECTED) @@ -410,7 +472,8 @@ class WiFiManager_NINA_Lite memset(myMenuItems[i].pdata, 0, myMenuItems[i].maxlen + 1); } - dueFlashStorage_put(); + //dueFlashStorage_put(); + saveConfigData(); } bool isConfigDataValid(void) @@ -474,11 +537,12 @@ class WiFiManager_NINA_Lite return RFC952_hostname; } - void displayConfigData(void) + void displayConfigData(WiFiNINA_Configuration configData) { - DEBUG_WM6(F("Hdr="), WiFiNINA_config.header, F(",SSID="), WiFiNINA_config.WiFi_Creds[0].wifi_ssid, - F(",PW="), WiFiNINA_config.WiFi_Creds[0].wifi_pw); - DEBUG_WM4(F("SSID1="), WiFiNINA_config.WiFi_Creds[1].wifi_ssid, F(",PW1="), WiFiNINA_config.WiFi_Creds[1].wifi_pw); + DEBUG_WM6(F("Hdr="), configData.header, F(",SSID="), configData.WiFi_Creds[0].wifi_ssid, + F(",PW="), configData.WiFi_Creds[0].wifi_pw); + DEBUG_WM4(F("SSID1="), configData.WiFi_Creds[1].wifi_ssid, F(",PW1="), configData.WiFi_Creds[1].wifi_pw); + DEBUG_WM2(F("BName="), configData.board_name); for (int i = 0; i < NUM_MENU_ITEMS; i++) { @@ -493,7 +557,16 @@ class WiFiManager_NINA_Lite } #define WIFININA_BOARD_TYPE "WIFININA" -#define NO_CONFIG "blank" +#define WM_NO_CONFIG "blank" + +#ifndef EEPROM_START +#define EEPROM_START 0 +#warning EEPROM_START not defined. Set to 0 +#endif + +// Stating positon to store Blynk8266_WM_config +#define NINA_EEPROM_START (EEPROM_START + DRD_FLAG_DATA_SIZE) + int calcChecksum() { @@ -506,18 +579,105 @@ class WiFiManager_NINA_Lite return checkSum; } + bool checkDynamicData(void) + { + int checkSum = 0; + int readCheckSum; + + uint16_t byteCount = 0; + + //#define BUFFER_LEN 128 + //char readBuffer[BUFFER_LEN + 1]; + + #define BIG_BUFFER_LEN 768 + byte bigBuffer[BIG_BUFFER_LEN + 1]; + + uint16_t offset = NINA_EEPROM_START + sizeof(WiFiNINA_config); + + // Make address 4-byte aligned + if ( (offset % 4) != 0 ) + { + offset += 4 - (offset % 4); + } + + // Find the longest pdata, then dynamically allocate buffer. Remember to free when done + // This is used to store tempo data to calculate checksum to see of data is valid + // We dont like to destroy myMenuItems[i].pdata with invalid data + int totalLength = 0; + + for (int i = 0; i < NUM_MENU_ITEMS; i++) + { + totalLength += myMenuItems[i].maxlen; + + if ( (totalLength > BIG_BUFFER_LEN) || (myMenuItems[i].maxlen > BIG_BUFFER_LEN) ) + { + // Size too large, abort and flag false + DEBUG_WM1(F("ChkCrR: Error Small Buffer.")); + return false; + } + } + + // Both Buffers big enough, read all dynamicData to BigBuffer once + // as address need to be 4-byte aligned + byte* dataPointer = (byte* ) dueFlashStorageData.readAddress(offset); + + // Prepare buffer, more than enough + memset(bigBuffer, 0, sizeof(bigBuffer)); + memcpy(bigBuffer, dataPointer, sizeof(bigBuffer)); + + // Don't need readBuffer + // Now to split into individual piece to add to CSum + for (int i = 0; i < NUM_MENU_ITEMS; i++) + { + char* _pointer = (char*) bigBuffer; + + for (uint16_t j = 0; j < myMenuItems[i].maxlen; j++, _pointer++, byteCount++) + { + *_pointer = bigBuffer[byteCount]; + + checkSum += *_pointer; + } + } + + memcpy(&readCheckSum, &bigBuffer[byteCount], sizeof(readCheckSum)); + + DEBUG_WM4(F("ChkCrR:CrCCsum="), String(checkSum, HEX), F(",CrRCsum="), String(readCheckSum, HEX)); + + if ( checkSum != readCheckSum ) + { + return false; + } + + return true; + } + bool dueFlashStorage_get(void) { - uint16_t offset = EEPROM_START; + uint16_t offset = NINA_EEPROM_START; - WiFiNINA_Configuration* dataPointer = (WiFiNINA_Configuration* ) dueFlashStorage.readAddress(offset); + byte* dataPointer = (byte* ) dueFlashStorageData.readAddress(offset); + memcpy(&WiFiNINA_config, dataPointer, sizeof(WiFiNINA_config)); offset += sizeof(WiFiNINA_config); - + + // Make address 4-byte aligned + if ( (offset % 4) != 0 ) + { + offset += 4 - (offset % 4); + } + int checkSum = 0; int readCheckSum; + uint16_t byteCount = 0; + + byte buffer[768]; + + dataPointer = (byte* ) dueFlashStorageData.readAddress(offset); + + memcpy(buffer, dataPointer, sizeof(buffer)); + totalDataSize = sizeof(WiFiNINA_config) + sizeof(readCheckSum); for (int i = 0; i < NUM_MENU_ITEMS; i++) @@ -527,18 +687,20 @@ class WiFiManager_NINA_Lite // Actual size of pdata is [maxlen + 1] memset(myMenuItems[i].pdata, 0, myMenuItems[i].maxlen + 1); - - for (uint16_t j = 0; j < myMenuItems[i].maxlen; j++,_pointer++,offset++) + + for (uint16_t j = 0; j < myMenuItems[i].maxlen; j++,_pointer++, byteCount++) { - *_pointer = * (char*) dueFlashStorage.readAddress(offset); + *_pointer = buffer[byteCount]; checkSum += *_pointer; - } + } } - readCheckSum = * (int*) dueFlashStorage.readAddress(offset); + memcpy(&readCheckSum, &buffer[byteCount], sizeof(readCheckSum)); + + byteCount += sizeof(readCheckSum); - DEBUG_WM4(F("CrCCsum="), checkSum, F(",CrRCsum="), readCheckSum); + DEBUG_WM6(F("CrCCsum="), checkSum, F(",CrRCsum="), readCheckSum, F(",TotalDataSz="), totalDataSize); if ( checkSum != readCheckSum) { @@ -550,82 +712,142 @@ class WiFiManager_NINA_Lite void dueFlashStorage_put(void) { - uint16_t offset = EEPROM_START; + uint16_t offset = NINA_EEPROM_START; - dueFlashStorage.write(offset, (byte *) &WiFiNINA_config, sizeof(WiFiNINA_config)); + dueFlashStorageData.write(offset, (byte *) &WiFiNINA_config, sizeof(WiFiNINA_config)); offset += sizeof(WiFiNINA_config); - int checkSum = 0; - + // Make address 4-byte aligned + if ( (offset % 4) != 0 ) + { + offset += 4 - (offset % 4); + } + + int checkSum = 0; + uint16_t byteCount = 0; + + // Use 2K buffer, if need more memory, can reduce this + byte buffer[2048]; + for (int i = 0; i < NUM_MENU_ITEMS; i++) { char* _pointer = myMenuItems[i].pdata; - DEBUG_WM4(F("pdata="), myMenuItems[i].pdata, F(",len="), myMenuItems[i].maxlen); + //DEBUG_WM4(F("pdata="), myMenuItems[i].pdata, F(",len="), myMenuItems[i].maxlen); - for (uint16_t j = 0; j < myMenuItems[i].maxlen; j++,_pointer++,offset++) + for (uint16_t j = 0; j < myMenuItems[i].maxlen; j++, _pointer++, /*offset++,*/ byteCount++) { - dueFlashStorage.write(offset, *_pointer); + if (byteCount >= sizeof(buffer)) + { + DEBUG_WM2(F("Danger:dynamic data too long >"), sizeof(buffer)); + } + buffer[byteCount] = *_pointer; checkSum += *_pointer; } } + + memcpy(&buffer[byteCount], &checkSum, sizeof(checkSum)); - dueFlashStorage.write(offset, (byte *) &checkSum, sizeof(checkSum)); + byteCount += sizeof(checkSum); - DEBUG_WM2(F("CrCCSum="), checkSum); + dueFlashStorageData.write(offset, buffer, byteCount); + + DEBUG_WM4(F("CrCCSum="), checkSum, F(",byteCount="), byteCount); } bool getConfigData() { - bool credDataValid; + bool dynamicDataValid; - hadConfigData = false; - - credDataValid = dueFlashStorage_get(); + hadConfigData = false; + + // For DUE, DATA_LENGTH = ((IFLASH1_PAGE_SIZE/sizeof(byte))*4) = 1KBytes + DEBUG_WM2(F("Simulate EEPROM, sz:"), DATA_LENGTH); + + if (LOAD_DEFAULT_CONFIG_DATA) + { + // Load default dynamicData, if checkSum OK => valid data => load + // otherwise, use default in sketch and just assume it's OK + if (checkDynamicData()) + { + DEBUG_WM1(F("Valid Stored Dynamic Data")); + dueFlashStorage_get(); + } + else + { + DEBUG_WM1(F("Ignore invalid Stored Dynamic Data")); + } + + dynamicDataValid = true; + } + else + { + dynamicDataValid = dueFlashStorage_get(); + } + + DEBUG_WM1(F("======= Start Stored Config Data =======")); + displayConfigData(WiFiNINA_config); int calChecksum = calcChecksum(); - DEBUG_WM4(F("CCSum="), calChecksum, F(",RCSum="), WiFiNINA_config.checkSum); + DEBUG_WM4(F("CCSum=0x"), String(calChecksum, HEX), + F(",RCSum=0x"), String(WiFiNINA_config.checkSum, HEX)); if ( (strncmp(WiFiNINA_config.header, WIFININA_BOARD_TYPE, strlen(WIFININA_BOARD_TYPE)) != 0) || - (calChecksum != WiFiNINA_config.checkSum) || !credDataValid ) + (calChecksum != WiFiNINA_config.checkSum) || !dynamicDataValid ) { - memset(&WiFiNINA_config, 0, sizeof(WiFiNINA_config)); - - for (int i = 0; i < NUM_MENU_ITEMS; i++) + // Including Credentials CSum + DEBUG_WM2(F("InitCfgFile,sz="), sizeof(WiFiNINA_config)); + + // doesn't have any configuration + if (LOAD_DEFAULT_CONFIG_DATA) { - // Actual size of pdata is [maxlen + 1] - memset(myMenuItems[i].pdata, 0, myMenuItems[i].maxlen + 1); + memcpy(&WiFiNINA_config, &defaultConfig, sizeof(WiFiNINA_config)); } + else + { + memset(&WiFiNINA_config, 0, sizeof(WiFiNINA_config)); - // Including Credentials CSum - DEBUG_WM2(F("InitData,sz="), totalDataSize); - - // doesn't have any configuration + for (int i = 0; i < NUM_MENU_ITEMS; i++) + { + // Actual size of pdata is [maxlen + 1] + memset(myMenuItems[i].pdata, 0, myMenuItems[i].maxlen + 1); + } + + strcpy(WiFiNINA_config.WiFi_Creds[0].wifi_ssid, WM_NO_CONFIG); + strcpy(WiFiNINA_config.WiFi_Creds[0].wifi_pw, WM_NO_CONFIG); + strcpy(WiFiNINA_config.WiFi_Creds[1].wifi_ssid, WM_NO_CONFIG); + strcpy(WiFiNINA_config.WiFi_Creds[1].wifi_pw, WM_NO_CONFIG); + strcpy(WiFiNINA_config.board_name, WM_NO_CONFIG); + + for (int i = 0; i < NUM_MENU_ITEMS; i++) + { + strncpy(myMenuItems[i].pdata, WM_NO_CONFIG, myMenuItems[i].maxlen); + } + } + strcpy(WiFiNINA_config.header, WIFININA_BOARD_TYPE); - strcpy(WiFiNINA_config.WiFi_Creds[0].wifi_ssid, NO_CONFIG); - strcpy(WiFiNINA_config.WiFi_Creds[0].wifi_pw, NO_CONFIG); - strcpy(WiFiNINA_config.WiFi_Creds[1].wifi_ssid, NO_CONFIG); - strcpy(WiFiNINA_config.WiFi_Creds[1].wifi_pw, NO_CONFIG); for (int i = 0; i < NUM_MENU_ITEMS; i++) { - strncpy(myMenuItems[i].pdata, NO_CONFIG, myMenuItems[i].maxlen); + DEBUG_WM4(F("g:myMenuItems["), i, F("]="), myMenuItems[i].pdata ); } - + // Don't need WiFiNINA_config.checkSum = 0; - dueFlashStorage_put(); + saveConfigData(); + + //dueFlashStorage_put(); return false; } - else if ( !strncmp(WiFiNINA_config.WiFi_Creds[0].wifi_ssid, NO_CONFIG, strlen(NO_CONFIG) ) || - !strncmp(WiFiNINA_config.WiFi_Creds[0].wifi_pw, NO_CONFIG, strlen(NO_CONFIG) ) || - !strncmp(WiFiNINA_config.WiFi_Creds[1].wifi_ssid, NO_CONFIG, strlen(NO_CONFIG) ) || - !strncmp(WiFiNINA_config.WiFi_Creds[1].wifi_pw, NO_CONFIG, strlen(NO_CONFIG) ) || + else if ( !strncmp(WiFiNINA_config.WiFi_Creds[0].wifi_ssid, WM_NO_CONFIG, strlen(WM_NO_CONFIG) ) || + !strncmp(WiFiNINA_config.WiFi_Creds[0].wifi_pw, WM_NO_CONFIG, strlen(WM_NO_CONFIG) ) || + !strncmp(WiFiNINA_config.WiFi_Creds[1].wifi_ssid, WM_NO_CONFIG, strlen(WM_NO_CONFIG) ) || + !strncmp(WiFiNINA_config.WiFi_Creds[1].wifi_pw, WM_NO_CONFIG, strlen(WM_NO_CONFIG) ) || !strlen(WiFiNINA_config.WiFi_Creds[0].wifi_ssid) || !strlen(WiFiNINA_config.WiFi_Creds[1].wifi_ssid) || !strlen(WiFiNINA_config.WiFi_Creds[0].wifi_pw) || @@ -636,7 +858,7 @@ class WiFiManager_NINA_Lite } else { - displayConfigData(); + displayConfigData(WiFiNINA_config); } return true; @@ -757,10 +979,22 @@ class WiFiManager_NINA_Lite // Reset configTimeout to stay here until finished. configTimeout = 0; + if ( RFC952_hostname[0] != 0 ) + { + // Replace only if Hostname is valid + result.replace("SAM_DUE_WM_NINA_Lite", RFC952_hostname); + } + else if ( WiFiNINA_config.board_name[0] != 0 ) + { + // Or replace only if board_name is valid. Otherwise, keep intact + result.replace("SAM_DUE_WM_NINA_Lite", WiFiNINA_config.board_name); + } + result.replace("[[id]]", WiFiNINA_config.WiFi_Creds[0].wifi_ssid); result.replace("[[pw]]", WiFiNINA_config.WiFi_Creds[0].wifi_pw); result.replace("[[id1]]", WiFiNINA_config.WiFi_Creds[1].wifi_ssid); result.replace("[[pw1]]", WiFiNINA_config.WiFi_Creds[1].wifi_pw); + result.replace("[[nm]]", WiFiNINA_config.board_name); for (int i = 0; i < NUM_MENU_ITEMS; i++) { @@ -811,6 +1045,14 @@ class WiFiManager_NINA_Lite else strncpy(WiFiNINA_config.WiFi_Creds[1].wifi_pw, value.c_str(), sizeof(WiFiNINA_config.WiFi_Creds[1].wifi_pw) - 1); } + else if (key == "nm") + { + number_items_Updated++; + if (strlen(value.c_str()) < sizeof(WiFiNINA_config.board_name) - 1) + strcpy(WiFiNINA_config.board_name, value.c_str()); + else + strncpy(WiFiNINA_config.board_name, value.c_str(), sizeof(WiFiNINA_config.board_name) - 1); + } for (int i = 0; i < NUM_MENU_ITEMS; i++) { diff --git a/src/WiFiManager_NINA_Lite_Debug.h b/src/WiFiManager_NINA_Lite_Debug.h index ed7f2dbf..08c8ed83 100644 --- a/src/WiFiManager_NINA_Lite_Debug.h +++ b/src/WiFiManager_NINA_Lite_Debug.h @@ -2,13 +2,13 @@ WiFiManager_NINA_Lite_Debug.h For Mega, Teensy, SAM DUE, SAMD and STM32 boards using WiFiNINA modules/shields - WiFiManager_NINA_WM_Lite is a library for the Mega, Teensy, SAM DUE, SAMD and STM32 boards (https://github.com/khoih-prog/WiFiManager_NINA_Lite) - to enable store Credentials in EEPROM to easy configuration/reconfiguration and autoconnect/autoreconnect of WiFi and other services - without Hardcoding. + WiFiManager_NINA_WM_Lite is a library for the Mega, Teensy, SAM DUE, SAMD and STM32 boards + (https://github.com/khoih-prog/WiFiManager_NINA_Lite) to enable store Credentials in EEPROM/LittleFS for easy + configuration/reconfiguration and autoconnect/autoreconnect of WiFi and other services without Hardcoding. Built by Khoi Hoang https://github.com/khoih-prog/WiFiManager_NINA_Lite Licensed under MIT license - Version: 1.0.3 + Version: 1.0.4 Version Modified By Date Comments ------- ----------- ---------- ----------- @@ -17,6 +17,7 @@ 1.0.2 K Hoang 15/04/2020 Fix bug. Add SAMD51 support. 1.0.3 K Hoang 24/04/2020 Fix bug. Add nRF5 (Adafruit, NINA_B302_ublox, etc.) support. Add MultiWiFi, HostName capability. SSID password maxlen is 63 now. Permit special chars # and % in input data. + 1.0.4 K Hoang 04/05/2020 Add Configurable Config Portal Title, Default Config Data and DRD. Update examples. *****************************************************************************************************************************/ #ifndef WiFiManager_NINA_Lite_Debug_h diff --git a/src/WiFiManager_NINA_Lite_NRF52840.h b/src/WiFiManager_NINA_Lite_NRF52840.h index 9801c20a..8b15ae36 100644 --- a/src/WiFiManager_NINA_Lite_NRF52840.h +++ b/src/WiFiManager_NINA_Lite_NRF52840.h @@ -1,14 +1,14 @@ /**************************************************************************************************************************** WiFiManager_NINA_Lite_NRF52840.h - For SAMD boards using WiFiNINA modules/shields, using much less code to support boards with smaller memory + For nRF52 boards using WiFiNINA modules/shields, using much less code to support boards with smaller memory - WiFiManager_NINA_WM_Lite is a library for the Mega, Teensy, SAM DUE, SAMD and STM32 boards (https://github.com/khoih-prog/WiFiManager_NINA_Lite) - to enable store Credentials in EEPROM to easy configuration/reconfiguration and autoconnect/autoreconnect of WiFi and other services - without Hardcoding. + WiFiManager_NINA_WM_Lite is a library for the Mega, Teensy, SAM DUE, SAMD and STM32 boards + (https://github.com/khoih-prog/WiFiManager_NINA_Lite) to enable store Credentials in EEPROM/LittleFS for easy + configuration/reconfiguration and autoconnect/autoreconnect of WiFi and other services without Hardcoding. Built by Khoi Hoang https://github.com/khoih-prog/WiFiManager_NINA_Lite Licensed under MIT license - Version: 1.0.3 + Version: 1.0.4 Version Modified By Date Comments ------- ----------- ---------- ----------- @@ -17,6 +17,7 @@ 1.0.2 K Hoang 15/04/2020 Fix bug. Add SAMD51 support. 1.0.3 K Hoang 24/04/2020 Fix bug. Add nRF5 (Adafruit, NINA_B302_ublox, etc.) support. Add MultiWiFi, HostName capability. SSID password maxlen is 63 now. Permit special chars # and % in input data. + 1.0.4 K Hoang 04/05/2020 Add Configurable Config Portal Title, Default Config Data and DRD. Update examples. *****************************************************************************************************************************/ #ifndef WiFiManager_NINA_Lite_NRF52840_h @@ -35,11 +36,40 @@ #endif #include -// Include EEPROM-like API for FlashStorage -//#include //https://github.com/cmaglie/FlashStorage -//#include //https://github.com/khoih-prog/FlashStorage_SAMD + +//Use LittleFS for nRF52 +#include +#include + +using namespace Adafruit_LittleFS_Namespace; +File file(InternalFS); + #include +///////// NEW for DRD ///////////// +// These defines must be put before #include +// to select where to store DoubleResetDetector_Generic's variable. +// Otherwise, library will use default EEPROM storage +#define DRD_FLAG_DATA_SIZE 4 + +#ifndef DOUBLERESETDETECTOR_DEBUG +#define DOUBLERESETDETECTOR_DEBUG false +#endif + +#include //https://github.com/khoih-prog/DoubleResetDetector_Generic + +// 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_Generic to use +#define DRD_ADDRESS 0 + +DoubleResetDetector_Generic* drd; + +///////// NEW for DRD ///////////// + + //NEW #define MAX_ID_LEN 5 #define MAX_DISPLAY_NAME_LEN 16 @@ -71,26 +101,33 @@ typedef struct #define NUM_WIFI_CREDENTIALS 2 -// Configurable items besides fixed Header -#define NUM_CONFIGURABLE_ITEMS ( 2 * NUM_WIFI_CREDENTIALS ) +// Configurable items besides fixed Header, just add board_name +#define NUM_CONFIGURABLE_ITEMS ( ( 2 * NUM_WIFI_CREDENTIALS ) + 1 ) //////////////// typedef struct Configuration { char header [16]; WiFi_Credentials WiFi_Creds [NUM_WIFI_CREDENTIALS]; + char board_name [24]; int checkSum; } WiFiNINA_Configuration; -// Currently CONFIG_DATA_SIZE = 212 = (16 + 96 * 2 + 4) +// Currently CONFIG_DATA_SIZE = 236 = (16 + 96 * 2 + 4 + 24) uint16_t CONFIG_DATA_SIZE = sizeof(WiFiNINA_Configuration); +///New from v1.0.4 +extern bool LOAD_DEFAULT_CONFIG_DATA; +extern WiFiNINA_Configuration defaultConfig; + // -- HTML page fragments const char WIFININA_HTML_HEAD[] /*PROGMEM*/ = "nRF52_WM_NINA_Lite
\
\
\
\ -
"; +
\ +
"; + const char WIFININA_FLDSET_START[] /*PROGMEM*/ = "
"; const char WIFININA_FLDSET_END[] /*PROGMEM*/ = "
"; const char WIFININA_HTML_PARAM[] /*PROGMEM*/ = "
"; @@ -99,7 +136,8 @@ const char WIFININA_HTML_SCRIPT[] /*PROGMEM*/ = ""; const char WIFININA_HTML_END[] /*PROGMEM*/ = ""; /// + String IPAddressToString(IPAddress _address) { String str = String(_address[0]); @@ -198,7 +231,28 @@ class WiFiManager_NINA_Lite DEBUG_WM2(F("Hostname="), RFC952_hostname); ////// - if (getConfigData()) + //// New DRD //// + drd = new DoubleResetDetector_Generic(DRD_TIMEOUT, DRD_ADDRESS); + bool noConfigPortal = true; + + if (drd->detectDoubleReset()) + { + DEBUG_WM1(F("Double Reset Detected")); + + noConfigPortal = false; + } + //// New DRD //// + DEBUG_WM1(F("======= Start Default Config Data =======")); + displayConfigData(defaultConfig); + + hadConfigData = getConfigData(); + + DEBUG_WM1(noConfigPortal? F("bg: noConfigPortal = true") : F("bg: noConfigPortal = false")); + + //// New DRD //// + // noConfigPortal when getConfigData() OK and no DRD'ed + if (hadConfigData && noConfigPortal) + //// New DRD //// { hadConfigData = true; @@ -265,6 +319,14 @@ class WiFiManager_NINA_Lite static unsigned long checkstatus_timeout = 0; #define WIFI_STATUS_CHECK_INTERVAL 2000L + //// New DRD //// + // Call the double reset detector loop method every so often, + // so that it can recognise when the timeout expires. + // You can also call drd.stop() when you wish to no longer + // consider the next reset as a double reset. + drd->loop(); + //// New DRD //// + if ((millis() > checkstatus_timeout) || (checkstatus_timeout == 0)) { if (WiFi.status() == WL_CONNECTED) @@ -411,7 +473,8 @@ class WiFiManager_NINA_Lite memset(myMenuItems[i].pdata, 0, myMenuItems[i].maxlen + 1); } - EEPROM_put(); + //EEPROM_put(); + saveConfigData(); } void resetFunc() @@ -492,11 +555,12 @@ class WiFiManager_NINA_Lite return RFC952_hostname; } - void displayConfigData(void) + void displayConfigData(WiFiNINA_Configuration configData) { - DEBUG_WM6(F("Hdr="), WiFiNINA_config.header, F(",SSID="), WiFiNINA_config.WiFi_Creds[0].wifi_ssid, - F(",PW="), WiFiNINA_config.WiFi_Creds[0].wifi_pw); - DEBUG_WM4(F("SSID1="), WiFiNINA_config.WiFi_Creds[1].wifi_ssid, F(",PW1="), WiFiNINA_config.WiFi_Creds[1].wifi_pw); + DEBUG_WM6(F("Hdr="), configData.header, F(",SSID="), configData.WiFi_Creds[0].wifi_ssid, + F(",PW="), configData.WiFi_Creds[0].wifi_pw); + DEBUG_WM4(F("SSID1="), configData.WiFi_Creds[1].wifi_ssid, F(",PW1="), configData.WiFi_Creds[1].wifi_pw); + DEBUG_WM2(F("BName="), configData.board_name); for (int i = 0; i < NUM_MENU_ITEMS; i++) { @@ -511,7 +575,24 @@ class WiFiManager_NINA_Lite } #define WIFININA_BOARD_TYPE "WIFININA" -#define NO_CONFIG "blank" +#define WM_NO_CONFIG "blank" + +// DRD_FLAG_DATA_SIZE is 4, to store DRD flag, defined in DRD +#if (EEPROM_SIZE < DRD_FLAG_DATA_SIZE + CONFIG_DATA_SIZE) +#error EEPROM_SIZE must be > CONFIG_DATA_SIZE. +#endif + +#ifndef EEPROM_START +#define EEPROM_START 0 +#warning EEPROM_START not defined. Set to 0 +#else +#if (EEPROM_START + DRD_FLAG_DATA_SIZE + CONFIG_DATA_SIZE > EEPROM_SIZE) +#error EPROM_START + DRD_FLAG_DATA_SIZE + CONFIG_DATA_SIZE > EEPROM_SIZE. Please adjust. +#endif +#endif + +// Stating positon to store Blynk8266_WM_config +#define NINA_EEPROM_START (EEPROM_START + DRD_FLAG_DATA_SIZE) int calcChecksum() { @@ -524,12 +605,51 @@ class WiFiManager_NINA_Lite return checkSum; } + bool checkDynamicData(void) + { + // It's too bad that emulate EEPROM.read()/write() can only deal with bytes. + // Have to read/write each byte. To rewrite the library + + int checkSum = 0; + int readCheckSum; + + uint16_t offset = NINA_EEPROM_START + sizeof(WiFiNINA_config); + + // Find the longest pdata, then dynamically allocate buffer. Remember to free when done + // This is used to store tempo data to calculate checksum to see of data is valid + // We dont like to destroy myMenuItems[i].pdata with invalid data + + for (int i = 0; i < NUM_MENU_ITEMS; i++) + { + for (uint16_t j = 0; j < myMenuItems[i].maxlen; j++, offset++) + { + checkSum += EEPROM.read(offset); + } + } + + uint8_t* _pointer = (uint8_t *) &readCheckSum; + + for (int i = 0; i < sizeof(readCheckSum); i++, _pointer++, offset++) + { + *_pointer = EEPROM.read(offset); + } + + DEBUG_WM4(F("ChkCrR:CrCCsum="), String(checkSum, HEX), F(",CrRCsum="), String(readCheckSum, HEX)); + + if ( checkSum != readCheckSum) + { + return false; + } + + return true; + } + bool EEPROM_get() { - // It's too bad that emulate EEPROM.read()/writ() can only deal with bytes. + // It's too bad that emulate EEPROM.read()/write() can only deal with bytes. // Have to read/write each byte. To rewrite the library - uint16_t offset = EEPROM_START; + uint16_t offset = NINA_EEPROM_START; uint8_t* _pointer = (uint8_t *) &WiFiNINA_config; @@ -580,7 +700,7 @@ class WiFiManager_NINA_Lite // It's too bad that emulate EEPROM.read()/writ() can only deal with bytes. // Have to read/write each byte. To rewrite the library - uint16_t offset = EEPROM_START; + uint16_t offset = NINA_EEPROM_START; uint8_t* _pointer = (uint8_t *) &WiFiNINA_config; @@ -595,7 +715,7 @@ class WiFiManager_NINA_Lite { _pointer = (uint8_t *) myMenuItems[i].pdata; - DEBUG_WM4(F("pdata="), myMenuItems[i].pdata, F(",len="), myMenuItems[i].maxlen); + //DEBUG_WM4(F("pdata="), myMenuItems[i].pdata, F(",len="), myMenuItems[i].maxlen); for (uint16_t j = 0; j < myMenuItems[i].maxlen; j++,_pointer++,offset++) { @@ -615,57 +735,94 @@ class WiFiManager_NINA_Lite EEPROM.commit(); DEBUG_WM2(F("CrCCSum="), checkSum); - } + } bool getConfigData() { - bool credDataValid; + bool dynamicDataValid; - hadConfigData = false; + hadConfigData = false; - credDataValid = EEPROM_get(); + if (LOAD_DEFAULT_CONFIG_DATA) + { + // Load default dynamicData, if checkSum OK => valid data => load + // otherwise, use default in sketch and just assume it's OK + if (checkDynamicData()) + { + DEBUG_WM1(F("Valid Stored Dynamic Data")); + EEPROM_get(); + } + else + { + DEBUG_WM1(F("Ignore invalid Stored Dynamic Data")); + } + + dynamicDataValid = true; + } + else + { + dynamicDataValid = EEPROM_get(); + } + + DEBUG_WM1(F("======= Start Stored Config Data =======")); + displayConfigData(WiFiNINA_config); int calChecksum = calcChecksum(); - DEBUG_WM4(F("CCSum="), calChecksum, F(",RCSum="), WiFiNINA_config.checkSum); + DEBUG_WM4(F("CCSum=0x"), String(calChecksum, HEX), + F(",RCSum=0x"), String(WiFiNINA_config.checkSum, HEX)); if ( (strncmp(WiFiNINA_config.header, WIFININA_BOARD_TYPE, strlen(WIFININA_BOARD_TYPE)) != 0) || - (calChecksum != WiFiNINA_config.checkSum) || !credDataValid ) + (calChecksum != WiFiNINA_config.checkSum) || !dynamicDataValid ) { - memset(&WiFiNINA_config, 0, sizeof(WiFiNINA_config)); - - for (int i = 0; i < NUM_MENU_ITEMS; i++) + // Including Credentials CSum + DEBUG_WM2(F("InitCfgFile,sz="), sizeof(WiFiNINA_config)); + + // doesn't have any configuration + if (LOAD_DEFAULT_CONFIG_DATA) { - // Actual size of pdata is [maxlen + 1] - memset(myMenuItems[i].pdata, 0, myMenuItems[i].maxlen + 1); + memcpy(&WiFiNINA_config, &defaultConfig, sizeof(WiFiNINA_config)); } + else + { + memset(&WiFiNINA_config, 0, sizeof(WiFiNINA_config)); - // Including Credentials CSum - DEBUG_WM4(F("InitEEPROM,sz="), EEPROM.length(), F(",Datasz="), totalDataSize); - - // doesn't have any configuration + for (int i = 0; i < NUM_MENU_ITEMS; i++) + { + // Actual size of pdata is [maxlen + 1] + memset(myMenuItems[i].pdata, 0, myMenuItems[i].maxlen + 1); + } + + strcpy(WiFiNINA_config.WiFi_Creds[0].wifi_ssid, WM_NO_CONFIG); + strcpy(WiFiNINA_config.WiFi_Creds[0].wifi_pw, WM_NO_CONFIG); + strcpy(WiFiNINA_config.WiFi_Creds[1].wifi_ssid, WM_NO_CONFIG); + strcpy(WiFiNINA_config.WiFi_Creds[1].wifi_pw, WM_NO_CONFIG); + strcpy(WiFiNINA_config.board_name, WM_NO_CONFIG); + + for (int i = 0; i < NUM_MENU_ITEMS; i++) + { + strncpy(myMenuItems[i].pdata, WM_NO_CONFIG, myMenuItems[i].maxlen); + } + } + strcpy(WiFiNINA_config.header, WIFININA_BOARD_TYPE); - strcpy(WiFiNINA_config.WiFi_Creds[0].wifi_ssid, NO_CONFIG); - strcpy(WiFiNINA_config.WiFi_Creds[0].wifi_pw, NO_CONFIG); - strcpy(WiFiNINA_config.WiFi_Creds[1].wifi_ssid, NO_CONFIG); - strcpy(WiFiNINA_config.WiFi_Creds[1].wifi_pw, NO_CONFIG); for (int i = 0; i < NUM_MENU_ITEMS; i++) { - strncpy(myMenuItems[i].pdata, NO_CONFIG, myMenuItems[i].maxlen); + DEBUG_WM4(F("g:myMenuItems["), i, F("]="), myMenuItems[i].pdata ); } - + // Don't need WiFiNINA_config.checkSum = 0; - EEPROM_put(); - - return false; + saveConfigData(); + + return false; } - else if ( !strncmp(WiFiNINA_config.WiFi_Creds[0].wifi_ssid, NO_CONFIG, strlen(NO_CONFIG) ) || - !strncmp(WiFiNINA_config.WiFi_Creds[0].wifi_pw, NO_CONFIG, strlen(NO_CONFIG) ) || - !strncmp(WiFiNINA_config.WiFi_Creds[1].wifi_ssid, NO_CONFIG, strlen(NO_CONFIG) ) || - !strncmp(WiFiNINA_config.WiFi_Creds[1].wifi_pw, NO_CONFIG, strlen(NO_CONFIG) ) || + else if ( !strncmp(WiFiNINA_config.WiFi_Creds[0].wifi_ssid, WM_NO_CONFIG, strlen(WM_NO_CONFIG) ) || + !strncmp(WiFiNINA_config.WiFi_Creds[0].wifi_pw, WM_NO_CONFIG, strlen(WM_NO_CONFIG) ) || + !strncmp(WiFiNINA_config.WiFi_Creds[1].wifi_ssid, WM_NO_CONFIG, strlen(WM_NO_CONFIG) ) || + !strncmp(WiFiNINA_config.WiFi_Creds[1].wifi_pw, WM_NO_CONFIG, strlen(WM_NO_CONFIG) ) || !strlen(WiFiNINA_config.WiFi_Creds[0].wifi_ssid) || !strlen(WiFiNINA_config.WiFi_Creds[1].wifi_ssid) || !strlen(WiFiNINA_config.WiFi_Creds[0].wifi_pw) || @@ -676,7 +833,7 @@ class WiFiManager_NINA_Lite } else { - displayConfigData(); + displayConfigData(WiFiNINA_config); } return true; @@ -797,10 +954,22 @@ class WiFiManager_NINA_Lite // Reset configTimeout to stay here until finished. configTimeout = 0; + if ( RFC952_hostname[0] != 0 ) + { + // Replace only if Hostname is valid + result.replace("SAMD_WM_NINA_Lite", RFC952_hostname); + } + else if ( WiFiNINA_config.board_name[0] != 0 ) + { + // Or replace only if board_name is valid. Otherwise, keep intact + result.replace("SAMD_WM_NINA_Lite", WiFiNINA_config.board_name); + } + result.replace("[[id]]", WiFiNINA_config.WiFi_Creds[0].wifi_ssid); result.replace("[[pw]]", WiFiNINA_config.WiFi_Creds[0].wifi_pw); result.replace("[[id1]]", WiFiNINA_config.WiFi_Creds[1].wifi_ssid); result.replace("[[pw1]]", WiFiNINA_config.WiFi_Creds[1].wifi_pw); + result.replace("[[nm]]", WiFiNINA_config.board_name); for (int i = 0; i < NUM_MENU_ITEMS; i++) { @@ -851,6 +1020,14 @@ class WiFiManager_NINA_Lite else strncpy(WiFiNINA_config.WiFi_Creds[1].wifi_pw, value.c_str(), sizeof(WiFiNINA_config.WiFi_Creds[1].wifi_pw) - 1); } + else if (key == "nm") + { + number_items_Updated++; + if (strlen(value.c_str()) < sizeof(WiFiNINA_config.board_name) - 1) + strcpy(WiFiNINA_config.board_name, value.c_str()); + else + strncpy(WiFiNINA_config.board_name, value.c_str(), sizeof(WiFiNINA_config.board_name) - 1); + } for (int i = 0; i < NUM_MENU_ITEMS; i++) { diff --git a/src/WiFiManager_NINA_Lite_STM32.h b/src/WiFiManager_NINA_Lite_STM32.h index 11c77962..38aee92a 100644 --- a/src/WiFiManager_NINA_Lite_STM32.h +++ b/src/WiFiManager_NINA_Lite_STM32.h @@ -2,13 +2,13 @@ WiFiManager_NINA_Lite_STM32.h For STM32 boards using WiFiNINA modules/shields, using much less code to support boards with smaller memory - WiFiManager_NINA_WM_Lite is a library for the Mega, Teensy, SAM DUE, SAMD and STM32 boards (https://github.com/khoih-prog/WiFiManager_NINA_Lite) - to enable store Credentials in EEPROM to easy configuration/reconfiguration and autoconnect/autoreconnect of WiFi and other services - without Hardcoding. + WiFiManager_NINA_WM_Lite is a library for the Mega, Teensy, SAM DUE, SAMD and STM32 boards + (https://github.com/khoih-prog/WiFiManager_NINA_Lite) to enable store Credentials in EEPROM/LittleFS for easy + configuration/reconfiguration and autoconnect/autoreconnect of WiFi and other services without Hardcoding. Built by Khoi Hoang https://github.com/khoih-prog/WiFiManager_NINA_Lite Licensed under MIT license - Version: 1.0.3 + Version: 1.0.4 Version Modified By Date Comments ------- ----------- ---------- ----------- @@ -17,6 +17,7 @@ 1.0.2 K Hoang 15/04/2020 Fix bug. Add SAMD51 support. 1.0.3 K Hoang 24/04/2020 Fix bug. Add nRF5 (Adafruit, NINA_B302_ublox, etc.) support. Add MultiWiFi, HostName capability. SSID password maxlen is 63 now. Permit special chars # and % in input data. + 1.0.4 K Hoang 04/05/2020 Add Configurable Config Portal Title, Default Config Data and DRD. Update examples. *****************************************************************************************************************************/ #ifndef WiFiManager_NINA_Lite_STM32_h @@ -37,6 +38,29 @@ #include #include +///////// NEW for DRD ///////////// +// These defines must be put before #include +// to select where to store DoubleResetDetector_Generic's variable. +// Otherwise, library will use default EEPROM storage +#define DRD_FLAG_DATA_SIZE 4 + +#ifndef DRD_GENERIC_DEBUG +#define DRD_GENERIC_DEBUG false +#endif + +#include //https://github.com/khoih-prog/DoubleResetDetector_Generic + +// 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_Generic to use +#define DRD_ADDRESS 0 + +DoubleResetDetector_Generic* drd; + +///////// NEW for DRD ///////////// + //NEW #define MAX_ID_LEN 5 #define MAX_DISPLAY_NAME_LEN 16 @@ -69,25 +93,32 @@ typedef struct #define NUM_WIFI_CREDENTIALS 2 // Configurable items besides fixed Header -#define NUM_CONFIGURABLE_ITEMS ( 2 * NUM_WIFI_CREDENTIALS ) +#define NUM_CONFIGURABLE_ITEMS ( ( 2 * NUM_WIFI_CREDENTIALS ) + 1 ) //////////////// typedef struct Configuration { char header [16]; WiFi_Credentials WiFi_Creds [NUM_WIFI_CREDENTIALS]; + char board_name [24]; int checkSum; } WiFiNINA_Configuration; -// Currently CONFIG_DATA_SIZE = 212 = (16 + 96 * 2 + 4) +// Currently CONFIG_DATA_SIZE = 236 = (16 + 96 * 2 + 4 + 24) uint16_t CONFIG_DATA_SIZE = sizeof(WiFiNINA_Configuration); +///New from v1.0.4 +extern bool LOAD_DEFAULT_CONFIG_DATA; +extern WiFiNINA_Configuration defaultConfig; + // -- HTML page fragments const char WIFININA_HTML_HEAD[] /*PROGMEM*/ = "STM32_WM_NINA_Lite
\
\
\
\ -
"; +
\ +
"; + const char WIFININA_FLDSET_START[] /*PROGMEM*/ = "
"; const char WIFININA_FLDSET_END[] /*PROGMEM*/ = "
"; const char WIFININA_HTML_PARAM[] /*PROGMEM*/ = "
"; @@ -96,12 +127,14 @@ const char WIFININA_HTML_SCRIPT[] /*PROGMEM*/ = ""; const char WIFININA_HTML_END[] /*PROGMEM*/ = ""; /// + String IPAddressToString(IPAddress _address) { String str = String(_address[0]); @@ -186,7 +219,28 @@ class WiFiManager_NINA_Lite DEBUG_WM2(F("Hostname="), RFC952_hostname); ////// - if (getConfigData()) + //// New DRD //// + drd = new DoubleResetDetector_Generic(DRD_TIMEOUT, DRD_ADDRESS); + bool noConfigPortal = true; + + if (drd->detectDoubleReset()) + { + DEBUG_WM1(F("Double Reset Detected")); + + noConfigPortal = false; + } + //// New DRD //// + DEBUG_WM1(F("======= Start Default Config Data =======")); + displayConfigData(defaultConfig); + + hadConfigData = getConfigData(); + + DEBUG_WM1(noConfigPortal? F("bg: noConfigPortal = true") : F("bg: noConfigPortal = false")); + + //// New DRD //// + // noConfigPortal when getConfigData() OK and no DRD'ed + if (hadConfigData && noConfigPortal) + //// New DRD //// { hadConfigData = true; @@ -253,6 +307,14 @@ class WiFiManager_NINA_Lite static unsigned long checkstatus_timeout = 0; #define WIFI_STATUS_CHECK_INTERVAL 2000L + //// New DRD //// + // Call the double reset detector loop method every so often, + // so that it can recognise when the timeout expires. + // You can also call drd.stop() when you wish to no longer + // consider the next reset as a double reset. + drd->loop(); + //// New DRD //// + if ((millis() > checkstatus_timeout) || (checkstatus_timeout == 0)) { if (WiFi.status() == WL_CONNECTED) @@ -399,7 +461,8 @@ class WiFiManager_NINA_Lite memset(myMenuItems[i].pdata, 0, myMenuItems[i].maxlen + 1); } - EEPROM_put(); + //EEPROM_put(); + saveConfigData(); } bool isConfigDataValid(void) @@ -468,11 +531,12 @@ class WiFiManager_NINA_Lite return RFC952_hostname; } - void displayConfigData(void) + void displayConfigData(WiFiNINA_Configuration configData) { - DEBUG_WM6(F("Hdr="), WiFiNINA_config.header, F(",SSID="), WiFiNINA_config.WiFi_Creds[0].wifi_ssid, - F(",PW="), WiFiNINA_config.WiFi_Creds[0].wifi_pw); - DEBUG_WM4(F("SSID1="), WiFiNINA_config.WiFi_Creds[1].wifi_ssid, F(",PW1="), WiFiNINA_config.WiFi_Creds[1].wifi_pw); + DEBUG_WM6(F("Hdr="), configData.header, F(",SSID="), configData.WiFi_Creds[0].wifi_ssid, + F(",PW="), configData.WiFi_Creds[0].wifi_pw); + DEBUG_WM4(F("SSID1="), configData.WiFi_Creds[1].wifi_ssid, F(",PW1="), configData.WiFi_Creds[1].wifi_pw); + DEBUG_WM2(F("BName="), configData.board_name); for (int i = 0; i < NUM_MENU_ITEMS; i++) { @@ -487,7 +551,7 @@ class WiFiManager_NINA_Lite } #define WIFININA_BOARD_TYPE "WIFININA" -#define NO_CONFIG "blank" +#define WM_NO_CONFIG "blank" //#define EEPROM_SIZE E2END //#define EEPROM_SIZE 512 @@ -544,13 +608,17 @@ class WiFiManager_NINA_Lite #endif #ifndef EEPROM_START -#define EEPROM_START 0 +#define EEPROM_START 0 //define 256 in DRD +#warning EEPROM_START not defined. Set to 0 #else -#if (EEPROM_START + CONFIG_DATA_SIZE > EEPROM_SIZE) -#error EPROM_START + CONFIG_DATA_SIZE > EEPROM_SIZE. Please adjust. +#if (EEPROM_START + DRD_FLAG_DATA_SIZE + CONFIG_DATA_SIZE > EEPROM_SIZE) +#error EPROM_START + DRD_FLAG_DATA_SIZE + CONFIG_DATA_SIZE > EEPROM_SIZE. Please adjust. #endif #endif +// Stating positon to store Blynk8266_WM_config +#define NINA_EEPROM_START (EEPROM_START + DRD_FLAG_DATA_SIZE) + int calcChecksum() { int checkSum = 0; @@ -562,19 +630,72 @@ class WiFiManager_NINA_Lite return checkSum; } - bool EEPROM_get() + bool checkDynamicData(void) { - uint16_t offset = EEPROM_START; + int checkSum = 0; + int readCheckSum; - EEPROM.get(offset, WiFiNINA_config); + #define BUFFER_LEN 128 + char readBuffer[BUFFER_LEN + 1]; - offset += sizeof(WiFiNINA_config); + uint16_t offset = NINA_EEPROM_START + sizeof(WiFiNINA_config); + + // Find the longest pdata, then dynamically allocate buffer. Remember to free when done + // This is used to store tempo data to calculate checksum to see of data is valid + // We dont like to destroy myMenuItems[i].pdata with invalid data - int checkSum = 0; - int readCheckSum; + for (int i = 0; i < NUM_MENU_ITEMS; i++) + { + if (myMenuItems[i].maxlen > BUFFER_LEN) + { + // Size too large, abort and flag false + DEBUG_WM1(F("ChkCrR: Error Small Buffer.")); + return false; + } + } + + for (int i = 0; i < NUM_MENU_ITEMS; i++) + { + char* _pointer = readBuffer; + + // Prepare buffer, more than enough + memset(readBuffer, 0, sizeof(readBuffer)); + + // Read more than necessary, but OK and easier to code + EEPROM.get(offset, readBuffer); + // NULL terminated + readBuffer[myMenuItems[i].maxlen] = 0; + + DEBUG_WM4(F("ChkCrR:pdata="), readBuffer, F(",len="), myMenuItems[i].maxlen); + + for (uint16_t j = 0; j < myMenuItems[i].maxlen; j++,_pointer++) + { + checkSum += *_pointer; + } + + offset += myMenuItems[i].maxlen; + } + + EEPROM.get(offset, readCheckSum); + + DEBUG_WM4(F("ChkCrR:CrCCsum="), String(checkSum, HEX), F(",CrRCsum="), String(readCheckSum, HEX)); + + if ( checkSum != readCheckSum) + { + return false; + } + return true; + } + + bool EEPROM_getDynamicData(void) + { + int readCheckSum; + int checkSum = 0; + uint16_t offset = NINA_EEPROM_START + sizeof(WiFiNINA_config); + totalDataSize = sizeof(WiFiNINA_config) + sizeof(readCheckSum); - + for (int i = 0; i < NUM_MENU_ITEMS; i++) { char* _pointer = myMenuItems[i].pdata; @@ -601,24 +722,19 @@ class WiFiManager_NINA_Lite } return true; - } - - void EEPROM_put() + } + + void EEPROM_putDynamicData(void) { - uint16_t offset = EEPROM_START; - - EEPROM.put(offset, WiFiNINA_config); - - offset += sizeof(WiFiNINA_config); - int checkSum = 0; - + uint16_t offset = NINA_EEPROM_START + sizeof(WiFiNINA_config); + for (int i = 0; i < NUM_MENU_ITEMS; i++) { char* _pointer = myMenuItems[i].pdata; - DEBUG_WM4(F("pdata="), myMenuItems[i].pdata, F(",len="), myMenuItems[i].maxlen); - + //DEBUG_WM4(F("pdata="), myMenuItems[i].pdata, F(",len="), myMenuItems[i].maxlen); + for (uint16_t j = 0; j < myMenuItems[i].maxlen; j++,_pointer++,offset++) { EEPROM.write(offset, *_pointer); @@ -628,59 +744,101 @@ class WiFiManager_NINA_Lite } EEPROM.put(offset, checkSum); + //EEPROM.commit(); DEBUG_WM2(F("CrCCSum="), checkSum); - } + } bool getConfigData() { - bool credDataValid; + bool dynamicDataValid; - hadConfigData = false; - - credDataValid = EEPROM_get(); + hadConfigData = false; + + EEPROM.begin(); + DEBUG_WM2(F("EEPROMsz:"), EEPROM_SIZE); + EEPROM.get(NINA_EEPROM_START, WiFiNINA_config); + + DEBUG_WM1(F("======= Start Stored Config Data =======")); + displayConfigData(WiFiNINA_config); int calChecksum = calcChecksum(); - DEBUG_WM4(F("CCSum="), calChecksum, F(",RCSum="), WiFiNINA_config.checkSum); - - if ( (strncmp(WiFiNINA_config.header, WIFININA_BOARD_TYPE, strlen(WIFININA_BOARD_TYPE)) != 0) || - (calChecksum != WiFiNINA_config.checkSum) || !credDataValid ) + DEBUG_WM4(F("CCSum=0x"), String(calChecksum, HEX), + F(",RCSum=0x"), String(WiFiNINA_config.checkSum, HEX)); + + if (LOAD_DEFAULT_CONFIG_DATA) { - memset(&WiFiNINA_config, 0, sizeof(WiFiNINA_config)); - - for (int i = 0; i < NUM_MENU_ITEMS; i++) + // Load default dynamicData, if checkSum OK => valid data => load + // otherwise, use default in sketch and just assume it's OK + if (checkDynamicData()) { - // Actual size of pdata is [maxlen + 1] - memset(myMenuItems[i].pdata, 0, myMenuItems[i].maxlen + 1); + DEBUG_WM1(F("Valid Stored Dynamic Data")); + EEPROM_getDynamicData(); } - + else + { + DEBUG_WM1(F("Ignore invalid Stored Dynamic Data")); + } + + dynamicDataValid = true; + } + else + { + dynamicDataValid = EEPROM_getDynamicData(); + } + + if ( (strncmp(WiFiNINA_config.header, WIFININA_BOARD_TYPE, strlen(WIFININA_BOARD_TYPE)) != 0) || + (calChecksum != WiFiNINA_config.checkSum) || !dynamicDataValid ) + { // Including Credentials CSum - DEBUG_WM4(F("InitEEPROM,sz="), EEPROM.length(), F(",Datasz="), totalDataSize); - - // doesn't have any configuration + DEBUG_WM2(F("InitCfgFile,sz="), sizeof(WiFiNINA_config)); + + // doesn't have any configuration + if (LOAD_DEFAULT_CONFIG_DATA) + { + memcpy(&WiFiNINA_config, &defaultConfig, sizeof(WiFiNINA_config)); + } + else + { + memset(&WiFiNINA_config, 0, sizeof(WiFiNINA_config)); + + for (int i = 0; i < NUM_MENU_ITEMS; i++) + { + // Actual size of pdata is [maxlen + 1] + memset(myMenuItems[i].pdata, 0, myMenuItems[i].maxlen + 1); + } + + strcpy(WiFiNINA_config.WiFi_Creds[0].wifi_ssid, WM_NO_CONFIG); + strcpy(WiFiNINA_config.WiFi_Creds[0].wifi_pw, WM_NO_CONFIG); + strcpy(WiFiNINA_config.WiFi_Creds[1].wifi_ssid, WM_NO_CONFIG); + strcpy(WiFiNINA_config.WiFi_Creds[1].wifi_pw, WM_NO_CONFIG); + strcpy(WiFiNINA_config.board_name, WM_NO_CONFIG); + + for (int i = 0; i < NUM_MENU_ITEMS; i++) + { + strncpy(myMenuItems[i].pdata, WM_NO_CONFIG, myMenuItems[i].maxlen); + } + } + strcpy(WiFiNINA_config.header, WIFININA_BOARD_TYPE); - strcpy(WiFiNINA_config.WiFi_Creds[0].wifi_ssid, NO_CONFIG); - strcpy(WiFiNINA_config.WiFi_Creds[0].wifi_pw, NO_CONFIG); - strcpy(WiFiNINA_config.WiFi_Creds[1].wifi_ssid, NO_CONFIG); - strcpy(WiFiNINA_config.WiFi_Creds[1].wifi_pw, NO_CONFIG); for (int i = 0; i < NUM_MENU_ITEMS; i++) { - strncpy(myMenuItems[i].pdata, NO_CONFIG, myMenuItems[i].maxlen); + DEBUG_WM4(F("g:myMenuItems["), i, F("]="), myMenuItems[i].pdata ); } - + // Don't need WiFiNINA_config.checkSum = 0; - EEPROM_put(); - - return false; + saveConfigData(); + + return false; } - else if ( !strncmp(WiFiNINA_config.WiFi_Creds[0].wifi_ssid, NO_CONFIG, strlen(NO_CONFIG) ) || - !strncmp(WiFiNINA_config.WiFi_Creds[0].wifi_pw, NO_CONFIG, strlen(NO_CONFIG) ) || - !strncmp(WiFiNINA_config.WiFi_Creds[1].wifi_ssid, NO_CONFIG, strlen(NO_CONFIG) ) || - !strncmp(WiFiNINA_config.WiFi_Creds[1].wifi_pw, NO_CONFIG, strlen(NO_CONFIG) ) || + else if ( !strncmp(WiFiNINA_config.WiFi_Creds[0].wifi_ssid, WM_NO_CONFIG, strlen(WM_NO_CONFIG) ) || + !strncmp(WiFiNINA_config.WiFi_Creds[0].wifi_pw, WM_NO_CONFIG, strlen(WM_NO_CONFIG) ) || + !strncmp(WiFiNINA_config.WiFi_Creds[1].wifi_ssid, WM_NO_CONFIG, strlen(WM_NO_CONFIG) ) || + !strncmp(WiFiNINA_config.WiFi_Creds[1].wifi_pw, WM_NO_CONFIG, strlen(WM_NO_CONFIG) ) || !strlen(WiFiNINA_config.WiFi_Creds[0].wifi_ssid) || !strlen(WiFiNINA_config.WiFi_Creds[1].wifi_ssid) || !strlen(WiFiNINA_config.WiFi_Creds[0].wifi_pw) || @@ -691,7 +849,7 @@ class WiFiManager_NINA_Lite } else { - displayConfigData(); + displayConfigData(WiFiNINA_config); } return true; @@ -704,7 +862,9 @@ class WiFiManager_NINA_Lite DEBUG_WM6(F("SaveEEPROM,sz="), EEPROM.length(), F(",Datasz="), totalDataSize, F(",CSum="), calChecksum); - EEPROM_put(); + //EEPROM_put(); + EEPROM.put(NINA_EEPROM_START, WiFiNINA_config); + EEPROM_putDynamicData(); } bool connectMultiWiFi(int timeout) @@ -811,11 +971,23 @@ class WiFiManager_NINA_Lite // Reset configTimeout to stay here until finished. configTimeout = 0; + + if ( RFC952_hostname[0] != 0 ) + { + // Replace only if Hostname is valid + result.replace("Teensy_WM_NINA_Lite", RFC952_hostname); + } + else if ( WiFiNINA_config.board_name[0] != 0 ) + { + // Or replace only if board_name is valid. Otherwise, keep intact + result.replace("Teensy_WM_NINA_Lite", WiFiNINA_config.board_name); + } result.replace("[[id]]", WiFiNINA_config.WiFi_Creds[0].wifi_ssid); result.replace("[[pw]]", WiFiNINA_config.WiFi_Creds[0].wifi_pw); result.replace("[[id1]]", WiFiNINA_config.WiFi_Creds[1].wifi_ssid); result.replace("[[pw1]]", WiFiNINA_config.WiFi_Creds[1].wifi_pw); + result.replace("[[nm]]", WiFiNINA_config.board_name); for (int i = 0; i < NUM_MENU_ITEMS; i++) { @@ -866,6 +1038,14 @@ class WiFiManager_NINA_Lite else strncpy(WiFiNINA_config.WiFi_Creds[1].wifi_pw, value.c_str(), sizeof(WiFiNINA_config.WiFi_Creds[1].wifi_pw) - 1); } + else if (key == "nm") + { + number_items_Updated++; + if (strlen(value.c_str()) < sizeof(WiFiNINA_config.board_name) - 1) + strcpy(WiFiNINA_config.board_name, value.c_str()); + else + strncpy(WiFiNINA_config.board_name, value.c_str(), sizeof(WiFiNINA_config.board_name) - 1); + } for (int i = 0; i < NUM_MENU_ITEMS; i++) {