Added basic Scheduling Functionality
A JSON message can now be received on: 'vesper/DEV_ID/control/addSchedule" Each message, must hold a "file" and "data". The file is the month's name in 3 letter mode (eg jan, feb, mar) The data is an entry for each day of the month. Each day can be an array containing multiple items.
This commit is contained in:
89
vesper/build/sketch/MQTT_Message_Handling.hpp
Normal file
89
vesper/build/sketch/MQTT_Message_Handling.hpp
Normal file
@@ -0,0 +1,89 @@
|
||||
#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 handleJSON(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(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.");
|
||||
}
|
||||
}
|
||||
|
||||
// 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);
|
||||
}
|
||||
Reference in New Issue
Block a user