From 24dd98c1e60ceefd59fdb5fdbede2ef5e7de0771 Mon Sep 17 00:00:00 2001 From: Khoi Hoang <57012152+khoih-prog@users.noreply.github.com> Date: Wed, 18 May 2022 22:48:13 -0400 Subject: [PATCH] v1.8.0 to add support to `AmebaD RRL8720DN` ### Release v1.8.0 1. Add support to Realtek RTL8720DN, RTL8722DM and RTL8722CSM using [AmebaD core](https://github.com/ambiot/ambd_arduino) and [FlashStorage_RTL8720 library](https://github.com/khoih-prog/FlashStorage_RTL8720). RTL8720DN WiFi can use either 2.4GHz or 5GHz band. 2. Restructure library 3. Add and Update examples 4. Update `Packages' Patches` --- examples/MKR1000_WiFi101/defines.h | 2 +- .../MKR1000_WiFi101_MQTT.ino | 30 +- examples/MKR1000_WiFi101_MQTT/defines.h | 2 +- examples/RP2040_WiFi/defines.h | 2 +- .../RP2040_WiFi_MQTT/RP2040_WiFi_MQTT.ino | 29 +- examples/RP2040_WiFi_MQTT/defines.h | 2 +- examples/RTL8720_WiFi/Credentials.h | 89 +++++ examples/RTL8720_WiFi/RTL8720_WiFi.ino | 166 ++++++++ examples/RTL8720_WiFi/defines.h | 140 +++++++ examples/RTL8720_WiFi/dynamicParams.h | 74 ++++ examples/RTL8720_WiFi_MQTT/Credentials.h | 89 +++++ .../RTL8720_WiFi_MQTT/RTL8720_WiFi_MQTT.ino | 376 ++++++++++++++++++ examples/RTL8720_WiFi_MQTT/defines.h | 140 +++++++ examples/RTL8720_WiFi_MQTT/dynamicParams.h | 82 ++++ examples/SAMD_WiFi/defines.h | 2 +- examples/SAMD_WiFi_MQTT/SAMD_WiFi_MQTT.ino | 30 +- examples/SAMD_WiFi_MQTT/defines.h | 2 +- examples/SAM_DUE_WiFi/defines.h | 2 +- .../SAM_DUE_WiFi_MQTT/SAM_DUE_WiFi_MQTT.ino | 30 +- examples/SAM_DUE_WiFi_MQTT/defines.h | 2 +- examples/STM32_WiFi/defines.h | 2 +- examples/STM32_WiFi_MQTT/STM32_WiFi_MQTT.ino | 30 +- examples/STM32_WiFi_MQTT/defines.h | 2 +- examples/Teensy_WiFi/defines.h | 2 +- .../Teensy_WiFi_MQTT/Teensy_WiFi_MQTT.ino | 30 +- examples/Teensy_WiFi_MQTT/defines.h | 2 +- examples/nRF52_WiFi/defines.h | 2 +- examples/nRF52_WiFi_MQTT/defines.h | 2 +- examples/nRF52_WiFi_MQTT/nRF52_WiFi_MQTT.ino | 31 +- 29 files changed, 1183 insertions(+), 211 deletions(-) create mode 100644 examples/RTL8720_WiFi/Credentials.h create mode 100644 examples/RTL8720_WiFi/RTL8720_WiFi.ino create mode 100644 examples/RTL8720_WiFi/defines.h create mode 100644 examples/RTL8720_WiFi/dynamicParams.h create mode 100644 examples/RTL8720_WiFi_MQTT/Credentials.h create mode 100644 examples/RTL8720_WiFi_MQTT/RTL8720_WiFi_MQTT.ino create mode 100644 examples/RTL8720_WiFi_MQTT/defines.h create mode 100644 examples/RTL8720_WiFi_MQTT/dynamicParams.h diff --git a/examples/MKR1000_WiFi101/defines.h b/examples/MKR1000_WiFi101/defines.h index a324bd42..807cac6f 100644 --- a/examples/MKR1000_WiFi101/defines.h +++ b/examples/MKR1000_WiFi101/defines.h @@ -140,7 +140,7 @@ ///////////////////////////////////////////// -#include +#include #define HOST_NAME "SAMD-Master-Controller" diff --git a/examples/MKR1000_WiFi101_MQTT/MKR1000_WiFi101_MQTT.ino b/examples/MKR1000_WiFi101_MQTT/MKR1000_WiFi101_MQTT.ino index fda4c072..f1b52981 100644 --- a/examples/MKR1000_WiFi101_MQTT/MKR1000_WiFi101_MQTT.ino +++ b/examples/MKR1000_WiFi101_MQTT/MKR1000_WiFi101_MQTT.ino @@ -9,34 +9,8 @@ Built by Khoi Hoang https://github.com/khoih-prog/WiFiManager_Generic_Lite Licensed under MIT license *****************************************************************************************************************************/ -/**************************************************************************************************************************** - You have to modify file ./libraries/Adafruit_MQTT_Library/Adafruit_MQTT.cpp as follows to avoid dtostrf error, if exists - - #ifdef __cplusplus - extern "C" { - #endif - extern char* itoa(int value, char *string, int radix); - extern char* ltoa(long value, char *string, int radix); - extern char* utoa(unsigned value, char *string, int radix); - extern char* ultoa(unsigned long value, char *string, int radix); - #ifdef __cplusplus - } // extern "C" - #endif - - //#if defined(ARDUINO_SAMD_ZERO) || defined(ARDUINO_SAMD_MKR1000) || defined(ARDUINO_ARCH_SAMD) - #if !( ESP32 || ESP8266 || defined(CORE_TEENSY) || defined(STM32F1) || defined(STM32F2) || defined(STM32F3) || defined(STM32F4) || defined(STM32F7) || \ - ( defined(ARDUINO_ARCH_RP2040) && !defined(ARDUINO_ARCH_MBED) ) || ARDUINO_ARCH_SEEED_SAMD || ( defined(SEEED_WIO_TERMINAL) || defined(SEEED_XIAO_M0) || \ - defined(SEEED_FEMTO_M0) || defined(Wio_Lite_MG126) || defined(WIO_GPS_BOARD) || defined(SEEEDUINO_ZERO) || defined(SEEEDUINO_LORAWAN) || defined(WIO_LTE_CAT) || \ - defined(SEEED_GROVE_UI_WIRELESS) ) ) - static char *dtostrf(double val, signed char width, unsigned char prec, char *sout) - { - char fmt[20]; - sprintf(fmt, "%%%d.%df", width, prec); - sprintf(sout, fmt, val); - return sout; - } - #endif - *****************************************************************************************************************************/ + +// You have to use forked and modified library https://github.com/khoih-prog/Adafruit_MQTT_Library #include "defines.h" #include "Credentials.h" diff --git a/examples/MKR1000_WiFi101_MQTT/defines.h b/examples/MKR1000_WiFi101_MQTT/defines.h index b92c2f03..a14c17b0 100644 --- a/examples/MKR1000_WiFi101_MQTT/defines.h +++ b/examples/MKR1000_WiFi101_MQTT/defines.h @@ -140,7 +140,7 @@ ///////////////////////////////////////////// -#include +#include #define HOST_NAME "SAMD-MQTT-Controller" diff --git a/examples/RP2040_WiFi/defines.h b/examples/RP2040_WiFi/defines.h index de5a1df6..fbeef076 100644 --- a/examples/RP2040_WiFi/defines.h +++ b/examples/RP2040_WiFi/defines.h @@ -208,7 +208,7 @@ ///////////////////////////////////////////// -#include +#include #define HOST_NAME "RP2040-Master-Controller" diff --git a/examples/RP2040_WiFi_MQTT/RP2040_WiFi_MQTT.ino b/examples/RP2040_WiFi_MQTT/RP2040_WiFi_MQTT.ino index 7179a913..0e9aca53 100644 --- a/examples/RP2040_WiFi_MQTT/RP2040_WiFi_MQTT.ino +++ b/examples/RP2040_WiFi_MQTT/RP2040_WiFi_MQTT.ino @@ -10,34 +10,7 @@ Licensed under MIT license *****************************************************************************************************************************/ -/**************************************************************************************************************************** - You have to modify file ./libraries/Adafruit_MQTT_Library/Adafruit_MQTT.cpp as follows to avoid dtostrf error, if exists - - #ifdef __cplusplus - extern "C" { - #endif - extern char* itoa(int value, char *string, int radix); - extern char* ltoa(long value, char *string, int radix); - extern char* utoa(unsigned value, char *string, int radix); - extern char* ultoa(unsigned long value, char *string, int radix); - #ifdef __cplusplus - } // extern "C" - #endif - - //#if defined(ARDUINO_SAMD_ZERO) || defined(ARDUINO_SAMD_MKR1000) || defined(ARDUINO_ARCH_SAMD) - #if !( ESP32 || ESP8266 || defined(CORE_TEENSY) || defined(STM32F1) || defined(STM32F2) || defined(STM32F3) || defined(STM32F4) || defined(STM32F7) || \ - ( defined(ARDUINO_ARCH_RP2040) && !defined(ARDUINO_ARCH_MBED) ) || ARDUINO_ARCH_SEEED_SAMD || ( defined(SEEED_WIO_TERMINAL) || defined(SEEED_XIAO_M0) || \ - defined(SEEED_FEMTO_M0) || defined(Wio_Lite_MG126) || defined(WIO_GPS_BOARD) || defined(SEEEDUINO_ZERO) || defined(SEEEDUINO_LORAWAN) || defined(WIO_LTE_CAT) || \ - defined(SEEED_GROVE_UI_WIRELESS) ) ) - static char *dtostrf(double val, signed char width, unsigned char prec, char *sout) - { - char fmt[20]; - sprintf(fmt, "%%%d.%df", width, prec); - sprintf(sout, fmt, val); - return sout; - } - #endif - *****************************************************************************************************************************/ +// You have to use forked and modified library https://github.com/khoih-prog/Adafruit_MQTT_Library #include "defines.h" #include "Credentials.h" diff --git a/examples/RP2040_WiFi_MQTT/defines.h b/examples/RP2040_WiFi_MQTT/defines.h index f7805831..3d030c81 100644 --- a/examples/RP2040_WiFi_MQTT/defines.h +++ b/examples/RP2040_WiFi_MQTT/defines.h @@ -215,7 +215,7 @@ ///////////////////////////////////////////// -#include +#include #define HOST_NAME "RP2040-MQTT-Controller" diff --git a/examples/RTL8720_WiFi/Credentials.h b/examples/RTL8720_WiFi/Credentials.h new file mode 100644 index 00000000..7bc6b07d --- /dev/null +++ b/examples/RTL8720_WiFi/Credentials.h @@ -0,0 +1,89 @@ +/**************************************************************************************************************************** + Credentials.h + For RTL8720DN, RTL8722DM and RTL8722CSM WiFi shields + + WiFiManager_Generic_WM_Lite is a library for the Mega, Teensy, SAM DUE, SAMD and STM32 boards + (https://github.com/khoih-prog/WiFiManager_Generic_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_Generic_Lite + Licensed under MIT license + *****************************************************************************************************************************/ + +#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; +} WIFI_GENERIC_Configuration; +*/ + +#define TO_LOAD_DEFAULT_CONFIG_DATA false + +#if TO_LOAD_DEFAULT_CONFIG_DATA + +// This feature is primarily used in development to force a known set of values as Config Data +// It will NOT force the Config Portal to activate. Use DRD or erase Config Data with Blynk.clearConfigData() + +// Used mostly for development and debugging. FORCES default values to be loaded each run. +// Config Portal data input will be ignored and overridden by DEFAULT_CONFIG_DATA +//bool LOAD_DEFAULT_CONFIG_DATA = true; + +// Used mostly once debugged. Assumes good data already saved in device. +// Config Portal data input will be override DEFAULT_CONFIG_DATA +bool LOAD_DEFAULT_CONFIG_DATA = false; + + +WIFI_GENERIC_Configuration defaultConfig = +{ + //char header[16], dummy, not used + "WIFI_GENERIC", + // WiFi_Credentials WiFi_Creds [NUM_WIFI_CREDENTIALS]; + // WiFi_Credentials.wifi_ssid and WiFi_Credentials.wifi_pw + "SSID1", "password1", + "SSID2", "password2", + //char board_name [24]; + "SAMD-Control", + // terminate the list + //int checkSum, dummy, not used + 0 + /////////// End Default Config Data ///////////// +}; + +#else + +bool LOAD_DEFAULT_CONFIG_DATA = false; + +WIFI_GENERIC_Configuration defaultConfig; + +#endif // TO_LOAD_DEFAULT_CONFIG_DATA + +/////////// End Default Config Data ///////////// + + +#endif //Credentials_h diff --git a/examples/RTL8720_WiFi/RTL8720_WiFi.ino b/examples/RTL8720_WiFi/RTL8720_WiFi.ino new file mode 100644 index 00000000..13b82f41 --- /dev/null +++ b/examples/RTL8720_WiFi/RTL8720_WiFi.ino @@ -0,0 +1,166 @@ +/**************************************************************************************************************************** + RTL8720_WiFi.ino + + For RTL8720DN, RTL8722DM and RTL8722CSM WiFi shields + + WiFiManager_Generic_WM_Lite is a library for the Mega, Teensy, SAM DUE, SAMD and STM32 boards + (https://github.com/khoih-prog/WiFiManager_Generic_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_Generic_Lite + Licensed under MIT license + *****************************************************************************************************************************/ + +#include "defines.h" +#include "Credentials.h" +#include "dynamicParams.h" + +WiFiManager_Generic_Lite* WiFiManager_Generic; + +void heartBeatPrint(void) +{ + static int num = 1; + + if (WiFiManager_Generic->isConfigMode()) + Serial.print("C"); // C means in Config Mode + else + { + if (WiFi.status() == WL_CONNECTED) + { + Serial.print("H"); // H means connected to WiFi + // Bad bug or RTL8720 => if WiFi lost, WiFi.status() still is WL_CONNECTED and WiFi.RSSI() still OK. + // Similar bug in Portenta_H7 without workaround as in Portenta_H7, WiFi.RSSI() => 0 + //Serial.print(WiFi.RSSI()); + } + else + Serial.print("F"); // F means not connected to WiFi + } + + if (num == 80) + { + Serial.println(); + num = 1; + } + else if (num++ % 10 == 0) + { + Serial.print(F(" ")); + } +} + +void check_status() +{ + static unsigned long checkstatus_timeout = 0; + + //KH +#define HEARTBEAT_INTERVAL 20000L + // Print hearbeat every HEARTBEAT_INTERVAL (20) seconds. + if ((millis() > checkstatus_timeout) || (checkstatus_timeout == 0)) + { + heartBeatPrint(); + checkstatus_timeout = millis() + HEARTBEAT_INTERVAL; + } +} + +#if USING_CUSTOMS_STYLE +const char NewCustomsStyle[] /*PROGMEM*/ = ""; +#endif + +void setup() +{ + // Debug console + Serial.begin(115200); + while (!Serial && millis() < 5000); + + delay(200); + + Serial.print(F("\nStart RTL8720_WiFi on ")); Serial.println(BOARD_NAME); + Serial.println(WIFI_MANAGER_GENERIC_LITE_VERSION); + + if (WiFi.status() == WL_NO_SHIELD) + { + Serial.println(F("WiFi shield not present")); + // don't continue + while (true); + } + + String fv = WiFi.firmwareVersion(); + + Serial.print("Current Firmware Version = "); Serial.println(fv); + + if (fv != LATEST_RTL8720_FIRMWARE) + { + Serial.println("Please upgrade the firmware"); + } + + WiFiManager_Generic = new WiFiManager_Generic_Lite(); + + // Optional to change default AP IP(192.168.4.1) and channel(10) + //WiFiManager_Generic->setConfigPortalIP(IPAddress(192, 168, 120, 1)); + WiFiManager_Generic->setConfigPortalChannel(0); + +#if USING_CUSTOMS_STYLE + WiFiManager_Generic->setCustomsStyle(NewCustomsStyle); +#endif + +#if USING_CUSTOMS_HEAD_ELEMENT + WiFiManager_Generic->setCustomsHeadElement(""); +#endif + +#if USING_CORS_FEATURE + WiFiManager_Generic->setCORSHeader("Your Access-Control-Allow-Origin"); +#endif + + // Set customized DHCP HostName + WiFiManager_Generic->begin(HOST_NAME); + //Or use default Hostname "SAMD-WIFI-XXXXXX" + //WiFiManager_Generic->begin(); + +} + +#if USE_DYNAMIC_PARAMETERS +void displayCredentials() +{ + Serial.println(F("\nYour stored Credentials :")); + + for (uint16_t i = 0; i < NUM_MENU_ITEMS; i++) + { + Serial.print(myMenuItems[i].displayName); + Serial.print(F(" = ")); + Serial.println(myMenuItems[i].pdata); + } +} + +void displayCredentialsInLoop() +{ + static bool displayedCredentials = false; + + if (!displayedCredentials) + { + for (int i = 0; i < NUM_MENU_ITEMS; i++) + { + if (!strlen(myMenuItems[i].pdata)) + { + break; + } + + if ( i == (NUM_MENU_ITEMS - 1) ) + { + displayedCredentials = true; + displayCredentials(); + } + } + } +} + +#endif + +void loop() +{ + WiFiManager_Generic->run(); + check_status(); + +#if USE_DYNAMIC_PARAMETERS + displayCredentialsInLoop(); +#endif +} diff --git a/examples/RTL8720_WiFi/defines.h b/examples/RTL8720_WiFi/defines.h new file mode 100644 index 00000000..532da04c --- /dev/null +++ b/examples/RTL8720_WiFi/defines.h @@ -0,0 +1,140 @@ +/**************************************************************************************************************************** + defines.h + For RTL8720DN, RTL8722DM and RTL8722CSM WiFi shields + + WiFiManager_Generic_WM_Lite is a library for the Mega, Teensy, SAM DUE, SAMD and STM32 boards + (https://github.com/khoih-prog/WiFiManager_Generic_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_Generic_Lite + Licensed under MIT license + *****************************************************************************************************************************/ + +#ifndef defines_h +#define defines_h + +/* Comment this out to disable prints and save space */ +#define DEBUG_WIFI_WEBSERVER_PORT Serial +#define WIFI_GENERIC_DEBUG_OUTPUT Serial + +#define _WIFI_GENERIC_LOGLEVEL_ 4 +#define _WIFIMULTI_LOGLEVEL_ 1 + +#define DRD_GENERIC_DEBUG true + +#if defined(CONFIG_PLATFORM_8721D) + #if defined(WIFI_GENERIC_USE_RTL8720) + #undef WIFI_GENERIC_USE_RTL8720 + #undef WIFI_USE_RTL8720 + #endif + #define WIFI_GENERIC_USE_RTL8720 true + #define WIFI_USE_RTL8720 true +#else + #error This code is intended to run only on the RTL8720 boards ! Please check your Tools->Board setting. +#endif + +#define BOARD_NAME "Rtlduino RTL8720DN" + +/////////////////////////////////////////////////////////////// + +// 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) + +///////////////////////////////////////////// + +// Add customs headers from v1.1.0 +#define USING_CUSTOMS_STYLE true +#define USING_CUSTOMS_HEAD_ELEMENT true +#define USING_CORS_FEATURE true + +///////////////////////////////////////////// + +#define USE_WIFI_NINA false +#define USE_WIFI101 false +#define USE_WIFI_CUSTOM false + +///////////////////////////////////////////// + +// Permit running CONFIG_TIMEOUT_RETRYTIMES_BEFORE_RESET times before reset hardware +// to permit user another chance to config. Only if Config Data is valid. +// If Config Data is invalid, this has no effect as Config Portal will persist +#define RESET_IF_CONFIG_TIMEOUT true + +// Permitted range of user-defined RETRY_TIMES_RECONNECT_WIFI between 2-5 times +#define RETRY_TIMES_RECONNECT_WIFI 3 + +// Permitted range of user-defined CONFIG_TIMEOUT_RETRYTIMES_BEFORE_RESET between 2-100 +#define CONFIG_TIMEOUT_RETRYTIMES_BEFORE_RESET 5 + +// Config Timeout 120s (default 60s). Applicable only if Config Data is Valid +#define CONFIG_TIMEOUT 120000L + +///////////////////////////////////////////// + +// Permit input only one set of WiFi SSID/PWD. The other can be "NULL or "blank" +// Default is false (if not defined) => must input 2 sets of SSID/PWD +#define REQUIRE_ONE_SET_SSID_PW true //false + +// Max times to try WiFi per loop() iteration. To avoid blocking issue in loop() +// Default 1 if not defined, and minimum 1. +//#define MAX_NUM_WIFI_RECON_TRIES_PER_LOOP 2 + +// Default no interval between recon WiFi if lost +// Max permitted interval will be 10mins +// Uncomment to use. Be careful, WiFi reconnect will be delayed if using this method +// Only use whenever urgent tasks in loop() can't be delayed. But if so, it's better you have to rewrite your code, e.g. using higher priority tasks. +//#define WIFI_RECON_INTERVAL 30000 + +///////////////////////////////////////////// + +#define USE_DYNAMIC_PARAMETERS true + +///////////////////////////////////////////// + +#define SCAN_WIFI_NETWORKS true + +// To be able to manually input SSID, not from a scanned SSID lists +#define MANUAL_SSID_INPUT_ALLOWED true + +///////////////////////////////////////////// + +// From 2-15 +#define MAX_SSID_IN_LIST 8 + +///////////////////////////////////////////// + +// Optional, to use Board Name in Menu +#define USING_BOARD_NAME true + +///////////////////////////////////////////// + +// Optional, to use Board Name in Menu +#define USING_CONFIG_MODE_LED true + +#if USING_CONFIG_MODE_LED + #if defined(LED_BUILTIN) + #define CONFIG_MODE_LED LED_BUILTIN + #else + // Using default pin 13 for CONFIG_MODE_LED. To be changed as necessary + #define CONFIG_MODE_LED 13 + #endif + + #define LED_ON HIGH + #define LED_OFF LOW +#endif + +///////////////////////////////////////////// + +#include + +#define HOST_NAME "RTL8720-Controller" + +#ifdef LED_BUILTIN + #define LED_PIN LED_BUILTIN +#else + #define LED_PIN 13 +#endif + +#endif //defines_h diff --git a/examples/RTL8720_WiFi/dynamicParams.h b/examples/RTL8720_WiFi/dynamicParams.h new file mode 100644 index 00000000..b2a0e8d1 --- /dev/null +++ b/examples/RTL8720_WiFi/dynamicParams.h @@ -0,0 +1,74 @@ +/**************************************************************************************************************************** + dynamicParams.h + For RTL8720DN, RTL8722DM and RTL8722CSM WiFi shields + + WiFiManager_Generic_WM_Lite is a library for the Mega, Teensy, SAM DUE, SAMD and STM32 boards + (https://github.com/khoih-prog/WiFiManager_Generic_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_Generic_Lite + Licensed under MIT license + *****************************************************************************************************************************/ + +#ifndef dynamicParams_h +#define dynamicParams_h + +#include "defines.h" + +// USE_DYNAMIC_PARAMETERS defined in defined.h + +/////////////// 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 }, + { "prt", "Port", Blynk_Port, MAX_BLYNK_PORT_LEN }, + { "mqt", "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/RTL8720_WiFi_MQTT/Credentials.h b/examples/RTL8720_WiFi_MQTT/Credentials.h new file mode 100644 index 00000000..1a7d2a5f --- /dev/null +++ b/examples/RTL8720_WiFi_MQTT/Credentials.h @@ -0,0 +1,89 @@ +/**************************************************************************************************************************** + Credentials.h for SAMD_WiFiNINA_MQTT.ino + For SAMD boards using WiFiNINA modules/shields, using much less code to support boards with smaller memory + + WiFiManager_Generic_WM_Lite is a library for the Mega, Teensy, SAM DUE, SAMD and STM32 boards + (https://github.com/khoih-prog/WiFiManager_Generic_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_Generic_Lite + Licensed under MIT license + *****************************************************************************************************************************/ + +#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; +} WIFI_GENERIC_Configuration; +*/ + +#define TO_LOAD_DEFAULT_CONFIG_DATA false + +#if TO_LOAD_DEFAULT_CONFIG_DATA + +// This feature is primarily used in development to force a known set of values as Config Data +// It will NOT force the Config Portal to activate. Use DRD or erase Config Data with Blynk.clearConfigData() + +// Used mostly for development and debugging. FORCES default values to be loaded each run. +// Config Portal data input will be ignored and overridden by DEFAULT_CONFIG_DATA +//bool LOAD_DEFAULT_CONFIG_DATA = true; + +// Used mostly once debugged. Assumes good data already saved in device. +// Config Portal data input will be override DEFAULT_CONFIG_DATA +bool LOAD_DEFAULT_CONFIG_DATA = false; + + +WIFI_GENERIC_Configuration defaultConfig = +{ + //char header[16], dummy, not used + "WIFI_GENERIC", + // WiFi_Credentials WiFi_Creds [NUM_WIFI_CREDENTIALS]; + // WiFi_Credentials.wifi_ssid and WiFi_Credentials.wifi_pw + "SSID1", "password1", + "SSID2", "password2", + //char board_name [24]; + "SAMD-Control", + // terminate the list + //int checkSum, dummy, not used + 0 + /////////// End Default Config Data ///////////// +}; + +#else + +bool LOAD_DEFAULT_CONFIG_DATA = false; + +WIFI_GENERIC_Configuration defaultConfig; + +#endif // TO_LOAD_DEFAULT_CONFIG_DATA + +/////////// End Default Config Data ///////////// + + +#endif //Credentials_h diff --git a/examples/RTL8720_WiFi_MQTT/RTL8720_WiFi_MQTT.ino b/examples/RTL8720_WiFi_MQTT/RTL8720_WiFi_MQTT.ino new file mode 100644 index 00000000..0c6aa772 --- /dev/null +++ b/examples/RTL8720_WiFi_MQTT/RTL8720_WiFi_MQTT.ino @@ -0,0 +1,376 @@ +/**************************************************************************************************************************** + SAMD_WiFiNINA_MQTT.ino + For SAMD boards using WiFiNINA modules/shields, using much less code to support boards with smaller memory + + WiFiManager_Generic_WM_Lite is a library for the Mega, Teensy, SAM DUE, SAMD and STM32 boards + (https://github.com/khoih-prog/WiFiManager_Generic_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_Generic_Lite + Licensed under MIT license + *****************************************************************************************************************************/ + +// You have to use forked and modified library https://github.com/khoih-prog/Adafruit_MQTT_Library + +#include "defines.h" +#include "Credentials.h" +#include "dynamicParams.h" + +#define LOCAL_DEBUG true //false + +#include "Adafruit_MQTT.h" //https://github.com/adafruit/Adafruit_MQTT_Library +#include "Adafruit_MQTT_Client.h" //https://github.com/adafruit/Adafruit_MQTT_Library + +// Create a WiFiClient class to connect to the MQTT server +WiFiClient *client = NULL; + +Adafruit_MQTT_Client *mqtt = NULL; +Adafruit_MQTT_Publish *Temperature = NULL; +Adafruit_MQTT_Subscribe *LED_Control = NULL; + +// You have to get from a sensor. Here is just an example +uint32_t measuredTemp = 5; + +WiFiManager_Generic_Lite* WiFiManager_Generic; + +void heartBeatPrint(void) +{ + static int num = 1; + + if (WiFiManager_Generic->isConfigMode()) + Serial.print("C"); // C means in Config Mode + else + { + if (WiFi.status() == WL_CONNECTED) + { + Serial.print("H"); // H means connected to WiFi + // Bad bug or RTL8720 => if WiFi lost, WiFi.status() still is WL_CONNECTED and WiFi.RSSI() still OK. + // Similar bug in Portenta_H7 without workaround as in Portenta_H7, WiFi.RSSI() => 0 + //Serial.print(WiFi.RSSI()); + } + else + Serial.print("F"); // F means not connected to WiFi + } + + if (num == 80) + { + Serial.println(); + num = 1; + } + else if (num++ % 10 == 0) + { + Serial.print(F(" ")); + } +} + +void publishMQTT() +{ + MQTT_connect(); + + if (Temperature->publish(measuredTemp)) + { + //Serial.println(F("Failed to send value to Temperature feed!")); + Serial.print(F("T")); // T means publishing OK + } + else + { + //Serial.println(F("Value to Temperature feed sucessfully sent!")); + Serial.print(F("F")); // F means publishing failure + } +} + +void subscribeMQTT() +{ + Adafruit_MQTT_Subscribe *subscription; + + MQTT_connect(); + + while ((subscription = mqtt->readSubscription(5000))) + { + if (subscription == LED_Control) + { + Serial.print(F("\nGot: ")); + Serial.println((char *)LED_Control->lastread); + + if (!strcmp((char*) LED_Control->lastread, "ON")) + { + digitalWrite(LED_PIN, HIGH); + } + else + { + digitalWrite(LED_PIN, LOW); + } + } + } +} + +void check_status() +{ + static unsigned long checkstatus_timeout = 0; + + //KH +#define HEARTBEAT_INTERVAL 5000L + // Print WiFi hearbeat, Publish MQTT Topic every HEARTBEAT_INTERVAL (5) seconds. + if ((millis() > checkstatus_timeout) || (checkstatus_timeout == 0)) + { + if ( !WiFiManager_Generic->isConfigMode() ) + { + if (WiFi.status() == WL_CONNECTED) + { + // MQTT related jobs + publishMQTT(); + subscribeMQTT(); + } + } + + heartBeatPrint(); + checkstatus_timeout = millis() + HEARTBEAT_INTERVAL; + } +} + +void deleteOldInstances() +{ + // Delete previous instances + if (mqtt) + { + delete mqtt; + mqtt = NULL; + Serial.println(F("Deleting old MQTT object")); + } + + if (Temperature) + { + delete Temperature; + Temperature = NULL; + Serial.println(F("Deleting old Temperature object")); + } +} + +#define USE_GLOBAL_TOPIC true + +#if USE_GLOBAL_TOPIC + String completePubTopic; + String completeSubTopic; +#endif + +void createNewInstances() +{ + if (!client) + { + client = new WiFiClient; + + if (client) + { + Serial.println(F("\nCreating new WiFi client object OK")); + } + else + Serial.println(F("\nCreating new WiFi client object failed")); + } + + // Create new instances from new data + if (!mqtt) + { + // Setup the MQTT client class by passing in the WiFi client and MQTT server and login details. + mqtt = new Adafruit_MQTT_Client(client, AIO_SERVER, atoi(AIO_SERVERPORT), AIO_USERNAME, AIO_KEY); + + if (mqtt) + { + Serial.println(F("Creating new MQTT object OK")); + Serial.print(F("AIO_SERVER = ")); Serial.print(AIO_SERVER); Serial.print(F(", AIO_SERVERPORT = ")); Serial.println(AIO_SERVERPORT); + Serial.print(F("AIO_USERNAME = ")); Serial.print(AIO_USERNAME); Serial.print(F(", AIO_KEY = ")); Serial.println(AIO_KEY); + } + else + Serial.println(F("Creating new MQTT object failed")); + } + + if (!Temperature) + { +#if USE_GLOBAL_TOPIC + completePubTopic = String(AIO_USERNAME) + String(AIO_PUB_TOPIC); +#else + // Must be static or global + static String completePubTopic = String(AIO_USERNAME) + String(AIO_PUB_TOPIC); +#endif + + Temperature = new Adafruit_MQTT_Publish(mqtt, completePubTopic.c_str()); + Serial.print(F("Creating new MQTT_Pub_Topic, Temperature = ")); Serial.println(completePubTopic); + + if (Temperature) + { + Serial.println(F("Creating new Temperature object OK")); + Serial.print(F("Temperature MQTT_Pub_Topic = ")); Serial.println(completePubTopic); + } + else + Serial.println(F("Creating new Temperature object failed")); + } + + if (!LED_Control) + { +#if USE_GLOBAL_TOPIC + completeSubTopic = String(AIO_USERNAME) + String(AIO_SUB_TOPIC); +#else + // Must be static or global + static String completeSubTopic = String(AIO_USERNAME) + String(AIO_SUB_TOPIC); +#endif + + LED_Control = new Adafruit_MQTT_Subscribe(mqtt, completeSubTopic.c_str()); + + Serial.print(F("Creating new AIO_SUB_TOPIC, LED_Control = ")); Serial.println(completeSubTopic); + + if (LED_Control) + { + Serial.println(F("Creating new LED_Control object OK")); + Serial.print(F("LED_Control AIO_SUB_TOPIC = ")); Serial.println(completeSubTopic); + + mqtt->subscribe(LED_Control); + } + else + Serial.println(F("Creating new LED_Control object failed")); + } +} + +void MQTT_connect() +{ + int8_t ret; + + createNewInstances(); + + // Return if already connected + if (mqtt->connected()) + { + return; + } + +#if LOCAL_DEBUG + Serial.println(F("\nConnecting to WiFi MQTT (3 attempts)...")); +#endif + + uint8_t attempt = 3; + + while ( (ret = mqtt->connect()) ) + { + // connect will return 0 for connected + Serial.println(mqtt->connectErrorString(ret)); + +#if LOCAL_DEBUG + Serial.println(F("Another attemtpt to connect to MQTT in 5 seconds...")); +#endif + + mqtt->disconnect(); + delay(5000); // wait 5 seconds + attempt--; + + if (attempt == 0) + { + Serial.println(F("WiFi MQTT connection failed. Continuing with program...")); + return; + } + } + +#if LOCAL_DEBUG + Serial.println(F("WiFi MQTT connection successful!")); +#endif +} + +#if USING_CUSTOMS_STYLE +const char NewCustomsStyle[] /*PROGMEM*/ = ""; +#endif + +void setup() +{ + // Debug console + Serial.begin(115200); + while (!Serial && millis() < 5000); + + delay(200); + + Serial.print(F("\nStart RTL8720_WiFi_MQTT on ")); Serial.println(BOARD_NAME); + Serial.println(WIFI_MANAGER_GENERIC_LITE_VERSION); + + if (WiFi.status() == WL_NO_SHIELD) + { + Serial.println(F("WiFi shield not present")); + // don't continue + while (true); + } + + String fv = WiFi.firmwareVersion(); + + Serial.print("Current Firmware Version = "); Serial.println(fv); + + if (fv != LATEST_RTL8720_FIRMWARE) + { + Serial.println("Please upgrade the firmware"); + } + + WiFiManager_Generic = new WiFiManager_Generic_Lite(); + + // Optional to change default AP IP(192.168.4.1) and channel(10) + //WiFiManager_Generic->setConfigPortalIP(IPAddress(192, 168, 120, 1)); + WiFiManager_Generic->setConfigPortalChannel(0); + +#if USING_CUSTOMS_STYLE + WiFiManager_Generic->setCustomsStyle(NewCustomsStyle); +#endif + +#if USING_CUSTOMS_HEAD_ELEMENT + WiFiManager_Generic->setCustomsHeadElement(""); +#endif + +#if USING_CORS_FEATURE + WiFiManager_Generic->setCORSHeader("Your Access-Control-Allow-Origin"); +#endif + + // Set customized DHCP HostName + WiFiManager_Generic->begin(HOST_NAME); + //Or use default Hostname "SAMD-WIFI-XXXXXX" + //WiFiManager_Generic->begin(); +} + +#if USE_DYNAMIC_PARAMETERS +void displayCredentials() +{ + Serial.println(F("\nYour stored Credentials :")); + + for (uint16_t i = 0; i < NUM_MENU_ITEMS; i++) + { + Serial.print(myMenuItems[i].displayName); + Serial.print(F(" = ")); + Serial.println(myMenuItems[i].pdata); + } +} + +void displayCredentialsInLoop() +{ + static bool displayedCredentials = false; + + if (!displayedCredentials) + { + for (int i = 0; i < NUM_MENU_ITEMS; i++) + { + if (!strlen(myMenuItems[i].pdata)) + { + break; + } + + if ( i == (NUM_MENU_ITEMS - 1) ) + { + displayedCredentials = true; + displayCredentials(); + } + } + } +} + +#endif + +void loop() +{ + WiFiManager_Generic->run(); + check_status(); + +#if USE_DYNAMIC_PARAMETERS + displayCredentialsInLoop(); +#endif +} diff --git a/examples/RTL8720_WiFi_MQTT/defines.h b/examples/RTL8720_WiFi_MQTT/defines.h new file mode 100644 index 00000000..52d9dcaa --- /dev/null +++ b/examples/RTL8720_WiFi_MQTT/defines.h @@ -0,0 +1,140 @@ +/**************************************************************************************************************************** + defines.h + For RTL8720DN, RTL8722DM and RTL8722CSM WiFi shields + + WiFiManager_Generic_WM_Lite is a library for the Mega, Teensy, SAM DUE, SAMD and STM32 boards + (https://github.com/khoih-prog/WiFiManager_Generic_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_Generic_Lite + Licensed under MIT license + *****************************************************************************************************************************/ + +#ifndef defines_h +#define defines_h + +/* Comment this out to disable prints and save space */ +#define DEBUG_WIFI_WEBSERVER_PORT Serial +#define WIFI_GENERIC_DEBUG_OUTPUT Serial + +#define _WIFI_GENERIC_LOGLEVEL_ 4 +#define _WIFIMULTI_LOGLEVEL_ 1 + +#define DRD_GENERIC_DEBUG true + +#if defined(CONFIG_PLATFORM_8721D) + #if defined(WIFI_GENERIC_USE_RTL8720) + #undef WIFI_GENERIC_USE_RTL8720 + #undef WIFI_USE_RTL8720 + #endif + #define WIFI_GENERIC_USE_RTL8720 true + #define WIFI_USE_RTL8720 true +#else + #error This code is intended to run only on the RTL8720 boards ! Please check your Tools->Board setting. +#endif + +#define BOARD_NAME "Rtlduino RTL8720DN" + +/////////////////////////////////////////////////////////////// + +// 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) + +///////////////////////////////////////////// + +// Add customs headers from v1.1.0 +#define USING_CUSTOMS_STYLE true +#define USING_CUSTOMS_HEAD_ELEMENT true +#define USING_CORS_FEATURE true + +///////////////////////////////////////////// + +#define USE_WIFI_NINA false +#define USE_WIFI101 false +#define USE_WIFI_CUSTOM false + +///////////////////////////////////////////// + +// Permit running CONFIG_TIMEOUT_RETRYTIMES_BEFORE_RESET times before reset hardware +// to permit user another chance to config. Only if Config Data is valid. +// If Config Data is invalid, this has no effect as Config Portal will persist +#define RESET_IF_CONFIG_TIMEOUT true + +// Permitted range of user-defined RETRY_TIMES_RECONNECT_WIFI between 2-5 times +#define RETRY_TIMES_RECONNECT_WIFI 3 + +// Permitted range of user-defined CONFIG_TIMEOUT_RETRYTIMES_BEFORE_RESET between 2-100 +#define CONFIG_TIMEOUT_RETRYTIMES_BEFORE_RESET 5 + +// Config Timeout 120s (default 60s). Applicable only if Config Data is Valid +#define CONFIG_TIMEOUT 120000L + +///////////////////////////////////////////// + +// Permit input only one set of WiFi SSID/PWD. The other can be "NULL or "blank" +// Default is false (if not defined) => must input 2 sets of SSID/PWD +#define REQUIRE_ONE_SET_SSID_PW true //false + +// Max times to try WiFi per loop() iteration. To avoid blocking issue in loop() +// Default 1 if not defined, and minimum 1. +//#define MAX_NUM_WIFI_RECON_TRIES_PER_LOOP 2 + +// Default no interval between recon WiFi if lost +// Max permitted interval will be 10mins +// Uncomment to use. Be careful, WiFi reconnect will be delayed if using this method +// Only use whenever urgent tasks in loop() can't be delayed. But if so, it's better you have to rewrite your code, e.g. using higher priority tasks. +//#define WIFI_RECON_INTERVAL 30000 + +///////////////////////////////////////////// + +#define USE_DYNAMIC_PARAMETERS true + +///////////////////////////////////////////// + +#define SCAN_WIFI_NETWORKS true + +// To be able to manually input SSID, not from a scanned SSID lists +#define MANUAL_SSID_INPUT_ALLOWED true + +///////////////////////////////////////////// + +// From 2-15 +#define MAX_SSID_IN_LIST 8 + +///////////////////////////////////////////// + +// Optional, to use Board Name in Menu +#define USING_BOARD_NAME true + +///////////////////////////////////////////// + +// Optional, to use Board Name in Menu +#define USING_CONFIG_MODE_LED true + +#if USING_CONFIG_MODE_LED + #if defined(LED_BUILTIN) + #define CONFIG_MODE_LED LED_BUILTIN + #else + // Using default pin 13 for CONFIG_MODE_LED. To be changed as necessary + #define CONFIG_MODE_LED 13 + #endif + + #define LED_ON HIGH + #define LED_OFF LOW +#endif + +///////////////////////////////////////////// + +#include + +#define HOST_NAME "RTL8720-MQTT-Controller" + +#ifdef LED_BUILTIN + #define LED_PIN LED_BUILTIN +#else + #define LED_PIN 13 +#endif + +#endif //defines_h diff --git a/examples/RTL8720_WiFi_MQTT/dynamicParams.h b/examples/RTL8720_WiFi_MQTT/dynamicParams.h new file mode 100644 index 00000000..9066471c --- /dev/null +++ b/examples/RTL8720_WiFi_MQTT/dynamicParams.h @@ -0,0 +1,82 @@ +/**************************************************************************************************************************** + dynamicParams.h for SAMD_WiFiNINA_MQTT.ino + For SAMD boards using WiFiNINA modules/shields, using much less code to support boards with smaller memory + + WiFiManager_Generic_WM_Lite is a library for the Mega, Teensy, SAM DUE, SAMD and STM32 boards + (https://github.com/khoih-prog/WiFiManager_Generic_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_Generic_Lite + Licensed under MIT license + *****************************************************************************************************************************/ + +#ifndef dynamicParams_h +#define dynamicParams_h + +#include "defines.h" + +// USE_DYNAMIC_PARAMETERS defined in defined.h + +/////////////// 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; +**************************************/ + +#define AIO_SERVER_LEN 20 +#define AIO_SERVERPORT_LEN 6 +#define AIO_USERNAME_LEN 20 +#define AIO_KEY_LEN 40 + +// +#define AIO_PUB_TOPIC_LEN 40 +#define AIO_SUB_TOPIC_LEN 40 + +// If you don't USE_DYNAMIC_PARAMETERS, you can hardcode thse info to use + +///////////////////////////////////////////////////////////////////////////////////////// + +char AIO_SERVER [AIO_SERVER_LEN + 1] = "io.adafruit.com"; +char AIO_SERVERPORT [AIO_SERVERPORT_LEN + 1] = "1883"; //1883, or 8883 for SSL +char AIO_USERNAME [AIO_USERNAME_LEN + 1] = "private"; +char AIO_KEY [AIO_KEY_LEN + 1] = "private"; + +char AIO_PUB_TOPIC [AIO_PUB_TOPIC_LEN + 1] = "/feeds/Temperature"; +char AIO_SUB_TOPIC [AIO_SUB_TOPIC_LEN + 1] = "/feeds/LED_Control"; + +///////////////////////////////////////////////////////////////////////////////////////// + +#if USE_DYNAMIC_PARAMETERS + +MenuItem myMenuItems [] = +{ + { "svr", "AIO_SERVER", AIO_SERVER, AIO_SERVER_LEN }, + { "prt", "AIO_SERVERPORT", AIO_SERVERPORT, AIO_SERVERPORT_LEN }, + { "usr", "AIO_USERNAME", AIO_USERNAME, AIO_USERNAME_LEN }, + { "key", "AIO_KEY", AIO_KEY, AIO_KEY_LEN }, + { "pub", "AIO_PUB_TOPIC", AIO_PUB_TOPIC, AIO_PUB_TOPIC_LEN }, + { "sub", "AIO_SUB_TOPIC", AIO_SUB_TOPIC, AIO_SUB_TOPIC_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_WiFi/defines.h b/examples/SAMD_WiFi/defines.h index 48a08daf..53508924 100644 --- a/examples/SAMD_WiFi/defines.h +++ b/examples/SAMD_WiFi/defines.h @@ -294,7 +294,7 @@ ///////////////////////////////////////////// -#include +#include #define HOST_NAME "SAMD-Master-Controller" diff --git a/examples/SAMD_WiFi_MQTT/SAMD_WiFi_MQTT.ino b/examples/SAMD_WiFi_MQTT/SAMD_WiFi_MQTT.ino index 3dd6208e..6ce770ac 100644 --- a/examples/SAMD_WiFi_MQTT/SAMD_WiFi_MQTT.ino +++ b/examples/SAMD_WiFi_MQTT/SAMD_WiFi_MQTT.ino @@ -9,34 +9,8 @@ Built by Khoi Hoang https://github.com/khoih-prog/WiFiManager_Generic_Lite Licensed under MIT license *****************************************************************************************************************************/ -/**************************************************************************************************************************** - You have to modify file ./libraries/Adafruit_MQTT_Library/Adafruit_MQTT.cpp as follows to avoid dtostrf error, if exists - - #ifdef __cplusplus - extern "C" { - #endif - extern char* itoa(int value, char *string, int radix); - extern char* ltoa(long value, char *string, int radix); - extern char* utoa(unsigned value, char *string, int radix); - extern char* ultoa(unsigned long value, char *string, int radix); - #ifdef __cplusplus - } // extern "C" - #endif - - //#if defined(ARDUINO_SAMD_ZERO) || defined(ARDUINO_SAMD_MKR1000) || defined(ARDUINO_ARCH_SAMD) - #if !( ESP32 || ESP8266 || defined(CORE_TEENSY) || defined(STM32F1) || defined(STM32F2) || defined(STM32F3) || defined(STM32F4) || defined(STM32F7) || \ - ( defined(ARDUINO_ARCH_RP2040) && !defined(ARDUINO_ARCH_MBED) ) || ARDUINO_ARCH_SEEED_SAMD || ( defined(SEEED_WIO_TERMINAL) || defined(SEEED_XIAO_M0) || \ - defined(SEEED_FEMTO_M0) || defined(Wio_Lite_MG126) || defined(WIO_GPS_BOARD) || defined(SEEEDUINO_ZERO) || defined(SEEEDUINO_LORAWAN) || defined(WIO_LTE_CAT) || \ - defined(SEEED_GROVE_UI_WIRELESS) ) ) - static char *dtostrf(double val, signed char width, unsigned char prec, char *sout) - { - char fmt[20]; - sprintf(fmt, "%%%d.%df", width, prec); - sprintf(sout, fmt, val); - return sout; - } - #endif - *****************************************************************************************************************************/ + +// You have to use forked and modified library https://github.com/khoih-prog/Adafruit_MQTT_Library #include "defines.h" #include "Credentials.h" diff --git a/examples/SAMD_WiFi_MQTT/defines.h b/examples/SAMD_WiFi_MQTT/defines.h index a1e64040..8da005e7 100644 --- a/examples/SAMD_WiFi_MQTT/defines.h +++ b/examples/SAMD_WiFi_MQTT/defines.h @@ -310,7 +310,7 @@ ///////////////////////////////////////////// -#include +#include #define HOST_NAME "SAMD-MQTT-Controller" diff --git a/examples/SAM_DUE_WiFi/defines.h b/examples/SAM_DUE_WiFi/defines.h index 80e15bda..7eecdc84 100644 --- a/examples/SAM_DUE_WiFi/defines.h +++ b/examples/SAM_DUE_WiFi/defines.h @@ -207,7 +207,7 @@ ///////////////////////////////////////////// -#include +#include #define HOST_NAME "DUE-Master-Controller" diff --git a/examples/SAM_DUE_WiFi_MQTT/SAM_DUE_WiFi_MQTT.ino b/examples/SAM_DUE_WiFi_MQTT/SAM_DUE_WiFi_MQTT.ino index c2ea1c60..c2ab1ac4 100644 --- a/examples/SAM_DUE_WiFi_MQTT/SAM_DUE_WiFi_MQTT.ino +++ b/examples/SAM_DUE_WiFi_MQTT/SAM_DUE_WiFi_MQTT.ino @@ -9,34 +9,8 @@ Built by Khoi Hoang https://github.com/khoih-prog/WiFiManager_Generic_Lite Licensed under MIT license *****************************************************************************************************************************/ -/**************************************************************************************************************************** - You have to modify file ./libraries/Adafruit_MQTT_Library/Adafruit_MQTT.cpp as follows to avoid dtostrf error, if exists - - #ifdef __cplusplus - extern "C" { - #endif - extern char* itoa(int value, char *string, int radix); - extern char* ltoa(long value, char *string, int radix); - extern char* utoa(unsigned value, char *string, int radix); - extern char* ultoa(unsigned long value, char *string, int radix); - #ifdef __cplusplus - } // extern "C" - #endif - - //#if defined(ARDUINO_SAMD_ZERO) || defined(ARDUINO_SAMD_MKR1000) || defined(ARDUINO_ARCH_SAMD) - #if !( ESP32 || ESP8266 || defined(CORE_TEENSY) || defined(STM32F1) || defined(STM32F2) || defined(STM32F3) || defined(STM32F4) || defined(STM32F7) || \ - ( defined(ARDUINO_ARCH_RP2040) && !defined(ARDUINO_ARCH_MBED) ) || ARDUINO_ARCH_SEEED_SAMD || ( defined(SEEED_WIO_TERMINAL) || defined(SEEED_XIAO_M0) || \ - defined(SEEED_FEMTO_M0) || defined(Wio_Lite_MG126) || defined(WIO_GPS_BOARD) || defined(SEEEDUINO_ZERO) || defined(SEEEDUINO_LORAWAN) || defined(WIO_LTE_CAT) || \ - defined(SEEED_GROVE_UI_WIRELESS) ) ) - static char *dtostrf(double val, signed char width, unsigned char prec, char *sout) - { - char fmt[20]; - sprintf(fmt, "%%%d.%df", width, prec); - sprintf(sout, fmt, val); - return sout; - } - #endif - *****************************************************************************************************************************/ + +// You have to use forked and modified library https://github.com/khoih-prog/Adafruit_MQTT_Library #include "defines.h" #include "Credentials.h" diff --git a/examples/SAM_DUE_WiFi_MQTT/defines.h b/examples/SAM_DUE_WiFi_MQTT/defines.h index 5fd371cb..2dc43139 100644 --- a/examples/SAM_DUE_WiFi_MQTT/defines.h +++ b/examples/SAM_DUE_WiFi_MQTT/defines.h @@ -208,7 +208,7 @@ ///////////////////////////////////////////// -#include +#include #define HOST_NAME "DUE-MQTT-Controller" diff --git a/examples/STM32_WiFi/defines.h b/examples/STM32_WiFi/defines.h index 9f69fcab..2dbea9ab 100644 --- a/examples/STM32_WiFi/defines.h +++ b/examples/STM32_WiFi/defines.h @@ -270,7 +270,7 @@ ///////////////////////////////////////////// -#include +#include #define HOST_NAME "STM32-Master-Controller" diff --git a/examples/STM32_WiFi_MQTT/STM32_WiFi_MQTT.ino b/examples/STM32_WiFi_MQTT/STM32_WiFi_MQTT.ino index d4e14b38..61d9058e 100644 --- a/examples/STM32_WiFi_MQTT/STM32_WiFi_MQTT.ino +++ b/examples/STM32_WiFi_MQTT/STM32_WiFi_MQTT.ino @@ -9,34 +9,8 @@ Built by Khoi Hoang https://github.com/khoih-prog/WiFiManager_Generic_Lite Licensed under MIT license *****************************************************************************************************************************/ -/**************************************************************************************************************************** - You have to modify file ./libraries/Adafruit_MQTT_Library/Adafruit_MQTT.cpp as follows to avoid dtostrf error, if exists - - #ifdef __cplusplus - extern "C" { - #endif - extern char* itoa(int value, char *string, int radix); - extern char* ltoa(long value, char *string, int radix); - extern char* utoa(unsigned value, char *string, int radix); - extern char* ultoa(unsigned long value, char *string, int radix); - #ifdef __cplusplus - } // extern "C" - #endif - - //#if defined(ARDUINO_SAMD_ZERO) || defined(ARDUINO_SAMD_MKR1000) || defined(ARDUINO_ARCH_SAMD) - #if !( ESP32 || ESP8266 || defined(CORE_TEENSY) || defined(STM32F1) || defined(STM32F2) || defined(STM32F3) || defined(STM32F4) || defined(STM32F7) || \ - ( defined(ARDUINO_ARCH_RP2040) && !defined(ARDUINO_ARCH_MBED) ) || ARDUINO_ARCH_SEEED_SAMD || ( defined(SEEED_WIO_TERMINAL) || defined(SEEED_XIAO_M0) || \ - defined(SEEED_FEMTO_M0) || defined(Wio_Lite_MG126) || defined(WIO_GPS_BOARD) || defined(SEEEDUINO_ZERO) || defined(SEEEDUINO_LORAWAN) || defined(WIO_LTE_CAT) || \ - defined(SEEED_GROVE_UI_WIRELESS) ) ) - static char *dtostrf(double val, signed char width, unsigned char prec, char *sout) - { - char fmt[20]; - sprintf(fmt, "%%%d.%df", width, prec); - sprintf(sout, fmt, val); - return sout; - } - #endif - *****************************************************************************************************************************/ + +// You have to use forked and modified library https://github.com/khoih-prog/Adafruit_MQTT_Library #include "defines.h" #include "Credentials.h" diff --git a/examples/STM32_WiFi_MQTT/defines.h b/examples/STM32_WiFi_MQTT/defines.h index f3c1d5af..2f0c3389 100644 --- a/examples/STM32_WiFi_MQTT/defines.h +++ b/examples/STM32_WiFi_MQTT/defines.h @@ -271,7 +271,7 @@ ///////////////////////////////////////////// -#include +#include #define HOST_NAME "STM32-MQTT-Controller" diff --git a/examples/Teensy_WiFi/defines.h b/examples/Teensy_WiFi/defines.h index 3a199842..c8969df8 100644 --- a/examples/Teensy_WiFi/defines.h +++ b/examples/Teensy_WiFi/defines.h @@ -219,7 +219,7 @@ ///////////////////////////////////////////// -#include +#include #define HOST_NAME "Teensy-Master-Controller" diff --git a/examples/Teensy_WiFi_MQTT/Teensy_WiFi_MQTT.ino b/examples/Teensy_WiFi_MQTT/Teensy_WiFi_MQTT.ino index 83fb244c..51946a3d 100644 --- a/examples/Teensy_WiFi_MQTT/Teensy_WiFi_MQTT.ino +++ b/examples/Teensy_WiFi_MQTT/Teensy_WiFi_MQTT.ino @@ -9,34 +9,8 @@ Built by Khoi Hoang https://github.com/khoih-prog/WiFiManager_Generic_Lite Licensed under MIT license *****************************************************************************************************************************/ -/**************************************************************************************************************************** - You have to modify file ./libraries/Adafruit_MQTT_Library/Adafruit_MQTT.cpp as follows to avoid dtostrf error, if exists - - #ifdef __cplusplus - extern "C" { - #endif - extern char* itoa(int value, char *string, int radix); - extern char* ltoa(long value, char *string, int radix); - extern char* utoa(unsigned value, char *string, int radix); - extern char* ultoa(unsigned long value, char *string, int radix); - #ifdef __cplusplus - } // extern "C" - #endif - - //#if defined(ARDUINO_SAMD_ZERO) || defined(ARDUINO_SAMD_MKR1000) || defined(ARDUINO_ARCH_SAMD) - #if !( ESP32 || ESP8266 || defined(CORE_TEENSY) || defined(STM32F1) || defined(STM32F2) || defined(STM32F3) || defined(STM32F4) || defined(STM32F7) || \ - ( defined(ARDUINO_ARCH_RP2040) && !defined(ARDUINO_ARCH_MBED) ) || ARDUINO_ARCH_SEEED_SAMD || ( defined(SEEED_WIO_TERMINAL) || defined(SEEED_XIAO_M0) || \ - defined(SEEED_FEMTO_M0) || defined(Wio_Lite_MG126) || defined(WIO_GPS_BOARD) || defined(SEEEDUINO_ZERO) || defined(SEEEDUINO_LORAWAN) || defined(WIO_LTE_CAT) || \ - defined(SEEED_GROVE_UI_WIRELESS) ) ) - static char *dtostrf(double val, signed char width, unsigned char prec, char *sout) - { - char fmt[20]; - sprintf(fmt, "%%%d.%df", width, prec); - sprintf(sout, fmt, val); - return sout; - } - #endif - *****************************************************************************************************************************/ + +// You have to use forked and modified library https://github.com/khoih-prog/Adafruit_MQTT_Library #include "defines.h" #include "Credentials.h" diff --git a/examples/Teensy_WiFi_MQTT/defines.h b/examples/Teensy_WiFi_MQTT/defines.h index e18b7a62..f9131925 100644 --- a/examples/Teensy_WiFi_MQTT/defines.h +++ b/examples/Teensy_WiFi_MQTT/defines.h @@ -220,7 +220,7 @@ ///////////////////////////////////////////// -#include +#include #define HOST_NAME "Teensy-MQTT-Controller" diff --git a/examples/nRF52_WiFi/defines.h b/examples/nRF52_WiFi/defines.h index a857af7f..3536c45d 100644 --- a/examples/nRF52_WiFi/defines.h +++ b/examples/nRF52_WiFi/defines.h @@ -227,7 +227,7 @@ ///////////////////////////////////////////// -#include +#include #define HOST_NAME "nRF52-Master-Controller" diff --git a/examples/nRF52_WiFi_MQTT/defines.h b/examples/nRF52_WiFi_MQTT/defines.h index ec11974d..191ae775 100644 --- a/examples/nRF52_WiFi_MQTT/defines.h +++ b/examples/nRF52_WiFi_MQTT/defines.h @@ -226,7 +226,7 @@ ///////////////////////////////////////////// -#include +#include #define HOST_NAME "nRF52-MQTT-Controller" diff --git a/examples/nRF52_WiFi_MQTT/nRF52_WiFi_MQTT.ino b/examples/nRF52_WiFi_MQTT/nRF52_WiFi_MQTT.ino index b03a5ea6..66546050 100644 --- a/examples/nRF52_WiFi_MQTT/nRF52_WiFi_MQTT.ino +++ b/examples/nRF52_WiFi_MQTT/nRF52_WiFi_MQTT.ino @@ -9,35 +9,8 @@ Built by Khoi Hoang https://github.com/khoih-prog/WiFiManager_Generic_Lite Licensed under MIT license *****************************************************************************************************************************/ - -/**************************************************************************************************************************** - You have to modify file ./libraries/Adafruit_MQTT_Library/Adafruit_MQTT.cpp as follows to avoid dtostrf error, if exists - - #ifdef __cplusplus - extern "C" { - #endif - extern char* itoa(int value, char *string, int radix); - extern char* ltoa(long value, char *string, int radix); - extern char* utoa(unsigned value, char *string, int radix); - extern char* ultoa(unsigned long value, char *string, int radix); - #ifdef __cplusplus - } // extern "C" - #endif - - //#if defined(ARDUINO_SAMD_ZERO) || defined(ARDUINO_SAMD_MKR1000) || defined(ARDUINO_ARCH_SAMD) - #if !( ESP32 || ESP8266 || defined(CORE_TEENSY) || defined(STM32F1) || defined(STM32F2) || defined(STM32F3) || defined(STM32F4) || defined(STM32F7) || \ - ( defined(ARDUINO_ARCH_RP2040) && !defined(ARDUINO_ARCH_MBED) ) || ARDUINO_ARCH_SEEED_SAMD || ( defined(SEEED_WIO_TERMINAL) || defined(SEEED_XIAO_M0) || \ - defined(SEEED_FEMTO_M0) || defined(Wio_Lite_MG126) || defined(WIO_GPS_BOARD) || defined(SEEEDUINO_ZERO) || defined(SEEEDUINO_LORAWAN) || defined(WIO_LTE_CAT) || \ - defined(SEEED_GROVE_UI_WIRELESS) ) ) - static char *dtostrf(double val, signed char width, unsigned char prec, char *sout) - { - char fmt[20]; - sprintf(fmt, "%%%d.%df", width, prec); - sprintf(sout, fmt, val); - return sout; - } - #endif - *****************************************************************************************************************************/ + +// You have to use forked and modified library https://github.com/khoih-prog/Adafruit_MQTT_Library #include "defines.h" #include "Credentials.h"