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,10 +1,15 @@
#pragma once
extern std::vector<uint16_t> melody_steps;
extern uint16_t relayDurations[16];
void setMelodyAttributes(JsonDocument doc);
void loadMelodyInRAM(std::vector<uint16_t> &melody_steps);
void loadRelayTimings();
void saveRelayTimings();
// - - - - - - - - - - - - - - - - - - - - - - - - - - -
void setMelodyAttributes(JsonDocument doc){
@@ -68,3 +73,72 @@ void loadMelodyInRAM(std::vector<uint16_t> &melody_steps) {
// closing the file
}
void setRelayDurations(JsonDocument doc) {
// Iterate through the relays in the JSON payload
for (uint8_t i = 0; i < 16; i++) {
String key = String("b") + (i + 1); // Generate "b1", "b2", ...
if (doc.containsKey(key)) {
relayDurations[i] = doc[key].as<uint16_t>();
Serial.printf("Relay %d duration set to %d ms\n", i + 1, relayDurations[i]);
} else {
Serial.printf("Relay %d not found in JSON payload. Keeping previous duration: %d ms\n", i + 1, relayDurations[i]);
}
}
saveRelayTimings();
}
void saveRelayTimings() {
StaticJsonDocument<512> doc; // Adjust size if needed
// Populate the JSON object with relay durations
for (uint8_t i = 0; i < 16; i++) {
String key = String("b") + (i + 1);
doc[key] = relayDurations[i];
}
// Open the file for writing
File file = SPIFFS.open("/settings/relayTimings.json", FILE_WRITE);
if (!file) {
Serial.println("Failed to open file for writing");
return;
}
// Serialize JSON to the file
if (serializeJson(doc, file) == 0) {
Serial.println("Failed to write JSON to file");
} else {
Serial.println("Relay timings saved successfully");
}
file.close();
}
void loadRelayTimings() {
// Open the file for reading
File file = SPIFFS.open("/settings/relayTimings.json", FILE_READ);
if (!file) {
Serial.println("Settings file not found. Using default relay timings.");
return;
}
// Parse the JSON file
StaticJsonDocument<512> doc; // Adjust size if needed
DeserializationError error = deserializeJson(doc, file);
if (error) {
Serial.println("Failed to parse settings file. Using default relay timings.");
file.close();
return;
}
// Populate relayDurations array
for (uint8_t i = 0; i < 16; i++) {
String key = String("b") + (i + 1);
if (doc.containsKey(key)) {
relayDurations[i] = doc[key].as<uint16_t>();
Serial.printf("Loaded relay %d duration: %d ms\n", i + 1, relayDurations[i]);
}
}
file.close();
}