mirror of
https://github.com/rstrouse/ESPSomfy-RTS.git
synced 2025-12-13 02:52:11 +01:00
parent
cb14cd42dc
commit
b324f59c03
9 changed files with 124 additions and 7 deletions
|
|
@ -522,11 +522,13 @@ bool WifiSettings::begin() {
|
|||
bool WifiSettings::fromJSON(JsonObject &obj) {
|
||||
this->parseValueString(obj, "ssid", this->ssid, sizeof(this->ssid));
|
||||
this->parseValueString(obj, "passphrase", this->passphrase, sizeof(this->passphrase));
|
||||
if(obj.containsKey("roaming")) this->roaming = obj["roaming"];
|
||||
return true;
|
||||
}
|
||||
bool WifiSettings::toJSON(JsonObject &obj) {
|
||||
obj["ssid"] = this->ssid;
|
||||
obj["passphrase"] = this->passphrase;
|
||||
obj["roaming"] = this->roaming;
|
||||
return true;
|
||||
}
|
||||
bool WifiSettings::save() {
|
||||
|
|
@ -534,6 +536,7 @@ bool WifiSettings::save() {
|
|||
pref.clear();
|
||||
pref.putString("ssid", this->ssid);
|
||||
pref.putString("passphrase", this->passphrase);
|
||||
pref.putBool("roaming", this->roaming);
|
||||
pref.end();
|
||||
return true;
|
||||
}
|
||||
|
|
@ -543,6 +546,7 @@ bool WifiSettings::load() {
|
|||
pref.getString("passphrase", this->passphrase, sizeof(this->passphrase));
|
||||
this->ssid[sizeof(this->ssid) - 1] = '\0';
|
||||
this->passphrase[sizeof(this->passphrase) - 1] = '\0';
|
||||
this->roaming = pref.getBool("roaming", true);
|
||||
pref.end();
|
||||
return true;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -59,6 +59,7 @@ class NTPSettings: BaseSettings {
|
|||
class WifiSettings: BaseSettings {
|
||||
public:
|
||||
WifiSettings();
|
||||
bool roaming = true;
|
||||
char ssid[65] = "";
|
||||
char passphrase[65] = "";
|
||||
//bool ssdpBroadcast = true;
|
||||
|
|
|
|||
108
Network.cpp
108
Network.cpp
|
|
@ -16,6 +16,7 @@ extern MQTTClass mqtt;
|
|||
extern rebootDelay_t rebootDelay;
|
||||
extern Network net;
|
||||
|
||||
static bool _apScanning = false;
|
||||
int connectRetries = 0;
|
||||
void Network::end() {
|
||||
sockEmit.end();
|
||||
|
|
@ -24,9 +25,11 @@ void Network::end() {
|
|||
delay(100);
|
||||
}
|
||||
bool Network::setup() {
|
||||
WiFi.setScanMethod(WIFI_ALL_CHANNEL_SCAN);
|
||||
WiFi.setSortMethod(WIFI_CONNECT_AP_BY_SIGNAL);
|
||||
WiFi.persistent(false);
|
||||
WiFi.onEvent(this->networkEvent);
|
||||
if(WiFi.status() == WL_CONNECTED) WiFi.disconnect(true);
|
||||
if(WiFi.status() == WL_CONNECTED) WiFi.disconnect(true, true);
|
||||
if(settings.connType == conn_types::wifi || settings.connType == conn_types::unset) {
|
||||
WiFi.persistent(false);
|
||||
if(settings.hostname[0] != '\0') WiFi.setHostname(settings.hostname);
|
||||
|
|
@ -63,8 +66,36 @@ void Network::loop() {
|
|||
// the MDNS library. The original library required manual updates
|
||||
// to the MDNS or it would lose its hostname after 2 minutes.
|
||||
if(this->lastMDNS != 0) MDNS.setInstanceName(settings.hostname);
|
||||
// Every 60 seconds we are going to look at wifi connectivity
|
||||
// to get around the roaming issues with ESP32. We will try to do this in an async manner. If
|
||||
// there is a channel that is better we will stop the radio and reconnect
|
||||
if(this->connType == conn_types::wifi && settings.WIFI.roaming && !this->softAPOpened) {
|
||||
// If we are not already scanning then we need to start a passive scan
|
||||
// and only respond if there is a better connection.
|
||||
// 1. If there is currently a waiting scan don't do anything
|
||||
if(!_apScanning && WiFi.scanNetworks(true, false, true, 300, 0, settings.WIFI.ssid) == -1) {
|
||||
_apScanning = true;
|
||||
}
|
||||
}
|
||||
this->lastMDNS = millis();
|
||||
}
|
||||
if(_apScanning) {
|
||||
if(!settings.WIFI.roaming || this->connType != conn_types::wifi || this->softAPOpened) _apScanning = false;
|
||||
else {
|
||||
uint16_t n = WiFi.scanComplete();
|
||||
if( n > 0) {
|
||||
_apScanning = false;
|
||||
uint8_t bssid[6];
|
||||
int32_t channel = 0;
|
||||
if(this->getStrongestAP(settings.WIFI.ssid, bssid, &channel)) {
|
||||
if(memcmp(bssid, WiFi.BSSID(), sizeof(bssid)) != 0) {
|
||||
Serial.printf("Found stronger AP %d %02X:%02X:%02X:%02X:%02X:%02X\n", channel, bssid[0], bssid[1], bssid[2], bssid[3], bssid[4], bssid[5]);
|
||||
this->changeAP(bssid, channel);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if(settings.ssdpBroadcast) {
|
||||
if(!SSDP.isStarted) SSDP.begin();
|
||||
if(SSDP.isStarted) SSDP.loop();
|
||||
|
|
@ -72,6 +103,47 @@ void Network::loop() {
|
|||
else if(!settings.ssdpBroadcast && SSDP.isStarted) SSDP.end();
|
||||
mqtt.loop();
|
||||
}
|
||||
bool Network::changeAP(const uint8_t *bssid, const int32_t channel) {
|
||||
if(SSDP.isStarted) SSDP.end();
|
||||
mqtt.disconnect();
|
||||
WiFi.disconnect(false, true);
|
||||
WiFi.begin(settings.WIFI.ssid, settings.WIFI.passphrase, channel, bssid);
|
||||
uint8_t retries = 0;
|
||||
while(retries < 100) {
|
||||
wl_status_t stat = WiFi.status();
|
||||
if(stat == WL_CONNECTED) {
|
||||
Serial.println("WiFi module connected");
|
||||
this->ssid = WiFi.SSID();
|
||||
this->mac = WiFi.BSSIDstr();
|
||||
this->strength = WiFi.RSSI();
|
||||
this->channel = WiFi.channel();
|
||||
return true;
|
||||
}
|
||||
else if(stat == WL_CONNECT_FAILED) {
|
||||
Serial.println("WiFi Module connection failed");
|
||||
return false;
|
||||
}
|
||||
else if(stat == WL_NO_SSID_AVAIL) {
|
||||
Serial.println(" Connection failed the SSID ");
|
||||
return false;
|
||||
}
|
||||
else if(stat == WL_NO_SHIELD) {
|
||||
Serial.println("Connection failed - WiFi module not found");
|
||||
return false;
|
||||
}
|
||||
else if(stat == WL_IDLE_STATUS) {
|
||||
Serial.print("*");
|
||||
}
|
||||
else if(stat == WL_DISCONNECTED) {
|
||||
Serial.print("-");
|
||||
}
|
||||
else {
|
||||
Serial.printf("Unknown status %d\n", stat);
|
||||
}
|
||||
delay(300);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
void Network::emitSockets() {
|
||||
if(this->needsBroadcast ||
|
||||
(this->connType == conn_types::wifi && (abs(abs(WiFi.RSSI()) - abs(this->lastRSSI)) > 1 || WiFi.channel() != this->lastChannel))) {
|
||||
|
|
@ -91,7 +163,7 @@ void Network::emitSockets(uint8_t num) {
|
|||
}
|
||||
else {
|
||||
if(WiFi.status() == WL_CONNECTED) {
|
||||
snprintf(buf, sizeof(buf), "{\"ssid\":\"%s\",\"strength\":%d,\"channel\":%d}", WiFi.SSID().c_str(), WiFi.RSSI(), WiFi.channel());
|
||||
snprintf(buf, sizeof(buf), "{\"ssid\":\"%s\",\"strength\":%d,\"channel\":%d}", WiFi.SSID().c_str(), WiFi.RSSI(), this->channel);
|
||||
if(num == 255)
|
||||
sockEmit.sendToClients("wifiStrength", buf);
|
||||
else
|
||||
|
|
@ -122,6 +194,10 @@ void Network::setConnected(conn_types connType) {
|
|||
WiFi.softAPdisconnect(true);
|
||||
WiFi.mode(WIFI_STA);
|
||||
}
|
||||
this->ssid = WiFi.SSID();
|
||||
this->mac = WiFi.BSSIDstr();
|
||||
this->strength = WiFi.RSSI();
|
||||
this->channel = WiFi.channel();
|
||||
}
|
||||
else if(this->connType == conn_types::ethernet) {
|
||||
if(this->softAPOpened) {
|
||||
|
|
@ -356,7 +432,14 @@ bool Network::connectWiFi() {
|
|||
WiFi.mode(WIFI_STA);
|
||||
WiFi.setScanMethod(WIFI_ALL_CHANNEL_SCAN);
|
||||
WiFi.setSortMethod(WIFI_CONNECT_AP_BY_SIGNAL);
|
||||
WiFi.begin(settings.WIFI.ssid, settings.WIFI.passphrase);
|
||||
uint8_t bssid[6];
|
||||
int32_t channel = 0;
|
||||
if(this->getStrongestAP(settings.WIFI.ssid, bssid, &channel)) {
|
||||
Serial.printf("Found strongest AP %d %02X:%02X:%02X:%02X:%02X:%02X\n", channel, bssid[0], bssid[1], bssid[2], bssid[3], bssid[4], bssid[5]);
|
||||
WiFi.begin(settings.WIFI.ssid, settings.WIFI.passphrase, channel, bssid);
|
||||
}
|
||||
else
|
||||
WiFi.begin(settings.WIFI.ssid, settings.WIFI.passphrase);
|
||||
delay(100);
|
||||
int retries = 0;
|
||||
while(retries < 100) {
|
||||
|
|
@ -439,6 +522,25 @@ uint32_t Network::getChipId() {
|
|||
}
|
||||
return chipId;
|
||||
}
|
||||
|
||||
bool Network::getStrongestAP(const char *ssid, uint8_t *bssid, int32_t *channel) {
|
||||
// The new AP must be at least 10dbm greater.
|
||||
int32_t strength = this->connected() ? WiFi.RSSI() + 10 : -127;
|
||||
int32_t chan = -1;
|
||||
memset(bssid, 0x00, 6);
|
||||
uint8_t n = this->connected() ? WiFi.scanComplete() : WiFi.scanNetworks(false, false, false, 300, 0, ssid);
|
||||
for(uint8_t i = 0; i < n; i++) {
|
||||
if(WiFi.SSID(i).compareTo(ssid) == 0) {
|
||||
if(WiFi.RSSI(i) > strength) {
|
||||
strength = WiFi.RSSI(i);
|
||||
memcpy(bssid, WiFi.BSSID(i), 6);
|
||||
*channel = chan = WiFi.channel(i);
|
||||
}
|
||||
}
|
||||
}
|
||||
WiFi.scanDelete();
|
||||
return chan > 0;
|
||||
}
|
||||
int Network::getStrengthBySSID(const char *ssid) {
|
||||
int32_t strength = -100;
|
||||
int n = WiFi.scanNetworks(false, false);
|
||||
|
|
|
|||
|
|
@ -29,6 +29,8 @@ class Network {
|
|||
bool connectWiFi();
|
||||
bool connectWired();
|
||||
void setConnected(conn_types connType);
|
||||
bool getStrongestAP(const char *ssid, uint8_t *bssid, int32_t *channel);
|
||||
bool changeAP(const uint8_t *bssid, const int32_t channel);
|
||||
//int getStrengthByMac(const char *mac);
|
||||
int getStrengthBySSID(const char *ssid);
|
||||
void updateHostname();
|
||||
|
|
|
|||
Binary file not shown.
Binary file not shown.
Binary file not shown.
|
|
@ -3,11 +3,11 @@
|
|||
<head>
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
<meta charset="UTF-8">
|
||||
<link rel="stylesheet" href="main.css?v=2.4.1a" type="text/css" />
|
||||
<link rel="stylesheet" href="widgets.css?v=2.4.1a" type="text/css" />
|
||||
<link rel="stylesheet" href="icons.css?v=2.4.1a" type="text/css" />
|
||||
<link rel="stylesheet" href="main.css?v=2.4.1b" type="text/css" />
|
||||
<link rel="stylesheet" href="widgets.css?v=2.4.1b" type="text/css" />
|
||||
<link rel="stylesheet" href="icons.css?v=2.4.1b" type="text/css" />
|
||||
<link rel="icon" type="image/png" href="favicon.png" />
|
||||
<script type="text/javascript" src="index.js?v=2.4.1a"></script>
|
||||
<script type="text/javascript" src="index.js?v=2.4.1b"></script>
|
||||
</head>
|
||||
<body>
|
||||
<div id="divContainer" class="container main" data-auth="false">
|
||||
|
|
@ -157,6 +157,10 @@
|
|||
<div class="field-group" style="vertical-align:middle;color:#00bcd4;margin-top:-24px;margin-bottom:18px;">
|
||||
<input id="cbHardwired" name="hardwired" data-bind="ethernet.hardwired" type="checkbox" style="display:inline-block;" onclick="wifi.useEthernetClicked();" />
|
||||
<label for="cbHardwired" style="display:inline-block;cursor:pointer;">Use Ethernet</label>
|
||||
<div id="divRoaming" style="display:inline-block;padding-left:7px;">
|
||||
<input id="cbRoaming" name="roaming" data-bind="wifi.roaming" type="checkbox" style="display:inline-block;" />
|
||||
<label for="cbRoaming" style="display:inline-block;cursor:pointer;">Enable Roaming</label>
|
||||
</div>
|
||||
<div id="divFallbackWireless" style="display:inline-block;padding-left:7px;">
|
||||
<input id="cbFallbackWireless" name="fallbackwireless" data-bind="ethernet.wirelessFallback" type="checkbox" style="display:inline-block;" />
|
||||
<label for="cbFallbackWireless" style="display:inline-block;cursor:pointer;">Fallback to Wireless</label>
|
||||
|
|
|
|||
|
|
@ -1672,12 +1672,14 @@ class Wifi {
|
|||
if (settings.connType >= 2) {
|
||||
document.getElementById('divWiFiMode').style.display = 'none';
|
||||
document.getElementById('divEthernetMode').style.display = '';
|
||||
document.getElementById('divRoaming').style.display = 'none';
|
||||
document.getElementById('divFallbackWireless').style.display = 'inline-block';
|
||||
}
|
||||
else {
|
||||
document.getElementById('divWiFiMode').style.display = '';
|
||||
document.getElementById('divEthernetMode').style.display = 'none';
|
||||
document.getElementById('divFallbackWireless').style.display = 'none';
|
||||
document.getElementById('divRoaming').style.display = 'inline-block';
|
||||
}
|
||||
document.getElementById('divETHSettings').style.display = settings.ethernet.boardType === 0 ? '' : 'none';
|
||||
document.getElementById('divStaticIP').style.display = settings.ip.dhcp ? 'none' : '';
|
||||
|
|
@ -1691,6 +1693,7 @@ class Wifi {
|
|||
document.getElementById('divWiFiMode').style.display = useEthernet ? 'none' : '';
|
||||
document.getElementById('divEthernetMode').style.display = useEthernet ? '' : 'none';
|
||||
document.getElementById('divFallbackWireless').style.display = useEthernet ? 'inline-block' : 'none';
|
||||
document.getElementById('divRoaming').style.display = useEthernet ? 'none' : 'inline-block';
|
||||
}
|
||||
async loadAPs() {
|
||||
if (document.getElementById('btnScanAPs').classList.contains('disabled')) return;
|
||||
|
|
@ -1878,6 +1881,7 @@ class Wifi {
|
|||
}
|
||||
procWifiStrength(strength) {
|
||||
let ssid = strength.ssid || strength.name;
|
||||
console.log(strength);
|
||||
document.getElementById('spanNetworkSSID').innerHTML = !ssid || ssid === '' ? '-------------' : ssid;
|
||||
document.getElementById('spanNetworkChannel').innerHTML = isNaN(strength.channel) || strength.channel < 0 ? '--' : strength.channel;
|
||||
let cssClass = 'waveStrength-' + (isNaN(strength.strength) || strength > 0 ? -100 : this.calcWaveStrength(strength.strength));
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue