Understanding MQTT using ESP-IDF: The Backbone of IoT Communication

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone asite from innovate yourself welcome back to another exciting video in the series of ESP idea from our today's video we are going to gear up a journey into the field of Internet of Things So basically in today's video we are going to start a journey with a protocol that is mqd now what is mqd mktd is message CU Telemetry transport Now what is MTD and what is this exactly and why exactly we're going to use MTD mgtd is a standard messaging protocol for internet of things or IOD it is designed for extremely lightweight publish or subscribed messaging transcod that is ideal for connecting your remote devices with the small code footprint and with the minimal Network bandwidth now just because of this reasons this mq is widely being used in different Industries and that is your Automotive your manufacturing your telecommunication your oil and gas Etc so there are many Industries which are already implementing or let's say which have already adapted this technology and they are actually taking the benefits of that so this is mqt now why do we use mq so now let's understand about that so why mq firstly because of its lightweight and efficient secondly it's B directional communication thirdly it can be scaled to million things or you can say million of devices can be scaled together right and it's reliable message delivery also it supports for the reliable networks so that it can provide the uh better connectivity between the broker and the devices and accordingly we have the security enabled which means you can have have a secure communication between the things or you can say between the devices so this is the reasons why you should go for the mqt now how does mqt works so basically in mqt we have three terms mainly being used and that is publish subscribe and the broker so basically you can understand it this way that we have multiple devices and we have one Cloud Server and here I'm considering basically three things first is your publish or let's say that is one device that will be able to send some data so any device that can send some data we can treat it as the publish so this is going to publish the message which means it is going to transmit the message similarly on the other hand we have a second device that cannot send the message but yes it can receive the message so this receiving of the message is called as subscribe so basically this is published this is subscribed now second thing or let's say one more thing that comes in here is where it is publishing from where it is subscribing now the connectivity between these two is your broker so your broker you can consider it as some server where you have your broker now this device this first device is going to publish to this broker now the second device is going to connect to this broker as well and also it is going to receive the data from ha or you can say it is going to subscribe the data from ha now right now I'm talking with respect to oneway communication but these two devices can be two-way communication as well which means publishing as well as subscribing in both the cases so this is how your mgtt works so this is just a simple architecture that I have just explained you right now I hope you have got a clarity on this and now you can proceed so that we can also do the coding stuff so that we can see the implementation of M with esp32 along with the ESP IDF now let's move to the coding stuff and let's understand how does the mg takes place and how does this comes into the picture so let's move to the coding stuff all right everyone so now let's continue with the code and let's understand that how are we going to work with mqt and what exactly we're going to do in there right and for that you can see on my screen I already have the project directory uh for the code is already opened and don't worry about the code because currently I'm using the basic mqtt code and I will provide you the link uh for this code in the description below so you can directly check that and you can directly use that code uh to uh like check and to implement uh with your project or let's say with your esp32 right so uh let me uh tell you what exactly uh we need in here and what changes you have to do so that you can make this uh like code work for you so let's understand that so over here you can see this is the main directory inside uh you have your appor main which is your uh main code in which you have everything right and uh I will not be explaining you completely but yeah I'll give you an overview about this so that you will get a Clarity like what exactly we're going to do right so let me quickly do that so firstly as usual uh what exactly we need to do is to connect to mqt we first have to connect to the Internet so that we can connect to any of the protocol and in our case which we are using is that is mqt so we are going to use message cute Elementary transport uh protocol so that we can have a communication using the Subscribe and publish terminologies right so we're going to do that so firstly over here that is what we're going to do we are going to connect to the internet and for connecting to the internet we will provide the SS ID and password and I will show you like where exactly we're going to add up that right so once we are done with this step after that we're going to start with this mqt app start this is the main main function in which we are just setting up everything that like uh uh what will be the configuration for your mqtt and uh what will be uh the event handler for your mq so that whatever events comes accordingly how your mq broker will take care or how your configuration will take care of that communication thing right and let me go to this function first of all so inside this you can see this is a BAS basic configuration that we are putting and currently just because we are not using the SSL thing that's the reason the configuration is very basic but when the SSL thing comes in uh for that we have a uh like bit change in the configuration and that we will see in the further videos so this is how we are going to do it and accordingly we have uh this small uh like changes over here like please enter URL of mq broker if in case you are going to enter it manually and in fact not manually uh with that I mean to say is when you are going to run this with respect to your command prompt or you can say into your terminal if in case you going to enter these details into your command prompt or your in your terminal in that case this is how you're going to do it right but in our case we are not going to use this way so obviously we don't have to take care of this because we are going to provide that into the configuration right and I'll show you like where to put that so in this way we are just doing it and over here this is the mqt client register event in which we are just setting up what is the client uh what is the ideal or let's say what's the event name basically and here this is the main thing in which we are setting up everything like what will be your mqtt event handler so that how your mqtt will respond when it is connected when it is disconnected when it gets the error and everything right so this is the uh main event handler let me go in there so over here you can see uh this is the event handler and inside this you can see we have already created the switch cases and inside the switch cases we have the different events when the m is connected what will happen when it is disconnected what will happen when it is subscribed what will happen when it is unsubscribed published if what is the event data in fact for this I can say that when we will receive the data uh what we should do and accordingly if the error comes what we should do add and in the default case this is what we're going to do right so this is the complete thing that we are going to do and let me explain you about the main uh events which we will be taking care of so mainly we're going to take care about this connected and the disconnected event so when it is going to connect to the mqt or let's say we can say when you are connected to the internet and now when you are about to connect to the broker so that you can uh do this publish And subscribe thing so this is basically that so when we are connected to the mqt at that time what we have to do so at that time what we want to do is we just want to publish or we just want to subscribe it whatever you want to do you can do that it's not mandatory that you have to publish And subscribe both at the same time it can be either publish it can be either subscribe it can be either both of them right so it can be anything right so in this way we are just going to uh call the events right that I am I'm going to publish it so for that you can see in this case we are just publishing it and where we are publishing it this is the topic this is the client who is going to publish it and this is the message that we are passing in here right so in this way we are actually going to just pass on the data right so in this way here we also have the other uh three parameters uh in which one is your quality of service what is the quality of service and one is whether you want to uh retain the message that you're sending or not so this is just a flag for that so in this way we are just going to pass this up in the similar way subscribe is the other one subscribe simply means that you're going to fatch the data or you can say when you're going to read the data if we talk in the simple term this is the meaning of your subscribe and publish is basically to transmit the data or you can say to send the data this is just a simple uh way of understanding publish And subscribe I hope you have got it right so this is what exactly is going to happen when you're going to connect to the mqtt and when it is disconnected this is what exactly we're going to do we are just going to display a message that mqtt event disconnected so this is the message that you will be getting when it is going to be disconnected and in the similar way we have rest of the other things and one last thing we can check is that when we are going to receive a data receive a data means whenever we subscribe to the topic at that moment we can get some uh data on that particular topic so this is what exactly is going to happen when we will get that data so basically right now what we are going to do we just going to print what is the topic and secondly we're going to print what is the data that we have received so in this way this is uh going to work right so this is the way like how your mqd code will work and I hope you have got a Clarity on the coding stuff now let's uh finish up the last step for uh the m and that is to set up what is the mq broker that we're going to use what is the SSID and password and what is the port for that uh particular broker in case you have something right so this is how we're going to do it and let me show you where to add that for that just simply open up the terminal and let me uh call the basic functions just to set it up so we are done up to this now the next thing is that we simply have to build it so just build it all right my mistake uh we don't have to build it first of all so firstly I have to Simply call this id. py and I have to call manual configuration so just simply open it up now over here you just have to set up everything serial flash you have to set up the flash size you know that already you can change that so basic functionality of basic configuration I'm not telling I'm straightforward going to the main one so for that just go to the example configuration and inside this you can see that I have this mq. Eclipse projects doio so this is the broker URL which I am using right now and in your case you can use any other uh broker or let's say the mity broker as well it's totally up to you so in my case I'm going to use this only so you can use that if in case you want to use something else you can put that URL in the format that is passed right now like mt call and double slash and uh the name or let's say the URL for your uh broker right so in this way we have to do it and next is your SS ID and password so right now you can see the SS ID and password I have already passed in here so that's the reason I don't have to pass it again so it's the SS ID and the next is the password so in this way I have this right so now I can go back and rest of the configurations I don't have to make any changes so that's it now just press Escape AP and just save the configurations once you done with the saving of this now then you have to Simply build it right so now you can build it up all right so it is built successfully now the next thing is that you have to Simply flash or you can say you have to Simply upload your code to the esp32 so in my case uh I have to first of all connect my esp32 to my system so then only you will be able to upload it so obviously in my case I have already uploaded or let's say Ive already connected my es32 to my system and now I'll be able to Simply upload it but just to uh confirm that is it actually connected to my system or not so for that I'll simply check up that uh is the ports available on my system or not and just because I'm using MacBook and especially for the Linux users also this is the same way like how you're going to use it uh so uh this is the way like how I'm going to do it but yeah in uh ubu system or you can say in the Linux system definitely this is not going to be the same thing like Dev in your case maybe it's USR or something so in that case like you will be able to check it like that right so uh I'll check simply like whether I have the port available or not so yeah you can see in my case I have the port available but yeah this is the one which is going to be used Dev /cu do USB serial hyen 001 right right so in this way that means I have it but yeah obviously if in case you don't know like where to get the port number from or like what is the port to which my esp32 is connected then don't worry I've already covered it in the previous videos as well then you can directly uh simply pass ID of. py Flash and uh I also want to see the monitor so I can simply pass Monitor and after that you don't have to pass the uh like port number like which Port you're going to use so you you can leave it so it will automatically take that one right so now just press enter so that it will simply upload it so it's uploading now it is connecting to the Wi-Fi so you can see my Wi-Fi was already like on and that's the reason you can see it is connected to the mqtt publish successfully subscribe successfully send successfully like everything like we're getting everything right and also you can see we are also able to receive the data but just because currently we're not passing anything or uh we are not sending anything from the mq broker side uh so that's the reason it is just passing like a normal thing but yeah we want the actual data now let me show you how you're going to do that for that you need some application or you need some kind of setup so that you can receive the data but currently I don't have the entire setup so for that what I have to do is I simply have to open up uh this mqtt broker in one of the application and the application which I will be using in my today's scenario or let's say in the today's video and that will be mq TTX so I'm going to use that application and let me show you like from where you can download it so if in case you're getting confused no worries I'll also leave a link in the description so that you can directly click and start downloading it as per your operating system right so let me go to the browser and let me show you from from where you're going to get it so let me go there so just simply Type mqtt X press enter so the first link that you will get is mqx this one so just click on this and over here you can see it is asking you to download it so just click on download and uh you can see according to Windows you can download the windows according to Mac you can download the DMG uh version as per your Intel or apple silicon or Linux or Docker so whichever you prefer or whichever operating system you're working with you can use that accordingly you can uh download it right so in my case uh I have to download this one so I'll use that accordingly but in my case I already have that installed on my system so I don't have to do it uh again so I'll simply open it up but yeah once you are uh you have downloaded that just install it and then just open it up right but now I already have it so let me open it up MTD X all right everyone so now you can see that uh I have the MDX open and over here you have multiple tabs like this one this one this one and so many right so basically right now what exactly we have to do is we have to Simply connect our uh broker over here right so for that you can see it is asking you for a new connection so you can simply click on this right so I'll simply click on this and now it is asking you to enter up the details right so over here you have to give it a name and you can write any name so I'll give it a name as ESP 32 and mqd like this I'm giving it this name and client ID you don't have to do anything just leave it like that only and over here you have to pass the broker so the broker which we are using is mq. Eclipse projects. so that is the one which we using so just simply pass that so just uh write it here so I have it here now now the port number is something like 1883 so if in case you have something else you can change that but in our case this is the one which we will be using so you don't have to change it and the next thing is username and password we don't have anything we don't have any SSL we don't have any security right now because we are just using mqtt not the mqts so that's the reason we don't have to do anything else so rest of the configuration rest of the options just leave it like that only no changes needed now just simply click on connect now wait for it to connect everyone can you see it is connected so it is connected and now we don't have to do anything so the next thing is that we simply have to pass up the new subscription so with subscription I going to say the topics we are talking about like what is the topic uh from which we have to like subscribe and on which we have to publish so this publish And subscribe accordingly we can pass it here right so right now I'll uh write it here now let me select what topics which I'm going to use in this case right so for that let me go there okay so right now on uh this code you can see we are using this and this as a subscribe topics so subscribe topics are this so basically subscribe for esp32 means publish on the other side on the other side means on the other device or on mqx I'm talking about right now right so currently I'm going to use these two topics so that I can send some message from there and I can show you that how I'm getting it over here in this right so let me go there now let me create a new subscription just simply write the topic first it's a quality of service zero uh rest everything leave it like that confirm now second topic also let me create quality of service one and confirm right so right now you can see that we have these two uh topics right and accordingly we have this okay so now uh these are the two topics uh which I have it with me right so these are the two now let me expand it so that I can accordingly show you as well okay so right now I have this first let me do one thing let me send the data on quality of service zero right so for that I'll have to pass it here and let me pass the data quality of service is zero and let me pass the data let's say hello I'm passing hello and currently you can see we don't have anything let me click on send unexpected token okay it's a problem with Json so it's not a Json data it's a plain text right so let me send it again you can see it is sent now go back here can you see hello I'm getting this and this time we are sending it over there here it is subscribing to that topic and here we are publishing that and let me expanded hello there my name is is Ashi s right let me send it can you see I'm getting this right so this is about the quality of service zero similarly let me change it and let me check it on the quality of service one and uh let me send the extra message from innovate yourself let me send it now you can see quality of service one you can see here I'm sending it but here you can see I didn't receive received it and why I didn't received it the reason for that is that quality of service one has been unsubscribed that's the reason quality of service zero is working but quality of service one is not working because it's already unsubscribed right so this is how exactly you can uh simply create your own topics and accordingly you can communicate with that using the publish and the Subscribe with respect to the broker right so this is all about the mqt and implementation of that I hope this was clear to you but still if you guys are facing any difficulties feel free to leave a comment below in the comment section and if you have liked this video do click on the like button and if you're new to this channel then feel free to click on the Subscribe button so that whenever we'll upload a new video you will get a notification and you don't miss any useful content uh from our channel right so this was it so see you in the next video till then bye-bye and happy learning
Info
Channel: Innovate Yourself
Views: 167
Rating: undefined out of 5
Keywords:
Id: CE7DflA0jW4
Channel Id: undefined
Length: 24min 32sec (1472 seconds)
Published: Fri Dec 15 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.