Skip to content

Commit

Permalink
Merge pull request #766 from visualapproach/development_v4
Browse files Browse the repository at this point in the history
WiFi connect to last known if not saved + clarification on network co…
  • Loading branch information
visualapproach committed Jul 18, 2024
2 parents e8bb02b + bc8119a commit 8cda33b
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 71 deletions.
1 change: 0 additions & 1 deletion Code/data/upload.html
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@
<form method="post" enctype="multipart/form-data" style="margin: 0px auto 8px auto">
<label class="button" for="browse_for_file" id="select_image">Browse...</label>
<input type="file" id="browse_for_file" name="Choose file" style="visibility:hidden;position:absolute" accept=".txt,.gz,.html,.ico,.js,.css,.mel,.png,.json">
<!-- <input type="file" id="browse_for_file" name="Choose file" style="visibility:hidden;position:absolute" accept=".txt,.gz,.html,.ico,.js,.css,.mel"> -->
<input type="submit" value="upload" name="submit" class="button">
</form>
</footer>
Expand Down
14 changes: 8 additions & 6 deletions Code/data/wifi.html
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@
<h2>Access Point:</h2>
<table>
<tr>
<td><label for="enableAp">Enable specific AP:</label><span data-text="Enable Wi-Fi station to connect to your network. You'd want this on normally!" class="tooltip">?</span></td>
<td><label for="enableAp">Enable specific AP:</label><span data-text=
"Checked: ESP will connect using these credentials. Unchecked: ESP will use last known credentials." class="tooltip">?</span></td>
<td><input type="checkbox" id="enableAp"></td>
</tr>
<tr>
Expand All @@ -62,7 +63,9 @@ <h2>Access Point:</h2>
<h2>Soft Access Point:</h2>
<table>
<tr>
<td><label for="enableWM">Enable Soft AP:</label><span data-text="Enable ESP access point to be able to open this page before your network credentials are set. You'd want this on normally! If your network is unavailable the ESP will fire up its own AP." class="tooltip">?</span></td>
<td><label for="enableWM">Enable Soft AP (I don't see a reason to uncheck this! You can lock yourself out!):</label><span data-text=
"Enable ESP access point to be able to open this page before your network credentials are set. You'd want this on normally! If your network is unavailable the ESP will fire up its own AP."
class="tooltip left">?</span></td>
<td><input type="checkbox" id="enableWM"></td>
</tr>
</table>
Expand Down Expand Up @@ -133,10 +136,9 @@ <h2>NTP server:</h2>
<section>
<h2>Reset WiFi Config:</h2>
<p>
This button resets the access point settings.<br />
The ESP will restart and start the "WiFi Configuration Manager".
Connect to it's access point and configure your WiFi (just like the first configuration).
Don't forget to disable WiFi Manager AP again if you want it off.
This button deletes the WiFi credentials.<br />
The ESP will restart and enable its own AP (if you have it checked above).
Connect to WiFi AP with SSID "layzspa_module######" and go to address http://192.168.4.2/wifi.html
</p>
</section>

Expand Down
2 changes: 1 addition & 1 deletion Code/lib/BWC_unified/FW_VERSION.h
Original file line number Diff line number Diff line change
@@ -1 +1 @@
#define FW_VERSION "2024-07-18-0900"
#define FW_VERSION "2024-07-18-1553"
4 changes: 2 additions & 2 deletions Code/lib/BWC_unified/enums.h
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,6 @@ struct sWifi_info
String ip4DnsSecondary_str;
String ip4NTP_str;
bool enableAp;
bool enableWmApFallback = true;
bool enableStaticIp4 = false;
bool enableWmApFallback;
bool enableStaticIp4;
};
126 changes: 66 additions & 60 deletions Code/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ void setup()
mqtt_info->mqttTelemetryInterval = 600;
mqtt_info->mqttUsername = MQTT_USER_F;
mqtt_info->useMqtt = true;
wifi_info = new sWifi_info;
}
bwc->setup();
bwc->loop();
Expand All @@ -88,7 +89,7 @@ void setup()
updateWSTimer->attach(WS_PERIOD, []{ sendWSFlag = true; });
loadWebConfig();
startWiFi();
if(wifi_info.enableWmApFallback) startSoftAp(); // not blocking anymore so no use case should exist for this to be turned off.
if(wifi_info->enableWmApFallback) startSoftAp(); // not blocking anymore so no use case should exist for this to be turned off.
startHttpServer();
startWebSocket();
startOTA();
Expand Down Expand Up @@ -329,31 +330,36 @@ void startWiFi()
WiFi.mode(WIFI_STA);
loadWifi();

if (wifi_info.enableStaticIp4)
if (wifi_info->enableStaticIp4)
{
BWC_LOG_P(PSTR("Setting static IP\n"),0);
IPAddress ip4Address;
IPAddress ip4Gateway;
IPAddress ip4Subnet;
IPAddress ip4DnsPrimary;
IPAddress ip4DnsSecondary;
ip4Address.fromString(wifi_info.ip4Address_str);
ip4Gateway.fromString(wifi_info.ip4Gateway_str);
ip4Subnet.fromString(wifi_info.ip4Subnet_str);
ip4DnsPrimary.fromString(wifi_info.ip4DnsPrimary_str);
ip4DnsSecondary.fromString(wifi_info.ip4DnsSecondary_str);
ip4Address.fromString(wifi_info->ip4Address_str);
ip4Gateway.fromString(wifi_info->ip4Gateway_str);
ip4Subnet.fromString(wifi_info->ip4Subnet_str);
ip4DnsPrimary.fromString(wifi_info->ip4DnsPrimary_str);
ip4DnsSecondary.fromString(wifi_info->ip4DnsSecondary_str);
BWC_LOG_P(PSTR("WiFi > using static IP %s on gateway %s\n"),ip4Address.toString().c_str(), ip4Gateway.toString().c_str());
WiFi.config(ip4Address, ip4Gateway, ip4Subnet, ip4DnsPrimary, ip4DnsSecondary);
}

/* Connect in station mode to the AP given (your router/ap) */
if (wifi_info.enableAp)
if (wifi_info->enableAp)
{
BWC_LOG_P(PSTR("WiFi > using WiFi configuration with SSID %s\n"), wifi_info.apSsid.c_str());
BWC_LOG_P(PSTR("WiFi > using WiFi configuration with SSID %s\n"), wifi_info->apSsid.c_str());

WiFi.begin(wifi_info.apSsid.c_str(), wifi_info.apPwd.c_str());
WiFi.begin(wifi_info->apSsid.c_str(), wifi_info->apPwd.c_str());
// checkWifi_ticker->attach(2.0, checkWiFi_ISR);
BWC_LOG_P(PSTR("WiFi > Trying to connect ..."),0);
BWC_LOG_P(PSTR("WiFi > AP info loaded. Waiting for connection ...\n"),0);
}
else
{
BWC_LOG_P(PSTR("WiFi > AP info not found. Using last known AP ...\n"),0);
WiFi.begin();
}
BWC_YIELD;
}
Expand Down Expand Up @@ -421,7 +427,7 @@ void checkNTP()
void startNTP()
{
BWC_LOG_P(PSTR("NTP > start\n"),0);
configTime(0,0,wifi_info.ip4NTP_str, F("pool.ntp.org"), F("time.nist.gov"));
configTime(0,0,wifi_info->ip4NTP_str, F("pool.ntp.org"), F("time.nist.gov"));
ntpCheck_ticker->attach(3.0, checkNTP_ISR);
}

Expand Down Expand Up @@ -1283,19 +1289,19 @@ void loadWifi()
return;
}

wifi_info.enableAp = doc[F("enableAp")];
if(doc.containsKey(F("enableWM"))) wifi_info.enableWmApFallback = doc[F("enableWM")];
wifi_info.apSsid = doc[F("apSsid")].as<String>();
wifi_info.apPwd = doc[F("apPwd")].as<String>();
wifi_info->enableAp = doc[F("enableAp")];
if(doc.containsKey(F("enableWM"))) wifi_info->enableWmApFallback = doc[F("enableWM")];
wifi_info->apSsid = doc[F("apSsid")].as<String>();
wifi_info->apPwd = doc[F("apPwd")].as<String>();

wifi_info.enableStaticIp4 = doc[F("enableStaticIp4")];
wifi_info->enableStaticIp4 = doc[F("enableStaticIp4")];
String s(30);
wifi_info.ip4Address_str = doc[F("ip4Address")].as<String>();
wifi_info.ip4Gateway_str = doc[F("ip4Gateway")].as<String>();
wifi_info.ip4Subnet_str = doc[F("ip4Subnet")].as<String>();
wifi_info.ip4DnsPrimary_str = doc[F("ip4DnsPrimary")].as<String>();
wifi_info.ip4DnsSecondary_str = doc[F("ip4DnsSecondary")].as<String>();
wifi_info.ip4NTP_str = doc[F("ip4NTP")].as<String>();
wifi_info->ip4Address_str = doc[F("ip4Address")].as<String>();
wifi_info->ip4Gateway_str = doc[F("ip4Gateway")].as<String>();
wifi_info->ip4Subnet_str = doc[F("ip4Subnet")].as<String>();
wifi_info->ip4DnsPrimary_str = doc[F("ip4DnsPrimary")].as<String>();
wifi_info->ip4DnsSecondary_str = doc[F("ip4DnsSecondary")].as<String>();
wifi_info->ip4NTP_str = doc[F("ip4NTP")].as<String>();

BWC_YIELD;
}
Expand All @@ -1314,17 +1320,17 @@ void saveWifi()

DynamicJsonDocument doc(1024);

doc[F("enableAp")] = wifi_info.enableAp;
doc[F("enableWM")] = wifi_info.enableWmApFallback;
doc[F("apSsid")] = wifi_info.apSsid;
doc[F("apPwd")] = wifi_info.apPwd;
doc[F("enableStaticIp4")] = wifi_info.enableStaticIp4;
doc[F("ip4Address")] = wifi_info.ip4Address_str;
doc[F("ip4Gateway")] = wifi_info.ip4Gateway_str;
doc[F("ip4Subnet")] = wifi_info.ip4Subnet_str;
doc[F("ip4DnsPrimary")] = wifi_info.ip4DnsPrimary_str;
doc[F("ip4DnsSecondary")] = wifi_info.ip4DnsSecondary_str;
doc[F("ip4NTP")] = wifi_info.ip4NTP_str;
doc[F("enableAp")] = wifi_info->enableAp;
doc[F("enableWM")] = wifi_info->enableWmApFallback;
doc[F("apSsid")] = wifi_info->apSsid;
doc[F("apPwd")] = wifi_info->apPwd;
doc[F("enableStaticIp4")] = wifi_info->enableStaticIp4;
doc[F("ip4Address")] = wifi_info->ip4Address_str;
doc[F("ip4Gateway")] = wifi_info->ip4Gateway_str;
doc[F("ip4Subnet")] = wifi_info->ip4Subnet_str;
doc[F("ip4DnsPrimary")] = wifi_info->ip4DnsPrimary_str;
doc[F("ip4DnsSecondary")] = wifi_info->ip4DnsSecondary_str;
doc[F("ip4NTP")] = wifi_info->ip4NTP_str;

if (serializeJson(doc, file) == 0)
{
Expand All @@ -1344,22 +1350,22 @@ void handleGetWifi()

DynamicJsonDocument doc(1024);

doc[F("enableAp")] = wifi_info.enableAp;
doc[F("enableWM")] = wifi_info.enableWmApFallback;
doc[F("apSsid")] = wifi_info.apSsid;
doc[F("enableAp")] = wifi_info->enableAp;
doc[F("enableWM")] = wifi_info->enableWmApFallback;
doc[F("apSsid")] = wifi_info->apSsid;
doc[F("apPwd")] = F("<enter password>");
if (!hidePasswords)
{
doc[F("apPwd")] = wifi_info.apPwd;
doc[F("apPwd")] = wifi_info->apPwd;
}

doc[F("enableStaticIp4")] = wifi_info.enableStaticIp4;
doc[F("ip4Address")] = wifi_info.ip4Address_str;
doc[F("ip4Gateway")] = wifi_info.ip4Gateway_str;
doc[F("ip4Subnet")] = wifi_info.ip4Subnet_str;
doc[F("ip4DnsPrimary")] = wifi_info.ip4DnsPrimary_str;
doc[F("ip4DnsSecondary")] = wifi_info.ip4DnsSecondary_str;
doc[F("ip4NTP")] = wifi_info.ip4NTP_str;
doc[F("enableStaticIp4")] = wifi_info->enableStaticIp4;
doc[F("ip4Address")] = wifi_info->ip4Address_str;
doc[F("ip4Gateway")] = wifi_info->ip4Gateway_str;
doc[F("ip4Subnet")] = wifi_info->ip4Subnet_str;
doc[F("ip4DnsPrimary")] = wifi_info->ip4DnsPrimary_str;
doc[F("ip4DnsSecondary")] = wifi_info->ip4DnsSecondary_str;
doc[F("ip4NTP")] = wifi_info->ip4NTP_str;
String json;
json.reserve(200);
if (serializeJson(doc, json) == 0)
Expand Down Expand Up @@ -1387,18 +1393,18 @@ void handleSetWifi()
return;
}

wifi_info.enableAp = doc[F("enableAp")];
if(doc.containsKey("enableWM")) wifi_info.enableWmApFallback = doc[F("enableWM")];
wifi_info.apSsid = doc[F("apSsid")].as<String>();
wifi_info.apPwd = doc[F("apPwd")].as<String>();
wifi_info->enableAp = doc[F("enableAp")];
if(doc.containsKey("enableWM")) wifi_info->enableWmApFallback = doc[F("enableWM")];
wifi_info->apSsid = doc[F("apSsid")].as<String>();
wifi_info->apPwd = doc[F("apPwd")].as<String>();

wifi_info.enableStaticIp4 = doc[F("enableStaticIp4")];
wifi_info.ip4Address_str = doc[F("ip4Address")].as<String>();
wifi_info.ip4Gateway_str = doc[F("ip4Gateway")].as<String>();
wifi_info.ip4Subnet_str = doc[F("ip4Subnet")].as<String>();
wifi_info.ip4DnsPrimary_str = doc[F("ip4DnsPrimary")].as<String>();
wifi_info.ip4DnsSecondary_str = doc[F("ip4DnsSecondary")].as<String>();
wifi_info.ip4NTP_str = doc[F("ip4NTP")].as<String>();
wifi_info->enableStaticIp4 = doc[F("enableStaticIp4")];
wifi_info->ip4Address_str = doc[F("ip4Address")].as<String>();
wifi_info->ip4Gateway_str = doc[F("ip4Gateway")].as<String>();
wifi_info->ip4Subnet_str = doc[F("ip4Subnet")].as<String>();
wifi_info->ip4DnsPrimary_str = doc[F("ip4DnsPrimary")].as<String>();
wifi_info->ip4DnsSecondary_str = doc[F("ip4DnsSecondary")].as<String>();
wifi_info->ip4NTP_str = doc[F("ip4NTP")].as<String>();

saveWifi();

Expand Down Expand Up @@ -1428,10 +1434,10 @@ void handleResetWifi()

void resetWiFi()
{
wifi_info.enableAp = false;
wifi_info.enableWmApFallback = true;
wifi_info.apSsid = F("empty");
wifi_info.apPwd = F("empty");
wifi_info->enableAp = false;
wifi_info->enableWmApFallback = true;
wifi_info->apSsid = F("empty");
wifi_info->apPwd = F("empty");
saveWifi();
delay(3000);
periodicTimer->detach();
Expand Down
2 changes: 1 addition & 1 deletion Code/src/main.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ bool gotIP_flag = false;
bool disconnected_flag = false;

int periodicTimerInterval = 60;
sWifi_info wifi_info;
sWifi_info* wifi_info;

/** A file to store the uploads */
File fsUploadFile;
Expand Down

0 comments on commit 8cda33b

Please sign in to comment.