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:
89
build/sketch/MQTT.hpp
Normal file
89
build/sketch/MQTT.hpp
Normal file
@@ -0,0 +1,89 @@
|
||||
#line 1 "C:\\Users\\espi_\\Documents\\Arduino\\vesper\\MQTT.hpp"
|
||||
#pragma once
|
||||
|
||||
const IPAddress MQTT_HOST(10,98,20,10);
|
||||
const int MQTT_PORT = 1883;
|
||||
|
||||
AsyncMqttClient mqttClient;
|
||||
|
||||
extern bool forceStop;
|
||||
extern volatile bool playing;
|
||||
|
||||
String GetPayloadContent(char * data, size_t len) {
|
||||
String content = "";
|
||||
for(size_t i = 0; i < len; i++)
|
||||
{
|
||||
content.concat(data[i]);
|
||||
}
|
||||
return content;
|
||||
}
|
||||
|
||||
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(topic_eBrake.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 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 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){
|
||||
if (len == 1 && payload[0] == '0') { // Checking for raw '0' byte
|
||||
forceStop = true;
|
||||
Serial.println("STOPPING Playback!");
|
||||
}
|
||||
else if (len == 1 && payload[0] == '1'){
|
||||
playing = true;
|
||||
}
|
||||
}
|
||||
|
||||
else if (String(topic) == topicSetMelody) {
|
||||
// Handle the playback command
|
||||
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.");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user