ESP32 BLE Data Exchange Tutorial with ESP-IDF: Menuconfig and Code Implementation Explained

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone this is Ashish Sunny from innovate yourself welcome back to our Channel today we have got something super exciting lined up for you we are diving deep into the world of esp32 and ble connectivity with ESP IDF but don't worry we are going to break it down into easy to follow steps all right folks first thing first what's esp32 you ask well it's a powerful and versatile microcontroller that's perfect for all your iot projects and when we talk about ble we are talking about Bluetooth low energy the magic that allows your devices to talk wirelessly without draining your batteries today we are going to walk you through the entire process of connecting esp32 with ESP IDF and implementing ble but don't worry if you are new to this we will take it one step at a time we are going to get Hands-On with some coding and I promise you it's not as intimidating as it looks we will explain every line of code so you understand how it all works once you are done with the coding part we will connect our esp32 board to our smartphone you will see how we can control and exchange data between the devices seamlessly by the end of this video you will be a ble Pro and you will have a solid grass of esp32 and ESP IDF plus you will have the knowledge of creating your own awesome iot projects so what are you waiting for hit the like button subscribe to our Channel if you haven't already and let's get started with a fantastic journey into the world of esp32 ble connectivity get ready to barely connect like a pro let's jump into the Fantastic world of esp32 with ESP IDF stay tuned all right everyone so now it's time for us to further start with the coding stuff and to see like what application we are gonna use with the project because uh currently in our today's scenario we are just gonna start with the communication process which is about the ble which is Bluetooth low energy so in this basically what exactly will do is we will initialize our esp32 as the Bluetooth module so that we can connect our smartphone to the esp32 so that we can accordingly make a connection or let's say we can make a a successful connection between them so that we can exchange the data this is what exactly we have to do and for that we'll need one application that I'll show you like which we will be uh basically downloading from the play store so that you can download and you can send and receive the data right so this is what we are gonna do and just because uh it's just the process of ble so we don't have any hardware stuff or we don't have any hardware connections to be made so that's I am directly starting with the coding stuff and also don't worry about the coding I will leave a link in the description about the coding because I already have that on my git repository so let me open up that so that I can start with that uh code stuff right all right so you guys can see that uh this is a git repository or uh the page where I have the code with me right so this is that uh git repository right so I'll do one thing I'll simply copy the code from here and I'll simply uh add it to my current directory where I'm gonna show you like how we will do uh do the programming stuff and how we will upload it to the esp32 module right so let's see that now right so let me do it quickly thank you all right everyone you can see now I have the code over here right so everything is here so I'll open up this main.c right so this is what I have and right now you can see I'm getting this error so let me quickly add that uh link as well which I've already shown you in my previous videos as well right so just let me do it quickly okay all right so I have added that and now you can see the errors are gone right so basically this is uh our code I hope you guys are already aware that esp32 is having a inbuilt ble and your Wi-Fi module right so you don't have to interface that externally so we can use that directly so in our today's scenario we're gonna use ble which is already there so I'm gonna initialize it as a ble so that your esp32 will be exposed as a ble device so that it will be discoverable uh to your smartphone and you can accordingly connect it to your smartphone and one more thing is there like we are going to initialize it as ble so that means your ble or your esp32 will not be visible to you on your normal Bluetooth uh interface or let's say where you actually check for the Bluetooth devices it will not be available there just because it's a ble so we need an application in which we can actually check for the ble devices only right so this is the way like how we're gonna do that now let me do one thing let me explain the code one line by line so that you will understand everything right so over here you can see like uh firstly we have given it a name like ble uh server so we have created a variable for that and accordingly we have initialized one ble address type as well right and over here let me give you a overview first right yeah all right so as for my previous videos I hope you guys are already aware that app underscore main is the main function in which you actually start writing up the code because this app.app underscore main is the function from where your program actually starts so this is the starting point and inside is what exactly I'm doing is I have simply written up the points I have simply called up the functions and in fact in front of that I've also written what exactly these uh codes are doing right so firstly it's nvs underscore flash underscore init which is actually initializing your nvs flash right and the next is we are also initializing this uh Nimble underscore Port underscore init so that we can initialize the host stack for that and one more thing you can see over here like it's ESP underscore Nimble underscore HCI underscore and underscore control underscore init and I have commented that out and why I have done that because in the previous versions or let's say if you will explore on the Google or you know on different website side so you will find out like some of the websites may be using this command as well which is no longer required to be added to your code so that's the reason I have simply commented that out and also you don't have to write it so you can comment it or you can delete that line that's your uh call right but uh we can leave that and the next is we have initialized this and after that we have to start writing up the Nimble configuration and the one which I'm using right now is your ble and in fact that ble is your Nimble right and in fact we have other types as well I will show you like what other types we have but in our case we are going to use Nimble configuration so basically what exactly I'm doing here is first of all I'm setting up the name like what will be the name for my ble device whenever I will search my esp32 uh for Bluetooth in my application so this is the name that will be shown over there if you want to change the name you can change the name according to your ESP module so you can change it and you can check that accordingly and the next is that we are initializing the a gap server over here or let's say the Gap service over here which is basically your generic access profile so this Gap is your generic access profile uh that is what we are initializing over here and after that the next thing is that you have to initialize your Gat which is your generic attribute profile so this is what exactly you also have to initialize for nimble and the next is what configurations you are setting up here and you can see like I have written this Gat underscore svcs now what is this and where it is basically this is basically the profiling that we are doing over here right in fact this is all that we are deciding over here here you can see that I have uuid I have the flag for that I have the access CB for that or in other words you can say what function will be called uh when I will be trying to access them so basically these are the different profiling that I am having in here uh According to which you will be either reading the data either writing the data either getting the notification or or either you are doing something else so this is what exactly we are doing and also for each of the service we have decided or we have declared one uuid which is this one 0x fef4 0xdead so this is the way like we have it and in other words you can say like you can add on few more services in case you want to add more further which I will be showing you in my further videos so this is the initial one which I will be showing right now right so this is the same thing which we are doing right now and this is a configuration for Gat service and this is for the queue service right so this is what exactly we are doing in here and after that we are just initializing the application so this is the way like how we are initializing it so it's ble app underscore sync which is this one like from here we are doing is we are just determining the best address type automatically so that we don't have to specify it uh by ourselves right and after that we are just defining the ble connection and that is gonna be done through this function right so this is what exactly we are doing and we are just setting a bit accordingly right so this is all about this and uh after that we are just running up the thread so that we can accordingly uh make the connections or let's say we can so accordingly establish a connection between your ble device right so this is what exactly we are doing and apart from that uh there is other things that you also need to know like over here you can see in the profiling part I showed you that there are two services that I have created for which I have assigned uuid one is this one and the second one is this one so the first one is for the read function in which I'll be doing the reading operation and the second one is for the writing operation so here this device underscore read and device underscore write are the two functions that I have created right here in which I am deciding on in which I am writing what exactly I want to do from that right so basically you can see like this is the right one and this is the right one which is according to your application right means the writing will be done from the application side so this is the way like it is written so when the writing will be done on the application side we will read the data and we will be doing some instructions or we will be doing up some execution based on that so just for the demonstration purpose what I have done is I have taken up the messages from the application side and accordingly I have just simply print that message like what exactly I have in here I'm just doing the comparison and I'm accordingly printing that message again so this is what exactly I'm doing but in the similar way what exactly we can do is we can perform the executions like whether we want to turn on the light whether you want to turn on the LED or whether you want to send something or whether you want to do anything else so this is what exactly will be done so this one is for the writing operation similarly in the reading operation what exactly will be done is once you'll establish the connection it's like we are going to send the data from your esp32 to the application side and this thing will be done or this thing will be achieved over here and in fact you can see we have set up the flag that we are going to do the reading operation here we set up the flag that we want to write the operation right so this is what exactly we have uh set up over here right so this is the way like how we have done it and when you will try to read the data for this you will get a data from this site right so this is what message you will be able to see on the application side so this is the way like how we are doing it and apart from that there is one more thing is there and that is your event handler and that event handler is basically for your Bluetooth device so basically when your Bluetooth will be connected what it should do when your Bluetooth will be disconnected what it should do when your Bluetooth will uh like event advertisement will be complete then what it should do so in this way we have uh the three uh cases for your switch case or let's say for the switch statements so when your Bluetooth will be connected at that time it will simply display the message say something like this when your Bluetooth will be disconnected it will display the message like this and where the message will be displayed in the serial monitor obviously right so this is what exactly will be done over here and I hope now you are able to understand what exactly I have done in the coding stuff but still if you feel any difficulties in understanding any of the things just feel free to leave a comment below in the comment section so that I can check that and accordingly try to help you out as soon as possible now when you are done with the coding stuff our next step is that we have to upload a program to the esp32 so that we can accordingly upload it and we can further check the things in the application side so first let me upload the program and then we will start with the application part so first let me connect my esp32 to my system so you can see I have connected the esp32 to my system and it is asking me to allow it I just allowed it now simply open up the terminal so you can see the terminal is opened now here what exactly you have to do is as usual you first have to build the program and then you have to flash that program I'll also start up the monitor with that so I'll write all the three commands in just one line so let me type it okay before that we have to do one more thing just because right now I'm inside this esp32 underscore ble which is this one but the code is inside the another directory so I'll have to first of all switch to that so I'll go inside now okay before this one more thing I'll have to add up and uh which I forgot to tell you and that is that what is this Nimble which I'm adding over here and how it will affect our program right for that we need to make the changes in our configuration right so let me do that and for that you have to Simply open up the menu config first of all all right everyone so you can see in here I'll have to first of all set up those basic thing flash size I've already changed that to 4 MB similarly you'll go to config uh let's say the component config and inside this you have to go to the second option which is Bluetooth so just go inside and here you can see in the Bluetooth section I have the host and in this host I have two options one is blue uh blue droid which is in the Dual mode and the second one is Nimble So currently we are using ble only so that's why I have changed it to Nimble right so you this is the changes that you have to make according to your configuration so just make this uh like selected I have already selected that and accordingly you can see you have further more options for nimble so you can go inside and you can change them accordingly but currently I'll not make any changes in this so you can leave that as it is for now right so just go back in the controller option you have the option if you want to check you can check but I'll not record you to make any changes right now right so you can go back and you can proceed further and one more last thing is that you have to go to this ESP settings uh where it is thank you yeah in here and just change the CPU frequency to 240 megahertz so just change it and we'll uh configure accordingly right so now all the changes are made now just go back and uh everything will be saved right so in my case it was already there so that's why it's not making any changes now it's time to write that command again build it and Flash it all right so I have written the command now just press enter so it will start uploading and building the program right once you make the changes to your configuration it takes time so first it will take the time to build it and after that it will uh flash your program to your esp32 all right guys so now you can see the program is uploaded successfully now it's a time to further check like what application we're gonna use and whether our device is available on that application or not so now let's see the application which we are gonna use uh so guys you can see uh here is my screen like my Mobile screen you can see right over here I'm gonna open Play Store so I'll simply go in here and I'm gonna check for an RF connect so this is the first one I'll just click on that so now you can see like here you can see this option NRF connect for mobile I'll open this up So currently you can see in my case it's already installed so that's why it is asking me to uninstall it or to open it so basically this NRF connect for mobile is uh like the application from Nordic semiconductors so you have to install this one right so just install it and now I'll open up this up right so just open it just go to scanner now over here like you can see this is the area where uh you can see the devices will be shown to you right so currently you can see it is asking you to scan it right you can click on this or you can simply uh scroll down and it will simply do that right so I'll click on scan for now so you can see like this is the name that we uh write for the esp32 in the program right ble server right so this is the one which we have to use so right now I'll just click on these uh I mean this ble server and here you can see the device information is also given to you so device type classic and Le so advertising type is legacy and all the information you can have in here also you can click on this raw and you will be able to see further more information about this as well right so this is uh all the information about this but we don't have to do anything with that right now so just click on this three Dot and here it is asking you for the options to select either you can Bond it either you can connect with preferred phy or you can connect with auto connect so I'm gonna connect it with auto connect so just click on the first option on your screen you can see ble Gap event connect okay so you can see this is the option this is the message that we are getting and that message is coming from this uh where is it uh over here yeah this ble Gap event right so this message you will get ble Gap event connect and whatever is the status that is okay for now and that we are going to get when the device or let's say your Bluetooth device is connected to your smartphone so this is what we have got right so in this way we have got it now suppose I want to disconnect it so I'll just simply click on this disconnect and you can see ble Gap event disconnected and that is coming from here right so in this way you can simply connect and disconnect your ble esp32 basically right so this is what we have now I'm gonna connect it and I'm gonna show you like how we can simply uh communicate with this right so this is what we're gonna do right now uh maybe it's taking a bit time right it's okay sometimes it can take time as well right uh okay no issues maybe we are using the basic code so that's the reason it is not able to connect again so we can update that later on right so for now let me uh like uh reset this right okay it's reset and uh now you can connect it okay so you can see ble Gap event connect okay right so this is what we have and now I will show you like how you can send and receive the data right so you can see here in the client section you have three options so let's say you can have uh you can see the three different sections that we have created in this profiling part right so let me go in here okay over here you can see this is the profiling part where we have added all the stuff right so this 180 which is like one eight double zero which is the first one this is what you can see which is uh what we are having in here sorry not this one uh it's this one basically unknown service right so in here you can see we have created two characteristics and those characteristics are created with uuid which is this one fef4 and dead so these are the two fea4 and the ad so these are the two now as I said that this fef4 will be used to read the data basically you will click on this option which is uh you can see over here when you will click on this you will get a data from the esp32 site so basically esp32 will send you a data which you can read on this uuid right so let me click on this and you see data from the server and this is what we have passed over here data from the server this is the message that we passed right so this is the message that we are getting in here now this is about like when you want to read the data now suppose I don't want to read the data I want to send the data send the data from the application side which is the this one which we are going to use right now right so now for that you can see we have the second uh unknown characteristics which is having the uuid as 0xdead which is this one and that is this device right now just click on this option and now you can see it is asking you to enter a value right so right now I don't want to send the data in the byte array or byte format I want to send it in the text format so I'll click on text and now I will pass the values now for showing you that I'll go in here because here I have mentioned or here I have passed some data on the basis of which I'll be getting the message so basically what I've done here is I'll be simply uh passing let's say light on if I'll pass light on I will get this message if I will pass the other messages I'll get this and apart from all these conditions if I'm not passing any of this then in that case you will get the data like this data from the client so for example initially I want to send let's say innovate yourself right so this is the message that I'm passing and that is not in any of my condition right so I'll send this now you can see in the serial monitor I'm getting a message data from the client data from the client and whatever is the message you're getting that message over here and that was innovate yourself right now this is how we can simply send the message now similarly let's say I want to pass some different message this time and that message will be for example let's say uh fan on for example let me pass that the fan on right now let's send it so you can see the messages data from client and the message is fan on right so in this way I am getting it right okay so you can see in this way you are able to get the message uh fan on right now for example I want to send something else let's say light on for example I'll send this okay the spelling was wrong right so that's why it's like that so let me pass it again light on [Music] light on and there is no further spaces right send it okay so everyone you can see this this how we are able to send the message and how we are able to receive that message right so now I hope all of you are able to understand like how we have simply created our esp32 as a ble device and also how we have connected our smartphone to that ble esp32 so this is the way like how we have sent the message and how we have received the data or in short you can say how we have exchanged the data between the smartphone and the esp32 using the ble which is Bluetooth low energy right so that was all about the ble connect I hope I was able to make you clear about all the concepts uh like properly but still if in case you are facing any difficulties in understanding any of the concept feel free to 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 do click on the Subscribe button so that you do not miss any videos from our Channel and also you can press the Bell icon if you want to get a notification whenever we will upload the video and do like share comment subscribe and do whatever you want just we need support so that we can grow a community and we can serve you better so that's all from my side see you in the next video till then bye bye take care and happy coding
Info
Channel: Innovate Yourself
Views: 1,493
Rating: undefined out of 5
Keywords: ESP32, BLE, ESP-IDF, IoT, Tutorial, Bluetooth Low Energy, Menuconfig, Tech Explained, IoT Projects, ESP32 Tutorial, ESP32 BLE Tutorial, ESP-IDF Tutorial, Electronics, Wireless Technology, ESP32 Projects, Electronics Projects, Coding, Bluetooth Technology, Wireless Data Transfer, BLE Data Exchange Tutorial, Learn ESP32, ESP32 Development, ESP32 Microcontroller, Tech Education, ESP32 Configuration, Beginner-Friendly Tutorial, Technology Explained, ESP32 Bluetooth Communication.
Id: EIo5aZ3c89Q
Channel Id: undefined
Length: 26min 10sec (1570 seconds)
Published: Fri Sep 08 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.