Skip to content

Commit

Permalink
Use bssid to connect to best WIFI and added option to always perform …
Browse files Browse the repository at this point in the history
…a WIFI-scan #241

Thank's to @Joe91 !
  • Loading branch information
tueddy committed Jun 7, 2023
1 parent 41cd68f commit a14386f
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 9 deletions.
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
## DEV-branch
* 07.06.2023: Use bssid to connect to best WIFI and added option to always perform a WIFI-scan #241
* 06.06.2023: List available WiFi's in accesspoint view
* 06.06.2023: Remove support for Arduino 1.0.6 & playlist cache
* 05.06.2023: FastLED 3.6.0, PlatformIO package 6.3.1
Expand Down
14 changes: 11 additions & 3 deletions html/accesspoint.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@
border: 0;
padding: 0 15px
}
input[type="checkbox"] {
width: 20px;
height: 20px
}
body {
background: #007bff;
font-family: sans-serif;
Expand Down Expand Up @@ -81,6 +85,8 @@ <h1 data-i18n="wifi.title">WiFi-configuration</h1>
<label for="hostname" data-i18n="wifi.hostname.title">Hostname</label>:<br>
<input type="text" id="hostname" name="hostname" value="espuino" required><br><br>

<input type="checkbox" id="scan_wifi_on_start" name="scan_wifi_on_start" value=false>
<label for="scan_wifi_on_start" data-i18n="wifi.scan.enabled">Start with best WiFi</label><br><br>
<input type="checkbox" id="static_enabled" name="static_enabled" value=false>
<label for="static_enabled" data-i18n="wifi.static.enabled">Static IP</label>

Expand Down Expand Up @@ -110,15 +116,15 @@ <h1 data-i18n="wifi.restartPrompt">Ready to go?</h1>
loadPath: "/locales/{{lng}}.json"
},
debug: true,
fallbackLng: 'en',
fallbackLng: 'de',
}, (err, t) => {
localize = locI18next.init(i18next);
localize('body');
});

function selectWiFi(ssid) {
document.getElementById("ssid").value = ssid;
document.getElementById("ssid").focus();
document.getElementById("pwd").focus();
}

async function getWiFiList() {
Expand Down Expand Up @@ -152,11 +158,13 @@ <h1 data-i18n="wifi.restartPrompt">Ready to go?</h1>

async function wifiConfig() {
let static = document.getElementById('static_enabled').checked;
let scanwifionstart = document.getElementById('scan_wifi_on_start').checked;

var myObj = {
ssid: document.getElementById('ssid').value,
pwd: document.getElementById('pwd').value,
static: static
static: static,
scanwifionstart: scanwifionstart
};

if (static) {
Expand Down
3 changes: 3 additions & 0 deletions html/locales/de.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@
"dns1": " DNS 1 für statische IP-Konfiguration",
"dns2": "DNS 2 für statische IP-Konfiguration"
},
"scan": {
"enabled": "Start mit bestem WLAN"
},
"restartPrompt": "Fertig?"
},
"control": {
Expand Down
3 changes: 3 additions & 0 deletions html/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@
"dns1": " DNS 1 for static IP-Configuration",
"dns2": "DNS 2 for static IP-Configuration"
},
"scan": {
"enabled": "Start with strongest WiFi"
},
"restartPrompt": "Ready to go?"
},
"control": {
Expand Down
6 changes: 5 additions & 1 deletion html/management.html
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,8 @@
<label for="pwd" data-i18n="[prepend]wifi.password.title">:</label>
<input type="password" class="form-control" id="pwd" name="pwd" data-i18n="[placeholder]wifi.password.placeholder" required>

<input type="checkbox" id="scan_wifi_on_start" name="scan_wifi_on_start" value=false>
<label for="scan_wifi_on_start" data-i18n="wifi.scan.enabled">Start with best WiFi</label><br><br>
<input type="checkbox" id="static_enabled" name="static_enabled" value=false>
<label for="static_enabled" data-i18n="[prepend]wifi.static.enabled">:</label>
<div id="wifi_static_div" style="display:none">
Expand Down Expand Up @@ -1507,11 +1509,13 @@
async function wifiConfig(clickedId) {
lastIdclicked = clickedId;
let static = document.getElementById('static_enabled').checked;
let scanwifionstart = document.getElementById('scan_wifi_on_start').checked;

This comment has been minimized.

Copy link
@SZenglein

SZenglein Jun 16, 2023

Contributor

This is really something I would have put into "globalWifiConfig". In fact, it's exactly the kind of global setting I was thinking of when creating the method.
The wifiConfig is really for each network, and you're now overwriting this settings every time you don't click the checkbox.

This comment has been minimized.

Copy link
@tueddy

tueddy Jun 16, 2023

Author Collaborator

Good point! We will move this next time to global..


var myObj = {
ssid: document.getElementById('ssid').value,
pwd: document.getElementById('pwd').value,
static: static
static: static,
scanwifionstart: scanwifionstart
};

if (static) {
Expand Down
4 changes: 4 additions & 0 deletions src/Web.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1249,6 +1249,10 @@ void handlePostSavedSSIDs(AsyncWebServerRequest *request, JsonVariant &json) {
strncpy(networkSettings.password, (const char*) jsonObj["pwd"], 64);
networkSettings.password[64] = '\0';

// always perform perform a WiFi scan on startup?
static bool alwaysScan = (bool) jsonObj["scanwifionstart"];
gPrefsSettings.putBool("ScanWiFiOnStart", alwaysScan);

networkSettings.use_static_ip = (bool) jsonObj["static"];

if (jsonObj.containsKey("static_addr")) {
Expand Down
17 changes: 13 additions & 4 deletions src/Wlan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ void Wlan_Init(void) {
handleWifiStateInit();
}

void connectToKnownNetwork(WiFiSettings settings) {
void connectToKnownNetwork(WiFiSettings settings, byte *bssid = nullptr) {
// set hostname on connect, because when resetting wifi config elsewhere it could be reset
if (hostname.compareTo("-1")) {
WiFi.setHostname(hostname.c_str());
Expand All @@ -145,7 +145,7 @@ void connectToKnownNetwork(WiFiSettings settings) {

Log_Printf(LOGLEVEL_NOTICE, wifiConnectionInProgress, settings.ssid);

WiFi.begin(settings.ssid, settings.password);
WiFi.begin(settings.ssid, settings.password, 0, bssid);
}

void handleWifiStateInit() {
Expand All @@ -160,7 +160,15 @@ void handleWifiStateInit() {
connectionAttemptCounter = 0;
connectStartTimestamp = 0;
connectionFailedTimestamp = 0;
wifiState = WIFI_STATE_CONNECT_LAST;
bool scanWiFiOnStart = gPrefsSettings.getBool("ScanWiFiOnStart", false);
if (scanWiFiOnStart) {
// perform a scan to find the strongest network with same ssid (e.g. for mesh/repeater networks)
WiFi.scanNetworks(true, false, true, 120);
wifiState = WIFI_STATE_SCAN_CONN;
} else {
// quick connect without additional scan
wifiState = WIFI_STATE_CONNECT_LAST;
}
}

void handleWifiStateConnectLast() {
Expand Down Expand Up @@ -246,10 +254,11 @@ void handleWifiStateScanConnect() {
for (int i = scanIndex; i < wifiScanCompleteResult; i++) {
// try to connect to wifi network with index i
String issid = WiFi.SSID(i);
byte *bssid = WiFi.BSSID(i);
// check if ssid name matches any saved ssid
for (int j = 0; j < numKnownNetworks; j++) {
if (strncmp(issid.c_str(), knownNetworks[j].ssid, 32) ==0 ) {
connectToKnownNetwork(knownNetworks[j]);
connectToKnownNetwork(knownNetworks[j], bssid);

connectStartTimestamp = millis();

Expand Down
2 changes: 1 addition & 1 deletion src/revision.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#pragma once

#include "gitrevision.h"
constexpr const char softwareRevision[] = "Software-revision: 20230606-1";
constexpr const char softwareRevision[] = "Software-revision: 20230607-1";

0 comments on commit a14386f

Please sign in to comment.