Files
project-vesper/vesper/build/sketch/MQTT_Message_Handling.hpp
bonamin 101f9e7135 MAJOR update. More like a Backup before things get Crazy
Added Websocket Support
Added Universal Message Handling for both MQTT and WS
Added Timekeeper Class, that handles Physical Clock and Scheduling
Added Bell Assignment Settings, Note to Bell mapping
2025-09-05 19:27:13 +03:00

90 lines
3.2 KiB
C++

#line 1 "C:\\Users\\espi_\\Documents\\Arduino\\4. Bell Systems\\1. Main Projects\\Project - Vesper\\MQTT_Message_Handling.hpp"
#pragma once
void handlePlaybackCommands(char * command);
void setRelayDurations(JsonDocument& doc);
// - - - - - - - - - - - - - - - - - - - - - - - - - - -
// Handles the incoming payload. Returns it into a "JsonDocument" format.
JsonDocument payload2json(char * payload) {
JsonDocument doc;
DeserializationError error = deserializeJson(doc, payload);
if (error) {
Serial.print("deserializeJson() failed: ");
Serial.println(error.c_str());
}
return doc;
}
// Subscribes to certain topics on the MQTT Server.
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/addMelody";
String topicRelayTimers = String("vesper/") + DEV_ID + "/control/settings/relayTimers";
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);
uint16_t relay_timers_id = mqttClient.subscribe(topicRelayTimers.c_str(), 2);
Serial.print("Subscribing to Relay-Timers topic, QoS 2, packetId: ");
Serial.println(relay_timers_id);
}
// Handles incoming MQTT messages on subscribed topics.
// Could move logic out of this into a dedicated function.
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/addMelody";
String topicRelayTimers = String("vesper/") + DEV_ID + "/control/settings/relayTimers";
// Don't know what this is. Check it out later.
//String payloadContent = String(payload).substring(0, len);
if (String(topic) == topicPlayback){
handlePlaybackCommands(payload);
}
else if (String(topic) == topicSetMelody) {
setMelodyAttributes(payload2json(payload));
loadMelodyInRAM(melody_steps);
}
else if (String(topic) == topicAddMelody) {
// Handle adding melody
Serial.println("Adding melody...");
// You can call a function here to handle adding the melody
}
else if (String(topic) == topicRelayTimers) {
setRelayDurations(payload2json(payload));
}
else {
// Handle unknown topics
Serial.println("Unknown topic received.");
}
}
// Publishes a message on the MQTT server. Message passed as an argument.
void PublishMqtt(const char * data) {
String topicData = String("vesper/") + DEV_ID + "/data";
mqttClient.publish(topicData.c_str(), 0, true, data);
}