MQTT Publish and Subscribe Using Node Red

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this video we're going to look at her to publish messages to an MQTT broker and to subscribe to messages from an mqtt broker using node-red so here we are in a node read control panel and I've started a new flow flow 3 now before I configure this I just want to disable the other two flows too so they don't interfere with this demonstration so to do that I just double click flow 2 and disable and done and I go to flow 1 and double click that and disabled and done so those flows are now disabled you can see the little icon there has changed so let's not be flow 3 the first thing I'm going to do is drag and inject node into here and I'm going to use that to inject messages and then I'm going to have an mqtt input node this is a subscribe node and scroll further down and we can have a debug mode and then we're gonna have an mqt publish know yeah let's wire those together so this is going to publish a message and this is going to display the message so this is our subscriber amputee this is our publish and we're going to send a message into it so let's configure that first I'm gonna send a string and the topic is going to be since send someone and the payload is gonna be open we're going to simulate a simple sensor maybe a dual sensor open and closed okay let's just give it a name sense of one and we're done so there it is there and now let's configure em cuties publish node double click that now we need to edit this and add a new cert and new broker to it so click Edit and I'm gonna use that's where my mosquito brokers and I'm gonna use the standard port I'm not using Secure Sockets Layer I can leave this blank and it will auto generate one I'm going to create one myself and it's gonna be test no security username and password I don't need it in this instant birth message this is useful if you're not using the inject node this is this message will actually be sent when you deploy this node but I'm going to use the inject node to do that so I don't need this and the will message this is last will and testament and that's it happens if you get forcibly disconnected or you the connection drops and you have the last will message sent from the server and that's covered on the site I'll give you a link if you want to read about the last full message okay and then just add I'm not configuring a topic and I'm not configuring a quality of service I can send it to zero once I'm leaving it as default be defaults to zero and at the moment I'm not using and retain flag now the topic is going to be set from the inject node so we saw that I configured it to sensor sensor 1 that's the topic and the message is going to come from the inject node so I don't need to configure a message so that's that done there the subscribe node we're going to subscribe to sensors and I'm going to use a white world card their quality of service I go back to zero it's actually defaulted to using the same client and just notice I spelled it wrong let's edit it and that's all I need to do for the subscribe node if I go back to the publish node here you can see it's change the the client ID so we've done there notice the blue circles the same we're all configured now all I need to do is to deploy this so click deploy and straight away we were connected you see here connected on the publish and connected on the subscribe now they're using the same client so it's a single clients connected to the broker that it's not using a separate client to connect to subscribe and a separate another client to publish it's using the same client to publish and to subscribe so let's put the debug over there and let's test it out by clicking on here and you can see I've got the message comes through click again and as the the message so very simple publish and subscribe now with this inject note here I can get it to repeat so I can set it to repeat every five seconds and deploy it and we should see coming up over here every five seconds there is there again and there again so let me just disable that now so in the example we've just done we published and subscribed using the same client ID so how do we publish and subscribe using another client ID a second client and we may need to do that if we're connecting to a different broker but I'm going to show you how to do it we're going to connect to the same broker but we're going to connect with a different client ID so we're going to have two clients connected to the same broker so I'm going to use the publish node for this so I just drag the publish node over here double-click it to edit it and we need to go to add new broker here and that becomes blank and then we enter our click edit to create it and I'm gonna call it so we're gonna call this one client 3 and we're going to use a local mosquito broker say usually you'd probably be connecting to a different broker so you put a different address in here I'm using the same one and that's it ad not going to configure anything else and I'm not even gonna configure a topic because I don't really want to or they don't need to publish with this to demonstrate it all I need to do is deploy it and when I deployed and I go to the broker I should see two client connections so let's deploy it and it's deployed now I'm just gonna go over to the broker and I'm gonna stop the broke out and then I'm going to restart it and doing that because I just want to get rid of all the previous messages on the broker so we can see the connection messages coming in so let's started and that was connected straightaway this so let's go to the broker and take a look so this is our broker you can see here I started it here just up here and this is our first client connecting client 3 and we should see our second client connecting this up they actually test client there that's our first line that's the first one we can figured and there's the second one I've actually stopped the broker again because I am using this broker for other purposes so there are other clients connecting to it you can see there's another one here that's connected to it so it now our brokers receiving connections from those two clients we configured in in node-red before we finish this video I just want to show you some something else is that these note here this is a published node and this is the subscribe node but they do share one thing in common they're sharing a common client so if I go to this node here and I change some of the properties that are particular to the client connection then it will change it on the other one so let's change it now the keeper live time is 60 seconds now I'm going to change that to 120 seconds and that is a client connection property and I'm gonna use use a clean connection I also did not use a clean connection and I just update that and done now if I go down to the subscribe client and edit it you can see that that's configured exactly the same so let's just tick that box to make it user clean connection again update it done and now go back to the publish one edit and you can see that's been updated now if I configure properties of this that are particular to the publish rather than to the the client connection and the retain message is particular to the publish then it's not going to affect the subscribe so click done and I go down here it doesn't affect this it doesn't affect this because the SUBSCRIBE doesn't have a retain property so it's never going to affect that okay I'm just going to finish by finishing a little sensor and I'm going to drag another inject node into here wire it into our publish node and get it to publish a message and it's gonna go on sensor we got to call this close it's going to publish the closed message and I'm just gonna put it closed there so we've got one that publishes open and the other one the pump is closed and let's just deploy it and click on here and we see open and click on here and we see closed so that's a very very simple door type sensor publishing open and closed we could have it as a light sensor sorry a light sensor publishing on okay that's the the end of the video if you've got any comments then please leave them in the comment form below if you like the video then use the like button below and if you'd like to be notified of new videos then you can always subscribe to the channel until next time bye
Info
Channel: Steve Cope
Views: 102,859
Rating: undefined out of 5
Keywords: node-red, MQTT, publish, subscribe, stevesinternetguide, tutorial, how to, video, IOT, SSL
Id: XDrwgMSQrEY
Channel Id: undefined
Length: 11min 20sec (680 seconds)
Published: Wed Mar 07 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.