add BSSID and Channel to network settings, full_scan

This commit is contained in:
MichaelDvP
2023-09-18 12:29:01 +02:00
parent 8d4b43e9f6
commit 09e2945c15
6 changed files with 100 additions and 41 deletions

View File

@@ -22,6 +22,8 @@ void NetworkSettingsService::begin() {
WiFi.mode(WIFI_MODE_MAX);
WiFi.mode(WIFI_MODE_NULL);
WiFi.setScanMethod(WIFI_ALL_CHANNEL_SCAN); // default is FAST_SCAN
WiFi.setSortMethod(WIFI_CONNECT_AP_BY_SIGNAL); // is default, no need to set
WiFi.onEvent(std::bind(&NetworkSettingsService::WiFiEvent, this, _1));
@@ -54,36 +56,42 @@ void NetworkSettingsService::manageSTA() {
}
// Connect or reconnect as required
WiFi.disconnect(true); // turn radio off
WiFiMode_t currentWiFiMode = WiFi.getMode();
if (currentWiFiMode == WIFI_MODE_APSTA || currentWiFiMode == WIFI_MODE_AP) {
WiFi.mode(WIFI_MODE_AP);
} else {
WiFi.mode(WIFI_MODE_NULL);
}
if (_state.staticIPConfig) {
WiFi.config(_state.localIP, _state.gatewayIP, _state.subnetMask, _state.dnsIP1, _state.dnsIP2); // configure for static IP
}
WiFi.setHostname(_state.hostname.c_str()); // set hostname
if ((WiFi.getMode() & WIFI_STA) == 0) {
if (_state.staticIPConfig) {
WiFi.config(_state.localIP, _state.gatewayIP, _state.subnetMask, _state.dnsIP1, _state.dnsIP2); // configure for static IP
}
WiFi.setHostname(_state.hostname.c_str()); // set hostname
// www.esp32.com/viewtopic.php?t=12055
if (_state.bandwidth20) {
esp_wifi_set_bandwidth((wifi_interface_t)ESP_IF_WIFI_STA, WIFI_BW_HT20);
} else {
esp_wifi_set_bandwidth((wifi_interface_t)ESP_IF_WIFI_STA, WIFI_BW_HT40);
}
if (_state.nosleep) {
WiFi.setSleep(false); // turn off sleep - WIFI_PS_NONE
}
WiFi.begin(_state.ssid.c_str(), _state.password.c_str()); // attempt to connect to the network
// www.esp32.com/viewtopic.php?t=12055
if (_state.bandwidth20) {
esp_wifi_set_bandwidth((wifi_interface_t)ESP_IF_WIFI_STA, WIFI_BW_HT20);
} else {
esp_wifi_set_bandwidth((wifi_interface_t)ESP_IF_WIFI_STA, WIFI_BW_HT40);
}
if (_state.nosleep) {
WiFi.setSleep(false); // turn off sleep - WIFI_PS_NONE
}
// attempt to connect to the network
uint mac[6];
if (!_state.bssid.isEmpty() && sscanf(_state.bssid.c_str(), "%X:%X:%X:%X:%X:%X", &mac[0], &mac[1], &mac[2], &mac[3], &mac[4], &mac[5]) == 6) {
uint8_t mac1[6];
for (uint8_t i = 0; i < 6; i++) {
mac1[i] = (uint8_t)mac[i];
}
WiFi.begin(_state.ssid.c_str(), _state.password.c_str(), _state.channel, mac1);
} else {
WiFi.begin(_state.ssid.c_str(), _state.password.c_str(), _state.channel);
}
// set power after wifi is startet, fixed value for C3_V1
// set power after wifi is startet, fixed value for C3_V1
#ifdef BOARD_C3_MINI_V1
// v1 needs this value, see https://github.com/emsesp/EMS-ESP32/pull/620#discussion_r993173979
WiFi.setTxPower(WIFI_POWER_8_5dBm); // https://www.wemos.cc/en/latest/c3/c3_mini_1_0_0.html#about-wifi
// v1 needs this value, see https://github.com/emsesp/EMS-ESP32/pull/620#discussion_r993173979
WiFi.setTxPower(WIFI_POWER_8_5dBm); // https://www.wemos.cc/en/latest/c3/c3_mini_1_0_0.html#about-wifi
#else
esp_wifi_set_max_tx_power(_state.tx_power * 4);
// esp_wifi_set_max_tx_power(_state.tx_power * 4);
WiFi.setTxPower((wifi_power_t)(_state.tx_power * 4));
#endif
}
}
// handles if wifi stopped

View File

@@ -30,17 +30,19 @@
class NetworkSettings {
public:
// core wifi configuration
String ssid;
String password;
String hostname;
bool staticIPConfig;
bool enableIPv6;
bool bandwidth20;
int8_t tx_power;
bool nosleep;
bool enableMDNS;
bool enableCORS;
String CORSOrigin;
String ssid;
String bssid;
String password;
String hostname;
uint8_t channel;
bool staticIPConfig;
bool enableIPv6;
bool bandwidth20;
int8_t tx_power;
bool nosleep;
bool enableMDNS;
bool enableCORS;
String CORSOrigin;
// optional configuration for static IP address
IPAddress localIP;
@@ -52,6 +54,8 @@ class NetworkSettings {
static void read(NetworkSettings & settings, JsonObject & root) {
// connection settings
root["ssid"] = settings.ssid;
root["bssid"] = settings.bssid;
root["channel"] = settings.channel;
root["password"] = settings.password;
root["hostname"] = settings.hostname;
root["static_ip_config"] = settings.staticIPConfig;
@@ -75,6 +79,8 @@ class NetworkSettings {
auto enableCORS = settings.enableCORS;
auto CORSOrigin = settings.CORSOrigin;
settings.ssid = root["ssid"] | FACTORY_WIFI_SSID;
settings.bssid = root["bssid"] | "";
settings.channel = root["channel"] | 0;
settings.password = root["password"] | FACTORY_WIFI_PASSWORD;
settings.hostname = root["hostname"] | FACTORY_WIFI_HOSTNAME;
settings.staticIPConfig = root["static_ip_config"] | false;