Go Down

Topic: ArduinoJson to MQTT displaying gibberish but prints to serial fine (Read 87 times) previous topic - next topic

MarkMPH

Hello, I am new to arduino, and am trying to send a Json document to MQTT. It will send, but it doesn't display the message in the format I need/want. It just shows some random characters (which I'm guessing is something to do with the encoding) but not what I actually want to display. The print to serial output works fine and displays :

"{"Measurement":"Quench1","fields":{},"temperature":["22.25"],"tags":{"Machinename":"QuenchTank1","Machinetype":"Quench"},"Timestamp":{}}"


The Json document is meant to display in MQTT as above but I'm getting funny characters like "������".

Here's my code:

        StaticJsonDocument<200> doc;
        //Add "Location" to doc
        doc["Measurement"] = "Quench1"; 
        //Add "fields" to the Json document 
        JsonObject fields = doc.createNestedObject("fields");
        //Add temperature underneath "fields"   
        JsonArray temperature = doc.createNestedArray("temperature");
        // Add "tags" to the Json document
        JsonObject tags = doc.createNestedObject("tags");
        // This adds the readout from the thermocouple to the Json Doc
        temperature.add(String(thermocouple.readCelsius()));
        // Adding "machine name" and "machine type" to "tags"
        tags["Machinename"] = "QuenchTank1";
        tags["Machinetype"] = "Quench";
        JsonObject Timestamp = doc.createNestedObject("exampleTimestamp");
        // New line 
        Serial.println();
        serializeJson(doc, Serial);
        client.connect("TYZ040-777" );
        client.publish("TYZ040-777", serializeJsonPretty(doc, Serial)));


Any help would be greatly appreciated!



lesept

Have you tried the ArduinoJson assistant ? It provides the following code:
Code: [Select]
const size_t capacity = JSON_ARRAY_SIZE(1) + 2*JSON_OBJECT_SIZE(0) + JSON_OBJECT_SIZE(2) + JSON_OBJECT_SIZE(5);
DynamicJsonDocument doc(capacity);

doc["Measurement"] = "Quench1";
JsonObject fields = doc.createNestedObject("fields");
JsonArray temperature = doc.createNestedArray("temperature");
temperature.add("22.25");

JsonObject tags = doc.createNestedObject("tags");
tags["Machinename"] = "QuenchTank1";
tags["Machinetype"] = "Quench";
JsonObject Timestamp = doc.createNestedObject("Timestamp");

serializeJson(doc, Serial);

This should ensure there is no memory problem for your json document
A force d'essayer on finit par réussir... Donc, plus ça rate, plus on a de chances que ça marche (proverbe Sharduinok).

MarkMPH

I've had a go at this and used the ArduinoJson assistant, everything still prints to serial fine, but now rather than gibberish, I'm getting blank messages through. I'm using an arduino uno, is there a different set up for this board?


PaulS

Quote
I've had a go at this and
failed to post any code or any example of the results. Good luck.
The art of getting good answers lies in asking good questions.

Go Up