Files
project-vesper/MQTT_Message_Handling.hpp
bonamin 84534025f4 Added Separate Timings for each Relay
The setting is saved on a file in:
/settings/relayTimings.json

Then during bootup, it's read and restored. 
Settings can be set via MQTT command on Topic: 
vesoer/*dev-id*/control/settings/relayTimers
using a json format. 
{ "b1":50, b2...}
where b1 is Bell 1, b2 Bell 2, etc..
2025-01-21 11:42:23 +02:00

85 lines
2.7 KiB
C++

#pragma once
void handlePlaybackCommands(char * command);
void setRelayDurations(JsonDocument& doc);
// - - - - - - - - - - - - - - - - - - - - - - - - - - -
JsonDocument handleJSON(char * payload) {
JsonDocument doc;
DeserializationError error = deserializeJson(doc, payload);
if (error) {
Serial.print("deserializeJson() failed: ");
Serial.println(error.c_str());
}
return doc;
}
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);
}
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(handleJSON(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(handleJSON(payload));
}
else {
// Handle unknown topics
Serial.println("Unknown topic received.");
}
}
void PublishMqtt(const char *data) {
String topicData = String("vesper/") + DEV_ID + "/data";
mqttClient.publish(topicData.c_str(), 0, true, data);
}