How to use MQTT in Node-Red: basics, examples, tip & tricks

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
welcome to my NoDa tutorial video and today I'm going to show you how you can use mqtt under node-red in this video we are going to talk about the following so I will talk firstly about how you can connect to an mqtt broker in node red in order to start the communication we are going to see a couple of examples how you can send and receive messages I'm also going to show you that outside node red what is the best tool in order to look at the mqtt messages so if you need a second application just to make sure that everything is going right I would recommend using mqtt Explorer so I'm going to talk about that a little bit and I also going to show you a couple of examples like if you really have to think about how you would use mqtt in a home scenario in a home automation scenario I'm going to go through a couple of examples I also want to talk about the importance of the various data type and when you what you need to look out for for example if you are dealing with numbers or Json and also there is some new function in the mqtt which has been introduced a couple of months ago so you have more options how you can dynamically Contour control control the mqtt element I personally don't use this but I think there are some good scenarios where you might want to control your mqtt node and then finally I made a separate video on some really uh useful toolbox functions especially if you are using mqtt4 devices or sensors and you know you might want to measure how what was the last time you communicated with it so for example maybe the the device has gone offline so I have a set of functions that I took in a separate video but I'm going to quickly talk about this so if you are interested you can watch that video as well and if you are only interested in certain part of this video there are jump links in the video description and you can see all those other chapters in the YouTube timeline as well I want to talk about how you use nqtt in node-red so I'm going to skip on how you install mqtt what mqtt is I'm going to talk about some details but if you don't know these Basics or if you need some more knowledge I would probably recommend it just to search for these videos I haven't made such videos but I think there are plenty on YouTube already and since we talk about mqtt and setup I just want to make sure that your mqtt is up and running by the time you watch this video and I also want to just convey some like you know good practices making sure that your mqtt is correctly configured the anonymous access is not enabled so you have a user ID and a password created and if you are planning to use the mqtt over the open internet then you should really set up some sort of security because otherwise your data and your password are going to travel in plain text over the Internet which is definitely not good so let's start with the basic things and as you can see I already prepared a couple of examples but I want to go through the the basics with you manually if you filter on the mqtt itself you see that it's actually fairly simple because we only have two nodes the mqtt in where you receive messages from the mqg network and the mqtt out where you send messages out so what I'm going to do is I just drag the mqtt message on to my into my flow and I'm going to double click and the first thing that you need to configure is your server on the mqtt broker and as you can see I already have a few configured so I have one broker which is running on my this on the same server where the note that is running and if you are using node red or if you are even using home assistant with node-red mqtt that's going to be the case for you as well most probably but I have other services as well so for example I have a unsubscribed or part of a tiny GS Network where we run our own satellite ground stations and that is using mqtt as well so I configured that as well but if you are new you would just have one option which is add new broker and then you just click on that and the basic that you need to configure is the server address so you can just put 127.0.0.1 if it's the same server where your node is running I mean I just like to actually specify the the server address or the IP of the server so this is why I did like 192.168 1.80 which is my address and I would also configure it to connect automatically that's the easiest example and if you are running a local server all you have to do is provide your username and the password so that's the username and the password that you have configured uh in the for the mqtt broker in the setup and of course if you would connect to a mqtt service which is on the network you most probably have to configure some sort of security so you can maybe you need to enable the TLs and you have to provide a certificate you have to create a new TLS config and there are many different ways how you can do that but if you are using any of these internet-based Services they would definitely have a documentation how you can set this up or you know what are the different parameters that they require because most probably the product is going to be different so I'm trying to stick to the easiest scenario which is your local server which is on the same IPS your let's say main server the default Port is 1883 and then you provide your user and the other password and then so and then you just select that configuration and then you need to subscribe to your topic so if you know that much about mqtt you or you always have a topic and yeah the topics are in a hierarchy or structure and then they are separated by slash so again if you are in a home automation scenario maybe you have like um like you can have something like home slash like ground floor slash bathroom and then that would give you that then you will subscribe to uh let's say a sensor which is called a bathroom sensor which is probably in the in your ground floor but in most cases people don't tend to create such an elaborate structure so you just say that okay I have a sensor which is called bathroom or something and then you just uh type that and of course uh in mqtt you have the option to say that let's say in my bathroom there are multiple sensors that have a topic of bathroom slash something or maybe that sensor is going to send you multiple messages on different topics you can then just say that I want to subscribe to the topic slash and and the uh the hashtag character which says that everything under it I want to subscribe to I think for testing I'm just going to create a test topic and I want to subscribe to that and then you can specify a qos and the the output type and I'm going to talk about the output type later so that's the that's the basic thing so now I also create a debug node and so everything that comes out of this mqttn I just put it into the debug notes so we can see what's in the messages and if I deploy this nothing really going to happen because nobody is sending messages to the test topic yet so all we can see that we have connected to the mqtt server so this is a good indication that the server configuration is correct the user ID and the password and everything and we can actually connect to it so if you have done something wrong or there is an issue with your mqtt server you probably see messages here either disconnected or you will see a yellow message that is constantly trying to connect in that case you need to check your mqtt server in order for me to test this well actually this is the receiving part in order for me to test this we actually have to send a message so I need an mqtt out node so that's where we can send messages into the mqtt network and I'm going to configure this to the same broker and to the same topic so ideally what I send in here it should come out here because we subscribe to the topic that we sent the messages to and um and I can create an inject node by the way if you are not using this you can control and click and that brings up this quick search and you don't have to find your notes from here this is something that I just use so what I can do here is let's say I have a simple node a simple inject node which is going to send the time current timestamp and maybe I create another one as well which is again an inject node and here I just want to see show you a couple of examples so um that's going to send the text which is hello and maybe I have a third one as well so I just did copy and paste and this is going to send a number 10. and I'm going to link all of these into the mqtt node oops so we can see how it works so now what I expect to happen is that when I trigger this inject node the message goes in and because this is subscribed to the same topic the same message comes out so if I send this you can see that it is the debug 10 so it's coming from this node and we get the timestamp if I say hello then the hello comes out you can see the hello text here and if I say if I send 10 the the message 10 comes out so that's that's uh that's how it works and well that's that's what we expect at this point I want to show you this mqtt Explorer as well so you can just Google mqtt Explorer and this is a Windows application you can just download it and this has the same connection screen so I provide my IP port user ID and the password to the to my same mqtt server and if I connect to it well I see a lot of messages because there is a lot which is happening on my mqd server and this is the uh the hierarchy that I was talking about like you know for example I have a device which is called um let's say I have a message here which actually comes in the net IO slash bar cable or zero C slash output slash one slash voltage and then the data is 235 which is 233 volts so this is why I like this one because you can just easily you know look at the traffic and and what's going on and now if I send this hello again then we can see the Halos ball and if you click on this topic you see it's the test topic you can also send a new message so you can see here that I can send I don't know uh this is mine and if I publish it comes out here so we can definitely see that this would subscribe to any message not just the ones which is coming from node red but obviously for many sensor so if I want to just copy and paste this example just to show you how it would you know work on a simple node so let's say I have a that the door sensor for example and that door sensor is going to send me to a different type of information so there is um any let's assume that is just going to be text so it is going to send the text open if the door is open and it's going to send something close if the door is closed just uh just to keep it simple and what I can do is I can subscribe to this topic and do something about that one so for example if I have a switch node where I'm going to check whether the the message that I've received is open or closed and depending on that let's assume that I want to just change the yeah the text so now I replace this open with the text the door is open and then I replace the other text with another text which is the door is closed and then I can send this text for example to the dashboard in an email notification or wherever for the just to keep it simple I'm going to send it to my debug node so if I deploy this and I just delete all the messages so we are going to see what's happening and then I do open um oh yeah so the the original message comes out here as well so let me just close this but you can see that the door is open is coming through this path here and if I say closed then the door is closed yeah fairly simple and but that's how you would control an input or that is how you would process an input that is coming from coming over mqtt of course this one is not going to be in your flow depth would be a function that your sensor is going to implement and send these messages to mqtt I mean in most cases it's not going to be words open close it would be like something like zeros and ones or something very similar or if it's temperature it's just a temperature value and without going into too much detail I just I want to give you another quick example so if you are not really collecting data from sensors but let's say you have just a very simple relay that you can control over mqtt which is going to turn some lights on and off you can just have a big timer component so this is something which you have to install separately but for example in this big timer you can configure that you want this switch to come on at dusk so that would always depend on when the sun is going down and so that's when the lamp is going to go come on and then it's going it should switch off at 22.30 so half past half past 10. and then here you specify what is the message that the the node should send out when it's switching on so for example one and then what is the message that it it sends when it's uh it goes off and that is just going to send a number one and the number is zero and then you can just uh send that send it to the mqtt to the topic of that Wi-Fi switch for example and then your Wi-Fi switches just automatically go come on at dusk and then it's going to turn off at half past 11. the next thing I want to talk about and I think it is important to pay attention to is the various data types and that also varies depending on you know what version of node red you have because uh the way the node-red uh sorry the mqtt in node handles the different data types have changed a lot so I have an example here where you can see that I have a node which is configured so that it receives your messages to the weather weather station so it is coming in a topic which is weather station slash temperature and what I have configured here in the output is this was the output or this was the type which was available almost right from the beginning of node red which says it the node is going to Auto detect what the value is and it's going to treat it as either a string or a buffer and what is happening is you can see that I've sent this data into this row output and you can see the values here but if you would notice while it says here as well that it's the this value is a string and it's in double quotes So it it shows like you know 10.06 but this is 10.06 not as a number but as a string which is important because once you start working with this type of data then um for example if you want to compare it like you know um what I've done here is like okay is this number is uh you know more than zero and then depending on that you want to change or you know split the direction if you want to compare a text with a number it might I think in this example it just go it's probably going to work because the you know the num the first number is in this case is one which is always bigger than the zero but this is this is something that you really have to pay attention to that if you want to work with these number values and they are not coming through as numbers you probably need to convert them because otherwise node red is not going to give you any errors because JavaScript is well this is how it works it you know it tries to work with all sorts of data but it might just not do the thing that you want to want the system to do so for example what you can do is you can add the change node and then you can say that my I want my payload to equal to an expression and you can do number and then message.payload and now the number is converted sorry the the payload is converted to a number value so if I scroll down and wait for the next one you can see that this is the tax value that we get from the row input and this is the the number values so now the number doesn't have a double quotes and it's appearing in blue so now it is being treated as a number and if you look at the output now it now it says Auto detect it's Json object string or buffer and I actually change that to this value already and not only that it can detect between string or and Json but it can only detect the order between numbers as well so now you can see that the row number is also a number type but again you have to pay attention to this because otherwise if you you are not checking what is the output data format then node that might do something that you don't expect to do and again if you are importing flows from you know examples that you find on the net and if the example is really all the example this top option wasn't available so the your flow is going to default to this option so the numbers are going to come through as strings so it's easy to fix but you know this is something that you just need to be aware of and the other thing is that if you're if your data is coming through as Json like in this example then again you have the option to uh convert it as or parse it as a Json so you can either select this parces the Json object or you can leave it on auto detect as well again in the past this option wasn't available and actually there was a separate Json node which you would have to put in and that would do the conversion so the the Json string was coming through as a as a string and then you would use this node to convert it to a Json so we don't have to do that anymore but again I'm telling you this because if you see older examples even examples of my flows loads of them I I published in the past would have that Json because this is how it worked in the past so now you can see that this comes through as an object so it's no longer just a Json string but it's a proper object so you can see that it has things like RSSI and uptime and pool and you know some other things so what you can do is for example you can reference these fields um as using the dot notation because it's an object so if you want to use the this these values you can just reference them to message.payload Dot uptime and that is going to give you 119 or message.payload dot Delta and that that is going to give you 30. and here as well you can see that the various data types so these are all numbers and these relays which says 0 1 1 1 is actually a string but again you know always put everything in the bag look at the debugger and then um pay attention to the type as well and it usually says the the type of the payload but then once you start if it's an object and you start going into it's not going to tell you the type but you can tell that it's a number type if it's blue and it's it's a string if it's if it's red like that okay so finally let's talk about the the dynamic option which again it's only available fairly recently in the mqtt node so it it is just an mqtt not just or like all the others that I've shown you you specify the server you have the same output option but here you can normally you would have this one which says subscribe to a single topic but now you can do Dynamic subscription and actually if you click here and if you read the documentation it's actually going to tell you what is the various options that are allowed so you have to send in an action which it says either connect disconnect subscriber answers card so you can connect disconnect from the server and you can dynamically subscribe to unsubscribe to topics and of course in the topic you specify what you want to subscribe to or unsubscribe to and then you can even specify the broker details so you can just pass in all the you know the IP addresses um I find this probably a little bit less um uh you well I wouldn't say less useful but probably it's going to be used less but I can see that the Subscribe and the unsubscribe would be a you know good feature dynamically if you only want to receive certain data for a limited period of time and then you can just unsubscribe to it depending on your um um you know your scenario or you can even disconnect it's maybe if you are having too much traffic and you just want to limit how much um resources your flow uses and you can just unsubscribe so you your server needs to deal with less messages but let's see how it works I'm just going to go back to the debug node and what I have here is I have two inject node so one is injecting into the test topic and the other one is to The Test 2 topic and I have this debug node here so what I'm going to do is let's just uh delete all my log entries and I think all my debugs are turned off so at the moment only we are only going to see this so if I send a message let me just go uh like that so if I send a message to the test node then nothing happens if I send a message to the test two node nothing happens because in this Dynamic node is connected to mqtt server but it's not really subscribed to anything at the moment so what I can do here is the first example is like how you can subscribe so I send the message.action to subscribe and the message to topic to the topic that I want to subscribe to and if I send this message into the node then what seemingly nothing happens but if I send in a message well we now get the response so from this point onward now this is subscribing to the test topic it is still not responding to uh responding to the test 2 topic but I can also subscribe to test two and now I get the messages of test one and I get the messages of test two as well so you can you can use a single mqtt in node to subscribe to as many topics as you want and maybe you can even you know set these inject nodes up to inject in the beginning when the node red starts so they would just you know subscribe to these topics immediately but you can just use one mqtt node to subscribe to multiple topics so you don't have to deal with multiple nodes maybe you can make your flow simpler that way so that's how we subscribe to those topics and then we can also unsubscribe so if I unsubscribe to test one then you can see that I can do inject on test one sorry on test but nothing happens but the test 2 still works and I cannot unsubscribe on test two as well and now does this doesn't work as well that I mean it doesn't work either but it still works we are sending the message into the mqtt network but this is just not subscribed to that anymore so we are not going to get the messages I guess you get the idea and um in here I just only wanted to show you how you can do the subscription and the unsubscription so you just put unsubscribe here and you can also do connect and disconnect here as well of course in that case you don't need the topic so you just have a separate message which very the message that action user says connect or disconnect and that would connect or disconnect this mqtt node from the mqtt broker and finally I want to talk about these toolbox functions that I mentioned in the beginning I'm just going to fly through these options because as I said I have a separate video on that so you can find a link to that in the video description so I created a couple of different scenarios and and these are mainly like these function nodes which do some calculation and you can see the results at the end and of course I'm going to test these with these sensor nodes but you can obviously connect an mqtt sensor to that so maybe uh you can have a you can have a function which is going to remember the state so whether the last message was on and off and also it's going to count how many seconds something was on so again yeah you maybe have a device or something you want to see how long something is on you want to monitor it now you can do that and then you know if the if the own status reaches let's say 300 milliseconds you want to do something about it maybe send an alert or maybe you can even switch it off so you can do this with this function node the other thing I created is also like a more counter and uptime calculator so again you switch the uh you send uh on on a message um and then it's going to remember that it's also going to show you that the thing is on now and it's uh it does the same counting but then it also can count how many times something has been switched on and off so now I'm switching this thing on and off and then it counts that it's been running for 22 seconds and then now it has been switched on and off for three times so yeah maybe you have a device or a pump which is sensitive to a lot of cycles and you want to monitor that now you can monitor the runtime and how how many times it has been switched on and off so that's another example or the other usual things which I usually do is I want to see on my flow what was the last time I received the data from a particular device so the mqtt out node or sorry the mqtt in node goes into these two flow sorry function nodes and the at the top here is just going to show you here what was the last time the message was received and the one below it's going to do accounting so you can see that like you know you can see the actual time as well but if you really well if you're more interested in how much time has elapsed since the last update this is just going to count up and it's going to say like hours and minutes and days and hours and minutes if it goes that far and these are all about the toolbox functions that I thought it could be useful for you as well but again if you want more information on that or you want to access the these examples then just go to the video description and you find the link to this video I'm really happy that you have managed to get this far in the video which means that you have really liked the content that I showed just to recap what we have learned today so we have learned how the use the basic mqtt in and out controls within node-red we also seen how we can set up a broker connection or how we can create multiple connections to multiple Brokers we also see how we can monitor and you look at the mqtt traffic outside node red if you need some additional help just to better understand how things are working we also seen a couple of examples how you can use it in a sensor scenario and we looked at a couple of basic data types that you would most probably use in mqtt messages and also we looked at the dynamic scenarios which allow you to control where how you want to connect or disconnect from the broker or whether you want which topics you want to subscribe to or unsubscribe to dynamically and finally we looked at a couple of toolbox examples which I have explained in more details in one of my other videos but I'm happy that you managed to stay with me for this long that will be all for today thanks for watching and hopefully see you next video
Info
Channel: Csongor Varga
Views: 22,655
Rating: undefined out of 5
Keywords: tutorial, how-to, how-to use node-red, using mqtt in node-red, connect to mqtt broker, connect mqtt broken in node-red, mqtt-in, mqtt-out, json payload, subscribe to mqtt topic, send data to mqtt topic in node-red, node-red, mqtt, iot, how to use mqtt in node-red, node-red tutorial, node-red mqtt, tips and trick for mqtt, toobox examples, best tips for using mqtt in node-red, best practice
Id: fkrbcnp8wpI
Channel Id: undefined
Length: 29min 21sec (1761 seconds)
Published: Sat Jan 07 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.