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