A new Melody Struct was added containing the melody's main attributes. The MQTT Topic Subscriptions were updated to a more proper format. Also added DEV_ID that corresponds to the device's ID or Serial Number. Chnaged the Deserialization function to actually adjust the MelodyStruct's attributes. Added basic play/stop functionality.
65 lines
1.8 KiB
C++
65 lines
1.8 KiB
C++
#pragma once
|
|
|
|
const IPAddress MQTT_HOST(10,98,20,10);
|
|
const int MQTT_PORT = 1883;
|
|
|
|
AsyncMqttClient mqttClient;
|
|
|
|
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 topicAddMelody = String("vesper/") + DEV_ID + "/control/add_melody";
|
|
|
|
uint16_t control_id = mqttClient.subscribe(topicPlayback.c_str(), 2);
|
|
Serial.print("Subscribing to playback topic, QoS 2, packetId: ");
|
|
Serial.println(control_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) {
|
|
|
|
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 topicAddMelody = String("vesper/") + DEV_ID + "/control/add_melody"; // doesn't work yet.
|
|
|
|
String payloadContent = String(payload).substring(0, len);
|
|
|
|
if (String(topic) == topicPlayback) {
|
|
// 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.");
|
|
}
|
|
}
|