File tree Expand file tree Collapse file tree 1 file changed +20
-2
lines changed
Expand file tree Collapse file tree 1 file changed +20
-2
lines changed Original file line number Diff line number Diff line change 33
44void mqtt_callback (char *topic, byte *payload, unsigned int length) {
55 JsonDocument json;
6- deserializeJson (json, payload, length);
7- setJsonPathPower (json);
6+
7+ // Try to parse as JSON first
8+ DeserializationError error = deserializeJson (json, payload, length);
9+
10+ if (error) {
11+ // If JSON parsing fails, try to parse as a raw number
12+ String payloadStr = String ((char *)payload);
13+ payloadStr.trim ();
14+
15+ // Check if payload is a valid number
16+ if (payloadStr.length () > 0 && (isdigit (payloadStr[0 ]) || payloadStr[0 ] == ' -' || payloadStr[0 ] == ' .' )) {
17+ float rawValue = payloadStr.toFloat ();
18+ setPowerData (rawValue);
19+ } else {
20+ DEBUG_SERIAL.print (" Error parsing MQTT payload" );
21+ }
22+ } else {
23+ // Successfully parsed as JSON
24+ setJsonPathPower (json);
25+ }
826}
927
1028void mqtt_reconnect () {
You can’t perform that action at this time.
0 commit comments