Added initial Melody Playback Ability

Now, controlled via MQTT a Playback of a melody can Start, Stop, Pause 
etc. 

Settings like speed, total duration, pauses etc can be set.
This commit is contained in:
2025-01-19 21:15:07 +02:00
parent 2cbbc8d591
commit a33f626dde
36 changed files with 4263 additions and 236 deletions

81
MQTT_Message_Handling.hpp Normal file
View File

@@ -0,0 +1,81 @@
#pragma once
void handlePlaybackCommands(char * command);
void handleJSON(char * payload) {
JsonDocument doc;
DeserializationError error = deserializeJson(doc, payload);
if (error) {
Serial.print("deserializeJson() failed: ");
Serial.println(error.c_str());
return;
}
setMelodyAttributes(doc);
loadMelodyInRAM(melody_steps);
}
void SuscribeMqtt() {
String topicPlayback = String("vesper/") + DEV_ID + "/control/playback";
String topicSetMelody = String("vesper/") + DEV_ID + "/control/setMelody";
String topicAddMelody = String("vesper/") + DEV_ID + "/control/add_melody";
uint16_t control_id = mqttClient.subscribe(topicPlayback.c_str(), 2);
Serial.print("Subscribing to Playback Control topic, QoS 2, packetId: ");
Serial.println(control_id);
uint16_t set_melody_id = mqttClient.subscribe(topicSetMelody.c_str(), 2);
Serial.print("Subscribing to Set-Melody topic, QoS 2, packetId: ");
Serial.println(set_melody_id);
// doesn't work yet:
uint16_t add_melody_id = mqttClient.subscribe(topicAddMelody.c_str(), 2);
Serial.print("Subscribing to Add-Melody topic, QoS 2, packetId: ");
Serial.println(add_melody_id);
}
void OnMqttReceived(char * topic, char * payload, AsyncMqttClientMessageProperties properties, size_t len, size_t index, size_t total) {
String topicPlayback = String("vesper/") + DEV_ID + "/control/playback";
String topicSetMelody = String("vesper/") + DEV_ID + "/control/setMelody";
String topicAddMelody = String("vesper/") + DEV_ID + "/control/add_melody";
//String payloadContent = String(payload).substring(0, len);
if (String(topic) == topicPlayback){
handlePlaybackCommands(payload);
}
else if (String(topic) == topicSetMelody) {
handleJSON(payload);
}
else if (String(topic) == topicAddMelody) {
// Handle adding melody
Serial.println("Adding melody...");
// You can call a function here to handle adding the melody
}
else {
// Handle unknown topics
Serial.println("Unknown topic received.");
}
}
// void PublishMqtt(unsigned long data) {
//
// //Doesn't publish anything yet.
// String topicData = String("vesper/") + DEV_ID + "/data";
// String payload = String(data);
// mqttClient.publish(topicData.c_str(), 0, true, (char*)payload.c_str());
//
// }
void PublishMqtt(const char *data) {
String topicData = String("vesper/") + DEV_ID + "/data";
mqttClient.publish(topicData.c_str(), 0, true, data);
}