Now, controlled via MQTT a Playback of a melody can Start, Stop, Pause etc. Settings like speed, total duration, pauses etc can be set.
82 lines
2.4 KiB
C++
82 lines
2.4 KiB
C++
#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);
|
|
}
|