From 7d1ba361c2041266efdabc5be6d249ef77d526e9 Mon Sep 17 00:00:00 2001 From: Torsten Stauder Date: Wed, 25 Nov 2020 09:15:19 +0100 Subject: [PATCH] Wifi-toggle immediately effective --- README.md | 4 ++-- src/logmessages.h | 4 ++-- src/logmessages_EN.h | 4 ++-- src/main.cpp | 49 +++++++++++++++++++++++++++++++++++++++++--- 4 files changed, 52 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 5431b93..b1992a0 100644 --- a/README.md +++ b/README.md @@ -143,7 +143,7 @@ Please note: hostname can be used to call webgui or FTP-server. I tested it with WiFi is mandatory for webgui, FTP and MQTT. However, WiFi can be temporarily or permanently disabled. There are two ways to do that: * Use a special modification-card that can be configured via webgui * Press previous + next-button in parallel shortly -This toggles the current WiFi-status which means: if it's currently enabled, it will be disabled and vice versa. Please note: change is *not effective until the next reboot* but will remain until you change it again. Having Wifi enabled is indicated in idle-mode (no playlist active) with four white slow rotating LEDs whereas disabled WiFi is represented by those ones colored blue. +This toggles the current WiFi-status which means: if it's currently enabled, it will be disabled and vice versa. Please note: This status will remain until you change it again. Having Wifi enabled is indicated in idle-mode (no playlist active) with four white slow rotating LEDs whereas disabled WiFi is represented by those ones colored blue. ## After Tonuino-ESP32 is connected to your WiFi After getting Tonuino part of your LAN/WiFi, the 'regular' webgui is available at the IP assigned by your router. Using this GUI, you can configure: * WiFi @@ -200,7 +200,7 @@ There are special RFID-tags, that don't start music by themself but can modify t * current track in loop-mode (is "stronger" than playlist-loop but doesn't overwrite it!) * playlist in loop-mode * track und playlist loop-mode can both be activated at the same time, but unless track-loop isn't deactivated, playlist-loop won't be effective -* Toggle WiFi (enable/disable; effective after reboot) +* Toggle WiFi (enable/disable) ### Neopixel-ring (optional) Indicates different things. Don't forget configuration of number of LEDs via #define NUM_LEDS diff --git a/src/logmessages.h b/src/logmessages.h index 2cc49d0..c76fec0 100644 --- a/src/logmessages.h +++ b/src/logmessages.h @@ -143,5 +143,5 @@ static const char restoredHostnameFromNvs[] PROGMEM = "Hostname aus NVS geladen" static const char currentVoltageMsg[] PROGMEM = "Aktuelle Batteriespannung"; static const char voltageTooLow[] PROGMEM = "Batteriespannung niedrig"; static const char sdBootFailedDeepsleep[] PROGMEM = "Bootgang wegen SD fehlgeschlagen. Gehe in Deepsleep..."; -static const char wifiEnabledAfterRestart[] PROGMEM = "WLAN wird ab dem nächsten Neustart aktiviert."; -static const char wifiDisabledAfterRestart[] PROGMEM = "WLAN wird ab dem nächsten Neustart deaktiviert."; \ No newline at end of file +static const char wifiEnabledAfterRestart[] PROGMEM = "WLAN wird aktiviert."; +static const char wifiDisabledAfterRestart[] PROGMEM = "WLAN wird deaktiviert."; \ No newline at end of file diff --git a/src/logmessages_EN.h b/src/logmessages_EN.h index ffbcaf9..84bd1a5 100644 --- a/src/logmessages_EN.h +++ b/src/logmessages_EN.h @@ -143,5 +143,5 @@ static const char restoredHostnameFromNvs[] PROGMEM = "Restored hostname from NV static const char currentVoltageMsg[] PROGMEM = "Current battery-voltage"; static const char voltageTooLow[] PROGMEM = "Low battery-voltage"; static const char sdBootFailedDeepsleep[] PROGMEM = "Failed to boot due to SD. Will go to deepsleep..."; -static const char wifiEnabledAfterRestart[] PROGMEM = "WiFi will be enabled (effective after next boot)."; -static const char wifiDisabledAfterRestart[] PROGMEM = "WiFi will be disabled (effective after next boot)."; \ No newline at end of file +static const char wifiEnabledAfterRestart[] PROGMEM = "WiFi will be enabled."; +static const char wifiDisabledAfterRestart[] PROGMEM = "WiFi will be disabled ."; \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index fd7f501..695e571 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -261,6 +261,8 @@ static const char backupFile[] PROGMEM = "/backup.txt"; // File is written every unsigned long wifiCheckLastTimestamp = 0; bool wifiEnabled; // Current status if wifi is enabled uint32_t wifiStatusToggledTimestamp = 0; +bool webserverStarted = false; +bool wifiNeedsRestart = false; // Neopixel #ifdef NEOPIXEL_ENABLE bool showLedError = false; @@ -412,6 +414,7 @@ bool fileValid(const char *_fileItem); void freeMultiCharArray(char **arr, const uint32_t cnt); uint8_t getRepeatMode(void); bool getWifiEnableStatusFromNVS(void); +void handleUpload(AsyncWebServerRequest *request, String filename, size_t index, uint8_t *data, size_t len, bool final); void headphoneVolumeManager(void); bool isNumber(const char *str); void loggerNl(const char *str, const uint8_t logLevel); @@ -1297,6 +1300,8 @@ void playAudio(void *parameter) { trackCommand = 0; loggerNl((char *) FPSTR(cmndStop), LOGLEVEL_INFO); playProperties.pausePlay = true; + playProperties.playlistFinished = true; + playProperties.playMode = NO_PLAYLIST; continue; case PAUSEPLAY: @@ -2715,12 +2720,18 @@ bool writeWifiStatusToNVS(bool wifiStatus) { if (!wifiStatus) { if (prefsSettings.putUInt("enableWifi", 0)) { // disable loggerNl((char *) FPSTR(wifiDisabledAfterRestart), LOGLEVEL_NOTICE); + trackControlToQueueSender(STOP); + delay(300); + WiFi.mode(WIFI_OFF); + wifiEnabled = false; return true; } } else { if (prefsSettings.putUInt("enableWifi", 1)) { // enable loggerNl((char *) FPSTR(wifiEnabledAfterRestart), LOGLEVEL_NOTICE); + wifiNeedsRestart = true; + wifiEnabled = true; return true; } } @@ -2734,7 +2745,7 @@ wl_status_t wifiManager(void) { return WiFi.status(); } - if (wifiCheckLastTimestamp == 0) { + if (!wifiCheckLastTimestamp || wifiNeedsRestart) { // Get credentials from NVS String strSSID = prefsSettings.getString("SSID", "-1"); if (!strSSID.compareTo("-1")) { @@ -2781,6 +2792,7 @@ wl_status_t wifiManager(void) { } else { // Starts AP if WiFi-connect wasn't successful accessPointStart((char *) FPSTR(accessPointNetworkSSID), apIP, apNetmask); } + wifiNeedsRestart = false; } return WiFi.status(); @@ -3093,6 +3105,36 @@ bool isNumber(const char *str) { } +void webserverStart(void) { + if (wifiManager() == WL_CONNECTED && !webserverStarted) { + // attach AsyncWebSocket for Mgmt-Interface + ws.onEvent(onWebsocketEvent); + wServer.addHandler(&ws); + + // attach AsyncEventSource + wServer.addHandler(&events); + + wServer.on("/", HTTP_GET, [](AsyncWebServerRequest *request) { + request->send_P(200, "text/html", mgtWebsite, templateProcessor); + }); + + wServer.on("/upload", HTTP_POST, [](AsyncWebServerRequest *request){ + request->send_P(200, "text/html", backupRecoveryWebsite); + }, handleUpload); + + wServer.on("/restart", HTTP_GET, [] (AsyncWebServerRequest *request) { + request->send_P(200, "text/html", restartWebsite); + Serial.flush(); + ESP.restart(); + }); + + wServer.onNotFound(notFound); + wServer.begin(); + webserverStarted = true; + } +} + + // Dumps all RFID-entries from NVS into a file on SD-card bool dumpNvsToSd(char *_namespace, char *_destFile) { esp_partition_iterator_t pi; // Iterator for find @@ -3515,7 +3557,7 @@ void setup() { lastTimeActiveTimestamp = millis(); // initial set after boot - if (wifiManager() == WL_CONNECTED) { + /*if (wifiManager() == WL_CONNECTED) { // attach AsyncWebSocket for Mgmt-Interface ws.onEvent(onWebsocketEvent); wServer.addHandler(&ws); @@ -3539,7 +3581,7 @@ void setup() { wServer.onNotFound(notFound); wServer.begin(); - } + }*/ bootComplete = true; Serial.print(F("Free heap: ")); @@ -3548,6 +3590,7 @@ void setup() { void loop() { + webserverStart(); #ifdef HEADPHONE_ADJUST_ENABLE headphoneVolumeManager(); #endif