mirror of
https://github.com/arendst/Tasmota.git
synced 2025-07-24 11:16:34 +00:00
Refactor wifi code
This commit is contained in:
parent
3d00c4f093
commit
9efe67127d
@ -22,18 +22,16 @@
|
||||
\*********************************************************************************************/
|
||||
|
||||
#ifndef WIFI_RSSI_THRESHOLD
|
||||
// decrease the roam threshold to address devices connecting at very low RSSI and being close to inoperative
|
||||
//#define WIFI_RSSI_THRESHOLD 10 // Difference in dB between current network and scanned network
|
||||
// Decrease the roam threshold from 10 to 5 to address devices connecting at very low RSSI and being close to inoperative
|
||||
#define WIFI_RSSI_THRESHOLD 5 // Difference in dB between current network and scanned network
|
||||
#endif
|
||||
#ifndef WIFI_RESCAN_MINUTES
|
||||
// increase rescan interval to 5 minutes to improve ability for devices to reach network harmony
|
||||
//#define WIFI_RESCAN_MINUTES 44 // Number of minutes between wifi network rescan
|
||||
#define WIFI_RESCAN_MINUTES 5 // Number of minutes between wifi network rescan
|
||||
// Increase rescan interval from 44 to 5 minutes to improve ability for devices to reach network harmony
|
||||
#define WIFI_RESCAN_MINUTES 5 // Number of minutes between wifi network rescan
|
||||
#endif
|
||||
|
||||
const uint8_t WIFI_CONFIG_SEC = 180; // seconds before restart
|
||||
// drop from 20 seconds to 5 seconds since we control the reconnections, not the Arduino SDK
|
||||
// Drop from 20 seconds to 5 seconds since we control the reconnections, not the Arduino SDK
|
||||
const uint8_t WIFI_CHECK_SEC = 5; // seconds
|
||||
const uint8_t WIFI_RETRY_OFFSET_SEC = 20; // seconds
|
||||
|
||||
@ -56,6 +54,7 @@ struct WIFI {
|
||||
uint8_t scan_state;
|
||||
uint8_t bssid[6] = {0};
|
||||
uint8_t bssid_last[6] = {0}; // store the last connect bssid
|
||||
int8_t best_network_db;
|
||||
} Wifi;
|
||||
|
||||
int WifiGetRssiAsQuality(int rssi)
|
||||
@ -168,7 +167,6 @@ void WiFiSetSleepMode(void)
|
||||
void WifiBegin(uint8_t flag, uint8_t channel)
|
||||
{
|
||||
const char kWifiPhyMode[] = " BGN";
|
||||
char hex_char[18];
|
||||
|
||||
#ifdef USE_EMULATION
|
||||
UdpDisconnect();
|
||||
@ -189,9 +187,8 @@ void WifiBegin(uint8_t flag, uint8_t channel)
|
||||
// if (WiFi.getPhyMode() != WIFI_PHY_MODE_11N) { WiFi.setPhyMode(WIFI_PHY_MODE_11N); } // B/G/N
|
||||
// if (WiFi.getPhyMode() != WIFI_PHY_MODE_11G) { WiFi.setPhyMode(WIFI_PHY_MODE_11G); } // B/G
|
||||
if (!WiFi.getAutoConnect()) { WiFi.setAutoConnect(true); }
|
||||
// WiFi.setAutoReconnect(true);
|
||||
// handle the reconnection in WifiCheckIp() since the autoreconnect keeps sending deauthentication messages which causes the AP to block traffic as it looks like an DoS attack
|
||||
// this needs to be explicitly called as "false" otherwise the default is enabled
|
||||
// Handle the reconnection in WifiCheckIp() since the autoreconnect keeps sending deauthentication messages which causes the AP to block traffic as it looks like an DoS attack
|
||||
// This needs to be explicitly called as "false" otherwise the default is enabled
|
||||
WiFi.setAutoReconnect(false);
|
||||
switch (flag) {
|
||||
case 0: // AP1
|
||||
@ -208,19 +205,18 @@ void WifiBegin(uint8_t flag, uint8_t channel)
|
||||
WiFi.config(Settings.ip_address[0], Settings.ip_address[1], Settings.ip_address[2], Settings.ip_address[3]); // Set static IP
|
||||
}
|
||||
WiFi.hostname(my_hostname);
|
||||
|
||||
char stemp[40] = { 0 };
|
||||
if (channel) {
|
||||
WiFi.begin(SettingsText(SET_STASSID1 + Settings.sta_active), SettingsText(SET_STAPWD1 + Settings.sta_active), channel, Wifi.bssid);
|
||||
// add debug output to console to show connected BSSID and channel for multi-AP installations
|
||||
AddLog_P2(LOG_LEVEL_INFO, PSTR(D_LOG_WIFI D_CONNECTING_TO_AP "%d %s Channel %d BSSId %s " D_IN_MODE " 11%c " D_AS " %s..."),
|
||||
Settings.sta_active +1, SettingsText(SET_STASSID1 + Settings.sta_active),
|
||||
channel, ToHex_P((unsigned char*)Wifi.bssid, 6, hex_char, sizeof(hex_char), ':'),
|
||||
kWifiPhyMode[WiFi.getPhyMode() & 0x3], my_hostname);
|
||||
// Add connected BSSID and channel for multi-AP installations
|
||||
char hex_char[18];
|
||||
snprintf_P(stemp, sizeof(stemp), PSTR(" Channel %d BSSId %s"), channel, ToHex_P((unsigned char*)Wifi.bssid, 6, hex_char, sizeof(hex_char), ':'));
|
||||
} else {
|
||||
WiFi.begin(SettingsText(SET_STASSID1 + Settings.sta_active), SettingsText(SET_STAPWD1 + Settings.sta_active));
|
||||
AddLog_P2(LOG_LEVEL_INFO, PSTR(D_LOG_WIFI D_CONNECTING_TO_AP "%d %s " D_IN_MODE " 11%c " D_AS " %s..."),
|
||||
Settings.sta_active +1, SettingsText(SET_STASSID1 + Settings.sta_active),
|
||||
kWifiPhyMode[WiFi.getPhyMode() & 0x3], my_hostname);
|
||||
}
|
||||
AddLog_P2(LOG_LEVEL_INFO, PSTR(D_LOG_WIFI D_CONNECTING_TO_AP "%d %s%s " D_IN_MODE " 11%c " D_AS " %s..."),
|
||||
Settings.sta_active +1, SettingsText(SET_STASSID1 + Settings.sta_active), stemp, kWifiPhyMode[WiFi.getPhyMode() & 0x3], my_hostname);
|
||||
|
||||
#if LWIP_IPV6
|
||||
for (bool configured = false; !configured;) {
|
||||
@ -239,22 +235,22 @@ void WifiBegin(uint8_t flag, uint8_t channel)
|
||||
|
||||
void WifiBeginAfterScan(void)
|
||||
{
|
||||
static int8_t best_network_db;
|
||||
|
||||
// Not active
|
||||
if (0 == Wifi.scan_state) { return; }
|
||||
// Init scan when not connected
|
||||
if (1 == Wifi.scan_state) {
|
||||
memset((void*) &Wifi.bssid, 0, sizeof(Wifi.bssid));
|
||||
best_network_db = -127;
|
||||
Wifi.best_network_db = -127;
|
||||
Wifi.scan_state = 3;
|
||||
}
|
||||
// Init scan when connected
|
||||
if (2 == Wifi.scan_state) {
|
||||
uint8_t* bssid = WiFi.BSSID(); // Get current bssid
|
||||
memcpy((void*) &Wifi.bssid, (void*) bssid, sizeof(Wifi.bssid));
|
||||
best_network_db = WiFi.RSSI(); // Get current rssi and add threshold
|
||||
if (best_network_db < -WIFI_RSSI_THRESHOLD) { best_network_db += WIFI_RSSI_THRESHOLD; }
|
||||
Wifi.best_network_db = WiFi.RSSI(); // Get current rssi and add threshold
|
||||
if (Wifi.best_network_db < -WIFI_RSSI_THRESHOLD) {
|
||||
Wifi.best_network_db += WIFI_RSSI_THRESHOLD;
|
||||
}
|
||||
Wifi.scan_state = 3;
|
||||
}
|
||||
// Init scan
|
||||
@ -305,7 +301,7 @@ void WifiBeginAfterScan(void)
|
||||
if (ssid_scan == SettingsText(SET_STASSID1 + j)) { // SSID match
|
||||
known = true;
|
||||
number_known++;
|
||||
if (rssi_scan > best_network_db) { // Best network
|
||||
if (rssi_scan > Wifi.best_network_db) { // Best network
|
||||
if (sec_scan == ENC_TYPE_NONE || SettingsText(SET_STAPWD1 + j)) { // Check for passphrase if not open wlan
|
||||
// store the max values in case there is only one AP and we need to try to reconnect
|
||||
memcpy((void*) &bssid_max, (void*) bssid_scan, sizeof(bssid_max));
|
||||
@ -314,7 +310,7 @@ void WifiBeginAfterScan(void)
|
||||
// if the bssid is not the same as the last failed attempt, force picking the next strongest AP to prevent getting stuck on a strong RSSI, but poor channel health
|
||||
for (uint32_t i = 0; i < sizeof(Wifi.bssid_last); i++) {
|
||||
if (bssid_scan[i] != Wifi.bssid_last[i]) {
|
||||
best_network_db = (int8_t)rssi_scan;
|
||||
Wifi.best_network_db = (int8_t)rssi_scan;
|
||||
channel = chan_scan;
|
||||
ap = j; // AP1 or AP2
|
||||
memcpy((void*) &Wifi.bssid, (void*) bssid_scan, sizeof(Wifi.bssid));
|
||||
|
Loading…
x
Reference in New Issue
Block a user