How can I receive & send Data in Node-RED as JSON ... in a easy to understand way?

Hi,

Currently I get data from Arduino modules, received into my project like this ...

Module 91 - Temperature - DHT 01 ... 21
Module 91 - Humidity - DHT 01 ... 85
Module 91 - Temperature - DHT 02 ... 18
Module 91 - Humidity - DHT 02 .... 75

04

The all arrive in separate messages and I have a MQTT node for each one.
So in total in this example I have 4 MQTT nodes.

I think it would be better to have them all inputted as one JSON message and separate only the Data I want ... for example Humidity - DHT 01 from the JSON ?
Then I only need one MQTT node.

Module 91 - Temperature - DHT 01:21 , Humidity - DHT 01:85 ... etc

The Arduino modules sending JSON data, I can most probably find out how to this with some searching.
But how do I do the Node-RED part ?

I am not sure what Node-RED nodes are best suited to this and the easiest to understand and implement and easy to update when i want to add more information ?

Any ideas welcome

Thanks

if you structure your Arduino code to send JSON e.g.

{
  hum: 45.5,
  temp: 19.7,
  whatever_else_you_need: 12345  
}

then you can simply subscribe to one topic.

For accessing the values, simply address them in your flow e.g. msg.payload.hum or msg.payload.temp


If you dont wish to change the Arduino code, then you can simply join all 4 into 1 payload using a join node.

See this article in the cookbook for an example of how to join messages into one object.

and there is a good library for arduino called arduinojson that will help create that structure.

What mqtt topics are your arduinos publishing to?
if msg.topic === "Module 91 - Temperature - DHT 02 ..." then you are missing much of the power of mqtt.
Change it to "Module91/DHT02/Temperature" then a single mqtt-in node can subscribe to everything from Module91 - "Module91/#"
or DHT01 and DHT02 temperature only - "Module91/+/Temperature"
etc.

Thanks to you all,

That was just the help I needed, I will look into each option and then decide.

At the moment I am doing a lot of restructuring of my flows in anticipation of this being run in Docker.
The flows have been built up over 4 years, while they work they could be improved, so these will all need updating, streamlining and hopefully easier to maintain in the future :slightly_smiling_face:

So it maybe a while until I can actually do the Arduino & JSON, so I may have to come back to this thread.

Thanks for your help on how i can restructure my messages. :+1:

Hi

Have been trying to work with that data being output from the Arduino it was not structured as JSON, but I may well do that in the future.

I have managed to get to this with a JOIN node in Node-RED. And this is the output of the debug mode.

{
    "Module 22 /current_temperature/data": 13.4,
    "Module 22 /electricity_counter/data": 3.106,
    "Module 22 /gas_counter/data": 0,
    "Module 22 /info/data": "No rapid increase in temperature"
}

With another debug note attached to the output of the JOIN node I can get just the electricity_counter latest value.

01

But if i use the same payload and add that to a filter node.
I get ALL of the Module 22 data, I was expecting to just see only the electricity_counter ?

I am sure I am doing this all wrong but I do find it very confusing :neutral_face:

Thanks

that is not what the filter node does. the filter node simply allows the msg (in its entirety) to pass (or not).

If you only want part of the data in the payload, use a change node to move or copy the msg.payload.xyz to msg.payload

The built in help explains what the node does.

Better still, simply ignore the other parts of the payload and use the value as and where required

Sorry, last comment. you get ALL data because you subscribed to Module 22/# that means you will get Module 22/x and Module 22/sausage and Module 22/scoobydoobydoo etc.

What are you REALLY trying to do?

PS:

a better topic structure would be module/22/xxxxx this way you can subscribe to all modules module/# or a single module module/22/# or a specif topic of all modules module/+/temperature

I just want to get the 3.106

getting the value is not the issue. you can simple access msg.payload['Module 22 /electricity_counter/data']

What do you want it do with it?

This goes into a Node-RED gauge

Then use a change node

Hi, yes I can see what you mean, I understand that now, must learn to look at the help on nodes, but my brain likes to work visually so I learn mostly from YouTube and Node-RED and you kind people on here :+1:

In fact you do not even need to use a Change node. The Gauge node has a Value Format field. In there you can put {{msg.payload.xyz}} and it will select that property for display for you automatically.

1 Like

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