Output a very simple JSON object instead of a string?

Hi,

I have looked at the ArdiunoJSON library, but it is very complicated and I only need something very simple.

At the moment I send out a string of data. Example of my code ...
MQTT_client.publish ( Module 99 /info/data , "22 - Current temperature = 8.9");

What I would like to have is a JSON pair of ...
MQTT_client.publish (Module 99 /info/data, {"22 - Current temperature =":8.9})

As you can see my requirements are very simple, but I do not know how to do it ?

I have asked this question on the Arduino forum with some success, this is the code from their ..

char json[80];

      Current_temperature = 21.345;
      Convert_temperature_to_2_decimal_places ();                                          
      dtostrf(Temperature_to_2_decimal_places, 4, 1, Buffer);                               

      strcpy(json, "{\"22 - Current temperature =\":");
      strcat(json, Buffer);
      strcat(json, "}");

      Serial.print( "Current_temperature = ");
      Serial.println(Current_temperature);
      Serial.print("json = ");
      Serial.println(json);

      Print_MQTT_temperature_sent ();
      MQTT_client.publish (Topic_02, json);

I have spent many more hours om my own trying to find what I did wrong.But when this is sent out via MQTT, I receive in Node-RED one single object, so I cannot separate the value ?

The value I receive is {"22 - Current temperature =":21.3}

The object is ...

image

Thanks

what do you mean? Since it is an object, you can simply access the value using msg.payload.name_of_property

However, since you have chosen a god awful property name and awful topic structure, you are just making things difficult for yourself!

Recommendation:

  1. structure your topics like module/99/info/blahblah
  2. name properties in a way that doesnt require square bracket notation to access them. e.g. current_temperature instead of 22 - Current temperature =

You will eventually realise the benefit of a good topic structure but until that moment of realisation, please take on board what we have suggested here and previously:

Hi Thanks for that Steve

I must admit I have been looking at this until late last night and I think I have confused myself !

Sometimes it takes someone to say that my JSON object is at least correct ( yes my format Topic is terrible ) but this is from when it was just a string sent out by my Arduino, so it didn't matter too much.

So I am now trying to sort out my Arduino code which was written about 4 years ago, before I had even heard of JSON.
:flushed:

Thanks

Hi Again,

I need more help, again I have tried for the last hour and I think I am a little closer.

I want temperature and humidity both to be in a single JSON Object.
Example ... {"temperature":21.3,"humidity":46.2}

I seem to have too many inverted commas ? and missing one comma after 21.3
My code so far produces a string "{"temperature":21.3"humidity":46.2}"

Any help would be great :+1:

  char json[80];                                                                        

  float   temperature = 21.345;                                      
  dtostrf(temperature, 4, 1, Buffer);   

  float   humidity = 46.222;
  dtostrf(humidity, 4, 1, Buffer1); 


  strcpy(json, "{\"temperature\":");
  strcat(json, Buffer);

  strcat(json, "\"humidity\":");
  strcat(json, Buffer1);

  strcat(json, "}");


  MQTT_client.publish (Topic_02, json);

I dont see any issue.

PS: arduinojson makes this MUCH easier

JsonDocument doc;
doc["temperature"] = temperature;
doc["humidity"] = humidity;
serializeJson(doc, json)
1 Like

Set the mqtt node to auto detect json

Colin I didn't know it could do that, although I am using a very old version and as I have about 15 ESP modules all around my house, I thought I would change as little as possible for now, then when I test in one unit and get it to work, then look to change all of them to the new code when I understand things better.

Steve, At the moment I am trying to avoid the ArduinoJSON because my modules are very time critical as they are constantly looping with as little delay as possible. But I may go to ArduinoJSON when i need to test it.

Example ... {"temperature":21.3,"humidity":46.2}
But for the moment if I can, I would like to this like the above as I know that will have no impact on timing.

I want to do this in Node-RED debug, ( image example got from internet ).
10

Here are the problems with my output,
problem

the outer quotes are denoting this is a string (they are added in the debug view)
you are missing a comma only.

I highly doubt the lib will be MUCH less efficient than the hand coded string parsing you are doing? But only a test would reveal that.

Thanks Steve,

Added the comma and voila all working. :grinning:

I have learnt more doing it this way as I have never used strcpy or strcat before.

One question i know that a \ is an escape but how is it being used here, as there are so many inverted commas and colon's, i get lost exactly what it is all doing ?

And for anyone that may read this in the future, I will add the finished code below.
As I hate it when you follow a subject and they find a solution but they forget to post the final code. This includes the missing comma.

  char json[80];            

  float   temperature = 21.345;                                      
  dtostrf(temperature, 4, 1, Buffer);   

  float   humidity = 46.222;
  dtostrf(humidity, 4, 1, Buffer1); 


  strcpy(json, "{\"temperature\":");
  strcat(json, Buffer);

  strcat(json, ",\"humidity\":");
  strcat(json, Buffer1);

  strcat(json, "}");


  MQTT_client.publish (Topic_02, json);
1 Like

Hi again,

I feel a little stupid but I cannot get this to work :roll_eyes:
It should be shown in Node-RED as a key value pair object.
What I am getting is "{"PIR":movement}" in my debug node.

I have tried everything I can think of and looked on the internet for an hour.

I think it is something very simple but I cannot find it !

My code

 char MQTT_Text_to_send[] = "movement";

      strcpy (json_data, "{\"PIR\":");                                                      // Copy this as it is the start of a key-value pair.
      strcat (json_data, MQTT_Text_to_send);                                                   // Append to json_data, this Joins 2 Char arrays ?
      strcat (json_data, "}");                                                              // Required json_data output.
        
      Serial.println ("");
      Serial.print   ("PIR = ");    
      Serial.println (json_data);

      MQTT_client.publish (Topic_01, json_data);      

The "movement" needs to be quoted, so the JSON string is valid.
try

char MQTT_Text_to_send[] = "movement";

      strcpy (json_data, "{\"PIR\": \"");                                                      // Copy this as it is the start of a key-value pair.
      strcat (json_data, MQTT_Text_to_send);                                                   // Append to json_data, this Joins 2 Char arrays ?
      strcat (json_data, "\"}");                                                              // Required json_data output.
        
      Serial.println ("");
      Serial.print   ("PIR = ");    
      Serial.println (json_data);

      MQTT_client.publish (Topic_01, json_data);

Thanks E1cid,

That worked, 1 hour wasted and I knew if I came on here someone would see my mistake !
But I do try and find the problem without just coming here and asking.

The inverted commas drive me mad :roll_eyes:

Thanks

ahem cough cough :wink:

HI Steve

Ha ha I knew you would say that, i have tried to use it, but it would not work on my code.
:roll_eyes:

I seem to remember it was throwing errors and I cannot update my Arduino board manager past 2.7.4 at the moment, as again I get errors with my code & Arduino libraries.

My code was added to over a 4 year period with various libraries added, of which most are out of date now. All rolled into one master universal code that can do everything i need for my home automation.
There are about 12 modules in my home all doing different things and reporting back to Node-RED. So it is complicated.

I will have another go soon at ArduinoJSON
Mistakingly I thought the simple way would be quicker :upside_down_face:

Thanks for all your help men. :slightly_smiling_face:

1 Like

Hi,

Hopefully the last time I will have to ask for help on this subject,

      strcpy (json_data, "{\"PIR-activated\": \""); 
      strcat (json_data, "true"); 
      strcat (json_data, "\"}");

This works fine, and true is received as text, is there away of sending a boolean true ?

Thanks again :+1:

true in JSON syntax would be with no quotes

strcpy (json_data, "{\"PIR-activated\": "); 
      strcat (json_data, "true"); 
      strcat (json_data, "}");

e.g

{"PIR-activated": true}

Thanks,

I thought I had tried that combination, obviously not !

Worked :+1:

Thanks again

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.