From 212f7ab1025a13fc46af9e6761627a4654e0da27 Mon Sep 17 00:00:00 2001 From: e2002 Date: Tue, 28 Mar 2023 10:47:39 +0300 Subject: [PATCH] v0.9.141 --- README.md | 4 + yoRadio/src/core/audiohandlers.h | 134 ++++++++++++++++++++++++++ yoRadio/src/core/controls.cpp | 2 + yoRadio/src/core/network.cpp | 26 ++++- yoRadio/src/core/network.h | 4 + yoRadio/src/core/options.h | 2 +- yoRadio/src/core/optionschecker.h | 5 +- yoRadio/yoRadio.ino | 154 +----------------------------- 8 files changed, 173 insertions(+), 158 deletions(-) create mode 100644 yoRadio/src/core/audiohandlers.h diff --git a/README.md b/README.md index 3937d8e..b03c765 100644 --- a/README.md +++ b/README.md @@ -226,6 +226,10 @@ Work is in progress... --- ## Version history +#### v0.9.141 +- fixed error reconnecting to WiFi when connection is lost +- ADDED a compilation error when choosing a board other than "ESP32 Dev Module" or "ESP32 Wrover Module" + #### v0.9.130 - fixed crash in configurations with NOKIA5110 displays - fixed bug with displaying buffer indicator when switching audioinfo diff --git a/yoRadio/src/core/audiohandlers.h b/yoRadio/src/core/audiohandlers.h new file mode 100644 index 0000000..4fa1fc6 --- /dev/null +++ b/yoRadio/src/core/audiohandlers.h @@ -0,0 +1,134 @@ +#ifndef AUDIOHANDLERS_H +#define AUDIOHANDLERS_H + +//=============================================// +// Audio handlers // +//=============================================// + +void audio_info(const char *info) { + if(player.lockOutput) return; + if(config.store.audioinfo) telnet.printf("##AUDIO.INFO#: %s\n", info); + #ifdef USE_NEXTION + nextion.audioinfo(info); + #endif + if (strstr(info, "skip metadata") != NULL) config.setTitle(config.station.name); + if (strstr(info, "Account already in use") != NULL || strstr(info, "HTTP/1.0 401") != NULL) { + player.setError(info); + + } + char* ici; char b[20]={0}; + if ((ici = strstr(info, "BitRate: ")) != NULL) { + strlcpy(b, ici + 9, 50); + audio_bitrate(b); + } +} + +void audio_bitrate(const char *info) +{ + if(config.store.audioinfo) telnet.printf("%s %s\n", "##AUDIO.BITRATE#:", info); + config.station.bitrate = atoi(info) / 1000; + display.putRequest(DBITRATE); + #ifdef USE_NEXTION + nextion.bitrate(config.station.bitrate); + #endif + netserver.requestOnChange(BITRATE, 0); +} + +bool printable(const char *info) { + bool p = true; + for (int c = 0; c < strlen(info); c++) + { + if ((uint8_t)info[c] > 0x7e || (uint8_t)info[c] < 0x20) p = false; + } + if (!p) p = (uint8_t)info[0] >= 0xC2 && (uint8_t)info[1] >= 0x80 && (uint8_t)info[1] <= 0xBF; + return p; +} + +void audio_showstation(const char *info) { + bool p = printable(info) && (strlen(info) > 0);(void)p; + //config.setTitle(p?info:config.station.name); + if(player.remoteStationName){ + config.setStation(p?info:config.station.name); + display.putRequest(NEWSTATION); + netserver.requestOnChange(STATION, 0); + } +} + +void audio_showstreamtitle(const char *info) { + DBGH(); + if (strstr(info, "Account already in use") != NULL || strstr(info, "HTTP/1.0 401") != NULL) player.setError(info); + bool p = printable(info) && (strlen(info) > 0); + #ifdef DEBUG_TITLES + config.setTitle(DEBUG_TITLES); + #else + config.setTitle(p?info:config.station.name); + #endif +} + +void audio_error(const char *info) { + //config.setTitle(info); + player.setError(info); + telnet.printf("##ERROR#:\t%s\n", info); +} + +void audio_id3artist(const char *info){ + if(printable(info)) config.setStation(info); + display.putRequest(NEWSTATION); + netserver.requestOnChange(STATION, 0); +} + +void audio_id3album(const char *info){ + if(player.lockOutput) return; + if(printable(info)){ + if(strlen(config.station.title)==0){ + config.setTitle(info); + }else{ + char out[BUFLEN]= {0}; + strlcat(out, config.station.title, BUFLEN); + strlcat(out, " - ", BUFLEN); + strlcat(out, info, BUFLEN); + config.setTitle(out); + } + } +} + +void audio_id3title(const char *info){ + audio_id3album(info); +} + +void audio_beginSDread(){ + config.setTitle(""); +} + +void audio_id3data(const char *info){ //id3 metadata + if(player.lockOutput) return; + telnet.printf("##AUDIO.ID3#: %s\n", info); +} + +void audio_eof_mp3(const char *info){ //end of file + config.sdResumePos = 0; + if(config.sdSnuffle){ + player.sendCommand({PR_PLAY, random(1, config.store.countStation)}); + }else{ + player.next(); + } +} + +void audio_eof_stream(const char *info){ + player.sendCommand({PR_STOP, 0}); + if(!player.resumeAfterUrl) return; + if (config.store.play_mode==PM_WEB){ + player.sendCommand({PR_PLAY, config.store.lastStation}); + }else{ + player.setResumeFilePos( config.sdResumePos==0?0:config.sdResumePos-player.sd_min); + player.sendCommand({PR_PLAY, config.store.lastStation}); + } +} + +void audio_progress(uint32_t startpos, uint32_t endpos){ + player.sd_min = startpos; + player.sd_max = endpos; + netserver.requestOnChange(SDLEN, 0); +} + +#endif diff --git a/yoRadio/src/core/controls.cpp b/yoRadio/src/core/controls.cpp index 17e5e37..075c67e 100644 --- a/yoRadio/src/core/controls.cpp +++ b/yoRadio/src/core/controls.cpp @@ -67,12 +67,14 @@ decode_results irResults; #if ENC_BTNL!=255 void IRAM_ATTR readEncoderISR() { + if(display.mode()==LOST || display.mode()==UPDATING) return; encoder.readEncoder_ISR(); } #endif #if ENC2_BTNL!=255 void IRAM_ATTR readEncoder2ISR() { + if(display.mode()==LOST || display.mode()==UPDATING) return; encoder2.readEncoder_ISR(); } #endif diff --git a/yoRadio/src/core/network.cpp b/yoRadio/src/core/network.cpp index 3098371..ff43f6f 100644 --- a/yoRadio/src/core/network.cpp +++ b/yoRadio/src/core/network.cpp @@ -1,11 +1,11 @@ #include "network.h" -#include "WiFi.h" #include "display.h" #include "options.h" #include "config.h" #include "telnet.h" #include "netserver.h" #include "player.h" +#include "mqtt.h" Network network; @@ -50,6 +50,27 @@ void ticks() { } } +void Network::WiFiReconnected(WiFiEvent_t event, WiFiEventInfo_t info){ + network.beginReconnect = false; + delay(100); + display.putRequest(NEWMODE, PLAYER); + if (network.lostPlaying) player.sendCommand({PR_PLAY, config.store.lastStation}); + #ifdef MQTT_ROOT_TOPIC + connectToMqtt(); + #endif +} + +void Network::WiFiLostConnection(WiFiEvent_t event, WiFiEventInfo_t info){ + if(!network.beginReconnect){ + Serial.println("Lost connection, reconnecting..."); + network.lostPlaying = player.isRunning(); + if (network.lostPlaying) { player.sendCommand({PR_STOP, 0}); } + display.putRequest(NEWMODE, LOST); + } + network.beginReconnect = true; + WiFi.begin(config.ssids[config.store.lastSSID].ssid, config.ssids[config.store.lastSSID].password); +} + #define DBGAP false void Network::begin() { @@ -98,7 +119,8 @@ void Network::begin() { if(LED_BUILTIN!=255) digitalWrite(LED_BUILTIN, LOW); status = CONNECTED; WiFi.setSleep(false); - + WiFi.onEvent(WiFiReconnected, WiFiEvent_t::ARDUINO_EVENT_WIFI_STA_CONNECTED); + WiFi.onEvent(WiFiLostConnection, WiFiEvent_t::ARDUINO_EVENT_WIFI_STA_DISCONNECTED); weatherBuf=NULL; trueWeather = false; #if (DSP_MODEL!=DSP_DUMMY || defined(USE_NEXTION)) && !defined(HIDE_WEATHER) diff --git a/yoRadio/src/core/network.h b/yoRadio/src/core/network.h index 2d70d72..7692c34 100644 --- a/yoRadio/src/core/network.h +++ b/yoRadio/src/core/network.h @@ -2,6 +2,7 @@ #define network_h #include #include "time.h" +#include "WiFi.h" #define apSsid "yoRadioAP" #define apPassword "12345987" @@ -16,6 +17,7 @@ class Network { n_Status_e status; struct tm timeinfo; bool firstRun, forceTimeSync, forceWeather; + bool lostPlaying = false, beginReconnect = false; //uint8_t tsFailCnt, wsFailCnt; public: Network() {}; @@ -28,6 +30,8 @@ class Network { private: Ticker rtimer; void raiseSoftAP(); + static void WiFiLostConnection(WiFiEvent_t event, WiFiEventInfo_t info); + static void WiFiReconnected(WiFiEvent_t event, WiFiEventInfo_t info); }; extern Network network; diff --git a/yoRadio/src/core/options.h b/yoRadio/src/core/options.h index d5c22f2..2484ec4 100644 --- a/yoRadio/src/core/options.h +++ b/yoRadio/src/core/options.h @@ -1,7 +1,7 @@ #ifndef options_h #define options_h -#define YOVERSION "0.9.130" +#define YOVERSION "0.9.141" /******************************************************* DO NOT EDIT THIS FILE. diff --git a/yoRadio/src/core/optionschecker.h b/yoRadio/src/core/optionschecker.h index 82086cc..23e7562 100644 --- a/yoRadio/src/core/optionschecker.h +++ b/yoRadio/src/core/optionschecker.h @@ -13,8 +13,9 @@ # error YOU MUST CHOOSE BETWEEN I2S DAC AND VS1053 BY DISABLING THE SECOND MODULE IN THE myoptions.h #endif - - +#ifndef ARDUINO_ESP32_DEV +# error ONLY MODULES "ESP32 Dev Module" AND "ESP32 Wrover Module" ARE SUPPORTED. PLEASE SELECT ONE OF THEM IN THE MENU >> TOOLS >> BOARD +#endif #endif diff --git a/yoRadio/yoRadio.ino b/yoRadio/yoRadio.ino index dbca3a3..e9f8934 100644 --- a/yoRadio/yoRadio.ino +++ b/yoRadio/yoRadio.ino @@ -61,160 +61,8 @@ void loop() { if (network.status == CONNECTED) { player.loop(); loopControls(); - checkConnection(); } netserver.loop(); } -void checkConnection(){ - static uint32_t checkMillis = 0; - static uint32_t checkInterval = 3000; - if ((WiFi.status() != WL_CONNECTED) && (millis() - checkMillis >=checkInterval)) { - bool playing = player.isRunning(); - if (playing) { player.sendCommand({PR_STOP, 0}); player.loop(); } - display.putRequest(NEWMODE, LOST); - Serial.println("Lost connection, reconnecting..."); - while(true){ - if (WiFi.status() == WL_CONNECTED) break; - WiFi.disconnect(); - WiFi.reconnect(); - delay(3000); - } - display.putRequest(NEWMODE, PLAYER); - if (playing) player.sendCommand({PR_PLAY, config.store.lastStation}); - checkMillis = millis(); - #ifdef MQTT_ROOT_TOPIC - connectToMqtt(); - #endif - } -} - -//=============================================// -// Audio handlers // -//=============================================// - -void audio_info(const char *info) { - if(player.lockOutput) return; - if(config.store.audioinfo) telnet.printf("##AUDIO.INFO#: %s\n", info); - #ifdef USE_NEXTION - nextion.audioinfo(info); - #endif - if (strstr(info, "skip metadata") != NULL) config.setTitle(config.station.name); - if (strstr(info, "Account already in use") != NULL || strstr(info, "HTTP/1.0 401") != NULL) { - player.setError(info); - - } - char* ici; char b[20]={0}; - if ((ici = strstr(info, "BitRate: ")) != NULL) { - strlcpy(b, ici + 9, 50); - audio_bitrate(b); - } -} - -void audio_bitrate(const char *info) -{ - if(config.store.audioinfo) telnet.printf("%s %s\n", "##AUDIO.BITRATE#:", info); - config.station.bitrate = atoi(info) / 1000; - display.putRequest(DBITRATE); - #ifdef USE_NEXTION - nextion.bitrate(config.station.bitrate); - #endif - netserver.requestOnChange(BITRATE, 0); -} - -bool printable(const char *info) { - bool p = true; - for (int c = 0; c < strlen(info); c++) - { - if ((uint8_t)info[c] > 0x7e || (uint8_t)info[c] < 0x20) p = false; - } - if (!p) p = (uint8_t)info[0] >= 0xC2 && (uint8_t)info[1] >= 0x80 && (uint8_t)info[1] <= 0xBF; - return p; -} - -void audio_showstation(const char *info) { - bool p = printable(info) && (strlen(info) > 0);(void)p; - //config.setTitle(p?info:config.station.name); - if(player.remoteStationName){ - config.setStation(p?info:config.station.name); - display.putRequest(NEWSTATION); - netserver.requestOnChange(STATION, 0); - } -} - -void audio_showstreamtitle(const char *info) { - DBGH(); - if (strstr(info, "Account already in use") != NULL || strstr(info, "HTTP/1.0 401") != NULL) player.setError(info); - bool p = printable(info) && (strlen(info) > 0); - #ifdef DEBUG_TITLES - config.setTitle(DEBUG_TITLES); - #else - config.setTitle(p?info:config.station.name); - #endif -} - -void audio_error(const char *info) { - //config.setTitle(info); - player.setError(info); - telnet.printf("##ERROR#:\t%s\n", info); -} - -void audio_id3artist(const char *info){ - if(printable(info)) config.setStation(info); - display.putRequest(NEWSTATION); - netserver.requestOnChange(STATION, 0); -} - -void audio_id3album(const char *info){ - if(player.lockOutput) return; - if(printable(info)){ - if(strlen(config.station.title)==0){ - config.setTitle(info); - }else{ - char out[BUFLEN]= {0}; - strlcat(out, config.station.title, BUFLEN); - strlcat(out, " - ", BUFLEN); - strlcat(out, info, BUFLEN); - config.setTitle(out); - } - } -} - -void audio_id3title(const char *info){ - audio_id3album(info); -} - -void audio_beginSDread(){ - config.setTitle(""); -} - -void audio_id3data(const char *info){ //id3 metadata - if(player.lockOutput) return; - telnet.printf("##AUDIO.ID3#: %s\n", info); -} - -void audio_eof_mp3(const char *info){ //end of file - config.sdResumePos = 0; - if(config.sdSnuffle){ - player.sendCommand({PR_PLAY, random(1, config.store.countStation)}); - }else{ - player.next(); - } -} - -void audio_eof_stream(const char *info){ - player.sendCommand({PR_STOP, 0}); - if(!player.resumeAfterUrl) return; - if (config.store.play_mode==PM_WEB){ - player.sendCommand({PR_PLAY, config.store.lastStation}); - }else{ - player.setResumeFilePos( config.sdResumePos==0?0:config.sdResumePos-player.sd_min); - player.sendCommand({PR_PLAY, config.store.lastStation}); - } -} - -void audio_progress(uint32_t startpos, uint32_t endpos){ - player.sd_min = startpos; - player.sd_max = endpos; - netserver.requestOnChange(SDLEN, 0); -} +#include "src/core/audiohandlers.h"