HOW TO CONTROL LED WITH PUBLISH/SUBSCRIBE MQTT IOT

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone this is Ashish yourself welcome back to my channel once again I am back with a new topic for mp3 protocol now in today's video I'm gonna demonstrate like how you can connect your load MC to the mqtt server so basically in today's video I'm gonna demonstrate that how you can connect both of them together and how you can control the components connected to the modems you using the embedia Reserve at in today's session we will be controlling the LED connected to the node MCU so this is all that we will be covering and other than that you will also see that how you can send the Callback signal as well or you can say you will also see that how you can send the signal from your node MCU to the MVD server so in short you will see both publish as well as subscribed so this is the thing that we will be covering up in today's session now let's not waste time and let's straight forward go to the project and let's understand how we are gonna do this thing so let's get started so guys as you can see that this is a new sketch file and here we have to start writing up the code and right now I'm not showing you any uh like uh Hardware stuff just because currently we're gonna use node MCU and we will not make any external connection to that just because we are going to use the inbuilt LED of node MCU so we will be controlling that led using the mqtt server So currently I'll be using that only so that's the reason we are directly shifting to the coding stuff and for the coding part currently you need to add a new sketch or you can say you need to add a new library into your sketch and that library is with the name Pub sub client which is like published subscribe client so this is a short form for that so we will be adding it up and to add that up you just have to go to sketch then include libraries and then go to manage libraries and uh window will be pop-up like the library manager and over here once this is loaded just search for Pub sub client so we'll do that right so just search for it now you can see it is showing you so many libraries but we don't have to go for all we need the one which is with the name Pub sub client which is this one which is by Nick or Larry right so this is the one which we have to install so just click on this install and if you want to change to any of the version you can select that accordingly but I'll go with the latest version only so just click on install and wait for it to install all right so it's installed so you can see Pub sub client is installed so now I can proceed further and now as the library is installed I'll go to file because along with this uh Pub sub client Library we have few examples which you can directly use and you can further modify it so right now I'll go to examples and I'll scroll down to Pub sub client and in here just because we are going to work with node MCU so I'll go for this mqtt underscore esp8266 so I'll open up this one so just open it up all right so you can see this is opened foreign already written and let me explain you what exactly we have inside this code uh basically we are going to connect uh to a server which is mqtt server and currently you can see this is a server or this is mqtt server which is written by default but we will not be using this one uh in our like in today's video just because this is the very common protocol or this is a very common broker for mqtt which everyone use and that's the reason it's uh having a very huge traffic over there and sometimes it doesn't perform well so that's the reason we'll go with this uh someone else and we'll try to connect to that mqtt server and we'll do the further publish And subscribe topics right so for that instead of using this I'll go with test dot mosquito dot o r g right so this is the one which I'll be using and in fact if in case you want to know more about this you can simply copy this and you can go to this website here you will get more details on this like what mqtt is what application what authentication we have what servers we have what caveat we have so all of these information we have regarding this so if you want you can go to this and you can check that So currently I'll be using this one and along with this you have to fill up the SSID and password uh so that you can connect to the network and after that you can connect to the mqtts protocol or mqtt server right so this is what you have to do so here you just need to fill up the details so I'll be filling it up okay so I have filled up the SSID and password and also I have put the mqtt server which I have in here now after that as you can see we are calling esp8266 Wi-Fi and Pub sub client so as for that we have to start creating a first object which is with the Wi-Fi client and I have given it a name as ESP client and accordingly I'm just connecting it to the pub sub client or you can say to the server basically so we're just linking it to that and uh after that we are simply creating few variables and why are we creating it you will see that at the end because we will be using it to further publish our data basically uh the message that I'll publish from the node MCU site will be the Subscribe topic for my mqtt server so basically this one I'll be using to publish it means node MCU will send that and your immigrated server will receive that in the similar way if mqtt server will send you some message or if the ambiguity server will publish something that will be a subscribe topic for your node MCU so this is how basically it works so this one we'll see at the end now after that you can see that we have few functions created over here right yeah right so you can see this is setup and this is Loop these are the two inbuilt functions in your Arduino sketch so this is by default you will have that definitely now let's start with the setup because you all know that setup is the first function which is called First and which will be executed first and it will be executed only once right so let me open it up and let's see what we have inside this so as you can see firstly we are defining what pin we are going to use or you can say what led we are going to connect to so we are using the built-in LED so that's what we have defined in here and also we are using the CL monitor for this so that's why we have specified the baud rate as 115 to double zero for the communication and after that setup underscore Wi-Fi just because we need to connect to the Wi-Fi network so that we can connect to the other server over the Internet so that's the reason we are just calling it up usually we write directly uh like this code inside the setup function but instead of writing it here we have simply created a separate function for that so that we can connect to the network and as you can see we are running it in a station mode so that we can act like a Wi-Fi and we are connecting to the SSID and password and until it is connected it will show dot dot and once it is connected it will show you that Wi-Fi connected and this is the IB address for that so this is the code for connecting to the network and that's what we have called in here after that now we are going to connect to the server basically we have already created a client through which we are going to connect it and this client is going to set the server as mqtt server which is this one which we have specified in here and the port number is 1883 and this port number which we have specified in here is having some meaning and what's the meaning of that you can go to this website which I showed you test.mosquito.org over here you can see that this is a server and these are the different ports that we have depending on the port number that you are using you are setting up the authentication and encryption method for your mqtt server like we are using a 1883 which means we are using the mqtt unencrypted server and unauthenticated server so this is what exactly we are doing if we are gonna change it to 1884 you can see it will be unencrypted but it will be authenticated similarly if you go for some other port number you will see we have the different function personalities over there right so in this way we can select like what port we want to use and what server we want to use right so this is your mqtt protocol and this is your immigrated server basically right so we have this now after that we are also setting up a callback method so that when the mqtt server will send some requests to you or you can say when mqtt will publish something you will be able to subscribe to that so that's the reason we have this callback function over here so basically this callback is passed as an argument which is a function and that is this one so over here if you will see it's like we are just receiving the data and once we have received the data based on the information or based on the bytes that we have received we are just gonna do the comparison or let's say we are just setting up the action like in our case what exactly we want to do is based on the value that we will get from the mqtt server like we will be sending one or zero so as for that if I will be getting one I will be simply turning on the LED now here you can see I have set it up as low why I decided up as low just because uh your node MCU by default pins gets turned on when you set the signal as low right so this is how we turn on the LED and similarly if the signal is zero or something else the LED will be turned off so this is what we are doing in this callback function right so this is what we have done now once this is done after that now let's comes to the loop part where we have the main functionality to repeat because this is just a one time step right so we have just set up the functions like whatever we want to use whether we want to connect to the network or what mode we want to use for the pin or what server we are going to use so this is what we have defined in here but now we are gonna come for this thing where we will like do the repeated things right so firstly we will check if the client is connected or not or let's say if we are connected to the mqtt server or not if you are connected to the mcreity server then it's gonna come out of this uh condition and it will directly jump further otherwise it will go to this reconnect reconnect is this one this is your reconnect where what exactly we are doing is we are just attempting to connect to the mqtt server where we also set up that what client we are going to use currently we are not specifying any particular client we are just using any random values for that or we are just using any random client ID that's what we are defining here and accordingly we are just connecting to that and once we are connected to this like over here we are just connecting to the client if the client is connected then we'll simply print that we are connected and now we are publishing something publishing something means now we are sending a signal or we are just sending a data from your load into you to the mqtt server so basically for that we are just writing it like we are just publishing that and the topic for that will be our topic this is the name that we have given uh let's say we can change this name right so let's say uh I'll give it like 2 mqtt means we are sending the data to mqtt so this is what I'm gonna write in here right similarly uh on the other side next one is client.subscribe now this time this client.subscribe means my node MCU is acting as a subscriber which means it will wait for you to send some value to receive it right so let me change this one as well like from mqtt right so in this way I'm gonna change it right so this is what we are doing and if everything goes fine then this is what it's gonna happen otherwise it's going to print as failed and what is the client state it is also going to show you that and also after five second it will try it again as you can see we have a delay of five seconds so this is uh what it exactly it is doing right now we'll go in here again and over here inside this once you are connected you will start the loop for that and I every two seconds after every two seconds this is exactly what the condition we have put after every two seconds we are gonna send this signal or we are gonna send this request to the mqtt server so that we can send back some message right like over here you can see as for this reconnect what we have done once we are connected we are just simply sending hello world but over here we are not sending only hello world we are sending hello world and some random values for that and how the values are gonna come like from here we're just doing the increment on that right so this is how we are going to do that and after that we are just publishing it now this time we have to change this just because we have changed the topic right and the topic is to mqtt so we are just sending their data to mqtt server right so this is how we have done that right so this is the exact and this is the complete code for your mqtt server now let's understand uh let's say Let's uh try to upload the data and let's see what will happen if I will execute this right so for that I'll have to upload it so let me upload it so as you can see that I have connected my node MCU to my system right so this is my system so I've connected to that now we'll upload the code and let's see how it's gonna react now let's upload the code just to verify that you have selected the right board and you have selected the right Port right so I have the right Port selected over here now I'll click on upload just because I mentioned everything already right all right so the code is uploaded and uh everything is working fine but currently we need to check that whether we are getting the signals or let's say we are getting the data sent from the node MCU or we are able to connect to that or not so for that we need uh some software through which we can connect to that and for that you have two options either you connect uh it on your system or you can download the application on your smartphone but for now I'll show you uh by uh adding one add-on to your um to your Google Chrome because in Google Chrome we have an add-on with the name mqtt lens so you can use that to connect to it and uh you can accordingly do it on your system basically right so for that if you have a Google Chrome you can go with that otherwise we can go for the application to be installed on your smartphone right so currently I'll show you with the Google Chrome so let me show you that and over here I'll simply search for mqtt lens press enter and over here you can see the first link is shown which is mqtt lens from Google Chrome so just click on that that will take you inside and it will simply uh show you this one currently as you can see it is showing me the option to launch app just because it's already installed on my Google Chrome or you can say it's already added over there that's why it's showing me like this but in your case it will ask you to download it or to add it right so if you want to work with it just click on add it to your Google Chrome and we can move further right so once it is done just click on this launch app or it will simply open up a page where it will show you uh to click on impurity lens just click on that and it will open up like this so as you can see in here this is showing me something like this and I think I have already started a connection before so let me delete this one first okay so this is how basically it will open when you will uh like start it for the first time right so now once uh we are at this step once we have opened this mqtt lens now we have to create a connection for this just click on plus and over here it will ask you for few details which you need to pass so first one is mqtt uh connection name basically it's a name that you can give anything right it's not like you have to pass the host name hostname will be passed here you need to pass any name let's say it's a mqtt uh innovate yourself iy for example right so this is the name that I'm giving because it's it can be anything first of all right and in the host name just select TCP and inside this host name you can simply pass this test dot mosquito.org just copy it and paste it here right and Port you can specify here and as we have already discussed we are going to work with one eight eight three which is unencrypted and unauthorized so we can directly use this and if you want to change it according to your requirement you can change it and you can use that one and here you can see the client ID is like any random value so this is how it's uh being shown to you and uh these things you can leave and overhead credentials are username and password currently we are not using any of the credentials that's why I will not add any of it but if you have your credentials or if you have the username and password uh with your requirement you can add that and you can use it further but currently I will not add it and here are also few details which you can fill up like what will be the quality of service that you will be using So currently we're using with zero only so we'll not make any changes in here right so that's all now I'll click on create connection and it will start a connection for me so let's start it so as you can see everything is green and we have made a connection and also you can see the status is connected so that means we are connected to the server and now we can see like what all we are getting over here right so here you can see we have subscribe and we have publish right so firstly let's try with the Subscribe because in subscribe we have given a topic as this the published topic for node MCU will be the Subscribe topic for your mqtt server so I'll simply pass two mqtt and the quality of service is 0 and click on subscribe can you see Hello World 149 hello world one five zero so basically after every two seconds you're getting this value which we have mentioned over here right so this is what we are doing now similarly the other way to send the data from your mgrt server to your node MCU and for that we have a second topic and that second topic is from mqtt right so I'll pass that and along with that we have to pass a value that what data you want you're gonna send and currently as you can see as per our coding what we have done we have simply set up that it will be either one or anything else one means to turn on the LED and anything else like zero or two three four something will be to turn off the LED so we'll send that right so as you can see the LED is turned on so now we have to turn it off and for that I'll be sending the signal as 0 so I'll click on publish so can you see the LED is turned off similarly if I'll do the other way click on one and pass the value so can you see LED is turned on so in this way uh we can simply turn on the LED or turn off the LED and I hope you have got an idea how do you publish the data and how do you subscribe to the data so subscribe to the topic basically so this is all that we uh do with the immigration protocol I hope everything was clear to you and you are not facing any difficulties but still if you're facing any difficulties or if you have any doubts regarding any of the topic that I have covered today then you can leave a comment below in the comment section and also if you are new to our Channel and if you haven't subscribed to our Channel yet then please click on the Subscribe button so that you do not miss any of the videos coming onto my channel and in fact I'll be keep on posting more videos uh very useful to you so that's all from eyesight for today so thank you so much for watching please do like share subscribe and comment uh if you have any doubts and also share it with your friends and everyone so that they can also have benefit of this so thank you so much for watching bye bye and take care
Info
Channel: Innovate Yourself
Views: 5,019
Rating: undefined out of 5
Keywords: HOW TO CONNECT ESP8266(WIFI MODULE) WITH ARDUINO, HOW TO CONNECT YOUR ARDUINO TO INTERNET, HOW TO USE THE AT COMMANDS TO COMMUNICATE YOUR WIFI MODULE TO ARDUINO, Why MQTT?, What are the types of Quality of Services?, Working of MQTT, Importance of MQTT, Applications of MQTT, Security in IoT with MQTT, data dissemination, lightweight and efficient, less power consumption, remote sensing and control, PUBLSIH AND SUBSCRIBE TO MQTT TOPICS, CONTROL AND LED WITH MQTT
Id: deIRY5NxXo4
Channel Id: undefined
Length: 21min 50sec (1310 seconds)
Published: Fri Apr 14 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.