Skip to content

Commit fc83cd8

Browse files
committed
Added support for DHCP, added WiFi signal strength info
1 parent 54b2d0f commit fc83cd8

File tree

3 files changed

+33
-15
lines changed

3 files changed

+33
-15
lines changed

src/BootstrapManager.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -512,4 +512,11 @@ void BootstrapManager::launchWebServerForOTAConfig() {
512512

513513
return wifiManager.launchWebServerForOTAConfig();
514514

515+
}
516+
517+
// get the wifi quality
518+
int BootstrapManager::getWifiQuality() {
519+
520+
return wifiManager.getQuality();
521+
515522
}

src/BootstrapManager.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ class BootstrapManager {
7272
String readValueFromFile(String filename, String paramName); // read a param from a json file
7373
bool isWifiConfigured(); // check if wifi is correctly configured
7474
void launchWebServerForOTAConfig(); // if no ssid available, launch web server to get config params via browser
75+
int getWifiQuality(); // get the wifi quality
7576

7677
};
7778

src/WifiManager.cpp

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -69,22 +69,29 @@ void WifiManager::setupWiFi(void (*manageDisconnections)(), void (*manageHardwar
6969
WiFi.mode(WIFI_STA); // Disable AP mode
7070
//WiFi.setSleepMode(WIFI_NONE_SLEEP);
7171
WiFi.setAutoConnect(true);
72-
WiFi.config(IPAddress(helper.getValue(microcontrollerIP,'.',0).toInt(),
73-
helper.getValue(microcontrollerIP,'.',1).toInt(),
74-
helper.getValue(microcontrollerIP,'.',2).toInt(),
75-
helper.getValue(microcontrollerIP,'.',3).toInt()),
76-
IPAddress(helper.getValue(IP_GATEWAY,'.',0).toInt(),
77-
helper.getValue(IP_GATEWAY,'.',1).toInt(),
78-
helper.getValue(IP_GATEWAY,'.',2).toInt(),
79-
helper.getValue(IP_GATEWAY,'.',3).toInt()),
80-
IPAddress(helper.getValue(IP_DNS,'.',0).toInt(),
81-
helper.getValue(IP_DNS,'.',1).toInt(),
82-
helper.getValue(IP_DNS,'.',2).toInt(),
83-
helper.getValue(IP_DNS,'.',3).toInt()));
72+
if (!microcontrollerIP.equals("DHCP")) {
73+
WiFi.config(IPAddress(helper.getValue(microcontrollerIP, '.', 0).toInt(),
74+
helper.getValue(microcontrollerIP, '.', 1).toInt(),
75+
helper.getValue(microcontrollerIP, '.', 2).toInt(),
76+
helper.getValue(microcontrollerIP, '.', 3).toInt()),
77+
IPAddress(helper.getValue(IP_GATEWAY, '.', 0).toInt(),
78+
helper.getValue(IP_GATEWAY, '.', 1).toInt(),
79+
helper.getValue(IP_GATEWAY, '.', 2).toInt(),
80+
helper.getValue(IP_GATEWAY, '.', 3).toInt()),
81+
IPAddress(helper.getValue(IP_DNS, '.', 0).toInt(),
82+
helper.getValue(IP_DNS, '.', 1).toInt(),
83+
helper.getValue(IP_DNS, '.', 2).toInt(),
84+
helper.getValue(IP_DNS, '.', 3).toInt()));
85+
} else {
86+
Serial.println("Using DHCP");
87+
}
8488
#if defined(ESP8266)
8589
WiFi.hostname(helper.string2char(deviceName));
8690
// Set wifi power in dbm range 0/0.25, set to 0 to reduce PIR false positive due to wifi power, 0 low, 20.5 max.
8791
WiFi.setOutputPower(WIFI_POWER);
92+
if (microcontrollerIP.equals("DHCP")) {
93+
WiFi.config(0U, 0U,0U);
94+
}
8895
#elif defined(ESP32)
8996
WiFi.setHostname(helper.string2char(deviceName));
9097
btStop();
@@ -294,14 +301,14 @@ void WifiManager::createWebServer() {
294301
server.on("/", []() {
295302
IPAddress ip = WiFi.softAPIP();
296303
String ipStr = String(ip[0]) + '.' + String(ip[1]) + '.' + String(ip[2]) + '.' + String(ip[3]);
297-
content = "<!DOCTYPE HTML>\r\n<html><head><style>body {padding:0% 5% 0% 5%;font-size:4vw;width: 90%;font-weight:bold;text-align:center; color:#202020}#centerContainer { margin: 0px auto; }input {font-size:4vw;width: 100%;padding: 12px 20px;margin: 8px 0;box-sizing: border-box;border: 3px solid orange;-webkit-transition: 0.5s;transition: 0.5s;outline: none;}input:focus {border: 3px solid #BF5F00;font-weight:bold;}#wifi {font-family: 'Trebuchet MS', Arial, Helvetica, sans-serif;border-collapse: collapse;width: 100%;}#wifi td, #wifi th {border: 1px solid #ddd;padding: 8px;}#wifi tr:nth-child(even){background-color: #f2f2f2;}#wifi tr:hover {background-color: #ddd;}#wifi th {padding-top: 12px;padding-bottom: 12px;text-align: left; background-color: orange;color: white;}.button {background-color: orange;border: none;color: white;padding: 20px;text-align: center;text-decoration: none;display: inline-block;font-size: 16px;margin: 4px 2px;cursor: pointer;}.button3 {font-size:4vw;border-radius: 8px;width:100%;font-weight:bold;}label{font-size:4vw;}</style></head><body><div id='centerContainer'>";
304+
content = "<!DOCTYPE HTML>\r\n<html><head><style>body {padding:0% 5% 0% 5%;font-size:4vw;width: 90%;font-weight:bold;text-align:center; color:#202020}#centerContainer { margin: 0px auto; }input {font-size:4vw;width: 100%;padding: 12px 20px;margin: 8px 0;box-sizing: border-box;border: 6px solid orange;-webkit-transition: 0.5s;transition: 0.5s;outline: none;}input:focus {border: 12px solid #BF5F00;font-weight:bold;}#wifi {font-family: 'Trebuchet MS', Arial, Helvetica, sans-serif;border-collapse: collapse;width: 100%;}#wifi td, #wifi th {border: 1px solid #ddd;padding: 8px;}#wifi tr:nth-child(even){background-color: #f2f2f2;}#wifi tr:hover {background-color: #ddd;}#wifi th {padding-top: 12px;padding-bottom: 12px;text-align: left; background-color: orange;color: white;}.button {background-color: orange;border: none;color: white;padding: 20px;text-align: center;text-decoration: none;display: inline-block;font-size: 16px;margin: 4px 2px;cursor: pointer;}.button3 {font-size:4vw;border-radius: 8px;width:100%;font-weight:bold;}label{font-size:4vw;}</style></head><body><div id='centerContainer'>";
298305
content += "<h1>";
299306
content += WIFI_DEVICE_NAME;
300307
content += "</h1>";
301308
content += htmlString;
302309
content += "<br><br><form method='get' action='setting' id='form1'>";
303310
content += "<label for='deviceName'>Device Name</label><input type='text' id='deviceName' name='deviceName' maxlength='25'>";
304-
content += "<label for='microcontrollerIP'>IP ADDRESS</label><input type='text' id='microcontrollerIP' name='microcontrollerIP'>";
311+
content += "<label for='microcontrollerIP'>IP ADDRESS</label><input type='text' id='microcontrollerIP' name='microcontrollerIP' placeholder='optional'>";
305312
content += "<label for='ssid'>SSID</label><input type='text' id='ssid' name='ssid'>";
306313
content += "<label for='pass'>WIFI PASSWORD</label><input type='password' id='pass' name='pass'>";
307314
content += "<label for='OTApass'>OTA PASSWORD</label><input type='password' id='OTApass' name='OTApass'>";
@@ -327,11 +334,14 @@ void WifiManager::createWebServer() {
327334
String mqttpass = server.arg("mqttpass");
328335
String additionalParam = server.arg("additionalParam");
329336

330-
if (deviceName.length() > 0 && microcontrollerIP.length() > 0 && qsid.length() > 0 && qpass.length() > 0 && OTApass.length() > 0 && mqttIP.length() > 0 && mqttPort.length() > 0 && mqttuser.length() > 0 && mqttpass.length() > 0 && additionalParam.length() > 0) {
337+
if (deviceName.length() > 0 && qsid.length() > 0 && qpass.length() > 0 && OTApass.length() > 0 && mqttIP.length() > 0 && mqttPort.length() > 0 && mqttuser.length() > 0 && mqttpass.length() > 0 && additionalParam.length() > 0) {
331338

332339
Serial.println("deviceName");
333340
Serial.println(deviceName);
334341
Serial.println("microcontrollerIP");
342+
if (microcontrollerIP.length() == 0) {
343+
microcontrollerIP = "DHCP";
344+
}
335345
Serial.println(microcontrollerIP);
336346
Serial.println("qsid");
337347
Serial.println(qsid);

0 commit comments

Comments
 (0)