MQTT Protocol with NodeMCU ESP8266 Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
let's learn how to use impurity protocol with node mcu esp8266 in the end we will control led connected to node mcu and monitor a temperature sensor data using publish and subscribe mechanism of mqtt protocol this video is sponsored by altium now you can use rtm pcb design with octo parts octopods offer you most up-to-date part data like specifications data sheet cad model right into the design environment so try octoparts and rtm today with the link given in the video description and i'm sure you will enjoy working with altium and the octopods our nodemcu doesn't know mqtt protocol so we have to install the mqtt library so we have to go to sketch include library and then we have to click on manage libraries so once we click on manage libraries it might take couple of minutes or seconds and then the library manager shows up and then we have to search pub sub client and then hit enter and you will have to find pub sub client by nick o'leary so somewhere in a search section you can see this is the pop sub client by nick o'leary and by default it takes the version the latest version of the mqtt library we don't uh we don't have to worry about the version of the uh mqtt library that's a pops up client so just click on this install button and this basically install the mqtt library for uh nodemcu esp8266 so once the library is installed we have to close this library manager and now we can go to file examples and we have to scroll down till we see pops up client and then under pops up client we can also see mqtt esp8266 so this is an example project so let's click so it's open up and now we can use this example project as a reference point for us so i'm just going to resize properly and then you can see here's an example project and what i'm gonna do is i would just remove those commands because the clean code will be much better when i explain now you see this esp8266 wi-fi dot h header file is not detected it shows in a black font and that is because we have to go to tools and make sure the board we have to select uh nodemcu 1.012e module the moment we select the right board you can see the color changes and now our arduino ide detect the wi-fi header file and also the mqtt client library which is in pubsubclient.h that's the c library for node mcu esp8266 it's an mqtt library now since nodemcu is the wi-fi device we have to put the ssid and password so over node mcu will get internet connectivity so in my case uh in a double quote my ssid is binary update so i would going to put the ssid that's the name of my internet access point or maybe a hotspot whatever internet that you want to use and then there will be a password and in a double quote we have to give the password it's the best courses in my case and then of course we need a mqtt server and mqtt server that we want to use and i would not suggest you to use this broker.mqtt dashboard.com because this is very common server mqtt server used in every other mqtt project ready example code and if you upload the same code then it will be a problem so we will find a new mqtt server and for my favorite mqtt server just open a command prompt and as uh as i said you know we need a server so to get the server we will say ping test dot musk q2 dot o rg this is the free uh to use mqtt broker or mqtt server i would say mqtt server so 5.196 uh dot 95.208 so this is an ip address that we will use so i will just copy and then i don't need this anymore so just close it open my arduino project we will have to put in a double quote ip address of the server it's a 5.196.95.208 or you can in fact write test.mosquito.org that's absolutely fine so let me just comment this dot q2.org so this is the server mqtt server so if you want to know more about the server you can just copy and uh maybe just put on a browser and let me just show you if i can open chrome for you so google chrome and just put and this is basically the information about the mqtt server we are using it's a test.mosquito.org and you can see the port is 1883 for mqtt unencrypted but it doesn't matter we don't need this anymore this old things we need this ip address of the server and we get it either you can put this ip address or you can put the name of the server both are absolutely fine now if we scroll down further then we can able to see that there will be a topic and you can see we are using a built-in led which is uh basically connected to gpio2 on nodemcu esp8266 so when we publish a message one on a topic that is basically in topic so if you scroll down somewhere you can also see client.subscribe to in a double quote we have in topic so i would rather make it more readable i would just say device slash led for this is the topic to control the led which is uh onboard led gpio2 on node mcu esp8266 so what this code basically does is whenever we publish uh the message one on device slash led okay this is the topic that nodemc will subscribe then what happens is basically the led turns on now it will generate a logic low but it's given here a reason why when we publish uh message one it makes the logic low and logic low basically turn on the built-in led because you can see built-in led is active low on esp 01 okay so logic low will turn on the led and logic high will turn off the led so when when you publish message one uh on device slash led topic then the led turns on so this will light up the led and if you publish let's say a message zero on device led topic then the led turns off because it generates a logic high on built-in led that's just gpio two pin so that's basically how we're going to control the led on node mcu esp8266 so if you scroll down further then you can able to see there is an out topic and for out topic to send a sensor data i would rather give the name device slash temp for temperature and then i would just say that mqtt server is connected okay so this is basically just a message it will throw even if you comment this line of code it really doesn't make that much sense but the real magic is happening here in the loop function so here in a loop function what is happening is send this message hello world and this value keep incrementing so the value variable keep incrementing and it will basically publish those messages on out topic now our topic we have to change device slash temp because uh later on we want to send a temperature data now if you uh if you just want to modify this code for sending a temperature data that is connected to nodemcu then i would rather look at this value variable okay and this is a global variable so look at this integer variable value is equals to zero and what i'm gonna do is because we have connected an lm35 temperature sensor on a0 pin rather modify this code and i would say value is equals to analog read and i would say a0 and because we have connected a temperature sensor lm35 why not we should multiply 0.32 that's basically converts the temperature in degrees celsius and it will be stored into this value variable and then it will print every time onto the serial monitor so i would just make this temperature is and then it will make the pound sign and then it will just throw the number whatever the number is i mean if you just want you can just put double dots and i think looks absolutely okay i mean this this part of the code basically will publish the temperature to the mqtt server and uh so nodemcu will send a temperature data in degree celsius to the mqtt server which is um this server on the top we said 5.196.95.208 and then as far as the control goes the led control goes device led so this is the topic that we have to subscribe so when somebody send from server one then it will turn on the led and when somebody publish a message 0 on device led topic the led light up that's how you know node mcu will going to control the led and send and publish the sensor data on device slash temp topic so device led topic is for controlling and this device slash temp is the topic for monitoring that means we want to send a temperature data now i already have connected nodemcu the laptop but we need uh mqtt client for this and one of the most simple client that i want uh you to install uh of course we need this to be on a google chrome so we have to install the mqtt client that's basically an mqtt lens so i will search mqtt lens to install and we get to see this google chrome extension so this only works with google chrome you have to have a google chrome for that to keep it simple we will going to install this mqtt lens so click on mqtt lens and it will take you to the google chrome app so just click on add to chrome and this basically quickly installed it asked us to log into our google account so we just have to click on add app and then close it and it's basically downloading in lower left corner you can see so it's adding basically to the chrome and now you can see mqtt lens shows up so when i click you can see the software opens up right so this mqtt lens is the software or the mqtt client that we will use in order to control the node mcu or esp8266 so i will take the edges and fit in properly into the screen so everybody can see every corner of the mqtt lens when we will have to configure the mqtt lens so click on this plus button that's basically to create a connection so it asks us to create a connection name and you can give any name i would basically give the connection name as a binary updates and we need an ip address of the server rather i would go to my code and i would copy this ip address i would prefer all the time to to put the numbers of the server rather than the name so just copy the ip address of the server just paste it here under the host name so we want this mqtt lens as a mqtt decline to connect to this mqtt server on 1883 which is an mqtt port um and then we can scroll down we don't touch anything you don't need to do any advanced settings we might talk about the settings a little bit later in another video just right now click on create connections and you can see connection is created it says uh the green color um you know i can if you hover over your mouse it says connect it so if it stays red or some other color that means something is wrong and something is not up to the mark so what i'm going to do is i would just keep this mqtt lens opened up okay just maybe make it a little bit small so you can probably see one corner i open mqtt lens and i would open the arduino ide side by side so you can see what's going on here and basically uh because the code is ready go to tools make sure the port is selected properly com4 and board we already have selected nodemcu that's basically 1.012e module and we can just upload the code and see if we can able to uh monitor the temperature sensor data and we can even control the led which is a built-in led on board led on node mcu which is a blue color led i hope you know if you don't know if you don't know the basics then you probably go back and check our other videos uh about nodemcu we have a complete series of video lessons and they are very effective you would get to learn a lot of things about iot and nodemcu and uh about mqtt protocol also so i have given a link in the video description where you can learn more about mqtt protocol so the code is uploading you can see in lower left corner it's still uploading the code so we have to wait till we see done uploading it takes some times a little bit of time and you can see now in the lower left corner done uploading that means the code is uploaded we go to tools and just open serial monitor now the bot right here is fifty two hundred so just select the right baud rate here eleven fifty two hundred and once we select eleven fifty two hundred now you can see the data starts coming up publish message now how can you confirm that nodemcu esp8266 is connected to internet so just press the reset button on nodemcu and then it says connecting to binary updates that's our access point that is our internet and now it says wi-fi connected and ip address is caught by nodemcu and attempting mqtt it's connected and now you see the data start streaming i would just minimize the arduino ide and i would just keep it open uh the serial monitor and on on a subscribe section you remember in our arduino code just look at the arduino code and we have this topic device slash temp right that's where our sensor data is coming so basically we go to subscribe section on the mqtt lens just right click and just paste device stamp and then subscribe and now i can just make it a little bit long so you can able to see now the temperature data starts streaming you can see this count is increasing when in fact look at the timestamp and the temperature is coming as 27. now if i rough the flat surface of the sensor so let me rub the flat surface of the sensor and you can see the temperature starts rising up you can see on mqtt lens it's 28 now it's 28 and in mqtt and in serial monitor also you can see the temperature starts rising it's 31 now and on mqtt lens also it's coming as the 31 so it's working fine right now let's check to control um the led so if i uh take the uh topic for led and uh if you remember we have the topic which is uh basically a device slash led and if we publish the message one so we have to publish message one on device led topic make sure you don't press enter or something unnecessary we have to send one only one right and so we just write one because we have to publish the message one on device led and then click on publish and now you see the message arrived is one and the led lights up you can see here on the hardware side on serial monitor we we get the one that means led is on now when i send zero when i publish the message zero then the led goes off and we receive here the message zero right so to control the node mcu esp8266 using mobile app we have to install this android application called my mqtt you just open this app it gives you this kind of view that choose what to allow mqriti to access on a lower right corner there's a continue button we click this continue and it just gives us a pop-up we say okay and now you can see here we have on the top left corner there's a dashboard and there's a back icon so when we click on that it takes us to the settings and you see there's a setting button here so i click on setting and then you see we have to fill up quite a few parameters now the first parameter is the broker url that's basically an ip address of our mqtt server and that this mqtt uh client that is my mqtt should connect to so nine 5.196.95 dot two zero eight this is our mqtt servers ip address and the down below there is a port number so the mqtt port is 1883 and the username and password we don't have to fill in anything just click on save and then once it is saved it says save settings successfully connected and in the top left corner there's this icon back icon click on the back and then you see it turns out to be green and servers iphone server 5.196.95208 i think we have to take our hardware so here we have a node mcu and this is on board led i would just click on preset button once before we publish and then we will go to publish in the mobile app and then here on a topic section i would going to put my topic it's a device slash led and then in a message i enter 0 so i enter 0 so publish message 0 on device led topic and click on publish and you see the led on the board goes off right so that's basically how you turn off the led now if i publish the message 1 and if i click on publish you see the led lights up on board led i hope you can see this led lights up right so if i publish zero back again the led goes off if i publish the message one again the led lights up now if you want you can connect external led so i connect this external led to d4 and ground and that's basically the other way around right so maybe i would publish zero so that's basically turn off and you see the led lights up external led lights up and the built-in led goes off right so if i publish let's say message one so that's basically turn off the external led and turn it on the built-in led but it doesn't matter this led is not that not that much important now let's check the subscribe so we have to go and click on top left corner this back button and then there is a subscribe section so click on subscribe and once we go into subscribe there's a topic there we will type device slash that's the topic where we are getting temperature data and then i click on this add button here and you see the topic is subscribed and added so on the top left corner i click on back and now on the dashboard if i click on dashboard you see the data start streaming so 27 degrees celsius right so if i cut up the sensor look at this i am holding the sensor flat surface and rubbing and you can see the temperature start increasing now it's 30 now it's 31. if you are new to mqtt protocol and iot then don't forget to watch the other videos the link is given in a video description believe me they are awesome i hope you found this video educational and entertaining we'll see us into the next video lesson bye bye for now
Info
Channel: BINARYUPDATES
Views: 89,146
Rating: undefined out of 5
Keywords: MQTT, ESP8266, NodeMCU, Protocol
Id: c1st5cVRRzo
Channel Id: undefined
Length: 20min 11sec (1211 seconds)
Published: Sun Jul 03 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.