Valid JSON Output option - emonTX

@Robert.Wall, @TrystanLea, would it be possible to add an option to the emonTX firmware to send the serial output as a valid JSON string?

so

MSG:1,Vrms:246.29,P1:84,E1:0,T1:0.00,pulse:0

would be

{"MSG":1,"Vrms":246.29,"P1":84,"E1":0,"T1":0.00,"pulse":0}

If there is no builtin method to convert the string, it would need the brackets and double quotes added.

My thoughts are that this could then more easily be read and sent on (just needs to be published to an MQTT topic) by other serial devices. e.g. an ESP with Tasmota.

The emonTx single-phase sketches are not mine. I suggested to Trystan that he should simplify the emonTx CM sketch because the complication added to try to force it to behave exactly like emonLibDS appeared to be creating problems.

Which there isn’t, to my knowledge…
The easy way is to use the pre-processor directives. The big advantage of those is the unwanted parts don’t compile and thus don’t carry a memory usage cost nor a run-time processing cost.

#define JSON at the top of the sketch (assuming you want JSON output, don’t if you don’t), and surround the pertinent Serial.print statements thus:

#ifndef JSON

[existing output]

#else

Serial.print("{\"MSG\":"); Serial.print(emontx.Msg);
Serial.print(", \"Vrms\":"); Serial.print(EmonLibCM_getVrms());
[etc]

#endif

(Note the escaped double quotes.)

1 Like

I’m just going to revive this.

I’d still like to see it and as we are giving more options to use the serial output send it by Wi-Fi (ESP/Pico/PiZW) it would make things much easier to implement.

I’d suggest an option in the setup menu and then it can be changed easily. All it then needs is some additional characters added into the output string if the option is set.

[edit]
Just digging around and there is a JSON Library ArduinoJson - Arduino Reference

1 Like