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..
This commit is contained in:
2025-01-21 11:42:23 +02:00
parent a33f626dde
commit 84534025f4
5 changed files with 150 additions and 65 deletions

View File

@@ -1,32 +1,34 @@
#pragma once
void handlePlaybackCommands(char * command);
void setRelayDurations(JsonDocument& doc);
void handleJSON(char * payload) {
// - - - - - - - - - - - - - - - - - - - - - - - - - - -
JsonDocument handleJSON(char * payload) {
JsonDocument doc;
DeserializationError error = deserializeJson(doc, payload);
if (error) {
Serial.print("deserializeJson() failed: ");
Serial.println(error.c_str());
return;
}
setMelodyAttributes(doc);
loadMelodyInRAM(melody_steps);
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/add_melody";
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);
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);
@@ -35,15 +37,20 @@ void SuscribeMqtt() {
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/add_melody";
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){
@@ -51,7 +58,8 @@ void OnMqttReceived(char * topic, char * payload, AsyncMqttClientMessageProperti
}
else if (String(topic) == topicSetMelody) {
handleJSON(payload);
setMelodyAttributes(handleJSON(payload));
loadMelodyInRAM(melody_steps);
}
else if (String(topic) == topicAddMelody) {
@@ -59,22 +67,17 @@ void OnMqttReceived(char * topic, char * payload, AsyncMqttClientMessageProperti
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(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 PublishMqtt(const char *data) {
String topicData = String("vesper/") + DEV_ID + "/data";
mqttClient.publish(topicData.c_str(), 0, true, data);