Skip to content

Commit

Permalink
Merge pull request #421 from KipK/mqtt_scheduler
Browse files Browse the repository at this point in the history
added /scheduler /scheduler/set {json} to MQTT.
  • Loading branch information
glynhudson authored Sep 7, 2022
2 parents 85c091d + c0e2a90 commit 5de0d3c
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 8 deletions.
22 changes: 14 additions & 8 deletions docs/mqtt.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,27 @@ OpenEVSE can post its status values (e.g. amp, wh, temp1, temp2, temp3, pilot, s
**The default `<base-topic>` is `openevse-xxxx` where `xxxx` is the last 4 characters of the device ID**

Claims & manual override are read accessible here:
`<base-topic>/override/` : get manual override ([json data], {"state": "null"} when there's no override)
`<base-topic>/override/set <json data> ` : set/update manual override ( data as refered from API : https://openevse.stoplight.io/docs/openevse-wifi-v4/e0ab0a4ad5e1e-set-the-manual-override )
`<base-topic>/override/set toggle` : toggle manual override
`<base-topic>/override/set clear` : clear manual override
`<base-topic>/override/` : get manual override ([json data], {"state": "null"} when there's no override)
`<base-topic>/override/set <json data> ` : set/update manual override ( data as refered from API : https://openevse.stoplight.io/docs/openevse-wifi-v4/e0ab0a4ad5e1e-set-the-manual-override )
`<base-topic>/override/set toggle` : toggle manual override
`<base-topic>/override/set clear` : clear manual override

`<base-topic>/claim/` : get mqtt service claim ([json data], {"state": "null"} when there's no claim )
`<base-topic>/claim/set <json data>` : set/update claim from MQTT service. Has same priority as HTTP service. ( data as refered from API : https://openevse.stoplight.io/docs/openevse-wifi-v4/ebc578ffa7ca7-make-update-an-evse-claim )
`<base-topic>/claim/set release` : release claim
`<base-topic>/claim/` : get mqtt service claim ([json data], {"state": "null"} when there's no claim )
`<base-topic>/claim/set <json data>` : set/update claim from MQTT service. Has same priority as HTTP service. ( data as refered from API : https://openevse.stoplight.io/docs/openevse-wifi-v4/ebc578ffa7ca7-make-update-an-evse-claim )
`<base-topic>/claim/set release` : release claim

Claim & override properties can be set independantly. Sending json with only some fields will update the current claim properties only.
To remove a selected claim/override property, just send "clear" as property parameter ( i.e. `<base-topic>/claim/set {"charge_current": "clear"}` )

Scheduler data:
`<base-topic>/scheduler/` : get scheduler data ([json data]
`<base-topic>/scheduler/set <json data>` : set/update schedules ( data as refered from API: https://openevse.stoplight.io/docs/openevse-wifi-v4/e87e6f3f90787-batch-update-schedule )
`<base-topic>/scheduler/clear <id> :`remove related event

Main settings:

`<base-topic>/divertmode/set [1 | 2]` : enable (1)/ disable (2) divert mode
`<base-topic>/divertmode/set [1 | 2]` : enable (1)/ disable (2) divert mode
`<base-topic>/shaper/set [0 | 1]` : temporary enable (1)/ disable (0) current shaper ( doesn't survive reboot )


MQTT setup is pre-populated with OpenEnergyMonitor [emonPi default MQTT server credentials](https://guide.openenergymonitor.org/technical/credentials/#mqtt).
Expand Down
45 changes: 45 additions & 0 deletions src/mqtt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "web_server.h"
#include "event.h"
#include "manual.h"
#include "scheduler.h"


#include "openevse.h"
Expand Down Expand Up @@ -159,6 +160,15 @@ void mqttmsg_callback(MongooseString topic, MongooseString payload) {
mqtt_set_claim(false, claim_props);
}
}

//Schedule
else if (topic_string == mqtt_topic + "/schedule/set") {
mqtt_set_schedule(payload_str);
}
else if (topic_string == mqtt_topic + "/schedule/clear") {
mqtt_clear_schedule(payload_str.toInt());
}

else
{
// If MQTT message is RAPI command
Expand Down Expand Up @@ -254,6 +264,7 @@ mqtt_connect()
// Publish MQTT override/claim
mqtt_publish_override();
mqtt_publish_claim();
mqtt_publish_schedule();

// MQTT Topic to subscribe to receive RAPI commands via MQTT
String mqtt_sub_topic = mqtt_topic + "/rapi/in/#";
Expand Down Expand Up @@ -305,6 +316,11 @@ mqtt_connect()
mqtt_sub_topic = mqtt_topic + "/claim/set";
mqttclient.subscribe(mqtt_sub_topic);

mqtt_sub_topic = mqtt_topic + "/schedule/set";
mqttclient.subscribe(mqtt_sub_topic);
mqtt_sub_topic = mqtt_topic + "/schedule/clear";
mqttclient.subscribe(mqtt_sub_topic);

connecting = false;
});

Expand Down Expand Up @@ -393,6 +409,35 @@ mqtt_publish_override() {
mqtt_publish_json(override_data, "/override");
}

void mqtt_set_schedule(String schedule) {
Profile_Start(mqtt_set_schedule);
scheduler.deserialize(schedule);
mqtt_publish_schedule();
Profile_End(mqtt_set_schedule, 5);
}

void
mqtt_clear_schedule(uint32_t event) {
Profile_Start(mqtt_clear_schedule);
scheduler.removeEvent(event);
Profile_End(mqtt_clear_schedule, 5);
mqtt_publish_schedule();
}

void
mqtt_publish_schedule() {
if(!config_mqtt_enabled() || !mqttclient.connected()) {
return;
}
const size_t capacity = JSON_OBJECT_SIZE(40) + 2048;
DynamicJsonDocument schedule_data(capacity);
EvseProperties props;
bool success = scheduler.serialize(schedule_data);
if (success) {
mqtt_publish_json(schedule_data, "/schedule");
}
}

void
mqtt_publish_json(JsonDocument &data, const char* topic) {
Profile_Start(mqtt_publish_json);
Expand Down
4 changes: 4 additions & 0 deletions src/mqtt.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ extern void mqtt_publish_claim();
extern void mqtt_set_claim(bool override, EvseProperties &props);
extern void mqtt_publish_override();
extern void mqtt_publish_json(JsonDocument &data, const char* topic);
extern void mqtt_publish_schedule();
extern void mqtt_set_schedule(String schedule);
extern void mqtt_clear_schedule(uint32_t event);

// -------------------------------------------------------------------
// Restart the MQTT connection
// -------------------------------------------------------------------
Expand Down
4 changes: 4 additions & 0 deletions src/scheduler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "emonesp.h"
#include "app_config.h"
#include "event.h"
#include "mqtt.h"

#include <algorithm>
#include <vector>
Expand Down Expand Up @@ -365,6 +366,9 @@ void Scheduler::buildSchedule()
doc["schedule_plan_version"] = ++_plan_version;
event_send(doc);

// publish updated schedule to mqtt
mqtt_publish_schedule();

// wake the main task to see if we actually need to do something
MicroTask.wakeTask(this);
}
Expand Down

0 comments on commit 5de0d3c

Please sign in to comment.