Skip to content

Commit

Permalink
Some clarifications, ETH fix
Browse files Browse the repository at this point in the history
  • Loading branch information
blazoncek committed Aug 26, 2024
1 parent df4bd99 commit 0bf1caf
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 30 deletions.
29 changes: 19 additions & 10 deletions wled00/network.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include "wled_ethernet.h"


#ifdef WLED_USE_ETHERNET
#if defined(ARDUINO_ARCH_ESP32) && defined(WLED_USE_ETHERNET)
// The following six pins are neither configurable nor
// can they be re-assigned through IOMUX / GPIO matrix.
// See https://docs.espressif.com/projects/esp-idf/en/latest/esp32/hw-reference/esp32/get-started-ethernet-kit-v1.1.html#ip101gri-phy-interface
Expand Down Expand Up @@ -221,7 +221,7 @@ bool isWiFiConfigured() {
#define ARDUINO_EVENT_WIFI_STA_GOT_IP WIFI_EVENT_STAMODE_GOT_IP
#define ARDUINO_EVENT_WIFI_STA_CONNECTED WIFI_EVENT_STAMODE_CONNECTED
#define ARDUINO_EVENT_WIFI_STA_DISCONNECTED WIFI_EVENT_STAMODE_DISCONNECTED
#elif defined(ESP32) && !defined(ESP_ARDUINO_VERSION_MAJOR) //ESP_IDF_VERSION_MAJOR==3
#elif defined(ARDUINO_ARCH_ESP32) && !defined(ESP_ARDUINO_VERSION_MAJOR) //ESP_IDF_VERSION_MAJOR==3
// not strictly IDF v3 but Arduino core related
#define ARDUINO_EVENT_WIFI_AP_STADISCONNECTED SYSTEM_EVENT_AP_STADISCONNECTED
#define ARDUINO_EVENT_WIFI_AP_STACONNECTED SYSTEM_EVENT_AP_STACONNECTED
Expand Down Expand Up @@ -270,28 +270,36 @@ void WiFiEvent(WiFiEvent_t event)
interfacesInited = false;
}
break;
#ifdef ESP32
#ifdef ARDUINO_ARCH_ESP32
case ARDUINO_EVENT_WIFI_AP_START:
DEBUG_PRINTLN(F("WiFi: AP Started"));
break;
case ARDUINO_EVENT_WIFI_AP_STOP:
DEBUG_PRINTLN(F("WiFi: AP Stopped"));
break;
#endif
#if defined(WLED_USE_ETHERNET)
#if defined(WLED_USE_ETHERNET)
case ARDUINO_EVENT_ETH_START:
DEBUG_PRINTLN(F("ETH Started"));
break;
case ARDUINO_EVENT_ETH_CONNECTED:
{
DEBUG_PRINTLN(F("ETH Connected"));
if (!apActive) {
#ifndef WLED_DISABLE_ESPNOW
if (useESPNowSync && statusESPNow == ESP_NOW_STATE_ON) WiFi.disconnect(); // if using ESP-NOW just disconnect from current SSID
else WiFi.disconnect(true); // otherwise disable WiFi entirely
#else
#ifndef WLED_DISABLE_ESPNOW
if (useESPNowSync && statusESPNow == ESP_NOW_STATE_ON) {
DEBUG_PRINTLN(F("ESP-NOW restarting on ETH connected."));
quickEspNow.stop();
WiFi.disconnect(true); // disconnect from WiFi and disengage STA mode
WiFi.mode(WIFI_MODE_AP);
quickEspNow.onDataSent(espNowSentCB); // see udp.cpp
quickEspNow.onDataRcvd(espNowReceiveCB); // see udp.cpp
quickEspNow.setWiFiBandwidth(WIFI_IF_AP, WIFI_BW_HT20); // Only needed for ESP32 in case you need coexistence with ESP8266 in the same network
bool espNowOK = quickEspNow.begin(channelESPNow, WIFI_IF_AP); // Same channel must be used for both AP and ESP-NOW
statusESPNow = espNowOK ? ESP_NOW_STATE_ON : ESP_NOW_STATE_ERROR;
} else WiFi.disconnect(true); // otherwise disable WiFi entirely
#else
WiFi.disconnect(true); // disable WiFi entirely
#endif
#endif
}
if (multiWiFi[0].staticIP != (uint32_t)0x00000000 && multiWiFi[0].staticGW != (uint32_t)0x00000000) {
ETH.config(multiWiFi[0].staticIP, multiWiFi[0].staticGW, multiWiFi[0].staticSN, dnsAddress);
Expand All @@ -315,6 +323,7 @@ void WiFiEvent(WiFiEvent_t event)
if (interfacesInited && WiFi.scanComplete() >= 0) findWiFi(true); // reinit WiFi scan
forceReconnect = true;
break;
#endif
#endif
default:
break;
Expand Down
37 changes: 17 additions & 20 deletions wled00/wled.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -479,7 +479,7 @@ void WLED::setup()
DEBUG_PRINTLN(F("Initializing WiFi"));
WiFi.persistent(false);
WiFi.onEvent(WiFiEvent);
#if defined(ESP32) && ESP_IDF_VERSION_MAJOR==4
#if defined(ARDUINO_ARCH_ESP32) && ESP_IDF_VERSION_MAJOR==4
WiFi.useStaticBuffers(true); // use preallocated buffers (for speed)
#endif
#ifdef ESP8266
Expand Down Expand Up @@ -623,7 +623,7 @@ void WLED::stopAP(bool stopESPNow) {
}
#endif
dnsServer.stop();
WiFi.softAPdisconnect(true);
WiFi.softAPdisconnect(true); // disengage AP mode on stop
apActive = false;
}

Expand All @@ -645,8 +645,8 @@ void WLED::initAP(bool resetAP)
strcpy_P(apPass, PSTR(WLED_AP_PASS));
}
DEBUG_PRINTF_P(PSTR("WiFi: Opening access point %s @ %lu ms\r\n"), apSSID, millis());
WiFi.softAPConfig(IPAddress(4, 3, 2, 1), IPAddress(4, 3, 2, 1), IPAddress(255, 255, 255, 0));
WiFi.softAP(apSSID, apPass, apChannel, apHide); // WiFi mode can be either WIFI_MODE_AP or WIFI_MODE_APSTA
WiFi.softAPConfig(IPAddress(4, 3, 2, 1), IPAddress(4, 3, 2, 1), IPAddress(255, 255, 255, 0)); // will also engage WIFI_MODE_AP
WiFi.softAP(apSSID, apPass, apChannel, apHide); // WiFi mode can be either WIFI_MODE_AP or WIFI_MODE_APSTA(==WIFI_MODE_STA & WIFI_MODE_AP)
#ifdef ARDUINO_ARCH_ESP32
WiFi.setTxPower(wifi_power_t(txPower));
#endif
Expand Down Expand Up @@ -801,21 +801,15 @@ bool WLED::initEthernet()
void WLED::initConnection()
{
DEBUG_PRINTLN(F("initConnection() called."));
bool WiFiConfigured = isWiFiConfigured();

WiFi.disconnect(); // close old connections
WiFi.disconnect(true); // close old connections (and also disengage STA mode)

lastReconnectAttempt = millis();

if (!apActive) {
//DEBUG_PRINTF_P(PSTR("WiFi: Access point disabled (init). @ %lu ms\r\n"), millis());
//stopAP(true);
WiFi.mode(WIFI_MODE_STA);
}

if (WiFiConfigured) {
if (isWiFiConfigured()) {
DEBUG_PRINTF_P(PSTR("WiFi: Connecting to %s... @ %lu ms\r\n"), multiWiFi[selectedWiFi].clientSSID, millis());

WiFi.mode(WIFI_MODE_STA); // engage explicit STA mode
// determine if using DHCP or static IP address, will also engage STA mode if not already
if (multiWiFi[selectedWiFi].staticIP != 0U && multiWiFi[selectedWiFi].staticGW != 0U) {
WiFi.config(multiWiFi[selectedWiFi].staticIP, multiWiFi[selectedWiFi].staticGW, multiWiFi[selectedWiFi].staticSN, dnsAddress);
} else {
Expand Down Expand Up @@ -941,7 +935,7 @@ void WLED::handleConnection()
// this is first attempt at connecting to SSID or we were forced to reconnect
DEBUG_PRINTF_P(PSTR("WiFi: Initial connect or forced reconnect. @ %lu ms\r\n"), millis());
selectedWiFi = findWiFi(); // find strongest WiFi
initConnection();
initConnection(); // start connecting to preferred/configured WiFi
interfacesInited = false;
forceReconnect = false;
return;
Expand All @@ -950,13 +944,12 @@ void WLED::handleConnection()
if (!Network.isConnected()) {
if (!wifiConfigured && !apActive) {
DEBUG_PRINTF_P(PSTR("WiFi: Not configured, opening AP! @ %lu ms\r\n"), millis());
initAP(); // instantly go to AP mode
initAP(); // instantly go to AP mode (but will not disengage STA mode if engaged!)
return;
}
if (!apActive && apBehavior == AP_BEHAVIOR_ALWAYS) {
DEBUG_PRINTF_P(PSTR("WiFi: AP ALWAYS enabled. @ %lu ms\r\n"), millis());
WiFi.mode(WIFI_MODE_APSTA); // this will keep AP's channel in sync with STA channel
initAP();
initAP(); // if STA is engaged (it should be) this will keep AP's channel in sync with STA channel
}
//send improv failed 6 seconds after second init attempt (24 sec. after provisioning)
if (improvActive > 2 && now > lastReconnectAttempt + 6000) {
Expand All @@ -977,6 +970,9 @@ void WLED::handleConnection()
// ESP usually connects to WiFi within 10s but we should give it a bit of time before attempting another network
// when a disconnect happens (see onEvent()) the WiFi scan is reinitiated and forced reconnect scheduled
if (wifiConfigured && multiWiFi.size() > 1 && now > lastReconnectAttempt + ((apActive) ? WLED_AP_TIMEOUT : 15000)) {
// this code is executed if ESP was unsuccessful in connecting to prefered WiFi. it is repeated every 15s
// to select different WiFi from the list (if only one WiFi is configure there will be 2 min between calls) if connects
// are not successful. if AP is still active when AP timeout us reached AP is suspended
if ((!apActive || apClients == 0)
#ifndef WLED_DISABLE_ESPNOW
// wait for 3 skipped heartbeats if ESP-NOW sync is enabled
Expand All @@ -986,19 +982,20 @@ void WLED::handleConnection()
if (improvActive == 2) improvActive = 3;
DEBUG_PRINTF_P(PSTR("WiFi: Last reconnect too old @ %lu ms\r\n"), now);
if (++selectedWiFi >= multiWiFi.size()) selectedWiFi = 0; // we couldn't connect, try with another network from the list
stopAP(true);
stopAP(true); // stop ESP-NOW and disengage AP mode
initConnection();
// postpone searching for 2 min after last SSID from list and rely on auto reconnect
if (selectedWiFi + 1U == multiWiFi.size()) lastReconnectAttempt += 120000;
return;
}
}
// open AP if this is 12s after boot connect attempt or 12s after any disconnect (_NO_CONN)
// i.e. initial connect was unsuccessful
// !wasConnected means this is after boot and we haven't yet successfully connected to SSID
if (!apActive && now > lastReconnectAttempt + 12000 && (!wasConnected || apBehavior == AP_BEHAVIOR_NO_CONN)) {
if (!(apBehavior == AP_BEHAVIOR_TEMPORARY && now > WLED_AP_TIMEOUT)) {
DEBUG_PRINTF_P(PSTR("WiFi: Opening not connected AP. @ %lu ms\r\n"), millis());
WiFi.disconnect(true); // prevent connecting to WiFi while AP is open (may be reset above)
WiFi.disconnect(true); // disengage STA mode/prevent connecting to WiFi while AP is open (may be reset above)
WiFi.mode(WIFI_MODE_AP);
initAP(); // start temporary AP only within first 5min
return;
Expand Down

0 comments on commit 0bf1caf

Please sign in to comment.