WLED/wled00/wled.cpp

908 wiersze
29 KiB
C++
Czysty Zwykły widok Historia

2020-04-10 10:30:08 +00:00
#define WLED_DEFINE_GLOBAL_VARS //only in one source file, wled.cpp!
#include "wled.h"
#include "wled_ethernet.h"
#ifdef WLED_ENABLE_AOTA
#define NO_OTA_PORT
#include <ArduinoOTA.h>
#endif
2020-04-10 10:30:08 +00:00
#if defined(ARDUINO_ARCH_ESP32) && defined(WLED_DISABLE_BROWNOUT_DET)
#include "soc/soc.h"
#include "soc/rtc_cntl_reg.h"
#endif
extern "C" void usePWMFixedNMI();
/*
* Main WLED class implementation. Mostly initialization and connection logic
*/
WLED::WLED()
{
}
2020-04-10 10:30:08 +00:00
// turns all LEDs off and restarts ESP
void WLED::reset()
{
briT = 0;
#ifdef WLED_ENABLE_WEBSOCKETS
ws.closeAll(1012);
#endif
unsigned long dly = millis();
while (millis() - dly < 450) {
2020-04-10 10:30:08 +00:00
yield(); // enough time to send response to client
}
applyBri();
DEBUG_PRINTLN(F("WLED RESET"));
2020-04-10 10:30:08 +00:00
ESP.restart();
}
void WLED::loop()
{
static uint32_t lastHeap = UINT32_MAX;
static unsigned long heapTime = 0;
#ifdef WLED_DEBUG
static unsigned long lastRun = 0;
unsigned long loopMillis = millis();
size_t loopDelay = loopMillis - lastRun;
if (lastRun == 0) loopDelay=0; // startup - don't have valid data from last run.
if (loopDelay > 2) DEBUG_PRINTF_P(PSTR("Loop delayed more than %ums.\n"), loopDelay);
static unsigned long maxLoopMillis = 0;
static size_t avgLoopMillis = 0;
static unsigned long maxUsermodMillis = 0;
2023-07-05 15:16:54 +00:00
static size_t avgUsermodMillis = 0;
static unsigned long maxStripMillis = 0;
2023-07-05 15:16:54 +00:00
static size_t avgStripMillis = 0;
unsigned long stripMillis;
#endif
2021-05-26 22:09:52 +00:00
handleTime();
#ifndef WLED_DISABLE_INFRARED
2020-04-10 10:30:08 +00:00
handleIR(); // 2nd call to function needed for ESP32 to return valid results -- should be good for ESP8266, too
#endif
2020-04-10 10:30:08 +00:00
handleConnection();
#ifdef WLED_ENABLE_ADALIGHT
2020-04-10 10:30:08 +00:00
handleSerial();
#endif
handleImprovWifiScan();
2020-04-10 10:30:08 +00:00
handleNotifications();
handleTransitions();
#ifdef WLED_ENABLE_DMX
2023-08-23 12:52:57 +00:00
handleDMXOutput();
#endif
#ifdef WLED_ENABLE_DMX_INPUT
dmxInput.update();
#endif
#ifdef WLED_DEBUG
unsigned long usermodMillis = millis();
#endif
UsermodManager::loop();
#ifdef WLED_DEBUG
usermodMillis = millis() - usermodMillis;
avgUsermodMillis += usermodMillis;
if (usermodMillis > maxUsermodMillis) maxUsermodMillis = usermodMillis;
#endif
2020-04-10 10:30:08 +00:00
yield();
handleIO();
2023-01-12 19:35:34 +00:00
#ifndef WLED_DISABLE_INFRARED
2020-04-10 10:30:08 +00:00
handleIR();
2023-01-12 19:35:34 +00:00
#endif
#ifndef WLED_DISABLE_ESPNOW
handleRemote();
#endif
#ifndef WLED_DISABLE_ALEXA
2020-04-10 10:30:08 +00:00
handleAlexa();
#endif
2020-04-10 10:30:08 +00:00
2020-10-12 23:39:34 +00:00
if (doCloseFile) {
closeFile();
yield();
}
2020-04-10 10:30:08 +00:00
#ifdef WLED_DEBUG
stripMillis = millis();
#endif
if (!realtimeMode || realtimeOverride || (realtimeMode && useMainSegmentOnly)) // block stuff if WARLS/Adalight is enabled
2020-04-10 10:30:08 +00:00
{
if (apActive) dnsServer.processNextRequest();
#ifdef WLED_ENABLE_AOTA
if (Network.isConnected() && aOtaEnabled && !otaLock && correctPIN) ArduinoOTA.handle();
#endif
2020-04-10 10:30:08 +00:00
handleNightlight();
yield();
#ifndef WLED_DISABLE_HUESYNC
2020-04-10 10:30:08 +00:00
handleHue();
yield();
#endif
2024-12-12 18:30:56 +00:00
if (!presetNeedsSaving()) {
handlePlaylist();
yield();
}
handlePresets();
2020-04-10 10:30:08 +00:00
yield();
2024-01-04 16:40:23 +00:00
if (!offMode || strip.isOffRefreshRequired() || strip.needsUpdate())
2020-04-10 10:30:08 +00:00
strip.service();
#ifdef ESP8266
else if (!noWifiSleep)
delay(1); //required to make sure ESP enters modem sleep (see #1184)
#endif
2020-04-10 10:30:08 +00:00
}
#ifdef WLED_DEBUG
stripMillis = millis() - stripMillis;
avgStripMillis += stripMillis;
if (stripMillis > maxStripMillis) maxStripMillis = stripMillis;
#endif
2020-04-10 10:30:08 +00:00
yield();
#ifdef ESP8266
MDNS.update();
#endif
2021-05-30 11:22:42 +00:00
//millis() rolls over every 50 days
if (lastMqttReconnectAttempt > millis()) {
rolloverMillis++;
lastMqttReconnectAttempt = 0;
2024-02-06 13:47:20 +00:00
ntpLastSyncTime = NTP_NEVER; // force new NTP query
2021-12-20 10:29:03 +00:00
strip.restartRuntime();
2021-05-30 11:22:42 +00:00
}
if (millis() - lastMqttReconnectAttempt > 30000 || lastMqttReconnectAttempt == 0) { // lastMqttReconnectAttempt==0 forces immediate broadcast
lastMqttReconnectAttempt = millis();
2023-01-12 19:35:34 +00:00
#ifndef WLED_DISABLE_MQTT
2020-04-10 10:30:08 +00:00
initMqtt();
2023-01-12 19:35:34 +00:00
#endif
2021-05-30 11:22:42 +00:00
yield();
// refresh WLED nodes list
2021-01-22 15:17:18 +00:00
refreshNodeList();
if (nodeBroadcastEnabled) sendSysInfoUDP();
2021-03-20 17:43:05 +00:00
yield();
}
2021-03-20 17:43:05 +00:00
2022-03-01 22:37:28 +00:00
// 15min PIN time-out
if (strlen(settingsPIN)>0 && correctPIN && millis() - lastEditTime > PIN_TIMEOUT) {
2022-03-01 22:37:28 +00:00
correctPIN = false;
createEditHandler(false);
2022-03-01 22:37:28 +00:00
}
// reconnect WiFi to clear stale allocations if heap gets too low
if (millis() - heapTime > 15000) {
uint32_t heap = ESP.getFreeHeap();
if (heap < MIN_HEAP_SIZE && lastHeap < MIN_HEAP_SIZE) {
2024-09-10 13:20:34 +00:00
DEBUG_PRINTF_P(PSTR("Heap too low! %u\n"), heap);
forceReconnect = true;
2024-01-24 19:22:50 +00:00
strip.resetSegments(); // remove all but one segments from memory
} else if (heap < MIN_HEAP_SIZE) {
DEBUG_PRINTLN(F("Heap low, purging segments."));
strip.purgeSegments();
}
lastHeap = heap;
heapTime = millis();
}
2021-03-20 17:43:05 +00:00
//LED settings have been saved, re-init busses
2023-01-06 08:24:29 +00:00
//This code block causes severe FPS drop on ESP32 with the original "if (busConfigs[0] != nullptr)" conditional. Investigate!
if (doInitBusses) {
2021-03-23 02:10:24 +00:00
doInitBusses = false;
2021-05-30 11:22:42 +00:00
DEBUG_PRINTLN(F("Re-init busses."));
bool aligned = strip.checkSegmentAlignment(); //see if old segments match old bus(ses)
BusManager::removeAll();
strip.finalizeInit(); // will create buses and also load default ledmap if present
2024-10-02 18:15:58 +00:00
BusManager::setBrightness(bri); // fix re-initialised bus' brightness #4005
if (aligned) strip.makeAutoSegments();
else strip.fixInvalidSegments();
BusManager::setBrightness(bri); // fix re-initialised bus' brightness
configNeedsWrite = true;
2021-03-20 17:43:05 +00:00
}
2021-11-03 13:52:22 +00:00
if (loadLedmap >= 0) {
strip.deserializeMap(loadLedmap);
2021-11-03 13:52:22 +00:00
loadLedmap = -1;
}
yield();
if (configNeedsWrite) serializeConfigToFS();
2021-06-15 21:36:12 +00:00
yield();
handleWs();
#if defined(STATUSLED)
2021-10-27 15:49:35 +00:00
handleStatusLED();
#endif
2020-05-28 00:20:02 +00:00
toki.resetTick();
#if WLED_WATCHDOG_TIMEOUT > 0
// we finished our mainloop, reset the watchdog timer
static unsigned long lastWDTFeed = 0;
if (!strip.isUpdating() || millis() - lastWDTFeed > (WLED_WATCHDOG_TIMEOUT*500)) {
#ifdef ARDUINO_ARCH_ESP32
esp_task_wdt_reset();
#else
ESP.wdtFeed();
#endif
lastWDTFeed = millis();
}
#endif
if (doReboot && (!doInitBusses || !configNeedsWrite)) // if busses have to be inited & saved, wait until next iteration
reset();
// DEBUG serial logging (every 30s)
2020-04-10 10:30:08 +00:00
#ifdef WLED_DEBUG
loopMillis = millis() - loopMillis;
if (loopMillis > 30) {
DEBUG_PRINTF_P(PSTR("Loop took %lums.\n"), loopMillis);
DEBUG_PRINTF_P(PSTR("Usermods took %lums.\n"), usermodMillis);
DEBUG_PRINTF_P(PSTR("Strip took %lums.\n"), stripMillis);
}
avgLoopMillis += loopMillis;
if (loopMillis > maxLoopMillis) maxLoopMillis = loopMillis;
if (millis() - debugTime > 29999) {
2021-05-30 11:22:42 +00:00
DEBUG_PRINTLN(F("---DEBUG INFO---"));
2024-09-12 13:04:10 +00:00
DEBUG_PRINTF_P(PSTR("Runtime: %lu\n"), millis());
2024-09-10 13:20:34 +00:00
DEBUG_PRINTF_P(PSTR("Unix time: %u,%03u\n"), toki.getTime().sec, toki.getTime().ms);
DEBUG_PRINTF_P(PSTR("Free heap: %u\n"), ESP.getFreeHeap());
#if defined(ARDUINO_ARCH_ESP32)
if (psramFound()) {
DEBUG_PRINTF_P(PSTR("PSRAM: %dkB/%dkB\n"), ESP.getFreePsram()/1024, ESP.getPsramSize()/1024);
if (!psramSafe) DEBUG_PRINTLN(F("Not using PSRAM."));
}
DEBUG_PRINTF_P(PSTR("TX power: %d/%d\n"), WiFi.getTxPower(), txPower);
2021-05-30 11:22:42 +00:00
#endif
DEBUG_PRINTF_P(PSTR("Wifi state: %d\n"), WiFi.status());
#ifndef WLED_DISABLE_ESPNOW
2024-09-10 13:20:34 +00:00
DEBUG_PRINTF_P(PSTR("ESP-NOW state: %u\n"), statusESPNow);
#endif
2020-05-28 00:20:02 +00:00
2020-04-10 10:30:08 +00:00
if (WiFi.status() != lastWifiState) {
wifiStateChangedTime = millis();
}
lastWifiState = WiFi.status();
2024-09-12 13:04:10 +00:00
DEBUG_PRINTF_P(PSTR("State time: %lu\n"), wifiStateChangedTime);
DEBUG_PRINTF_P(PSTR("NTP last sync: %lu\n"), ntpLastSyncTime);
2024-09-10 13:20:34 +00:00
DEBUG_PRINTF_P(PSTR("Client IP: %u.%u.%u.%u\n"), Network.localIP()[0], Network.localIP()[1], Network.localIP()[2], Network.localIP()[3]);
if (loops > 0) { // avoid division by zero
2024-09-12 13:04:10 +00:00
DEBUG_PRINTF_P(PSTR("Loops/sec: %u\n"), loops / 30);
DEBUG_PRINTF_P(PSTR("Loop time[ms]: %u/%lu\n"), avgLoopMillis/loops, maxLoopMillis);
DEBUG_PRINTF_P(PSTR("UM time[ms]: %u/%lu\n"), avgUsermodMillis/loops, maxUsermodMillis);
DEBUG_PRINTF_P(PSTR("Strip time[ms]:%u/%lu\n"), avgStripMillis/loops, maxStripMillis);
}
2022-08-03 19:36:47 +00:00
strip.printSize();
server.printStatus(DEBUGOUT);
2020-04-10 10:30:08 +00:00
loops = 0;
maxLoopMillis = 0;
maxUsermodMillis = 0;
maxStripMillis = 0;
2024-03-18 23:49:56 +00:00
avgLoopMillis = 0;
avgUsermodMillis = 0;
avgStripMillis = 0;
2020-04-10 10:30:08 +00:00
debugTime = millis();
}
loops++;
lastRun = millis();
2020-05-28 00:20:02 +00:00
#endif // WLED_DEBUG
2020-04-10 10:30:08 +00:00
}
#if WLED_WATCHDOG_TIMEOUT > 0
void WLED::enableWatchdog() {
#ifdef ARDUINO_ARCH_ESP32
esp_err_t watchdog = esp_task_wdt_init(WLED_WATCHDOG_TIMEOUT, true);
DEBUG_PRINT(F("Watchdog enabled: "));
if (watchdog == ESP_OK) {
DEBUG_PRINTLN(F("OK"));
} else {
DEBUG_PRINTLN(watchdog);
return;
}
esp_task_wdt_add(NULL);
#else
ESP.wdtEnable(WLED_WATCHDOG_TIMEOUT * 1000);
#endif
}
void WLED::disableWatchdog() {
DEBUG_PRINTLN(F("Watchdog: disabled"));
#ifdef ARDUINO_ARCH_ESP32
esp_task_wdt_delete(NULL);
#else
ESP.wdtDisable();
#endif
}
#endif
2020-04-10 10:30:08 +00:00
void WLED::setup()
{
#if defined(ARDUINO_ARCH_ESP32) && defined(WLED_DISABLE_BROWNOUT_DET)
WRITE_PERI_REG(RTC_CNTL_BROWN_OUT_REG, 0); //disable brownout detection
#endif
2023-03-16 12:08:34 +00:00
#ifdef ARDUINO_ARCH_ESP32
pinMode(hardwareRX, INPUT_PULLDOWN); delay(1); // suppress noise in case RX pin is floating (at low noise energy) - see issue #3128
#endif
#ifdef WLED_BOOTUPDELAY
delay(WLED_BOOTUPDELAY); // delay to let voltage stabilize, helps with boot issues on some setups
#endif
2020-04-10 10:30:08 +00:00
Serial.begin(115200);
#if !ARDUINO_USB_CDC_ON_BOOT
Serial.setTimeout(50); // this causes troubles on new MCUs that have a "virtual" USB Serial (HWCDC)
#else
#endif
#if defined(WLED_DEBUG) && defined(ARDUINO_ARCH_ESP32) && (defined(CONFIG_IDF_TARGET_ESP32S2) || defined(CONFIG_IDF_TARGET_ESP32C3) || ARDUINO_USB_CDC_ON_BOOT)
delay(2500); // allow CDC USB serial to initialise
#endif
#if !defined(WLED_DEBUG) && defined(ARDUINO_ARCH_ESP32) && !defined(WLED_DEBUG_HOST) && ARDUINO_USB_CDC_ON_BOOT
Serial.setDebugOutput(false); // switch off kernel messages when using USBCDC
#endif
2020-04-10 10:30:08 +00:00
DEBUG_PRINTLN();
2024-09-10 13:20:34 +00:00
DEBUG_PRINTF_P(PSTR("---WLED %s %u INIT---\n"), versionString, VERSION);
DEBUG_PRINTLN();
2020-04-10 10:30:08 +00:00
#ifdef ARDUINO_ARCH_ESP32
2024-09-10 13:20:34 +00:00
DEBUG_PRINTF_P(PSTR("esp32 %s\n"), ESP.getSdkVersion());
#if defined(ESP_ARDUINO_VERSION)
2024-09-10 13:20:34 +00:00
DEBUG_PRINTF_P(PSTR("arduino-esp32 v%d.%d.%d\n"), int(ESP_ARDUINO_VERSION_MAJOR), int(ESP_ARDUINO_VERSION_MINOR), int(ESP_ARDUINO_VERSION_PATCH)); // available since v2.0.0
#else
DEBUG_PRINTLN(F("arduino-esp32 v1.0.x\n")); // we can't say in more detail.
#endif
2024-09-12 13:04:10 +00:00
DEBUG_PRINTF_P(PSTR("CPU: %s rev.%d, %d core(s), %d MHz.\n"), ESP.getChipModel(), (int)ESP.getChipRevision(), ESP.getChipCores(), ESP.getCpuFreqMHz());
DEBUG_PRINTF_P(PSTR("FLASH: %d MB, Mode %d "), (ESP.getFlashChipSize()/1024)/1024, (int)ESP.getFlashChipMode());
#ifdef WLED_DEBUG
switch (ESP.getFlashChipMode()) {
// missing: Octal modes
2024-09-10 13:20:34 +00:00
case FM_QIO: DEBUG_PRINT(F("(QIO)")); break;
case FM_QOUT: DEBUG_PRINT(F("(QOUT)"));break;
case FM_DIO: DEBUG_PRINT(F("(DIO)")); break;
case FM_DOUT: DEBUG_PRINT(F("(DOUT)"));break;
2024-11-01 20:51:46 +00:00
#if defined(CONFIG_IDF_TARGET_ESP32S3) && CONFIG_ESPTOOLPY_FLASHMODE_OPI
case FM_FAST_READ: DEBUG_PRINT(F("(OPI)")); break;
#else
case FM_FAST_READ: DEBUG_PRINT(F("(fast_read)")); break;
#endif
case FM_SLOW_READ: DEBUG_PRINT(F("(slow_read)")); break;
default: break;
}
#endif
2024-09-10 13:20:34 +00:00
DEBUG_PRINTF_P(PSTR(", speed %u MHz.\n"), ESP.getFlashChipSpeed()/1000000);
2020-04-10 10:30:08 +00:00
#else
2024-09-10 13:20:34 +00:00
DEBUG_PRINTF_P(PSTR("esp8266 @ %u MHz.\nCore: %s\n"), ESP.getCpuFreqMHz(), ESP.getCoreVersion());
DEBUG_PRINTF_P(PSTR("FLASH: %u MB\n"), (ESP.getFlashChipSize()/1024)/1024);
2020-04-10 10:30:08 +00:00
#endif
2024-09-10 13:20:34 +00:00
DEBUG_PRINTF_P(PSTR("heap %u\n"), ESP.getFreeHeap());
2020-04-10 10:30:08 +00:00
#if defined(ARDUINO_ARCH_ESP32)
// BOARD_HAS_PSRAM also means that a compiler flag "-mfix-esp32-psram-cache-issue" was used and so PSRAM is safe to use on rev.1 ESP32
#if !defined(BOARD_HAS_PSRAM) && !(defined(CONFIG_IDF_TARGET_ESP32S2) || defined(CONFIG_IDF_TARGET_ESP32S3) || defined(CONFIG_IDF_TARGET_ESP32C3))
if (psramFound() && ESP.getChipRevision() < 3) psramSafe = false;
if (!psramSafe) DEBUG_PRINTLN(F("Not using PSRAM."));
#endif
pDoc = new PSRAMDynamicJsonDocument((psramSafe && psramFound() ? 2 : 1)*JSON_BUFFER_SIZE);
2024-09-10 13:20:34 +00:00
DEBUG_PRINTF_P(PSTR("JSON buffer allocated: %u\n"), (psramSafe && psramFound() ? 2 : 1)*JSON_BUFFER_SIZE);
// if the above fails requestJsonBufferLock() will always return false preventing crashes
if (psramFound()) {
DEBUG_PRINTF_P(PSTR("PSRAM: %dkB/%dkB\n"), ESP.getFreePsram()/1024, ESP.getPsramSize()/1024);
}
DEBUG_PRINTF_P(PSTR("TX power: %d/%d\n"), WiFi.getTxPower(), txPower);
#endif
2021-05-30 11:22:42 +00:00
#ifdef ESP8266
usePWMFixedNMI(); // link the NMI fix
#endif
2024-01-22 19:48:03 +00:00
#if defined(WLED_DEBUG) && !defined(WLED_DEBUG_HOST)
PinManager::allocatePin(hardwareTX, true, PinOwner::DebugOut); // TX (GPIO1 on ESP32) reserved for debug output
2021-05-30 11:22:42 +00:00
#endif
#ifdef WLED_ENABLE_DMX //reserve GPIO2 as hardcoded DMX pin
PinManager::allocatePin(2, true, PinOwner::DMX);
#endif
2024-09-10 13:20:34 +00:00
DEBUG_PRINTF_P(PSTR("heap %u\n"), ESP.getFreeHeap());
2020-11-05 21:54:13 +00:00
bool fsinit = false;
DEBUGFS_PRINTLN(F("Mount FS"));
#ifdef ARDUINO_ARCH_ESP32
fsinit = WLED_FS.begin(true);
#else
fsinit = WLED_FS.begin();
2020-04-10 10:30:08 +00:00
#endif
2020-11-05 21:54:13 +00:00
if (!fsinit) {
DEBUGFS_PRINTLN(F("FS failed!"));
errorFlag = ERR_FS_BEGIN;
2023-01-06 08:24:29 +00:00
}
#ifdef WLED_ADD_EEPROM_SUPPORT
else deEEP();
#else
initPresetsFile();
#endif
2020-11-05 21:54:13 +00:00
updateFSInfo();
2021-05-30 11:22:42 +00:00
// generate module IDs must be done before AP setup
escapedMac = WiFi.macAddress();
escapedMac.replace(":", "");
escapedMac.toLowerCase();
WLED_SET_AP_SSID(); // otherwise it is empty on first boot until config is saved
multiWiFi.push_back(WiFiConfig(CLIENT_SSID,CLIENT_PASS)); // initialise vector with default WiFi
2021-05-30 11:22:42 +00:00
DEBUG_PRINTLN(F("Reading config"));
2025-07-01 08:17:17 +00:00
bool needsCfgSave = deserializeConfigFromFS();
2024-09-10 13:20:34 +00:00
DEBUG_PRINTF_P(PSTR("heap %u\n"), ESP.getFreeHeap());
2020-04-10 10:30:08 +00:00
2021-11-30 15:28:26 +00:00
#if defined(STATUSLED) && STATUSLED>=0
if (!PinManager::isPinAllocated(STATUSLED)) {
2021-10-27 15:49:35 +00:00
// NOTE: Special case: The status LED should *NOT* be allocated.
// See comments in handleStatusLed().
pinMode(STATUSLED, OUTPUT);
}
#endif
2021-05-30 11:22:42 +00:00
DEBUG_PRINTLN(F("Initializing strip"));
2020-04-10 10:30:08 +00:00
beginStrip();
2024-09-10 13:20:34 +00:00
DEBUG_PRINTF_P(PSTR("heap %u\n"), ESP.getFreeHeap());
2021-05-30 11:22:42 +00:00
DEBUG_PRINTLN(F("Usermods setup"));
UsermodManager::setup();
2024-09-10 13:20:34 +00:00
DEBUG_PRINTF_P(PSTR("heap %u\n"), ESP.getFreeHeap());
2025-07-01 08:17:17 +00:00
if (needsCfgSave) serializeConfigToFS(); // usermods required new parameters; need to wait for strip to be initialised #4752
if (strcmp(multiWiFi[0].clientSSID, DEFAULT_CLIENT_SSID) == 0)
2020-04-10 10:30:08 +00:00
showWelcomePage = true;
WiFi.persistent(false);
WiFi.onEvent(WiFiEvent);
WiFi.mode(WIFI_STA); // enable scanning
findWiFi(true); // start scanning for available WiFi-s
// all GPIOs are allocated at this point
serialCanRX = !PinManager::isPinAllocated(hardwareRX); // Serial RX pin (GPIO 3 on ESP32 and ESP8266)
serialCanTX = !PinManager::isPinAllocated(hardwareTX) || PinManager::getPinOwner(hardwareTX) == PinOwner::DebugOut; // Serial TX pin (GPIO 1 on ESP32 and ESP8266)
2021-05-30 11:22:42 +00:00
#ifdef WLED_ENABLE_ADALIGHT
//Serial RX (Adalight, Improv, Serial JSON) only possible if GPIO3 unused
//Serial TX (Debug, Improv, Serial JSON) only possible if GPIO1 unused
if (serialCanRX && serialCanTX) {
2021-05-30 11:22:42 +00:00
Serial.println(F("Ada"));
}
#endif
2020-04-10 10:30:08 +00:00
// fill in unique mdns default
2025-01-17 15:05:26 +00:00
if (strcmp(cmDNS, DEFAULT_MDNS_NAME) == 0) sprintf_P(cmDNS, PSTR("wled-%*s"), 6, escapedMac.c_str() + 6);
2023-01-12 19:35:34 +00:00
#ifndef WLED_DISABLE_MQTT
if (mqttDeviceTopic[0] == 0) sprintf_P(mqttDeviceTopic, PSTR("wled/%*s"), 6, escapedMac.c_str() + 6);
if (mqttClientID[0] == 0) sprintf_P(mqttClientID, PSTR("WLED-%*s"), 6, escapedMac.c_str() + 6);
2023-01-12 19:35:34 +00:00
#endif
2020-04-10 10:30:08 +00:00
#ifdef WLED_ENABLE_AOTA
2020-04-10 10:30:08 +00:00
if (aOtaEnabled) {
ArduinoOTA.onStart([]() {
#ifdef ESP8266
2020-04-10 10:30:08 +00:00
wifi_set_sleep_type(NONE_SLEEP_T);
#endif
#if WLED_WATCHDOG_TIMEOUT > 0
WLED::instance().disableWatchdog();
#endif
2020-09-19 23:18:31 +00:00
DEBUG_PRINTLN(F("Start ArduinoOTA"));
2020-04-10 10:30:08 +00:00
});
ArduinoOTA.onError([](ota_error_t error) {
#if WLED_WATCHDOG_TIMEOUT > 0
// reenable watchdog on failed update
WLED::instance().enableWatchdog();
#endif
});
2020-04-10 10:30:08 +00:00
if (strlen(cmDNS) > 0)
ArduinoOTA.setHostname(cmDNS);
}
#endif
#ifdef WLED_ENABLE_DMX
2023-08-23 13:04:28 +00:00
initDMXOutput();
2020-04-10 10:30:08 +00:00
#endif
#ifdef WLED_ENABLE_DMX_INPUT
2023-08-23 12:33:12 +00:00
dmxInput.init(dmxInputReceivePin, dmxInputTransmitPin, dmxInputEnablePin, dmxInputPort);
2020-04-10 10:30:08 +00:00
#endif
#ifdef WLED_ENABLE_ADALIGHT
if (serialCanRX && Serial.available() > 0 && Serial.peek() == 'I') handleImprovPacket();
#endif
2020-04-10 10:30:08 +00:00
// HTTP server page init
DEBUG_PRINTLN(F("initServer"));
2020-04-10 10:30:08 +00:00
initServer();
2024-09-10 13:20:34 +00:00
DEBUG_PRINTF_P(PSTR("heap %u\n"), ESP.getFreeHeap());
2021-05-30 11:22:42 +00:00
#ifndef WLED_DISABLE_INFRARED
// init IR
DEBUG_PRINTLN(F("initIR"));
initIR();
2024-09-10 13:20:34 +00:00
DEBUG_PRINTF_P(PSTR("heap %u\n"), ESP.getFreeHeap());
#endif
// Seed FastLED random functions with an esp random value, which already works properly at this point.
const uint32_t seed32 = hw_random();
random16_set_seed((uint16_t)seed32);
#if WLED_WATCHDOG_TIMEOUT > 0
enableWatchdog();
#endif
2021-05-30 11:22:42 +00:00
#if defined(ARDUINO_ARCH_ESP32) && defined(WLED_DISABLE_BROWNOUT_DET)
WRITE_PERI_REG(RTC_CNTL_BROWN_OUT_REG, 1); //enable brownout detector
#endif
2020-04-10 10:30:08 +00:00
}
void WLED::beginStrip()
{
// Initialize NeoPixel Strip and button
strip.setTransition(0); // temporarily prevent transitions to reduce segment copies
strip.finalizeInit(); // busses created during deserializeConfig() if config existed
strip.makeAutoSegments();
2020-11-05 21:54:13 +00:00
strip.setBrightness(0);
2020-04-10 10:30:08 +00:00
strip.setShowCallback(handleOverlayDraw);
doInitBusses = false;
2020-04-10 10:30:08 +00:00
2021-06-15 21:36:12 +00:00
if (turnOnAtBoot) {
if (briS > 0) bri = briS;
else if (bri == 0) bri = 128;
} else {
2023-05-30 14:09:51 +00:00
// fix for #3196
if (bootPreset > 0) {
// set all segments black (no transition)
for (unsigned i = 0; i < strip.getSegmentsNum(); i++) {
Segment &seg = strip.getSegment(i);
if (seg.isActive()) seg.colors[0] = BLACK;
}
2025-02-06 13:57:18 +00:00
colPri[0] = colPri[1] = colPri[2] = colPri[3] = 0; // needed for colorUpdated()
}
briLast = briS; bri = 0;
strip.fill(BLACK);
strip.show();
}
2024-12-12 18:30:56 +00:00
colorUpdated(CALL_MODE_INIT); // will not send notification but will initiate transition
2021-06-15 21:36:12 +00:00
if (bootPreset > 0) {
2021-07-10 15:01:20 +00:00
applyPreset(bootPreset, CALL_MODE_INIT);
2021-06-15 21:36:12 +00:00
}
2020-04-10 10:30:08 +00:00
strip.setTransition(transitionDelayDefault); // restore transitions
2021-01-16 23:20:31 +00:00
// init relay pin
if (rlyPin >= 0) {
pinMode(rlyPin, rlyOpenDrain ? OUTPUT_OPEN_DRAIN : OUTPUT);
2021-01-16 23:20:31 +00:00
digitalWrite(rlyPin, (rlyMde ? bri : !bri));
}
2020-04-10 10:30:08 +00:00
}
void WLED::initAP(bool resetAP)
{
if (apBehavior == AP_BEHAVIOR_BUTTON_ONLY && !resetAP)
return;
if (resetAP) {
WLED_SET_AP_SSID();
strcpy_P(apPass, PSTR(WLED_AP_PASS));
}
2020-09-19 23:18:31 +00:00
DEBUG_PRINT(F("Opening access point "));
2020-04-10 10:30:08 +00:00
DEBUG_PRINTLN(apSSID);
WiFi.softAPConfig(IPAddress(4, 3, 2, 1), IPAddress(4, 3, 2, 1), IPAddress(255, 255, 255, 0));
WiFi.softAP(apSSID, apPass, apChannel, apHide);
#ifdef ARDUINO_ARCH_ESP32
WiFi.setTxPower(wifi_power_t(txPower));
#endif
2020-04-10 10:30:08 +00:00
if (!apActive) // start captive portal if AP active
2020-04-10 10:30:08 +00:00
{
2020-09-19 23:18:31 +00:00
DEBUG_PRINTLN(F("Init AP interfaces"));
2020-04-10 10:30:08 +00:00
server.begin();
if (udpPort > 0 && udpPort != ntpLocalPort) {
udpConnected = notifierUdp.begin(udpPort);
}
if (udpRgbPort > 0 && udpRgbPort != ntpLocalPort && udpRgbPort != udpPort) {
udpRgbConnected = rgbUdp.begin(udpRgbPort);
}
if (udpPort2 > 0 && udpPort2 != ntpLocalPort && udpPort2 != udpPort && udpPort2 != udpRgbPort) {
udp2Connected = notifier2Udp.begin(udpPort2);
}
e131.begin(false, e131Port, e131Universe, E131_MAX_UNIVERSE_COUNT);
2021-10-01 19:56:54 +00:00
ddp.begin(false, DDP_DEFAULT_PORT);
2021-01-16 23:20:31 +00:00
2020-04-10 10:30:08 +00:00
dnsServer.setErrorReplyCode(DNSReplyCode::NoError);
dnsServer.start(53, "*", WiFi.softAPIP());
}
apActive = true;
}
void WLED::initConnection()
{
DEBUG_PRINTF_P(PSTR("initConnection() called @ %lus.\n"), millis()/1000);
#ifdef WLED_ENABLE_WEBSOCKETS
ws.onEvent(wsEvent);
#endif
#ifndef WLED_DISABLE_ESPNOW
if (statusESPNow == ESP_NOW_STATE_ON) {
DEBUG_PRINTLN(F("ESP-NOW stopping."));
quickEspNow.stop();
statusESPNow = ESP_NOW_STATE_UNINIT;
}
#endif
2024-01-24 18:52:41 +00:00
WiFi.disconnect(true); // close old connections
2025-02-15 09:34:44 +00:00
delay(5); // wait for hardware to be ready
2020-04-10 10:30:08 +00:00
#ifdef ESP8266
2023-12-23 20:05:01 +00:00
WiFi.setPhyMode(force802_3g ? WIFI_PHY_MODE_11G : WIFI_PHY_MODE_11N);
2020-04-10 10:30:08 +00:00
#endif
if (multiWiFi[selectedWiFi].staticIP != 0U && multiWiFi[selectedWiFi].staticGW != 0U) {
WiFi.config(multiWiFi[selectedWiFi].staticIP, multiWiFi[selectedWiFi].staticGW, multiWiFi[selectedWiFi].staticSN, dnsAddress);
2020-04-10 10:30:08 +00:00
} else {
WiFi.config(IPAddress((uint32_t)0), IPAddress((uint32_t)0), IPAddress((uint32_t)0));
2020-04-10 10:30:08 +00:00
}
lastReconnectAttempt = millis();
if (!WLED_WIFI_CONFIGURED) {
DEBUG_PRINTLN(F("No connection configured."));
if (!apActive) initAP(); // instantly go to ap mode
return;
2020-04-10 10:30:08 +00:00
} else if (!apActive) {
if (apBehavior == AP_BEHAVIOR_ALWAYS) {
DEBUG_PRINTLN(F("Access point ALWAYS enabled."));
2020-04-10 10:30:08 +00:00
initAP();
} else {
DEBUG_PRINTLN(F("Access point disabled (init)."));
2020-04-10 10:30:08 +00:00
WiFi.softAPdisconnect(true);
WiFi.mode(WIFI_STA);
2020-04-10 10:30:08 +00:00
}
}
if (WLED_WIFI_CONFIGURED) {
showWelcomePage = false;
DEBUG_PRINTF_P(PSTR("Connecting to %s...\n"), multiWiFi[selectedWiFi].clientSSID);
2020-04-10 10:30:08 +00:00
// convert the "serverDescription" into a valid DNS hostname (alphanumeric)
char hostname[25];
prepareHostname(hostname);
WiFi.begin(multiWiFi[selectedWiFi].clientSSID, multiWiFi[selectedWiFi].clientPass); // no harm if called multiple times
2020-04-10 10:30:08 +00:00
#ifdef ARDUINO_ARCH_ESP32
WiFi.setTxPower(wifi_power_t(txPower));
WiFi.setSleep(!noWifiSleep);
WiFi.setHostname(hostname);
2020-04-10 10:30:08 +00:00
#else
wifi_set_sleep_type((noWifiSleep) ? NONE_SLEEP_T : MODEM_SLEEP_T);
WiFi.hostname(hostname);
#endif
}
#ifndef WLED_DISABLE_ESPNOW
if (enableESPNow) {
quickEspNow.onDataSent(espNowSentCB); // see udp.cpp
quickEspNow.onDataRcvd(espNowReceiveCB); // see udp.cpp
bool espNowOK;
if (apActive) {
DEBUG_PRINTLN(F("ESP-NOW initing in AP mode."));
#ifdef ESP32
quickEspNow.setWiFiBandwidth(WIFI_IF_AP, WIFI_BW_HT20); // Only needed for ESP32 in case you need coexistence with ESP8266 in the same network
#endif //ESP32
espNowOK = quickEspNow.begin(apChannel, WIFI_IF_AP); // Same channel must be used for both AP and ESP-NOW
} else {
DEBUG_PRINTLN(F("ESP-NOW initing in STA mode."));
espNowOK = quickEspNow.begin(); // Use no parameters to start ESP-NOW on same channel as WiFi, in STA mode
}
statusESPNow = espNowOK ? ESP_NOW_STATE_ON : ESP_NOW_STATE_ERROR;
}
2020-04-10 10:30:08 +00:00
#endif
}
void WLED::initInterfaces()
{
2020-09-19 23:18:31 +00:00
DEBUG_PRINTLN(F("Init STA interfaces"));
2020-04-10 10:30:08 +00:00
2021-05-30 11:22:42 +00:00
#ifndef WLED_DISABLE_HUESYNC
IPAddress ipAddress = Network.localIP();
2020-04-10 10:30:08 +00:00
if (hueIP[0] == 0) {
hueIP[0] = ipAddress[0];
hueIP[1] = ipAddress[1];
hueIP[2] = ipAddress[2];
2020-04-10 10:30:08 +00:00
}
2021-05-30 11:22:42 +00:00
#endif
2020-04-10 10:30:08 +00:00
2023-01-12 19:35:34 +00:00
#ifndef WLED_DISABLE_ALEXA
2020-04-10 10:30:08 +00:00
// init Alexa hue emulation
if (alexaEnabled)
alexaInit();
2023-01-12 19:35:34 +00:00
#endif
2020-04-10 10:30:08 +00:00
#ifdef WLED_ENABLE_AOTA
if (aOtaEnabled) ArduinoOTA.begin();
2020-04-10 10:30:08 +00:00
#endif
// Set up mDNS responder:
if (strlen(cmDNS) > 0) {
// "end" must be called before "begin" is called a 2nd time
// see https://github.com/esp8266/Arduino/issues/7213
MDNS.end();
MDNS.begin(cmDNS);
2020-04-10 10:30:08 +00:00
2020-09-19 23:18:31 +00:00
DEBUG_PRINTLN(F("mDNS started"));
2020-04-10 10:30:08 +00:00
MDNS.addService("http", "tcp", 80);
MDNS.addService("wled", "tcp", 80);
MDNS.addServiceTxt("wled", "tcp", "mac", escapedMac.c_str());
}
server.begin();
if (udpPort > 0 && udpPort != ntpLocalPort) {
udpConnected = notifierUdp.begin(udpPort);
if (udpConnected && udpRgbPort != udpPort)
udpRgbConnected = rgbUdp.begin(udpRgbPort);
if (udpConnected && udpPort2 != udpPort && udpPort2 != udpRgbPort)
udp2Connected = notifier2Udp.begin(udpPort2);
2020-04-10 10:30:08 +00:00
}
if (ntpEnabled)
ntpConnected = ntpUdp.begin(ntpLocalPort);
2020-04-12 22:42:27 +00:00
e131.begin(e131Multicast, e131Port, e131Universe, E131_MAX_UNIVERSE_COUNT);
2021-10-01 19:56:54 +00:00
ddp.begin(false, DDP_DEFAULT_PORT);
2020-04-10 10:30:08 +00:00
reconnectHue();
interfacesInited = true;
wasConnected = true;
}
void WLED::handleConnection()
{
2024-01-26 17:38:56 +00:00
static bool scanDone = true;
static byte stacO = 0;
const unsigned long now = millis();
2025-04-26 18:08:15 +00:00
#ifdef WLED_DEBUG
const unsigned long nowS = now/1000;
2025-04-26 18:08:15 +00:00
#endif
2024-01-24 18:52:41 +00:00
const bool wifiConfigured = WLED_WIFI_CONFIGURED;
// ignore connection handling if WiFi is configured and scan still running
// or within first 2s if WiFi is not configured or AP is always active
2024-01-27 07:39:54 +00:00
if ((wifiConfigured && multiWiFi.size() > 1 && WiFi.scanComplete() < 0) || (now < 2000 && (!wifiConfigured || apBehavior == AP_BEHAVIOR_ALWAYS)))
2020-04-10 10:30:08 +00:00
return;
if (lastReconnectAttempt == 0 || forceReconnect) {
DEBUG_PRINTF_P(PSTR("Initial connect or forced reconnect (@ %lus).\n"), nowS);
selectedWiFi = findWiFi(); // find strongest WiFi
2020-04-10 10:30:08 +00:00
initConnection();
interfacesInited = false;
forceReconnect = false;
wasConnected = false;
return;
}
2020-04-10 10:30:08 +00:00
byte stac = 0;
if (apActive) {
#ifdef ESP8266
stac = wifi_softap_get_station_num();
#else
wifi_sta_list_t stationList;
esp_wifi_ap_get_sta_list(&stationList);
stac = stationList.num;
#endif
if (stac != stacO) {
stacO = stac;
DEBUG_PRINTF_P(PSTR("Connected AP clients: %d\n"), (int)stac);
if (!Network.isConnected() && wifiConfigured) { // trying to connect, but not connected
2020-04-10 10:30:08 +00:00
if (stac)
WiFi.disconnect(); // disable search so that AP can work
else
initConnection(); // restart search
2020-04-10 10:30:08 +00:00
}
}
}
if (!Network.isConnected()) {
2020-04-10 10:30:08 +00:00
if (interfacesInited) {
2024-01-27 07:39:54 +00:00
if (scanDone && multiWiFi.size() > 1) {
2024-01-26 17:38:56 +00:00
DEBUG_PRINTLN(F("WiFi scan initiated on disconnect."));
findWiFi(true); // reinit scan
scanDone = false;
return; // try to connect in next iteration
}
2020-09-19 23:18:31 +00:00
DEBUG_PRINTLN(F("Disconnected!"));
2024-01-26 17:38:56 +00:00
selectedWiFi = findWiFi();
2020-04-10 10:30:08 +00:00
initConnection();
interfacesInited = false;
2024-01-26 17:38:56 +00:00
scanDone = true;
return;
2020-04-10 10:30:08 +00:00
}
//send improv failed 6 seconds after second init attempt (24 sec. after provisioning)
if (improvActive > 2 && now - lastReconnectAttempt > 6000) {
sendImprovStateResponse(0x03, true);
improvActive = 2;
}
2024-01-24 18:52:41 +00:00
if (now - lastReconnectAttempt > ((stac) ? 300000 : 18000) && wifiConfigured) {
if (improvActive == 2) improvActive = 3;
DEBUG_PRINTF_P(PSTR("Last reconnect (%lus) too old (@ %lus).\n"), lastReconnectAttempt/1000, nowS);
if (++selectedWiFi >= multiWiFi.size()) selectedWiFi = 0; // we couldn't connect, try with another network from the list
2020-04-10 10:30:08 +00:00
initConnection();
}
if (!apActive && now - lastReconnectAttempt > 12000 && (!wasConnected || apBehavior == AP_BEHAVIOR_NO_CONN)) {
2024-01-26 14:31:09 +00:00
if (!(apBehavior == AP_BEHAVIOR_TEMPORARY && now > WLED_AP_TIMEOUT)) {
DEBUG_PRINTF_P(PSTR("Not connected AP (@ %lus).\n"), nowS);
initAP(); // start AP only within first 5min
}
2024-01-26 14:31:09 +00:00
}
if (apActive && apBehavior == AP_BEHAVIOR_TEMPORARY && now > WLED_AP_TIMEOUT && stac == 0) { // disconnect AP after 5min if no clients connected
2023-12-10 08:59:36 +00:00
// if AP was enabled more than 10min after boot or if client was connected more than 10min after boot do not disconnect AP mode
2024-01-26 14:31:09 +00:00
if (now < 2*WLED_AP_TIMEOUT) {
2023-12-10 08:59:36 +00:00
dnsServer.stop();
WiFi.softAPdisconnect(true);
apActive = false;
DEBUG_PRINTF_P(PSTR("Temporary AP disabled (@ %lus).\n"), nowS);
2023-12-10 08:59:36 +00:00
}
}
} else if (!interfacesInited) { //newly connected
DEBUG_PRINTLN();
2020-09-19 23:18:31 +00:00
DEBUG_PRINT(F("Connected! IP address: "));
DEBUG_PRINTLN(Network.localIP());
if (improvActive) {
if (improvError == 3) sendImprovStateResponse(0x00, true);
sendImprovStateResponse(0x04);
if (improvActive > 1) sendImprovIPRPCResult(ImprovRPCType::Command_Wifi);
}
2020-04-10 10:30:08 +00:00
initInterfaces();
UsermodManager::connected();
lastMqttReconnectAttempt = 0; // force immediate update
2020-04-10 10:30:08 +00:00
// shut down AP
if (apBehavior != AP_BEHAVIOR_ALWAYS && apActive) {
dnsServer.stop();
WiFi.softAPdisconnect(true);
apActive = false;
DEBUG_PRINTLN(F("Access point disabled (connected)."));
2020-04-10 10:30:08 +00:00
}
}
}
// If status LED pin is allocated for other uses, does nothing
// else blink at 1Hz when Network.isConnected() is false (no WiFi, ?? no Ethernet ??)
// else blink at 2Hz when MQTT is enabled but not connected
// else turn the status LED off
#if defined(STATUSLED)
void WLED::handleStatusLED()
{
2021-11-30 15:28:26 +00:00
uint32_t c = 0;
2021-11-30 15:28:26 +00:00
#if STATUSLED>=0
if (PinManager::isPinAllocated(STATUSLED)) {
return; //lower priority if something else uses the same pin
}
2021-11-30 15:28:26 +00:00
#endif
2021-01-16 23:20:31 +00:00
if (Network.isConnected()) {
2021-11-30 15:28:26 +00:00
c = RGBW32(0,255,0,0);
ledStatusType = 2;
} else if (WLED_MQTT_CONNECTED) {
c = RGBW32(0,128,0,0);
ledStatusType = 4;
} else if (apActive) {
c = RGBW32(0,0,255,0);
ledStatusType = 1;
}
if (ledStatusType) {
if (millis() - ledStatusLastMillis >= (1000/ledStatusType)) {
ledStatusLastMillis = millis();
2021-11-30 15:28:26 +00:00
ledStatusState = !ledStatusState;
#if STATUSLED>=0
digitalWrite(STATUSLED, ledStatusState);
2021-11-30 15:28:26 +00:00
#else
BusManager::setStatusPixel(ledStatusState ? c : 0);
2021-11-30 15:28:26 +00:00
#endif
}
} else {
2021-11-30 15:28:26 +00:00
#if STATUSLED>=0
#ifdef STATUSLEDINVERTED
digitalWrite(STATUSLED, HIGH);
2021-11-30 15:28:26 +00:00
#else
digitalWrite(STATUSLED, LOW);
2021-11-30 15:28:26 +00:00
#endif
#else
BusManager::setStatusPixel(0);
#endif
}
2021-10-11 08:56:25 +00:00
}
#endif