Skip to content

Commit

Permalink
Add cfgMqttWattJson to extract a value from a JSON string (#54)
Browse files Browse the repository at this point in the history
  • Loading branch information
steff393 committed Feb 11, 2023
1 parent f42c98c commit 49e1444
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/globalConfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ char cfgMqttUser[32]; // MQTT: Username
char cfgMqttPass[32]; // MQTT: Password
uint8_t cfgMqttLp[WB_CNT]; // Array with assignments to openWB loadpoints, e.g. [4,2,0,1]: Box0 = LP4, Box1 = LP2, Box2 = no MQTT, Box3 = LP1
char cfgMqttWattTopic[60]; // MQTT: Topic for setting the watt value for PV charging, default: "wbec/pv/setWatt"
char cfgMqttWattJson[15]; // MQTT: Optional: Element in a JSON string, which contains the power in watt, default: ""
char cfgNtpServer[30]; // NTP server
char cfgFoxUser[32]; // powerfox: Username
char cfgFoxPass[16]; // powerfox: Password
Expand Down Expand Up @@ -136,6 +137,7 @@ void loadConfig() {
strncpy(cfgMqttUser, doc["cfgMqttUser"] | "", sizeof(cfgMqttUser));
strncpy(cfgMqttPass, doc["cfgMqttPass"] | "", sizeof(cfgMqttPass));
strncpy(cfgMqttWattTopic, doc["cfgMqttWattTopic"] | "wbec/pv/setWatt", sizeof(cfgMqttWattTopic));
strncpy(cfgMqttWattJson, doc["cfgMqttWattJson"] | "", sizeof(cfgMqttWattJson));
strncpy(cfgNtpServer, doc["cfgNtpServer"] | "europe.pool.ntp.org", sizeof(cfgNtpServer));
strncpy(cfgFoxUser, doc["cfgFoxUser"] | "", sizeof(cfgFoxUser));
strncpy(cfgFoxPass, doc["cfgFoxPass"] | "", sizeof(cfgFoxPass));
Expand Down
1 change: 1 addition & 0 deletions src/globalConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ extern char cfgMqttUser[32]; // MQTT: Username
extern char cfgMqttPass[32]; // MQTT: Password
extern uint8_t cfgMqttLp[WB_CNT]; // Array with assignments to openWB loadpoints, e.g. [4,2,0,1]: Box0 = LP4, Box1 = LP2, Box2 = no MQTT, Box3 = LP1
extern char cfgMqttWattTopic[60]; // MQTT: Topic for setting the watt value for PV charging, default: "wbec/pv/setWatt"
extern char cfgMqttWattJson[15]; // MQTT: Optional: Element in a JSON string, which contains the power in watt, default: ""
extern char cfgNtpServer[30]; // NTP server
extern char cfgFoxUser[32]; // powerfox: Username
extern char cfgFoxPass[16]; // powerfox: Password
Expand Down
20 changes: 19 additions & 1 deletion src/mqtt.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) 2021 steff393, MIT license

#include <Arduino.h>
#include <ArduinoJson.h>
#include <ESP8266WiFi.h>
#include <globalConfig.h>
#include <inverter.h>
Expand All @@ -10,6 +11,10 @@
#include <PubSubClient.h>
#include <pvAlgo.h>


#define MAX_JSON_LEN 200


const uint8_t m = 2;

WiFiClient espClient;
Expand Down Expand Up @@ -95,7 +100,20 @@ void callback(char* topic, byte* payload, uint8_t length)

// set the watt value via MQTT (#54)
if (strcmp(topic, cfgMqttWattTopic) == 0) {
pv_setWatt(atol(buffer));
if (strcmp(cfgMqttWattJson, "") == 0) {
// directly take the value from the buffer
pv_setWatt(atol(buffer));
} else {
// extract the value from a JSON string
StaticJsonDocument<MAX_JSON_LEN> doc;
// Parse JSON object
DeserializationError error = deserializeJson(doc, buffer);
if (error) {
LOG(m, "deserializeJson() failed: %s", error.f_str())
} else {
pv_setWatt((long) doc[cfgMqttWattJson].as<float>());
}
}
}

callbackActive = false;
Expand Down

0 comments on commit 49e1444

Please sign in to comment.