Skip to content
This repository was archived by the owner on Nov 25, 2021. It is now read-only.

Commit aba886f

Browse files
authored
Update for v1.0.5
Add new features to enable : 1. configuring Portal Static IP address, Name and Password. 2. configuring Static IP address, Gateway, Subnet Mask and 2 DNS Servers IP addresses.
1 parent 6a11af9 commit aba886f

File tree

2 files changed

+163
-36
lines changed

2 files changed

+163
-36
lines changed

src/BlynkSimpleEsp32_WM.h

Lines changed: 81 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
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
@@ -24,6 +24,7 @@
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
#ifndef BlynkSimpleEsp32_WM_h
@@ -155,6 +156,13 @@ class BlynkWifi
155156
{
156157
BLYNK_LOG2(BLYNK_F("Connecting to "), ssid);
157158
WiFi.mode(WIFI_STA);
159+
160+
// New from v1.0.5
161+
if (static_IP != IPAddress(0, 0, 0, 0))
162+
{
163+
BLYNK_LOG1(BLYNK_F("Use static IP"));
164+
WiFi.config(static_IP, static_GW, static_SN, static_DNS1, static_DNS2);
165+
}
158166
setHostname();
159167

160168
if (pass && strlen(pass)) {
@@ -167,8 +175,9 @@ class BlynkWifi
167175
}
168176
BLYNK_LOG1(BLYNK_F("Connected to WiFi"));
169177

170-
IPAddress myip = WiFi.localIP();
171-
BLYNK_LOG_IP("IP: ", myip);
178+
BLYNK_LOG6(BLYNK_F("IP = "), WiFi.localIP().toString(), BLYNK_F(", GW = "), WiFi.gatewayIP().toString(),
179+
BLYNK_F(", SN = "), WiFi.subnetMask().toString());
180+
BLYNK_LOG4(BLYNK_F("DNS1 = "), WiFi.dnsIP(0).toString(), BLYNK_F(", DNS2 = "), WiFi.dnsIP(1).toString());
172181
}
173182

174183
void config(const char* auth,
@@ -412,12 +421,57 @@ class BlynkWifi
412421
WiFi.setHostname(RFC952_hostname);
413422
}
414423
}
424+
425+
void setConfigPortalIP(IPAddress portalIP = IPAddress(192, 168, 4, 1))
426+
{
427+
portal_apIP = portalIP;
428+
}
429+
430+
void setConfigPortal(String ssid = "", String pass = "")
431+
{
432+
portal_ssid = ssid;
433+
portal_pass = pass;
434+
}
435+
436+
void setSTAStaticIPConfig(IPAddress ip, IPAddress gw, IPAddress sn = IPAddress(255, 255, 255, 0),
437+
IPAddress dns_address_1 = IPAddress(0, 0, 0, 0),
438+
IPAddress dns_address_2 = IPAddress(0, 0, 0, 0))
439+
{
440+
static_IP = ip;
441+
static_GW = gw;
442+
static_SN = sn;
443+
444+
// Default to local GW
445+
if (dns_address_1 == IPAddress(0, 0, 0, 0))
446+
static_DNS1 = gw;
447+
else
448+
static_DNS1 = dns_address_1;
415449

450+
// Default to Google DNS (8, 8, 8, 8)
451+
if (dns_address_2 == IPAddress(0, 0, 0, 0))
452+
static_DNS2 = IPAddress(8, 8, 8, 8);
453+
else
454+
static_DNS2 = dns_address_2;
455+
}
456+
416457
private:
417458
WebServer server;
418459
bool configuration_mode = false;
419460
struct Configuration BlynkESP32_WM_config;
461+
462+
// For Config Portal, from v1.0.5
463+
IPAddress portal_apIP = IPAddress(192, 168, 4, 1);
464+
465+
String portal_ssid = "";
466+
String portal_pass = "";
420467

468+
// For static IP, from v1.0.5
469+
IPAddress static_IP = IPAddress(0, 0, 0, 0);
470+
IPAddress static_GW = IPAddress(0, 0, 0, 0);
471+
IPAddress static_SN = IPAddress(255, 255, 255, 0);
472+
IPAddress static_DNS1 = IPAddress(0, 0, 0, 0);
473+
IPAddress static_DNS2 = IPAddress(0, 0, 0, 0);
474+
421475
#define RFC952_HOSTNAME_MAXLEN 24
422476
char RFC952_hostname[RFC952_HOSTNAME_MAXLEN + 1];
423477

@@ -655,6 +709,13 @@ class BlynkWifi
655709
WiFi.mode(WIFI_STA);
656710
setHostname();
657711

712+
// New from v1.0.5
713+
if (static_IP != IPAddress(0, 0, 0, 0))
714+
{
715+
BLYNK_LOG1(BLYNK_F("Use static IP"));
716+
WiFi.config(static_IP, static_GW, static_SN, static_DNS1, static_DNS2);
717+
}
718+
658719
BLYNK_LOG1(BLYNK_F("connectToWifi: start"));
659720

660721
if (BlynkESP32_WM_config.wifi_passphrase && strlen(BlynkESP32_WM_config.wifi_passphrase))
@@ -675,8 +736,9 @@ class BlynkWifi
675736
if (WiFi.status() == WL_CONNECTED)
676737
{
677738
BLYNK_LOG1(BLYNK_F("connectToWifi: connected OK"));
678-
IPAddress myip = WiFi.localIP();
679-
BLYNK_LOG_IP("IP: ", myip);
739+
BLYNK_LOG6(BLYNK_F("IP = "), WiFi.localIP().toString(), BLYNK_F(", GW = "), WiFi.gatewayIP().toString(),
740+
BLYNK_F(", SN = "), WiFi.subnetMask().toString());
741+
BLYNK_LOG4(BLYNK_F("DNS1 = "), WiFi.dnsIP(0).toString(), BLYNK_F(", DNS2 = "), WiFi.dnsIP(1).toString());
680742
}
681743
else
682744
{
@@ -794,24 +856,26 @@ class BlynkWifi
794856
#define CONFIG_TIMEOUT 60000L
795857

796858
// turn the LED_BUILTIN ON to tell us we are in configuration mode.
797-
digitalWrite(LED_BUILTIN, HIGH);
798-
799-
String chipID = String(ESP_getChipId(), HEX);
800-
chipID.toUpperCase();
859+
digitalWrite(LED_BUILTIN, HIGH);
801860

802-
String ssid = "ESP_" + chipID;
861+
if ( (portal_ssid == "") || portal_pass == "" )
862+
{
863+
String chipID = String(ESP_getChipId(), HEX);
864+
chipID.toUpperCase();
865+
866+
portal_ssid = "ESP_" + chipID;
803867

804-
String pass = "MyESP_" + chipID;
868+
portal_pass = "MyESP_" + chipID;
869+
}
805870

806-
BLYNK_LOG4(BLYNK_F("startConfigurationMode with SSID = "), ssid, BLYNK_F(" and PW = "), pass);
807-
808-
IPAddress apIP(192, 168, 4, 1);
809-
871+
BLYNK_LOG6(BLYNK_F("startConfigMode with SSID = "), portal_ssid, BLYNK_F(", PW = "), portal_pass,
872+
BLYNK_F(" and IP = "), portal_apIP.toString());
873+
810874
WiFi.mode(WIFI_AP);
811-
WiFi.softAP(ssid.c_str(), pass.c_str());
875+
WiFi.softAP(portal_ssid.c_str(), portal_pass.c_str());
812876

813877
delay(100); // ref: https://github.com/espressif/arduino-esp32/issues/985#issuecomment-359157428
814-
WiFi.softAPConfig(apIP, apIP, IPAddress(255, 255, 255, 0));
878+
WiFi.softAPConfig(portal_apIP, portal_apIP, IPAddress(255, 255, 255, 0));
815879

816880
//See https://stackoverflow.com/questions/39803135/c-unresolved-overloaded-function-type?rq=1
817881
server.on("/", [this](){ handleRequest(); });

src/BlynkSimpleEsp8266_WM.h

Lines changed: 82 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
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
@@ -24,6 +24,7 @@
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+
421461
private:
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

Comments
 (0)