mirror of
https://github.com/emsesp/EMS-ESP32.git
synced 2025-12-06 15:59:52 +03:00
network init
This commit is contained in:
@@ -54,31 +54,36 @@ void NetworkSettingsService::manageSTA() {
|
||||
}
|
||||
|
||||
// Connect or reconnect as required
|
||||
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
|
||||
read([&](NetworkSettings & networkSettings) {
|
||||
if (networkSettings.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 (networkSettings.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
|
||||
esp_wifi_set_max_tx_power(networkSettings.tx_power * 4); // set power after wifi is startet for C3
|
||||
});
|
||||
#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
|
||||
#endif
|
||||
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
|
||||
|
||||
// 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
|
||||
|
||||
// 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
|
||||
#else
|
||||
esp_wifi_set_max_tx_power(_state.tx_power * 4);
|
||||
#endif
|
||||
}
|
||||
|
||||
// handles if wifi stopped
|
||||
@@ -89,4 +94,8 @@ void NetworkSettingsService::WiFiEvent(WiFiEvent_t event) {
|
||||
_stopping = false;
|
||||
}
|
||||
}
|
||||
// wait 3 seconds before reconnecting
|
||||
if (event == ARDUINO_EVENT_WIFI_STA_DISCONNECTED) {
|
||||
_lastConnectionAttempt = millis();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user