Publish JSON Data over MQTT with Node-Red

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this video we can look at sending Jason theta over MQTT using node-red now the main use of this is if we were combining sense the data so imagine we got several sensors and we could publish this that sensor data individually so one sensor one one censor topic and we could also combine that their data into a single sensor topic and publish the data together as a single unit and if we have a look here this is there are two methods that we could use we could use the single sensor single topic method or we could use the combined method where we publish on a combined topic and we published the data together now we don't normally publish data like this although we could do because there's no clear correlation between this value here and the sensor one here so what we normally do is we actually combine it into an object and it will look more like this sensor one on sensor to on sensor 3 off and this would start off as a JavaScript object and it would be converted into a JSON data the JSON where a string to be sent over onto and qtt and what we're going to simulate or demonstrate in the video is we're going to simulate something like this where we got multiple sensory inputs they could be door sensors we're going to simulate a house they could be door sensors temperature sensors or they could be coming from a machine so they could be the speed of the rotation they could be pressure and all kinds of things we feed them into a hub and now usually this hub is what we do is we scan these input values at a predetermined time so every second or every half a second or whatever at the interval is and then we combine all the sense of values in to a single value and we send it over on mqtt and that's what we're gonna go into doing the humour video so this is our node read flow and this is the publish side of the flow and this is the subscribe side of the flow so we were you just using this really so we can debug it and the first thing here I've got this some inject nodes and these inject nodes are going to be used to simulate the sensors and the first one here I just want to show you that we get we inject the data directly as JSON encoding dates used in this one here and you can see here that we're using JSON and we're sending the data directly as a JSON encoded string and we send it into AM qtt node which publishes it this node here receives it and we then decode it and then we pass it on to the debug node this debug node here will publish the entire object the message object this debug node here will publish just the payload and it won't decode it it will just publish as it is okay as I say these are really just for demonstration so let's give it a go and you can see it's worked as expected the we're publishing s1 on and with us what we saw s1 on the reason I published too this one here so we could actually see the entire message object and you can see here I'm publishing on topics as sensor one which is what I expected okay so down aside these are the other to inject nodes and these basically are simulating our sensors so all I do is press this and it when I inject the value on topic house sensor 2 into this function node and this one will do sensor 3 now what we we have what we would have in real life is that the hub would be collecting the sensor data and the predetermined interval as I said every second every other second whatever it was it would combine that data and send it off now this function is basically going to combine the data and it's going to wait until it actually is told to send the data so it's not going to send it as it comes in it's gonna send it as a predetermined interval and this is the control that basically sets that interval I'm gonna do it manually then we're gonna do it automatically so let's have a look at the function so here's a function the first thing I do is set up the context object which is going to store our data context or data object which is storing our data I long a few things here I pick out the topic and then the first thing is there is an F and I checked the payload I'm going to use the message payload to indicate whether I should be sending the data if it is sent then I bring the data out of the context object put it into the payload and then log a few things set the topic on the message then I set the context on dated and all I clear it now you can choose whether you clear it or whether you leave it if you leave it then it will get overwritten when the new data comes in I'm choosing to clear it but it's a I would say this like design decision I'll talk about it towards the end of the video and then we return the the message and that message then gets passed on to the JSON node which then passes on to the mqt node which publishes it now if it is isn't send we go on to the else thing here and this is where we store our data we store our payload into the owed topic we store that into context data where it gets remembered so that's going to remember it in the between the function calls and then we return a null returning and all means nothing gets passed on to the next node which which means that nothing gets public so it's done so here we go here so what should happen now is I inject something in here and it gets stored in the function I injecting with this one here it gets stored in the function nothing's gonna get sent because remember it just stores it and returns a null and when I inject the send message in here then the function releases the message gets encoded as a JSON string and sent to our MQTT out and then we should pick it up over here so let's clear that and let's do it pressed it see we've injected nothing's happened over here inject it again again nothing happened no we send it and there we see the message and you can see we're publishing house sensor to our nose sensor 3 off and that's what we're gonna see over here and we're publishing on how sensors that's the compose main topic and you remember he saw that when I looked at the function and let's do it again but this time I'm only going to inject the first one and so basically this is simulating that only one sense has changed in that interval this sense it didn't change and if we send it you can see we only publish the one now remember I said we can choose to clear it if I had the chosen to clear it if I didn't left it then basically I would have still published data for this for both sensors but the one wouldn't have changed so if you only want to publish change data then you clear the the object if you want to publish the data for for the census even though it hasn't hasn't changed then you don't clear the object okay now let's automate that a bit so this time I'm going to set these inject nodes so they run automatically I'm gonna set this one for every second so every second it's going to inject on into here every two seconds I'm going to inject off and if I look at the other control and I know I'm gonna do that every three seconds no these are just random I just picked these just there just for demonstration so now let's deploy it and you can see the data started coming in over here this is all automatic but this is what would happen in real life the the hub would have a scaling interval like I said maybe every second every half a second and you know interval it would collect the data from the sensors and when they're interval came or expired he would send off the data so let's stop that the easiest way to stop it is to disable it and deploy it and it stopped I just want to step back a bit now and talk about more about in Jason part of it now I'm not going to go into the detail of formatting I'll put a link to an article that you tore on the web about JSON formatting and you can read that but if you look at it here you can see everything is in double quotes this is the message payload and the message payload here is a JSON string because I haven't converted it into a JavaScript object now the thing that's doing all the work is this node here and this node is a standard node I find it and there it is there and that node will take a javascript object and convert it into a JSON string encode it as JSON put in the string on it will do the opposite it will take a string a json encoded string and convert it into a javascript object which is what it's doing here and you can see the message i'm printing out the entire message you can see that's what it looks like that no this jason node here requires a javascript object and if i look back at the function you can see the context on data which are used for storing the the data is actually a javascript object and you can see if we go further down do we set the message payload here to the context on data object so that the message payload is actually a javascript object and that our javascript object is the payload is passing to the adjacent node and then sent off to MQTT or the MQTT publisher there's nothing special about the mqtt publisher that we haven't already seen in the other videos so that brings us to the end of the video if you've got any comments then please leave them below if you like the video then you click on the like button below and if you want to be notified when I publish new videos to the channel then you can always subscribe until next time goodbye
Info
Channel: Steve Cope
Views: 32,593
Rating: undefined out of 5
Keywords: JSON, node-red, MQTT, stevesinternetguide, tutorial, encode, decode, send json data, javascript, object, video, publish JSON data MQTT, IOT, sensors
Id: D70BNVai5AA
Channel Id: undefined
Length: 11min 1sec (661 seconds)
Published: Mon Apr 09 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.