Added Melody Struct & Ajusted MQTT Subs and Deserialization

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.
This commit is contained in:
2025-01-12 19:46:24 +02:00
parent 30e708f048
commit 540d8a14fe
9 changed files with 149 additions and 56 deletions

View File

@@ -1,12 +1,11 @@
#pragma once
const IPAddress MQTT_HOST(10, 98, 20, 10);
const IPAddress MQTT_HOST(10,98,20,10);
const int MQTT_PORT = 1883;
AsyncMqttClient mqttClient;
String GetPayloadContent(char* data, size_t len)
{
String GetPayloadContent(char* data, size_t len) {
String content = "";
for(size_t i = 0; i < len; i++)
{
@@ -15,28 +14,51 @@ String GetPayloadContent(char* data, size_t len)
return content;
}
void SuscribeMqtt()
{
uint16_t packetIdSub = mqttClient.subscribe("user123456/channel", 0);
Serial.print("Subscribing at QoS 2, packetId: ");
Serial.println(packetIdSub);
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);
}
String payload;
void PublishMqtt(unsigned long data)
{
void PublishMqtt(unsigned long data) {
String topicData = String("vesper/") + DEV_ID + "/data";
String payload = String(data);
mqttClient.publish("hello/world", 0, true, (char*)payload.c_str());
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)
{
//Serial.print("Received on ");
//Serial.print(topic);
//Serial.print(": ");
String content = GetPayloadContent(payload, len);
reconstr(content);
//Serial.print(content);
//Serial.println();
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.");
}
}