66 Forked from Blynk library v0.6.1 https://github.com/blynkkk/blynk-library/releases
77 Built by Khoi Hoang https://github.com/khoih-prog/BlynkGSM_ESPManager
88 Licensed under MIT license
9- Version: 1.0.4
9+ Version: 1.0.5
1010
1111 Based on orignal code by Crosswalkersam (https://community.blynk.cc/u/Crosswalkersam)
1212 posted in https://community.blynk.cc/t/select-connection-type-via-switch/43176
2020 1.0.2 K Hoang 04/02/2020 Add Blynk WiFiManager support similar to Blynk_WM library
2121 1.0.3 K Hoang 24/02/2020 Add checksum, clearConfigData()
2222 1.0.4 K Hoang 14/03/2020 Enhance GUI. Reduce code size.
23+ 1.0.5 K Hoang 18/04/2020 MultiWiFi/Blynk. Dynamic custom parameters. SSID password maxlen is 63 now.
24+ Permit special chars # and % in input data.
2325 *****************************************************************************************************************************/
24-
26+ /* ***************************************************************************************************************************
27+ Important Notes:
28+ 1) Sketch is ~0.9MB of code because only 1 instance of Blynk if #define BLYNK_USE_BT_ONLY => true
29+ 2) Sketch is very large (~1.3MB code) because 2 instances of Blynk if #define BLYNK_USE_BT_ONLY => false
30+ 3) To conmpile, use Partition Scheem with large APP size, such as
31+ a) 8MB Flash (3MB APP, 1.5MB FAT) if use EEPROM
32+ b) No OTA (2MB APP, 2MB SPIFFS)
33+ c) No OTA (2MB APP, 2MB FATFS) if use EEPROM
34+ d) Huge APP (3MB No OTA, 1MB SPIFFS) <===== Preferable if use SPIFFS
35+ e) Minimal SPIFFS (1.9MB APP with OTA, 190KB SPIFFS)
36+ *****************************************************************************************************************************/
2537#ifndef ESP32
2638#error This code is intended to run on the ESP32 platform! Please check your Tools->Board setting.
2739#endif
2840
2941#define BLYNK_PRINT Serial
3042
43+ #define ESP32_BLE_WF_DEBUG true
44+
3145#define USE_BLYNK_WM true
3246// #define USE_BLYNK_WM false
3347
34- #define USE_SPIFFS true
35- // #define USE_SPIFFS false
48+ // #define USE_SPIFFS true
49+ #define USE_SPIFFS false
3650
3751#if (!USE_SPIFFS)
3852// EEPROM_SIZE must be <= 2048 and >= CONFIG_DATA_SIZE
3953#define EEPROM_SIZE (2 * 1024 )
4054// EEPROM_START + CONFIG_DATA_SIZE must be <= EEPROM_SIZE
41- #define EEPROM_START 768
55+ #define EEPROM_START 0
4256#endif
4357
4458// Force some params in Blynk, only valid for library version 1.0.1 and later
5872#if USE_BLYNK_WM
5973#warning Please select 1.3MB+ for APP (Minimal SPIFFS (1.9MB APP, OTA), HugeAPP(3MB APP, NoOTA) or NoOA(2MB APP)
6074#include < BlynkSimpleEsp32_WFM.h>
75+
76+ #define USE_DYNAMIC_PARAMETERS true
77+
78+ // ///////////// Start dynamic Credentials ///////////////
79+
80+ // Defined in <BlynkSimpleEsp32_WFM.h>
81+ /* *************************************
82+ #define MAX_ID_LEN 5
83+ #define MAX_DISPLAY_NAME_LEN 16
84+
85+ typedef struct
86+ {
87+ char id [MAX_ID_LEN + 1];
88+ char displayName [MAX_DISPLAY_NAME_LEN + 1];
89+ char *pdata;
90+ uint8_t maxlen;
91+ } MenuItem;
92+ **************************************/
93+
94+ #if USE_DYNAMIC_PARAMETERS
95+
96+ #define MAX_MQTT_SERVER_LEN 34
97+ char MQTT_Server [MAX_MQTT_SERVER_LEN + 1 ] = " " ;
98+
99+ #define MAX_MQTT_PORT_LEN 6
100+ char MQTT_Port [MAX_MQTT_PORT_LEN + 1 ] = " " ;
101+
102+ #define MAX_MQTT_USERNAME_LEN 34
103+ char MQTT_UserName [MAX_MQTT_USERNAME_LEN + 1 ] = " " ;
104+
105+ #define MAX_MQTT_PW_LEN 34
106+ char MQTT_PW [MAX_MQTT_PW_LEN + 1 ] = " " ;
107+
108+ #define MAX_MQTT_SUBS_TOPIC_LEN 34
109+ char MQTT_SubsTopic [MAX_MQTT_SUBS_TOPIC_LEN + 1 ] = " " ;
110+
111+ #define MAX_MQTT_PUB_TOPIC_LEN 34
112+ char MQTT_PubTopic [MAX_MQTT_PUB_TOPIC_LEN + 1 ] = " " ;
113+
114+ MenuItem myMenuItems [] =
115+ {
116+ { " mqtt" , " MQTT Server" , MQTT_Server, MAX_MQTT_SERVER_LEN },
117+ { " mqpt" , " Port" , MQTT_Port, MAX_MQTT_PORT_LEN },
118+ { " user" , " MQTT UserName" , MQTT_UserName, MAX_MQTT_USERNAME_LEN },
119+ { " mqpw" , " MQTT PWD" , MQTT_PW, MAX_MQTT_PW_LEN },
120+ { " subs" , " Subs Topics" , MQTT_SubsTopic, MAX_MQTT_SUBS_TOPIC_LEN },
121+ { " pubs" , " Pubs Topics" , MQTT_PubTopic, MAX_MQTT_PUB_TOPIC_LEN },
122+ };
123+
124+ #else
125+
126+ MenuItem myMenuItems [] = {};
127+
128+ #endif
129+
130+ uint16_t NUM_MENU_ITEMS = sizeof (myMenuItems) / sizeof (MenuItem); // MenuItemSize;
131+ // ///// // End dynamic Credentials ///////////
132+
61133#else
62134#include < BlynkSimpleEsp32_WF.h>
63135
@@ -77,23 +149,68 @@ char auth[] = "****";
77149bool USE_BLE = true ;
78150long timePreviousMeassure = 0 ;
79151
80- #define MEASURE_INTERVAL_MS 20000L
81-
82152#define WIFI_BLE_SELECTION_PIN 14 // Pin D14 mapped to pin GPIO14/HSPI_SCK/ADC16/TOUCH6/TMS of ESP32
83153
84- void checkStatus ()
154+ BlynkTimer timer;
155+
156+ #include < Ticker.h>
157+ Ticker led_ticker;
158+
159+ void set_led (byte status)
85160{
86- if (millis () - timePreviousMeassure > MEASURE_INTERVAL_MS)
161+ digitalWrite (LED_BUILTIN, status);
162+ }
163+
164+ void noticeAlive (void )
165+ {
166+ if (USE_BLE)
167+ Blynk_BLE.virtualWrite (V0, F (" OK" ));
168+ else
169+ Blynk_WF.virtualWrite (V0, F (" OK" ));
170+ }
171+
172+ void heartBeatPrint (void )
173+ {
174+ static int num = 1 ;
175+
176+ if (Blynk.connected ())
87177 {
178+ set_led (HIGH);
179+ led_ticker.once_ms (111 , set_led, (byte) LOW);
180+ Serial.print (" B" );
181+ }
182+ else
183+ {
184+ Serial.print (" F" );
185+ }
186+
187+ if (num == 80 )
188+ {
189+ Serial.println ();
190+ num = 1 ;
191+ }
192+ else if (num++ % 10 == 0 )
193+ {
194+ Serial.print (" " );
195+ }
196+ }
197+
198+ void checkStatus ()
199+ {
200+ static unsigned long checkstatus_timeout = 0 ;
201+
202+ #define STATUS_CHECK_INTERVAL 60000L
203+
204+ // Send status report every STATUS_REPORT_INTERVAL (60) seconds: we don't need to send updates frequently if there is no status change.
205+ if ((millis () > checkstatus_timeout) || (checkstatus_timeout == 0 ))
206+ {
88207 if (!USE_BLE)
89208 {
90- if (Blynk.connected ())
91- Serial.println (F (" B" ));
92- else
93- Serial.println (F (" F" ));
209+ // report Blynk connection
210+ heartBeatPrint ();
94211 }
95212
96- timePreviousMeassure = millis ();
213+ checkstatus_timeout = millis () + STATUS_CHECK_INTERVAL ;
97214 }
98215}
99216
@@ -104,19 +221,34 @@ void setup()
104221 Serial.begin (115200 );
105222 Serial.println (F (" \n Starting ESP32_BLE_WF" ));
106223
107- pinMode (WIFI_BLE_SELECTION_PIN, INPUT );
224+ pinMode (WIFI_BLE_SELECTION_PIN, INPUT_PULLUP );
108225
109226#if BLYNK_USE_BLE_ONLY
110227 Blynk_BLE.setDeviceName (BLE_Device_Name);
228+
229+ #if ESP32_BLE_WF_DEBUG
230+ Serial.println (F (" Blynk_BLE begin" ));
231+ #endif
232+
111233 Blynk_BLE.begin (auth);
112234#else
113235 if (digitalRead (WIFI_BLE_SELECTION_PIN) == HIGH)
114236 {
115237 USE_BLE = false ;
116238 Serial.println (F (" GPIO14 HIGH, Use WiFi" ));
117239#if USE_BLYNK_WM
240+ #if ESP32_BLE_WF_DEBUG
241+ Serial.println (F (" USE_BLYNK_WM: Blynk_WF begin" ));
242+ #endif
243+ // Set config portal channel, defalut = 1. Use 0 => random channel from 1-13 to avoid conflict
244+ Blynk_WF.setConfigPortalChannel (0 );
245+
118246 Blynk_WF.begin (BLE_Device_Name);
119247#else
248+ // Blynk_WF.begin(auth, ssid, pass);
249+ #if ESP32_BLE_WF_DEBUG
250+ Serial.println (F (" Not USE_BLYNK_WM: Blynk_WF begin" ));
251+ #endif
120252 Blynk_WF.begin (auth, ssid, pass, cloudBlynkServer.c_str (), BLYNK_SERVER_HARDWARE_PORT);
121253#endif
122254 }
@@ -126,10 +258,15 @@ void setup()
126258 Serial.println (F (" GPIO14 LOW, Use BLE" ));
127259 Blynk_BLE.setDeviceName (BLE_Device_Name);
128260#if USE_BLYNK_WM
129- if (Blynk_WF.getBlynkBLEToken () == String (" nothing " ))
261+ if (Blynk_WF.getBlynkBLEToken () == NO_CONFIG) // String("blank "))
130262 {
131263 Serial.println (F (" No valid stored BLE auth. Have to run WiFi then enter config portal" ));
132264 USE_BLE = false ;
265+
266+ #if ESP32_BLE_WF_DEBUG
267+ Serial.println (F (" USE_BLYNK_WM: No BLE Token. Blynk_WF begin" ));
268+ #endif
269+
133270 Blynk_WF.begin (BLE_Device_Name);
134271 }
135272 String BLE_auth = Blynk_WF.getBlynkBLEToken ();
@@ -141,11 +278,32 @@ void setup()
141278 {
142279 Serial.print (F (" Connecting Blynk via BLE, using auth = " ));
143280 Serial.println (BLE_auth);
281+
282+ #if ESP32_BLE_WF_DEBUG
283+ Serial.println (F (" USE_BLE: Blynk_BLE begin" ));
284+ #endif
285+
144286 Blynk_BLE.begin (BLE_auth.c_str ());
145287 }
146288 }
147289#endif
290+
291+ // Important, need to keep constant communication to Blynk Server at least once per ~25s
292+ // Or Blynk will lost and have to (auto)reconnect
293+ timer.setInterval (10000L , noticeAlive);
294+ }
295+
296+ #if (USE_BLYNK_WM && USE_DYNAMIC_PARAMETERS)
297+ void displayCredentials (void )
298+ {
299+ Serial.println (" \n Your stored Credentials :" );
300+
301+ for (int i = 0 ; i < NUM_MENU_ITEMS; i++)
302+ {
303+ Serial.println (String (myMenuItems[i].displayName ) + " = " + myMenuItems[i].pdata );
304+ }
148305}
306+ #endif
149307
150308void loop ()
151309{
@@ -158,5 +316,27 @@ void loop()
158316 Blynk_WF.run ();
159317#endif
160318
319+ timer.run ();
161320 checkStatus ();
321+
322+ #if (USE_BLYNK_WM && USE_DYNAMIC_PARAMETERS)
323+ static bool displayedCredentials = false ;
324+
325+ if (!displayedCredentials)
326+ {
327+ for (int i = 0 ; i < NUM_MENU_ITEMS; i++)
328+ {
329+ if (!strlen (myMenuItems[i].pdata ))
330+ {
331+ break ;
332+ }
333+
334+ if ( i == (NUM_MENU_ITEMS - 1 ) )
335+ {
336+ displayedCredentials = true ;
337+ displayCredentials ();
338+ }
339+ }
340+ }
341+ #endif
162342}
0 commit comments