Skip to content

Commit

Permalink
Expose config and config updates through MQTT
Browse files Browse the repository at this point in the history
  • Loading branch information
Carbou, Mathieu authored and Carbou, Mathieu committed May 9, 2023
1 parent 40c7ee1 commit 71fa0d9
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 1 deletion.
51 changes: 51 additions & 0 deletions src/mqtt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,7 @@ mqtt_connect()
event_send(doc);

// Publish MQTT override/claim
mqtt_publish_config();
mqtt_publish_override();
mqtt_publish_claim();
mqtt_publish_schedule();
Expand Down Expand Up @@ -396,6 +397,9 @@ mqtt_publish(JsonDocument &data) {
topic += kv.key().c_str();
String val = kv.value().as<String>();
mqttclient.publish(topic, val, config_mqtt_retained());
if("config_updates" == kv.key().c_str()) {
mqtt_publish_config();
}
topic = mqtt_topic + "/";
}

Expand Down Expand Up @@ -489,6 +493,53 @@ mqtt_publish_schedule() {
}
}

void
mqtt_publish_config() {
if(!config_mqtt_enabled() || !mqttclient.connected()) {
return;
}
const size_t capacity = JSON_OBJECT_SIZE(128) + 1024;
DynamicJsonDocument doc(capacity);

// Read only information
doc["firmware"] = evse.getFirmwareVersion();
doc["protocol"] = "-";
doc["espflash"] = ESPAL.getFlashChipSize();
doc["espinfo"] = ESPAL.getChipInfo();
doc["buildenv"] = buildenv;
doc["version"] = currentfirmware;
doc["evse_serial"] = evse.getSerial();
doc["wifi_serial"] = serial;

// Static supported protocols
JsonArray mqtt_supported_protocols = doc.createNestedArray("mqtt_supported_protocols");
mqtt_supported_protocols.add("mqtt");
mqtt_supported_protocols.add("mqtts");

JsonArray http_supported_protocols = doc.createNestedArray("http_supported_protocols");
http_supported_protocols.add("http");

// OpenEVSE module config
doc["diode_check"] = evse.isDiodeCheckEnabled();
doc["gfci_check"] = evse.isGfiTestEnabled();
doc["ground_check"] = evse.isGroundCheckEnabled();
doc["relay_check"] = evse.isStuckRelayCheckEnabled();
doc["vent_check"] = evse.isVentRequiredEnabled();
doc["temp_check"] = evse.isTemperatureCheckEnabled();
doc["service"] = static_cast<uint8_t>(evse.getServiceLevel());
doc["scale"] = evse.getCurrentSensorScale();
doc["offset"] = evse.getCurrentSensorOffset();
doc["max_current_soft"] = evse.getMaxConfiguredCurrent();

doc["min_current_hard"] = evse.getMinCurrent();
doc["max_current_hard"] = evse.getMaxHardwareCurrent();

// WiFi module config
config_serialize(doc, true, false, true);

mqtt_publish_json(doc, "/config");
}

void
mqtt_publish_json(JsonDocument &data, const char* topic) {
Profile_Start(mqtt_publish_json);
Expand Down
1 change: 1 addition & 0 deletions src/mqtt.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ extern void mqtt_loop();
// data: a comma seperated list of name:value pairs to send
// -------------------------------------------------------------------
extern void mqtt_publish(JsonDocument &data);
extern void mqtt_publish_config();
extern void mqtt_publish_claim();
extern void mqtt_set_claim(bool override, EvseProperties &props);
extern void mqtt_publish_override();
Expand Down
2 changes: 1 addition & 1 deletion src/web_server_config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ handleConfigGet(MongooseHttpServerRequest *request, MongooseHttpServerResponseSt

// WiFi module config
config_serialize(doc, true, false, true);

response->setCode(200);
serializeJson(doc, *response);
}
Expand Down Expand Up @@ -240,6 +239,7 @@ bool web_server_config_deserialise(DynamicJsonDocument &doc, bool factory)

StaticJsonDocument<128> event;
event["config_version"] = config_version;

event_send(event);
}

Expand Down

0 comments on commit 71fa0d9

Please sign in to comment.