Skip to content

Commit 3f8fc61

Browse files
committed
Change cfgMqttWattJson parsing to be more flexible
1 parent 49e1444 commit 3f8fc61

File tree

1 file changed

+7
-13
lines changed

1 file changed

+7
-13
lines changed

src/mqtt.cpp

+7-13
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// Copyright (c) 2021 steff393, MIT license
22

33
#include <Arduino.h>
4-
#include <ArduinoJson.h>
54
#include <ESP8266WiFi.h>
65
#include <globalConfig.h>
76
#include <inverter.h>
@@ -12,9 +11,6 @@
1211
#include <pvAlgo.h>
1312

1413

15-
#define MAX_JSON_LEN 200
16-
17-
1814
const uint8_t m = 2;
1915

2016
WiFiClient espClient;
@@ -104,15 +100,13 @@ void callback(char* topic, byte* payload, uint8_t length)
104100
// directly take the value from the buffer
105101
pv_setWatt(atol(buffer));
106102
} else {
107-
// extract the value from a JSON string
108-
StaticJsonDocument<MAX_JSON_LEN> doc;
109-
// Parse JSON object
110-
DeserializationError error = deserializeJson(doc, buffer);
111-
if (error) {
112-
LOG(m, "deserializeJson() failed: %s", error.f_str())
113-
} else {
114-
pv_setWatt((long) doc[cfgMqttWattJson].as<float>());
115-
}
103+
// extract the value from a JSON string (only 1st occurence)
104+
// Example: {"Time":"2022-12-10T17:25:46","Main":{"power":-123,"from_grid":441.231,"to_grid":9578.253}}
105+
// cfgMqttWattJson = power\": | |------>
106+
// the slash \ will escape the quote " sign
107+
char * pch;
108+
pch = strstr(buffer, cfgMqttWattJson) + strlen(cfgMqttWattJson); // search the index of cfgMqttWattJson, then add it's length
109+
pv_setWatt(atol(pch));
116110
}
117111
}
118112

0 commit comments

Comments
 (0)