mirror of
https://github.com/rstrouse/ESPSomfy-RTS.git
synced 2025-12-13 02:52:11 +01:00
Fix WiFi setup
* Increased the number of APs that can be queried and filtered out those that have an RSSI -95. Previously, the number of returned SSIDs could blow the output buffer. * Do not disable the Save button during scan of potential APs. If the scan is taking a long time you should just be able to type it in. * If a connection is lost and cannot be recovered fall back to AP mode to allow resetting the creds without power off.
This commit is contained in:
parent
dcd90a4c29
commit
e1aada23b0
3 changed files with 46 additions and 22 deletions
26
Network.cpp
26
Network.cpp
|
|
@ -15,6 +15,7 @@ extern SocketEmitter sockEmit;
|
|||
extern MQTTClass mqtt;
|
||||
extern rebootDelay_t rebootDelay;
|
||||
|
||||
int connectRetries = 0;
|
||||
void Network::end() {
|
||||
sockEmit.end();
|
||||
SSDP.end();
|
||||
|
|
@ -34,11 +35,21 @@ bool Network::setup() {
|
|||
}
|
||||
void Network::loop() {
|
||||
if(millis() - this->lastEmit > 1500) {
|
||||
if(this->connect()) {}
|
||||
while(!this->connect()) {
|
||||
// If we lost our connenction
|
||||
connectRetries++;
|
||||
if(connectRetries > 100) {
|
||||
this->openSoftAP();
|
||||
break;
|
||||
}
|
||||
sockEmit.loop();
|
||||
}
|
||||
connectRetries = 0;
|
||||
this->lastEmit = millis();
|
||||
this->emitSockets();
|
||||
if(WiFi.status() != WL_CONNECTED) return;
|
||||
}
|
||||
else sockEmit.loop();
|
||||
sockEmit.loop();
|
||||
if(settings.WIFI.ssdpBroadcast) {
|
||||
if(!SSDP.isStarted) SSDP.begin();
|
||||
SSDP.loop();
|
||||
|
|
@ -147,7 +158,7 @@ bool Network::connect() {
|
|||
Serial.print(this->channel);
|
||||
Serial.print(" (");
|
||||
Serial.print(this->strength);
|
||||
Serial.print("dbm) ");
|
||||
Serial.println("dbm) ");
|
||||
}
|
||||
else Serial.println("Connecting to AP");
|
||||
this->connectAttempts++;
|
||||
|
|
@ -274,17 +285,10 @@ bool Network::openSoftAP() {
|
|||
|
||||
while ((WiFi.status() != WL_CONNECTED))
|
||||
{
|
||||
for(int i = 0; i < 3; i++) {
|
||||
//delay(100);
|
||||
//digitalWrite(LED_BUILTIN, HIGH);
|
||||
//delay(100);
|
||||
//digitalWrite(LED_BUILTIN, LOW);
|
||||
}
|
||||
int clients = WiFi.softAPgetStationNum();
|
||||
|
||||
webServer.loop();
|
||||
if(millis() - this->lastEmit > 1500) {
|
||||
if(this->connect()) {}
|
||||
//if(this->connect()) {}
|
||||
this->lastEmit = millis();
|
||||
this->emitSockets();
|
||||
if(clients > 0)
|
||||
|
|
|
|||
22
Web.cpp
22
Web.cpp
|
|
@ -798,6 +798,25 @@ void Web::begin() {
|
|||
Serial.print("Scanned ");
|
||||
Serial.print(n);
|
||||
Serial.println(" networks");
|
||||
DynamicJsonDocument doc(16384);
|
||||
JsonObject obj = doc.to<JsonObject>();
|
||||
JsonObject connected = obj.createNestedObject("connected");
|
||||
connected["name"] = settings.WIFI.ssid;
|
||||
connected["passphrase"] = settings.WIFI.passphrase;
|
||||
connected["strength"] = WiFi.RSSI();
|
||||
connected["channel"] = WiFi.channel();
|
||||
JsonArray arr = obj.createNestedArray("accessPoints");
|
||||
for(int i = 0; i < n; ++i) {
|
||||
if(WiFi.SSID(i).length() == 0 || WiFi.RSSI(i) < -95) continue; // Ignore hidden and weak networks that we cannot connect to anyway.
|
||||
JsonObject a = arr.createNestedObject();
|
||||
a["name"] = WiFi.SSID(i);
|
||||
a["channel"] = WiFi.channel(i);
|
||||
a["encryption"] = settings.WIFI.mapEncryptionType(WiFi.encryptionType(i));
|
||||
a["strength"] = WiFi.RSSI(i);
|
||||
a["macAddress"] = WiFi.BSSIDstr(i);
|
||||
}
|
||||
serializeJson(doc, g_content);
|
||||
/*
|
||||
String content = "{\"connected\": {\"name\":\"" + String(settings.WIFI.ssid) + "\",\"passphrase\":\"" + String(settings.WIFI.passphrase) + "\",\"strength\":" + WiFi.RSSI() + ",\"channel\":" + WiFi.channel() + "}, \"accessPoints\":[";
|
||||
for (int i = 0; i < n; ++i) {
|
||||
if (i != 0) content += ",";
|
||||
|
|
@ -805,7 +824,8 @@ void Web::begin() {
|
|||
delay(10);
|
||||
}
|
||||
content += "]}";
|
||||
server.send(statusCode, "application/json", content);
|
||||
*/
|
||||
server.send(statusCode, "application/json", g_content);
|
||||
});
|
||||
server.on("/reboot", []() {
|
||||
webServer.sendCORSHeaders();
|
||||
|
|
|
|||
|
|
@ -131,6 +131,8 @@
|
|||
document.getElementsByName('hostname')[0].value = settings.hostname;
|
||||
document.getElementsByName('ntptimeserver')[0].value = settings.ntpServer;
|
||||
document.getElementsByName('ssdpBroadcast')[0].checked = settings.ssdpBroadcast;
|
||||
document.getElementById('fldSsid').value = settings.ssid;
|
||||
document.getElementById('fldPassphrase').value = settings.passphrase;
|
||||
}
|
||||
});
|
||||
|
||||
|
|
@ -215,17 +217,15 @@
|
|||
class Wifi {
|
||||
init() {
|
||||
document.getElementById("divNetworkStrength").innerHTML = this.displaySignal(-100);
|
||||
|
||||
//this.loadAPs();
|
||||
};
|
||||
async loadAPs() {
|
||||
if (document.getElementById('btnScanAPs').classList.contains('disabled')) return;
|
||||
document.getElementById('divAps').innerHTML = '<div style="display:flex;justify-content:center;align-items:center;"><div class="lds-roller"><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div></div></div>';
|
||||
document.getElementById('btnScanAPs').classList.add('disabled');
|
||||
document.getElementById('btnConnectWiFi').classList.add('disabled');
|
||||
//document.getElementById('btnConnectWiFi').classList.add('disabled');
|
||||
getJSON('/scanaps', (err, aps) => {
|
||||
document.getElementById('btnScanAPs').classList.remove('disabled');
|
||||
document.getElementById('btnConnectWiFi').classList.remove('disabled');
|
||||
//document.getElementById('btnConnectWiFi').classList.remove('disabled');
|
||||
console.log(aps);
|
||||
if (err) {
|
||||
this.displayAPs({ connected: { name: '', passphrase: '' }, accessPoints: [] });
|
||||
|
|
@ -258,9 +258,9 @@
|
|||
let divAps = document.getElementById('divAps');
|
||||
divAps.setAttribute('data-lastloaded', new Date().getTime());
|
||||
divAps.innerHTML = div;
|
||||
document.getElementsByName('ssid')[0].value = aps.connected.name;
|
||||
document.getElementsByName('passphrase')[0].value = aps.connected.passphrase;
|
||||
this.procWifiStrength(aps.connected);
|
||||
//document.getElementsByName('ssid')[0].value = aps.connected.name;
|
||||
//document.getElementsByName('passphrase')[0].value = aps.connected.passphrase;
|
||||
//this.procWifiStrength(aps.connected);
|
||||
};
|
||||
selectSSID(el) {
|
||||
let obj = {
|
||||
|
|
@ -309,7 +309,7 @@
|
|||
elWave.classList.remove('waveStrength-0', 'waveStrength-1', 'waveStrength-2', 'waveStrength-3', 'waveStrength-4');
|
||||
elWave.classList.add(cssClass);
|
||||
}
|
||||
document.getElementById('spanNetworkStrength').innerHTML = (isNaN(strength.strength) || strength < -100) ? '---' : strength.strength;
|
||||
document.getElementById('spanNetworkStrength').innerHTML = (isNaN(strength.strength) || strength.strength <= -100) ? '----' : strength.strength;
|
||||
}
|
||||
};
|
||||
var wifi = new Wifi();
|
||||
|
|
@ -1660,8 +1660,8 @@
|
|||
tab.classList.add('selected');
|
||||
if (tab.getAttribute('data-grpid') === 'fsWiFiSettings') {
|
||||
// Only load the networks if we need them.
|
||||
if(new Date().getTime() - parseInt(document.getElementById('divAps').getAttribute('data-lastloaded'), 10) > 60000)
|
||||
wifi.loadAPs();
|
||||
//if(new Date().getTime() - parseInt(document.getElementById('divAps').getAttribute('data-lastloaded'), 10) > 60000)
|
||||
// wifi.loadAPs();
|
||||
}
|
||||
let grp = document.getElementById(tab.getAttribute('data-grpid'));
|
||||
if (typeof grp !== 'undefined' && grp) grp.style.display = '';
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue