From ae8aac8e0fd38b6afc87deeb6812d40e4966644a Mon Sep 17 00:00:00 2001 From: Torsten Stauder Date: Mon, 21 Dec 2020 22:00:25 +0100 Subject: [PATCH 1/3] MQTT: added rfid-state and moved topicTrackCmnd to topicRfidCmnd --- README.md | 6 +++--- openHAB/items/Home.items | 2 +- openHAB/sitemaps/Home.sitemap | 3 ++- openHAB/things/mqttConntections.things | 2 +- src/logmessages.h | 2 +- src/main.cpp | 13 ++++++------- src/settings.h | 3 ++- 7 files changed, 16 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index ff6becb..49c90f7 100644 --- a/README.md +++ b/README.md @@ -390,9 +390,9 @@ Feel free to use your own smarthome-environments (instead of openHAB). The MQTT- | ----------------------- | --------------- | ------------------------------------------------------------------------------ | | topicSleepCmnd | 0 or OFF | Power off Tonuino immediately | | topicSleepState | ON or OFF | Sends Tonuino's current/last state | -| topicTrackCmnd | 12 digits | Set number of RFID-tag which 'emulates' an RFID-tag (e.g. `123789456089`) | -| topicTrackState | String | Sends current track number, total number of tracks and full path of current | -| | | track. E.g. "(2/10) /mp3/kinderlieder/Ri ra rutsch.mp3" +| topicRfidCmnd | 12 digits | Set number of RFID-tag which 'emulates' an RFID-tag (e.g. `123789456089`) | +| topicRfidState | 12 digits | ID of current RFID-tag (if not a modification-card) | +| topicTrackState | String | Sends current track number, total number of tracks and full path of curren track. E.g. "(2/10) /mp3/kinderlieder/Ri ra rutsch.mp3" | | topicTrackControlCmnd | 1 -> 7 | `1`=stop; `2`=unused!; `3`=play/pause; `4`=next; `5`=prev; `6`=first; `7`=last | | topicLoudnessCmnd | 0 -> 21 | Set loudness (depends on minVolume / maxVolume) | | topicLoudnessState | 0 -> 21 | Sends loudness (depends on minVolume / maxVolume | diff --git a/openHAB/items/Home.items b/openHAB/items/Home.items index 35484a2..c3d5fde 100644 --- a/openHAB/items/Home.items +++ b/openHAB/items/Home.items @@ -7,7 +7,7 @@ Group ZimmerTim "Kinderzimmer Tim" // Zimmer Kind Switch Tonuino_Sleep { channel="mqtt:topic:MQTT:tonuino:tonuino_sleep_mqtt", autoupdate="true" } -String Tonuino_Track { channel="mqtt:topic:MQTT:tonuino:tonuino_track_mqtt", autoupdate="true" } +String Tonuino_Rfid { channel="mqtt:topic:MQTT:tonuino:rfid", autoupdate="true" } Number Tonuino_Loudness "Lautstärke [%d]" { channel="mqtt:topic:MQTT:tonuino:tonuino_loudness_mqtt", autoupdate="true" } String Tonuino_SleepTimer { channel="mqtt:topic:MQTT:tonuino:tonuino_sleeptimer_mqtt", autoupdate="true" } String Tonuino_Status { channel="mqtt:topic:MQTT:tonuino:tonuino_status_mqtt", autoupdate="true", expire="2m, command=Offline" } diff --git a/openHAB/sitemaps/Home.sitemap b/openHAB/sitemaps/Home.sitemap index b261476..0735c71 100644 --- a/openHAB/sitemaps/Home.sitemap +++ b/openHAB/sitemaps/Home.sitemap @@ -6,7 +6,7 @@ sitemap Home label="Home" { Text item=Tonuino_Status label="Status" valuecolor=[Tonuino_Status==Online="green", ==Offline="red"] icon="network" Switch item=Tonuino_Sleep label="Ausschalten" icon="wallswitch" mappings=[OFF="AUS"] visibility=[Tonuino_Status=="Online"] Switch item=Tonuino_LockControls label="Sperren" icon="wallswitch" mappings=[OFF="AUS", ON="AN"] visibility=[Tonuino_Status=="Online"] - Selection item=Tonuino_Track label="Titelauswahl" icon="text" visibility=[Tonuino_Status=="Online"] mappings=[ + Selection item=Tonuino_Rfid label="Titelauswahl" icon="text" visibility=[Tonuino_Status=="Online"] mappings=[ "244189084042"="Yakari und die Pferdediebe", "244042007042"="Yakari - Der Gesang des Raben", "004134024043"="Yakari - Schneeball in Gefahr", @@ -29,6 +29,7 @@ sitemap Home label="Home" { Setpoint item=Tonuino_LedBrightness icon="lightbulb" label="LED-Helligkeit" visibility=[Tonuino_Status=="Online"] maxValue=64 step=2 Text item=Tonuino_IPv4 label="IP-Adresse" icon="network" visibility=[Tonuino_Status=="Online"] Text item=Tonuino_CurrentTrack label="Aktuell:" icon="mediacontrol" visibility=[Tonuino_Status=="Online"] + Text item=Tonuino_Voltage label="Akkuspannung:" icon="batterylevel" visibility=[Tonuino_Status=="Online"] } } } diff --git a/openHAB/things/mqttConntections.things b/openHAB/things/mqttConntections.things index 57512b4..0b7a3c1 100644 --- a/openHAB/things/mqttConntections.things +++ b/openHAB/things/mqttConntections.things @@ -3,7 +3,7 @@ Bridge mqtt:broker:MQTT [ host="", secure=false, username="= 100) { doRfidCardModifications(_playMode); } else { + #ifdef MQTT_ENABLE + publishMqtt((char *) FPSTR(topicRfidState), currentRfidTagId, false); + #endif trackQueueDispatcher(_file, _lastPlayPos, _playMode, _trackLastPlayed); } } @@ -4131,10 +4134,6 @@ void audio_showstation(const char *info) { publishMqtt((char *) FPSTR(topicTrackState), buf, false); #endif } -void audio_showstreaminfo(const char *info) { - snprintf(logBuf, serialLoglength, "streaminfo : %s", info); - loggerNl(logBuf, LOGLEVEL_INFO); -} void audio_showstreamtitle(const char *info) { snprintf(logBuf, serialLoglength, "streamtitle : %s", info); loggerNl(logBuf, LOGLEVEL_INFO); diff --git a/src/settings.h b/src/settings.h index 3cf6377..f0ea49f 100644 --- a/src/settings.h +++ b/src/settings.h @@ -152,7 +152,8 @@ float voltageIndicatorHigh = 4.2; // Upper range for Neopixel- #define DEVICE_HOSTNAME "ESP32-Tonuino" // Name that that is used for MQTT static const char topicSleepCmnd[] PROGMEM = "Cmnd/Tonuino/Sleep"; static const char topicSleepState[] PROGMEM = "State/Tonuino/Sleep"; - static const char topicTrackCmnd[] PROGMEM = "Cmnd/Tonuino/Track"; + static const char topicRfidCmnd[] PROGMEM = "Cmnd/Tonuino/Rfid"; + static const char topicRfidState[] PROGMEM = "State/Tonuino/Rfid"; static const char topicTrackState[] PROGMEM = "State/Tonuino/Track"; static const char topicTrackControlCmnd[] PROGMEM = "Cmnd/Tonuino/TrackControl"; static const char topicLoudnessCmnd[] PROGMEM = "Cmnd/Tonuino/Loudness"; From fb9d4584a3bd2f5f1234a512624a0dca90384c76 Mon Sep 17 00:00:00 2001 From: grch101 Date: Mon, 21 Dec 2020 13:56:50 +0100 Subject: [PATCH 2/3] platformio.ini: Add common variables as dynamic variables to all board configs. main.cpp: -Bug Fix: Handle return code of connecttofs routine. If it is not successful the track is considered as finished. This avoids freezing the tonuino. -Feature: Voltage Measurement averages over multiple analog reads, which is smoothing the voltage value -Feature: When pressing the previous button during audio play and the current track is already played for more than 2s, the current track is started again instead of the previous track --- platformio.ini | 13 ++++++++++++- src/main.cpp | 29 +++++++++++++++++++++++++---- 2 files changed, 37 insertions(+), 5 deletions(-) diff --git a/platformio.ini b/platformio.ini index 783e580..dbd8cd1 100644 --- a/platformio.ini +++ b/platformio.ini @@ -14,6 +14,8 @@ board = lolin32 framework = arduino monitor_speed = 115200 board_build.partitions = no_ota.csv +lib_deps = ${env:common.lib_deps} +extra_scripts = ${env:common.extra_scripts} upload_port = /dev/cu.SLAB_USBtoUART monitor_port = /dev/cu.SLAB_USBtoUART @@ -23,6 +25,8 @@ board = lolin_d32 framework = arduino monitor_speed = 115200 board_build.partitions = no_ota.csv +lib_deps = ${env:common.lib_deps} +extra_scripts = ${env:common.extra_scripts} upload_port = /dev/cu.SLAB_USBtoUART monitor_port = /dev/cu.SLAB_USBtoUART @@ -32,6 +36,8 @@ board = lolin_d32_pro framework = arduino monitor_speed = 115200 board_build.partitions = no_ota.csv +lib_deps = ${env:common.lib_deps} +extra_scripts = ${env:common.extra_scripts} upload_port = /dev/cu.SLAB_USBtoUART monitor_port = /dev/cu.SLAB_USBtoUART @@ -41,6 +47,8 @@ board = nodemcu-32s framework = arduino monitor_speed = 115200 board_build.partitions = no_ota.csv +lib_deps = ${env:common.lib_deps} +extra_scripts = ${env:common.extra_scripts} upload_port = /dev/cu.SLAB_USBtoUART monitor_port = /dev/cu.SLAB_USBtoUART @@ -50,6 +58,8 @@ board = az-delivery-devkit-v4 framework = arduino monitor_speed = 115200 board_build.partitions = no_ota.csv +lib_deps = ${env:common.lib_deps} +extra_scripts = ${env:common.extra_scripts} upload_port = /dev/cu.SLAB_USBtoUART monitor_port = /dev/cu.SLAB_USBtoUART @@ -59,12 +69,13 @@ monitor_port = /dev/cu.SLAB_USBtoUART ;WINDOWS: COM3 ;LINUX: /dev/ttyUSB0 (if it's the first USB-device attached, otherwise increase number) +[env:common] lib_deps = https://github.com/schreibfaul1/ESP32-audioI2S.git https://github.com/madhephaestus/ESP32Encoder.git https://github.com/knolleary/pubsubclient.git https://github.com/biologist79/ESP32FTPServer.git - https://github.com/FastLED/FastLED.git#3.3.3 + https://github.com/FastLED/FastLED.git https://github.com/biologist79/rfid.git https://github.com/tueddy/PN5180-Library.git ESP Async WebServer diff --git a/src/main.cpp b/src/main.cpp index c4d828d..d0cef08 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -590,10 +590,17 @@ void fileHandlingTask(void *arguments) { } // Measures voltage of a battery as per interval or after bootup (after allowing a few seconds to settle down) +// The average of several analog reads will be taken to reduce the noise (Note: One analog read takes ~10µs) #ifdef MEASURE_BATTERY_VOLTAGE float measureBatteryVoltage(void) { float factor = 1 / ((float) rdiv2/(rdiv2+rdiv1)); - return ((float) analogRead(VOLTAGE_READ_PIN) / maxAnalogValue) * refVoltage * factor; + float averagedAnalogValue = 0; + int i; + for(i=0; i<=19; i++){ + averagedAnalogValue += (float)analogRead(VOLTAGE_READ_PIN); + } + averagedAnalogValue /= 20.0; + return (averagedAnalogValue / maxAnalogValue) * refVoltage * factor; } void batteryVoltageTester(void) { @@ -1376,6 +1383,7 @@ void playAudio(void *parameter) { uint8_t currentVolume; static BaseType_t trackQStatus; static uint8_t trackCommand = 0; + bool audioReturnCode; for (;;) { if (xQueueReceive(volumeQueue, ¤tVolume, 0) == pdPASS ) { @@ -1518,7 +1526,10 @@ void playAudio(void *parameter) { #endif } if (playProperties.currentTrackNumber > 0) { - playProperties.currentTrackNumber--; + // play previous track when current track time is small, else play current track again + if(audio.getAudioCurrentTime() < 2) { + playProperties.currentTrackNumber--; + } if (playProperties.saveLastPlayPosition) { nvsRfidWriteWrapper(playProperties.playRfidTag, *(playProperties.playlist + playProperties.currentTrackNumber), 0, playProperties.playMode, playProperties.currentTrackNumber, playProperties.numberOfTracks); loggerNl((char *) FPSTR(trackStartAudiobook), LOGLEVEL_INFO); @@ -1544,7 +1555,12 @@ void playAudio(void *parameter) { #ifdef NEOPIXEL_ENABLE showRewind = true; #endif - audio.connecttoFS(FSystem, *(playProperties.playlist + playProperties.currentTrackNumber)); + audioReturnCode = audio.connecttoFS(FSystem, *(playProperties.playlist + playProperties.currentTrackNumber)); + // consider track as finished, when audio lib call was not successful + if(!audioReturnCode) { + playProperties.trackFinished = true; + continue; + } loggerNl((char *) FPSTR(trackStart), LOGLEVEL_INFO); trackCommand = 0; continue; @@ -1677,7 +1693,12 @@ void playAudio(void *parameter) { playProperties.trackFinished = true; continue; } else { - audio.connecttoFS(FSystem, *(playProperties.playlist + playProperties.currentTrackNumber)); + audioReturnCode = audio.connecttoFS(FSystem, *(playProperties.playlist + playProperties.currentTrackNumber)); + // consider track as finished, when audio lib call was not successful + if(!audioReturnCode) { + playProperties.trackFinished = true; + continue; + } #ifdef NEOPIXEL_ENABLE showPlaylistProgress = true; #endif From 45c603f71cc09d21753888f37fcb47938063a711 Mon Sep 17 00:00:00 2001 From: grch101 Date: Mon, 21 Dec 2020 17:30:55 +0100 Subject: [PATCH 3/3] Delete not required files. Update FastLed Lib back to 3.3.3. Led Error is shown when audio lib is returning an error --- platformio.ini | 6 +++--- src/main.cpp | 8 +++++++- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/platformio.ini b/platformio.ini index dbd8cd1..0f117ae 100644 --- a/platformio.ini +++ b/platformio.ini @@ -38,8 +38,8 @@ monitor_speed = 115200 board_build.partitions = no_ota.csv lib_deps = ${env:common.lib_deps} extra_scripts = ${env:common.extra_scripts} -upload_port = /dev/cu.SLAB_USBtoUART -monitor_port = /dev/cu.SLAB_USBtoUART +upload_port = COM6 +monitor_port = COM6 [env:nodemcu-32s] platform = espressif32 @@ -75,7 +75,7 @@ lib_deps = https://github.com/madhephaestus/ESP32Encoder.git https://github.com/knolleary/pubsubclient.git https://github.com/biologist79/ESP32FTPServer.git - https://github.com/FastLED/FastLED.git + https://github.com/FastLED/FastLED.git#3.3.3 https://github.com/biologist79/rfid.git https://github.com/tueddy/PN5180-Library.git ESP Async WebServer diff --git a/src/main.cpp b/src/main.cpp index d0cef08..3911f2e 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1557,7 +1557,10 @@ void playAudio(void *parameter) { #endif audioReturnCode = audio.connecttoFS(FSystem, *(playProperties.playlist + playProperties.currentTrackNumber)); // consider track as finished, when audio lib call was not successful - if(!audioReturnCode) { + if (!audioReturnCode) { + #ifdef NEOPIXEL_ENABLE + showLedError = true; + #endif playProperties.trackFinished = true; continue; } @@ -1696,6 +1699,9 @@ void playAudio(void *parameter) { audioReturnCode = audio.connecttoFS(FSystem, *(playProperties.playlist + playProperties.currentTrackNumber)); // consider track as finished, when audio lib call was not successful if(!audioReturnCode) { + #ifdef NEOPIXEL_ENABLE + showLedError = true; + #endif playProperties.trackFinished = true; continue; }