77 * Forked from Blynk library v0.6.1 https://github.com/blynkkk/blynk-library/releases
88 * Built by Khoi Hoang https://github.com/khoih-prog/Blynk_WM
99 * Licensed under MIT license
10- * Version: 1.0.4
10+ * Version: 1.0.5
1111 *
1212 * Original Blynk Library author:
1313 * @file BlynkSimpleEsp8266.h
2424 * 1.0.2 K Hoang 21/11/2019 Fix bug. Add features.
2525 * 1.0.3 K Hoang 31/11/2019 Fix compiler errors for ESP8266 core pre-2.5.2. Add examples.
2626 * 1.0.4 K Hoang 07/01/2020 Add configurable personalized RFC-952 DHCP hostname
27+ * 1.0.5 K Hoang 20/01/2020 Add configurable static IP, GW, SN, DNS1, DNS2 and Config Portal static IP and Credentials
2728 *****************************************************************************************************************************/
2829
2930
@@ -161,6 +162,13 @@ class BlynkWifi
161162 {
162163 BLYNK_LOG2 (BLYNK_F (" Connecting to " ), ssid);
163164 WiFi.mode (WIFI_STA);
165+ // New from v1.0.5
166+ if (static_IP != IPAddress (0 , 0 , 0 , 0 ))
167+ {
168+ BLYNK_LOG1 (BLYNK_F (" Use static IP" ));
169+ WiFi.config (static_IP, static_GW, static_SN, static_DNS1, static_DNS2);
170+ }
171+
164172 setHostname ();
165173
166174 if (WiFi.status () != WL_CONNECTED)
@@ -179,9 +187,9 @@ class BlynkWifi
179187 }
180188
181189 BLYNK_LOG1 (BLYNK_F (" Connected to WiFi" ));
182-
183- IPAddress myip = WiFi.localIP ( );
184- BLYNK_LOG_IP ( " IP: " , myip );
190+ BLYNK_LOG6 ( BLYNK_F ( " IP = " ), WiFi. localIP (). toString (), BLYNK_F ( " , GW = " ), WiFi. gatewayIP (). toString (),
191+ BLYNK_F ( " , SN = " ), WiFi.subnetMask (). toString () );
192+ BLYNK_LOG4 ( BLYNK_F ( " DNS1 = " ), WiFi. dnsIP ( 0 ). toString (), BLYNK_F ( " , DNS2 = " ), WiFi. dnsIP ( 1 ). toString () );
185193 }
186194
187195 void config (const char * auth,
@@ -272,7 +280,7 @@ class BlynkWifi
272280 }
273281 else
274282 {
275- BLYNK_LOG1 (BLYNK_F (" begin: WiFi connected but Blynk not connected" ));
283+ BLYNK_LOG1 (BLYNK_F (" begin: WiFi connected but Bynk not connected" ));
276284 // failed to connect to Blynk server, will start configuration mode
277285 startConfigurationMode ();
278286 }
@@ -417,12 +425,57 @@ class BlynkWifi
417425 WiFi.hostname (RFC952_hostname);
418426 }
419427 }
420-
428+
429+ void setConfigPortalIP (IPAddress portalIP = IPAddress(192 , 168 , 4 , 1 ))
430+ {
431+ portal_apIP = portalIP;
432+ }
433+
434+ void setConfigPortal (String ssid = " " , String pass = " " )
435+ {
436+ portal_ssid = ssid;
437+ portal_pass = pass;
438+ }
439+
440+ void setSTAStaticIPConfig (IPAddress ip, IPAddress gw, IPAddress sn = IPAddress(255 , 255 , 255 , 0 ),
441+ IPAddress dns_address_1 = IPAddress(0 , 0 , 0 , 0 ),
442+ IPAddress dns_address_2 = IPAddress(0 , 0 , 0 , 0 ))
443+ {
444+ static_IP = ip;
445+ static_GW = gw;
446+ static_SN = sn;
447+
448+ // Default to local GW
449+ if (dns_address_1 == IPAddress (0 , 0 , 0 , 0 ))
450+ static_DNS1 = gw;
451+ else
452+ static_DNS1 = dns_address_1;
453+
454+ // Default to Google DNS (8, 8, 8, 8)
455+ if (dns_address_2 == IPAddress (0 , 0 , 0 , 0 ))
456+ static_DNS2 = IPAddress (8 , 8 , 8 , 8 );
457+ else
458+ static_DNS2 = dns_address_2;
459+ }
460+
421461private:
422462 ESP8266WebServer server;
423463 bool configuration_mode = false ;
424464 struct Configuration Blynk8266_WM_config;
425-
465+
466+ // For Config Portal, from v1.0.5
467+ IPAddress portal_apIP = IPAddress(192 , 168 , 4 , 1 );
468+
469+ String portal_ssid = " " ;
470+ String portal_pass = " " ;
471+
472+ // For static IP, from v1.0.5
473+ IPAddress static_IP = IPAddress(0 , 0 , 0 , 0 );
474+ IPAddress static_GW = IPAddress(0 , 0 , 0 , 0 );
475+ IPAddress static_SN = IPAddress(255 , 255 , 255 , 0 );
476+ IPAddress static_DNS1 = IPAddress(0 , 0 , 0 , 0 );
477+ IPAddress static_DNS2 = IPAddress(0 , 0 , 0 , 0 );
478+
426479 #define RFC952_HOSTNAME_MAXLEN 24
427480 char RFC952_hostname[RFC952_HOSTNAME_MAXLEN + 1 ];
428481
@@ -657,6 +710,13 @@ class BlynkWifi
657710
658711 WiFi.mode (WIFI_STA);
659712 setHostname ();
713+
714+ // New from v1.0.5
715+ if (static_IP != IPAddress (0 , 0 , 0 , 0 ))
716+ {
717+ BLYNK_LOG1 (BLYNK_F (" Use static IP" ));
718+ WiFi.config (static_IP, static_GW, static_SN, static_DNS1, static_DNS2);
719+ }
660720
661721 BLYNK_LOG1 (BLYNK_F (" connectToWifi: start" ));
662722
@@ -678,8 +738,9 @@ class BlynkWifi
678738 if (WiFi.status () == WL_CONNECTED)
679739 {
680740 BLYNK_LOG1 (BLYNK_F (" connectToWifi: connected OK" ));
681- IPAddress myip = WiFi.localIP ();
682- BLYNK_LOG_IP (" IP: " , myip);
741+ BLYNK_LOG6 (BLYNK_F (" IP = " ), WiFi.localIP ().toString (), BLYNK_F (" , GW = " ), WiFi.gatewayIP ().toString (),
742+ BLYNK_F (" , SN = " ), WiFi.subnetMask ().toString ());
743+ BLYNK_LOG4 (BLYNK_F (" DNS1 = " ), WiFi.dnsIP (0 ).toString (), BLYNK_F (" , DNS2 = " ), WiFi.dnsIP (1 ).toString ());
683744 }
684745 else
685746 {
@@ -800,22 +861,24 @@ class BlynkWifi
800861 // turn the LED_BUILTIN ON to tell us we are in configuration mode.
801862 digitalWrite (LED_BUILTIN, LOW);
802863
803- String chipID = String (ESP.getChipId (), HEX);
804- chipID.toUpperCase ();
805-
806- String ssid = " ESP_" + chipID;
864+ if ( (portal_ssid == " " ) || portal_pass == " " )
865+ {
866+ String chipID = String (ESP.getChipId (), HEX);
867+ chipID.toUpperCase ();
868+
869+ portal_ssid = " ESP_" + chipID;
807870
808- String pass = " MyESP_" + chipID;
871+ portal_pass = " MyESP_" + chipID;
872+ }
809873
810- BLYNK_LOG4 (BLYNK_F (" startConfigurationMode with SSID = " ), ssid, BLYNK_F (" and PW = " ), pass);
874+ BLYNK_LOG6 (BLYNK_F (" startConfigMode with SSID = " ), portal_ssid, BLYNK_F (" , PW = " ), portal_pass,
875+ BLYNK_F (" and IP = " ), portal_apIP.toString ());
811876
812- IPAddress apIP (192 , 168 , 4 , 1 );
813-
814877 WiFi.mode (WIFI_AP);
815- WiFi.softAP (ssid .c_str (), pass .c_str ());
878+ WiFi.softAP (portal_ssid .c_str (), portal_pass .c_str ());
816879
817880 delay (100 ); // ref: https://github.com/espressif/arduino-esp32/issues/985#issuecomment-359157428
818- WiFi.softAPConfig (apIP, apIP , IPAddress (255 , 255 , 255 , 0 ));
881+ WiFi.softAPConfig (portal_apIP, portal_apIP , IPAddress (255 , 255 , 255 , 0 ));
819882
820883 // See https://stackoverflow.com/questions/39803135/c-unresolved-overloaded-function-type?rq=1
821884 server.on (" /" , [this ](){ handleRequest (); });
0 commit comments