@@ -241,6 +241,114 @@ class BlynkWifi
241241 while (this ->connect () != true ) {}
242242 }
243243
244+ #define LED_BUILTIN 2 // Pin D2 mapped to pin GPIO2/ADC12 of ESP32, control on-board LED
245+
246+ void begin (void )
247+ {
248+ #define TIMEOUT_CONNECT_WIFI 30000
249+
250+ getConfigData ();
251+
252+ Base::begin (BlynkESP32_WM_config.blynk_token );
253+ this ->conn .begin (BlynkESP32_WM_config.blynk_server , BlynkESP32_WM_config.blynk_port );
254+
255+ if (connectToWifi (TIMEOUT_CONNECT_WIFI))
256+ {
257+ BLYNK_LOG1 (BLYNK_F (" begin: WiFi connected. Try connecting to Blynk" ));
258+
259+ int i = 0 ;
260+ while ( (i++ < 10 ) && !this ->connect () )
261+ {
262+ }
263+
264+ if (this ->connected ())
265+ {
266+ BLYNK_LOG1 (BLYNK_F (" begin: WiFi and Blynk connected" ));
267+ }
268+ else
269+ {
270+ BLYNK_LOG1 (BLYNK_F (" begin: WiFi connected but Bynk not connected" ));
271+ // failed to connect to Blynk server, will start configuration mode
272+ // Turn the LED_BUILTIN ON in configuration mode. ESP32 LED_BUILDIN is correct polarity, HIGH to turn ON
273+ digitalWrite (LED_BUILTIN, HIGH);
274+ startConfigurationMode ();
275+ }
276+ }
277+ else
278+ {
279+ BLYNK_LOG1 (BLYNK_F (" begin: Fail to connect WiFi and Blynk" ));
280+ // failed to connect to Blynk server, will start configuration mode
281+ // Turn the LED_BUILTIN ON in configuration mode. ESP32 LED_BUILDIN is correct polarity, HIGH to turn ON
282+ digitalWrite (LED_BUILTIN, HIGH);
283+ startConfigurationMode ();
284+ }
285+ }
286+
287+ void run ()
288+ {
289+ #define TIMEOUT_RECONNECT_WIFI 10000
290+
291+ // Lost connection in running. Give chance to reconfig.
292+ if ( WiFi.status () != WL_CONNECTED || !this ->connected () )
293+ {
294+ if (configuration_mode)
295+ {
296+ server.handleClient ();
297+ return ;
298+ }
299+ else
300+ {
301+ // Not in config mode, try reconnecting before force to config mode
302+ if ( WiFi.status () != WL_CONNECTED )
303+ {
304+ BLYNK_LOG1 (BLYNK_F (" run: WiFi lost. Try reconnecting WiFi and Blynk" ));
305+ if (connectToWifi (TIMEOUT_RECONNECT_WIFI))
306+ {
307+ BLYNK_LOG1 (BLYNK_F (" run: WiFi reconnected. Trying connect to Blynk" ));
308+
309+ if (this ->connect ())
310+ {
311+ BLYNK_LOG1 (BLYNK_F (" run: WiFi and Blynk reconnected" ));
312+ }
313+ }
314+ }
315+ else
316+ {
317+ BLYNK_LOG1 (BLYNK_F (" run: Blynk lost. Try connecting Blynk" ));
318+ if (this ->connect ())
319+ {
320+ BLYNK_LOG1 (BLYNK_F (" run: Blynk reconnected" ));
321+ }
322+ }
323+
324+ // BLYNK_LOG1(BLYNK_F("run: Lost connection => configMode"));
325+ // startConfigurationMode();
326+ }
327+ }
328+ else if (configuration_mode)
329+ {
330+ configuration_mode = false ;
331+ BLYNK_LOG1 (BLYNK_F (" run: got WiFi/Blynk back, great" ));
332+ // Turn the LED_BUILTIN OFF when out of configuration mode. ESP32 LED_BUILDIN is correct polarity, LOW to turn OFF
333+ digitalWrite (LED_BUILTIN, LOW);
334+ }
335+
336+ if (this ->connected ())
337+ {
338+ Base::run ();
339+ }
340+ }
341+
342+ String getBoardName ()
343+ {
344+ return (String (BlynkESP32_WM_config.board_name ));
345+ }
346+
347+ private:
348+ WebServer server;
349+ boolean configuration_mode = false ;
350+ struct Configuration BlynkESP32_WM_config;
351+
244352#if USE_SPIFFS
245353
246354 #define CONFIG_FILENAME BLYNK_F (" /wmssl_conf.dat" )
@@ -396,114 +504,6 @@ class BlynkWifi
396504
397505#endif
398506
399- #define LED_BUILTIN 2 // Pin D2 mapped to pin GPIO2/ADC12 of ESP32, control on-board LED
400-
401- void begin (void )
402- {
403- #define TIMEOUT_CONNECT_WIFI 30000
404-
405- getConfigData ();
406-
407- Base::begin (BlynkESP32_WM_config.blynk_token );
408- this ->conn .begin (BlynkESP32_WM_config.blynk_server , BlynkESP32_WM_config.blynk_port );
409-
410- if (connectToWifi (TIMEOUT_CONNECT_WIFI))
411- {
412- BLYNK_LOG1 (BLYNK_F (" begin: WiFi connected. Try connecting to Blynk" ));
413-
414- int i = 0 ;
415- while ( (i++ < 10 ) && !this ->connect () )
416- {
417- }
418-
419- if (this ->connected ())
420- {
421- BLYNK_LOG1 (BLYNK_F (" begin: WiFi and Blynk connected" ));
422- }
423- else
424- {
425- BLYNK_LOG1 (BLYNK_F (" begin: WiFi connected but Bynk not connected" ));
426- // failed to connect to Blynk server, will start configuration mode
427- // Turn the LED_BUILTIN ON in configuration mode. ESP32 LED_BUILDIN is correct polarity, HIGH to turn ON
428- digitalWrite (LED_BUILTIN, HIGH);
429- startConfigurationMode ();
430- }
431- }
432- else
433- {
434- BLYNK_LOG1 (BLYNK_F (" begin: Fail to connect WiFi and Blynk" ));
435- // failed to connect to Blynk server, will start configuration mode
436- // Turn the LED_BUILTIN ON in configuration mode. ESP32 LED_BUILDIN is correct polarity, HIGH to turn ON
437- digitalWrite (LED_BUILTIN, HIGH);
438- startConfigurationMode ();
439- }
440- }
441-
442- void run ()
443- {
444- #define TIMEOUT_RECONNECT_WIFI 10000
445-
446- // Lost connection in running. Give chance to reconfig.
447- if ( WiFi.status () != WL_CONNECTED || !this ->connected () )
448- {
449- if (configuration_mode)
450- {
451- server.handleClient ();
452- return ;
453- }
454- else
455- {
456- // Not in config mode, try reconnecting before force to config mode
457- if ( WiFi.status () != WL_CONNECTED )
458- {
459- BLYNK_LOG1 (BLYNK_F (" run: WiFi lost. Try reconnecting WiFi and Blynk" ));
460- if (connectToWifi (TIMEOUT_RECONNECT_WIFI))
461- {
462- BLYNK_LOG1 (BLYNK_F (" run: WiFi reconnected. Trying connect to Blynk" ));
463-
464- if (this ->connect ())
465- {
466- BLYNK_LOG1 (BLYNK_F (" run: WiFi and Blynk reconnected" ));
467- }
468- }
469- }
470- else
471- {
472- BLYNK_LOG1 (BLYNK_F (" run: Blynk lost. Try connecting Blynk" ));
473- if (this ->connect ())
474- {
475- BLYNK_LOG1 (BLYNK_F (" run: Blynk reconnected" ));
476- }
477- }
478-
479- // BLYNK_LOG1(BLYNK_F("run: Lost connection => configMode"));
480- // startConfigurationMode();
481- }
482- }
483- else if (configuration_mode)
484- {
485- configuration_mode = false ;
486- BLYNK_LOG1 (BLYNK_F (" run: got WiFi/Blynk back, great" ));
487- // Turn the LED_BUILTIN OFF when out of configuration mode. ESP32 LED_BUILDIN is correct polarity, LOW to turn OFF
488- digitalWrite (LED_BUILTIN, LOW);
489- }
490-
491- if (this ->connected ())
492- {
493- Base::run ();
494- }
495- }
496-
497- String getBoardName ()
498- {
499- return (String (BlynkESP32_WM_config.board_name ));
500- }
501-
502- private:
503- WebServer server;
504- boolean configuration_mode = false ;
505- struct Configuration BlynkESP32_WM_config;
506-
507507 boolean connectToWifi (int timeout)
508508 {
509509 int sleep_time = 250 ;
0 commit comments