Bluetooth Classic & BLE with ESP32

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
today in the workshop we're working with Bluetooth on the esp32 we'll see how to code servers and clients for Bluetooth classic and ble we'll also learn how Bluetooth works and how to select the perfect esp32 board for your Bluetooth project it's a technology named after a king so welcome to the workshop [Music] [Applause] [Music] well hello and welcome to the workshop and today we are working with our old friend the es32 and the aspect of the esp32 that we're going to be focusing on today is Bluetooth now I don't need to introduce Bluetooth to you I'm sure you have a number of Bluetooth devices you may even be using Bluetooth devices to watch this video a Bluetooth mouse or Bluetooth speakers perhaps Bluetooth allows short range connectivity between different computer peripherals it can also be used for iot devices for wearable devices for medical devices there's a variety of things you can do with Bluetooth and being able to connect the Bluetooth devices or to connect to esp32s via Bluetooth can open up a number of doors and a number of new projects for you now we're going to be dividing this video into two different sections today the first section is going to be about Bluetooth which is now called Bluetooth classic and the second section is about a newer form of Bluetooth one that came out with version 4 and that is Bluetooth low energy or blle and so basically we're going to learn all about these two different forms of communication we are going to see how we can code the esp32 to use these and after watching today's video and looking at the accompanying article you should be able to work with Bluetooth in your projects so let's start by learning a bit about Bluetooth and blle Bluetooth and ble are a series of communications protocols used to Interlink computers smartphones tablets and other peripherals it finds use in Internet of Things applications as well as in the creation of a personal area network of interconnected devices Bluetooth was the brainchild of Engineers at the Swedish firm Ericson who in 1994 set out to find the method of interconnecting computer devices wirelessly in 1998 Ericson joined with IBM noia and tashiba to form the Bluetooth special interest group the group that sets all of the Bluetooth standards Bluetooth itself was named for the 10th Century King Harold Bluetooth gormon who also performed an interconnection in his case he connected the kingdoms of Denmark and Norway further respect is paid to the king in that the Nordic symbols for H and B his initials are the Bluetooth emblem since version 4 Bluetooth has been divided into two different categories the first is the standard Bluetooth now named Bluetooth classic it can also be called Bluetooth brr this is for basic rate enhanced data rate bluetooth classic is used for high bandwidth applications such as streaming the other form of Bluetooth is ble or Bluetooth low energy this was originally called bluetooth smart the this is not compatible at all with Bluetooth classic the main advantage of ble is its very low power consumption making it useful for iot devices and for wearable devices Bluetooth classic operates on the unlicensed 2.4 gahz industrial scientific medical band Bluetooth low energy also operates on the 2.4 GHz ISM band in Bluetooth classic the band is divided into 79 channels in Bluetooth low energy it's divided into 40 channels Bluetooth classic has a 1 mahz spacing between Channel whereas Bluetooth low energy has a 2 mahz Channel spacing in Bluetooth classic 32 of the channels are used for Discovery Bluetooth devices signal on all 32 channels Bluetooth devices also scan constantly for other Bluetooth devices in Bluetooth low energy only three of the channels are used for discovery these are called the primary advertising channels Bluetooth low energy devices do not continually advertise instead they advertise in intervals in order to save power pairing in Bluetooth classic is initiated manually Bluetooth classic devices share a secret link key this key can be established with a pin or a secure simple pairing SSP Bluetooth low energy uses bonding for trusted connections between devices devices share a long-term key and this eliminates the need for constant pairing protocols include just works Pass Key and oob in Bluetooth classic there is a continuous connection between devices Bluetooth profiles are used to determine the interaction between devices and we'll be taking a closer look at profiles in a bit Bluetooth classic will allow for data transmission and streaming Bluetooth low energy on the other hand uses intermittent connection to save power it uses one profile called the generic attribute profile or gat b makes use of services and characteristics and we'll be dealing with these later in this video Bluetooth classic is suitable for high-speed data applications it's good for applications where you need continuous data such as streaming it also exhibits a very low latency which is useful for audio applications Bluetooth low Energy's main feature is its very low power some devices can operate a year or more on a single battery blle is best used for short data bursts as opposed to continuous data blle devices can also be configured in a broadcas or mesh configuration today we'll be working with the espressive ESP 32 a microcontroller that supports both Bluetooth classic and blle the ESP 32 is an inexpensive and highly available microcontroller it has multiple IO ports including analog and digital ports it also supports programming in C++ or in micropython for blle only internally the esp32 has 1 2.4 GHz radio module this module works on the industrial scientific and medical band from 2.42 to 2.48 GHz the sp32 contains a Bluetooth controller and baseband it also contains a Wi-Fi media Access Controller and baseband the esp32 has a coexistence mechanism that can allow you to build applications that use both Bluetooth and Wifi if you wish to make use of the coexistence mechanism you'll require the ESP integrated development framework to develop with it today we'll be using the Arduino IDE to work with Bluetooth and blle on the esp32 so let's get going and here are selection of vssp 32 modules that you could use for some of your Bluetooth experiments now you'll note there's quite a variety of modules here and this is just a small sampling of things you can get you'll see that we have modules that even have cameras and displays on them there are some very tiny boards and these are great for working on selus bread boards and there also great for packing into small projects I've also got some larger boards and they've got a lot of IO ports on them you might find some of them a little wide for a standard selus breadboard but there are breadboards that can accommodate them and they bring out all of the io ports and the esp32 now uh you'll also notice a few of the boards have internal antennas and some of them have external antennas and for external antennas you can get tiny ones and you can get very very large ones and of course you can boost the performance of your Bluetooth by putting an external antenna with a very good performance on it but if you want something small internal antennas are the way to go now another thing about picking a board though is that not all esp32s are the same and not all of them have the same Bluetooth capabilities so right now let's go and take a look at the different versions of Bluetooth and the different capabilities of the different esp32 models there have been several revisions to the Bluetooth protocol since its Inception in 1999 Bluetooth 1.0 was just for wireless data transfer and had no audio streaming functions in 2001 Bluetooth 1.1 introduced audio functions intended for headsets Bluetooth 1.2 introduced in 2003 provided support for stereo audio Bluetooth 2.0 introduced in 2004 included functions for the audio video remote control profile allowing for multi-function bluetooth remote controls Bluetooth 2.1 which was introduced in 2007 contained many security enhancements and also introduced SSP a method of not using a PIN number to mate Bluetooth devices Bluetooth 3.0 was introduced in 2009 and its main feature was Bluetooth highspeed this allowed for integration with an existing Wi-Fi network for approved file transfer of course the Bluetooth 4 edition which was introduced in 2010 brought out bluetooth low energy or blle the current level of Bluetooth that we'll be coding for is Bluetooth 5 which was introduced in 2016 it added increased range and faster data transfer as well as improved audio quality Bluetooth 5.2 was introduced in 2020 and it included Bluetooth LE audio a method of sending audio over Bluetooth low energy and the latest revision is Bluetooth 5.4 introduced in 2023 now when it comes to selecting an esp32 chip you should note that not every esp32 supports the same level of Bluetooth the original esp32 supports Bluetooth up to level 4.2 and that includes blle 4.2 the esp32s series supports Bluetooth to level 5.0 it only supports blle it does not support Bluetooth classic and the esp32 S2 has no Bluetooth functions whatsoever the ESP 32c supports BL only at Bluetooth level 5.0 the ESP 328 series is also Bluetooth 5.0 it is BL only but it is B 5.3 certified note that the esp32 H2 does not have a wifi function so armed with this knowledge you should be able to choose an esp32 chip for your Bluetooth application now for one of the experiments we're going to be doing you're going to need to have a Serial Bluetooth terminal installed on either a phone or a tablet and if you're using an Android device I would recommend the serial Bluetooth terminal by Kai moric as you can see it gets very good ratings 4.8 stars and is excellent for our application now if you are on an iOS device such as an iPad or an iPhone I don't have one of those so there's nothing I can recommend to you personally but there are a few terminals on the Apple App Store you might want to look at the Nordic RF one it looks like it might be promising if there's someone out there who knows of an IOS app please put it down in the comment for everybody else but you will need a terminal on your device to do one of our experiments okay now that we know a little bit more about Bluetooth it's time to start working with it so we're going to out in esp32 and we're going to start working with Bluetooth classic the original form of Bluetooth now Bluetooth classic is an excellent choice when you want to stream data or when you want to transfer large amounts of data and what you need to understand in order to work with this properly is the use of different profiles and protocols so we're going to cover that and then we're going to take a look at the Bluetooth serial library a library that comes with the esp32 that makes transferring data with Bluetooth about as easy as working with the serial Port so let's go and learn a bit more about Bluetooth classic and the esp32 when we're working with Bluetooth we'll be making use of the Bluetooth stack the Bluetooth stack is software that implements all of the layers of the Bluetooth protocol there are many different Bluetooth Stacks available and the esp32 uses one called blue droid blue droid is a popular open-source Bluetooth stack that is also used with Android devices Blu Droid provides two different Services Bluetooth profiles and Bluetooth protocols let's check out a few Bluetooth profiles these would include Gap the generic access profile Gap is used for device Discovery it also manages all Bluetooth connections and security a2dp is the advanced audio distribution profile the term SNK is often included with this and this means sync a device that receives audio a2dp manages the received audio stream avrcp is the audio video remote control profile the CT included in the designation indicates it's used by a controller this is used for sending commands such as play pause stop volume control Etc now let's take a look at a few Bluetooth protocols the first protocol we'll examine is L2 cap or The Logical link control and adaptation protocol this is kind of a traffic cop it organizes all of the Bluetooth traffic L2 cap is responsible for data transmission and the quality of data sdp is the service Discovery protocol STP enables devices to advertise their services for example devices might advertise that they are capable of streaming or file transfer AV dtp is the audio video distrib distribution transport protocol this is the protocol responsible for all audio and video transmission and avctp is the Audio Video Control transport protocol this is the protocol used for control commands you'll find this protocol used heavily in things such as speakers and media players today we'll be making use of the esp32 Bluetooth serial Library this was installed in your Arduino IDE along with the esp32 board manager and it's very easy to use we'll start by including the library of course and then we'll Define an object to represent the Bluetooth device in this case we call it serial BT in setup we'll start the esp32 Bluetooth serial device with a begin command and we'll provide a name in this case I provided my ESP 32 BT this will become the name of my Bluetooth device and of course you can use any name you wish once we've done that using the esp32 Bluetooth serial library is very much like using the standard serial Library you'll note that you can do a write to send data to the connected device available to check to see if data is available to read and a read to read the incoming data from the incoming Bluetooth buffer so now let's go over to the workbench and start working with the esp32 Bluetooth serial library now the first Bluetooth classic experiment that we're going going to do is just a scan and it's called the BT classic device Discovery and this is a program that expressive has provided for testing Bluetooth now remember we're going to need to use this on an esp32 not an esp32s esp32 C or ESP 32h because we need one that supports classic Bluetooth so you'll need an esp32 module we start the code off by including the Bluetooth serial library and then we have a couple of sets of directives for the compiler over here and this just checks to make certain that the hardware we've attached is capable of classic Bluetooth so if you try to compile this for let's say an esp32 S3 it's going to fail down over here and it won't compile so make certain you're using the right chip assuming we get past this point we create a Bluetooth serial object called serial BT now we're going to be doing both an asynchronous and a synchronous scan now an async scan is a scan that operates in the background it operates all the time and it doesn't block any tasks if it finds something it creates an interrupt a synchronous scan on the other hand will only operate for a period of time that you tell it to run and it'll block all other tasks so when it's going nothing else can go and we're going to do both of these types of scans now the synchronous scan needs a timeout and so that's what this is the BT Discover Time and these are just booleans that tell us which scan that we're actually operating in now this next thing here is a function called BT advertise device found and this is what's called a call back function and this is used with the asynchronous scan whenever it finds something it's going to create an interrupt and this is the call back routine it's going to call and what it's going to do is it's just going to print a Serial monitor to let you know what it is we've discovered so we go into the setup now we set up both the serial port and the serial Bluetooth port and assuming we get past this point over here the serial Bluetooth is working so we'll just print to the serial monitor that the device has started we're going to go and do our asynchronous scan first or our async scan we're going to print that we're starting it and this is the line that sets it off BT discover async and we give it the call back that we're going to be using BT advertise device found so every time it finds something it's going to call that which is going to print the serial Port now we print out that we're going to have the find printed to the serial Port we go for 10 seconds and then we stop because remember the async one will keep going until you actually stop it so we stop the async down over here and print its results then we go to the synchronous scan now synchronous scan is the one that blocks all the other tasks so we basically started over here and we pass to it the amount of time that we want to run and so our Discover Time Again is about 10 seconds so if we have some results we'll dump the results of the serial Monitor and then we'll end and this ends the setup when we go into Loop and there's actually nothing to run in the loop because everything runs in setup so let's load this up to an ESP 32 and watch it work and so we're about to try out our scan I've got an esp32 W oom module connected to the computer and I'm just going to give it a reboot so that we'll start it from scratch and it's going to start the asynchronous scan and it has found one device and that's my phone over here so you can see it's found the pixel down here and it's given me the address the RSSI now it stopped the asynchronous and starting the synchronous scan and it should find the same thing and there we go it's found the same thing after 10 seconds so we found the same device by doing two different types of scan and you can of course decide which sort of a scan would be the most suitable for your application and use that in your program now for our next demonstration we're going to create a simple Bluetooth client and we're going to use it with a Bluetooth server the Bluetooth server can be your phone or a tablet running a terminal application the client code is pretty simple and you can use it as the basis for your own code now we start off again by including the Bluetooth serial Library as we did before and we're going to give our client a name and you can call it anything you want I call mine esp32 BT client now the next two sets of lines are again the compiler checks to make certain that we're using the right type of Hardware so assuming we're using a esp32 and not an s or a c or an H we'll get through this section over here and we create a Bluetooth serial object then we go into setup and we'll start our serial Monitor and we'll also start the Bluetooth serial so we'll pass a device name to it now this next line over here is commented out you can uncomment it if you wish after after you have paired this with a device the esp32 is going to store that pairing information in PS RAM for use for future applications now if you want to delete that information from the psram you can uncomment this line over here but normally we would leave it commented out after that we print the serial monitor to let it know that we're ready and we go into the loop to do the actual operation now basically we're just communicating between the terminal and our serial monitor so we're going to check serial monitor to see if we've typed anything into it and if there is something typed into it we're going to do a Serial Bluetooth right for what we're reading from the serial monitor so we just pass it out to serial Bluetooth and then we do the opposite we check to see if we've got a message from the paired Bluetooth device if we've got something available we'll write that to the serial Port we'll put a really short delay down here and go and do the loop over again so it's a pretty simple sketch let's load it up and watch it in action okay so now let's demonstrate our serial Bluetooth I've got the serial monitor set for my esp32 board and as you can see it's started I've got an esp32 BT client has started and can be paired with Bluetooth so let's go over to my phone and first what we'll do is we will go to Connected devices and try to pair a new device and we'll look for new devices and we do indeed see the esp32 BT client so we'll pair with that and we'll pair it okay so now that we're paired let's go and open our serial terminal now the first thing we need to do in the terminal is connect to the actual device we've paired with so we go to devices and we see the esp32 BT client so I've got that paired right right now and let's just type something up in the serial monitor I'll hit enter and if you can see it's very tiny but on the bottom of my client I do indeed have hello there I've already written the word hello into here let's send that off and so I sent that up and over here you can see I've got my hello back on my uh esp32 serial monitor and so we are exchanging text between the terminal and the esp32 and of course this is just very basic but you can use this is the basis for exchanging any type of data if you want to send files if you want to send Control Data to turn things on and off you basically use the same principle so now that we've worked with Bluetooth classic it's time to focus our attention on BL or Bluetooth low energy and in order to work with and understand ble irly you really need to know more about the generic attribute protocol or Gat so we're going to dig deeper into Gat and then we're going to take a look at the libraries that we can use with Bluetooth low energy and the esp32 there are actually two profiles that we need to concern ourselves when working with BL Gap and Gat Gap is the generic access profile and we've looked at this already with Bluetooth Gap controls things the device advertising and establishing connections it also takes care of security another thing performed by Gap is that it determines the network topology and there are a couple of different topologies we can use with B devices one topology involves a central device with a bit of intelligence such as a smartphone or a computer it attaches to a number of ble peripheral devices and it establishes a two-way communication this is known known as a connection oriented topology and the connection between a peripheral and the central device can also be referred to as a piconet another topology has our ble device operating as a broadcaster a number of external devices such as smartphones tablets and computers can be used as observers there's only a one-way connection here the broadcaster just sends out data but does not receive it this is known as a connectionless protocol once we've established a connection we use the generic attribute profile or Gat to determine how the data is actually transferred a gat profile consists of one or more services a service is a predefined collection of parameters which are called characteristics the service will have its own unique numeric identifier or uu ID a characteristic is a group of attributes the characteristics are actually the data that is being transferred via and it has a number of components as well the value component of the characteristic is the actual characteristic data it can be a number a string or an array of bytes the property component actually has three subcomponents a handle which is a 16-bit number used to access the characteristic the characteristics uu ID and the characteristics permissions another component of the characteristic is the descriptor and this this contains additional information or configuration options for the characteristic a gap profile can consist of more than one service and a service can have more than one characteristic here's an example of an actual service this is the heart rate service note that it has three different characteristics and the characteristics and the service all have a unique uu ID the Bluetooth special interest group keeps a list of profiles services and characteristics and you'll find a link to that list in the article accompanying this video official profiles use a 16bit uuid manufacturers can also create custom services and characteristics and those will use a 128bit uuid if you need to generate a uuid you can go to the online uuid generator to create one the esp32 has a number of different libraries exclusively for working with blle the ble device Library initializes and configures the Bluetooth stack the blle server Library lets you set up a BL server to which you can add Services characteristics and descriptors the B client Library performs the same function for a b client the BL utilities library has a number of useful utilities for converting data types and for working with uu IDs the B scan Library performs ble scanning and the B advertised device Library Library will give you information regarding external advertised devices so now let's go to the IDE and start working with Bluetooth low energy and the esp32 now for our first BL example we're going to create a b server a b server needs to perform a number of different tasks it needs to be able to advertise itself so the B clients can find it it needs to create a service and at least one characteristic underneath the service and it also needs to be able to have the client modify that characteristic by reading or writing to it and this example from expressive that we're going to look at does all three of these things now it's actually a fairly simple bit of code it starts off by including three different libraries and then we're going to generate some uuids both for our service and our characteristic now we're going to use the uu IDs that were provided to us but if you want to create your own you can go to this uu ID generator.net that they've got a link to over here and create your own IDs now we go into the setup and we're going to start our serial Monitor and print that we're starting everything then we're going to initialize the ble device and this is actually the name that we're giving the B device so you can give it some other name if you wish but this init initiates the B device then after that we're going to create a BL service and so we're going to create the server with a service underneath it and we're going to pass the service uu ID to that service then we're going to create a characteristic and these are the parameters for the characteristic you can see we got The UU ID we've got a read property and a write property on it so it can be read and written too and then we're want to set the value of the characteristic and it says hello world says Neil considering the original author of the sketch was named Neil if your name isn't Neil you could change that if you wish and then we're going to start our service and then after that we've got to do some advertising so we're going to set up advertising over here we're going to add the service uu ID to the advertising and a couple of parameters and then start advertising down there and then after that we'll print everything to the serial Monitor and we're basically done our server is working so let's load this up and test it out now there are a number of different methods I could use in order to test a b server the first thing is to start it up and look in my serial monitor where tells me it has started the B work and now I can read everything in my phone because the characteristic has been defined now if I go over to my phone and go into the Bluetooth I can hit pair a new device and I notice my available devices I have long name works now and that's the silly name that we actually gave our ble server now the conventional way of testing this with a phone would be to use something like the NRF Connect app which can connect to this and exchange data with the characteristic but another way of doing it is to Simply build a BL client and that's what we're going to do right now now the final B example that we're going to look at is the ble client which complements the server that we've just made now the B client needs to perform a couple of operations it needs to scan for BL services that are B devices that offer services it needs to connect to a specific service using the uuid and it needs to read and write to a characteristic on that service also using the uuid and we're going to of course use the same uuids that we did for our BL server now we'll start off by including the B device Library previous versions of this used the B scan but it's no longer required then we're going to go and give our uu IDs the service uu ID and the characteristic uu ID now these are identical to the ones that we used on the server side so if you have changed those for some reason if you use the uu ID generator to create your own you'll need to make the changes here as well then we have a couple of booleans that we set up just to Define what parameters that we have and a couple of characteristics that we Define as well and then we go to our first call back over here now the notify call back is a function that is called automatically whenever the server sends a notification to say that the characteristic has changed and so when we get that we're going to go and grab the data and print it out so that's what our call back does over here now the next callback over here is a class call back and it defines the actions to take when the client connects or disconnects now we're going to go to a function called connect to server and it does a number of different things I'm not going to go through all the code because there's quite a bit of it but essentially this creates the ble client it connects it to the remote ble server it reads the services and characteristics it reads the characteristics value and it registers for notifications if the Remote device allows for notification so you can go through all of that and that's what's happening over here quite a bit here's the notifying down at the bottom here and then after that we have a my advertised device callbacks this is a class call back and it handles what to do whenever we discover new BL devices now we're going to move on to the setup we'll start off our serial Monitor and print to it and then we'll initialize the B device we don't need to give it a name because we are a client and we'll retrieve the scanner and call back that we want to use down over here and so we'll set everything up and we'll go into the loop and we'll check to make sure we're connected if we're connected to the server we're going to print that we're connected to it else we're going to print that we have not connected and we're going to just fail right at this point if we have connected to the server we're going to do something we're going to get the time since Boot and we're going to do that with the Millis command the Millis function takes the amount of milliseconds since the ESP is 32 has been booted so we're going to get that and we're going to create a new value for that we're going to use that to modify the characteristics so we're going to do that within here we're going to put a short delay onto that and go and do it over and over again so there's a lot of code over here but when you break it down it's not too hard to understand and if you want to broken down in great detail the article accompanying this video on the dronebot workshop.com website we'll do that for you so now let's go and give this a test with our ble server now for this demonstration on the left side I have our server and on the right side I have got the client and uh I've got the client moved over so you can see what it's doing it is incrementing indeed every second it is changing the characteristic value and uh it gives it the time since Boot and this is the number of seconds since it is booted now on the server side it just once again just says characteristic Define now you can read it in your phone so these two are definitely communicating with one another and again it's a fairly simple demo but this demo can be used as the basis for other code where you can create B devices both clients and servers so that concludes our look at Bluetooth and BL with the esp32 I hope that you enjoyed it and if you need a bit more information or if you'd like to look deeper into the code that we use today you'll find all of that in the article that accompanies this video on the dronebot workshop.com website now while you're on the website if you haven't yet please consider signing up for my newsletter it's not a salesletter it's just a way of getting in touch with you to let you know what's going on in the workshop and to tell you about the subject of upcoming videos and of course it's free to sign up for the newslet all I need is your email address if you're in a signing up mood I've got a few more things you can sign up for the dronebot workshop forum is a great place to go if you need help with your electronics projects or just want to chat Electronics with a number of like-minded individuals and it's free to sign up for the Forum there's details right below this video and while you're in that signing up mood how about subscribing to the YouTube channel if you haven't done that yet I make videos about things like the esp32 microcontrollers and electronics and if you enjoyed this video I'm sure you'll enjoy my other ones and I'm planning on creating some more videos on esp32 Bluetooth projects and so if you want to see that when you subscribe to the video by clicking on the red subscribe button you'll also want to make sure to click on that Bell notification and that way you'll get notified every time that I make a new video so until we meet the next time please take good care of yourself stay safe out there and I will see you soon here here in the dronebot workshop goodbye for now [Music]
Info
Channel: DroneBot Workshop
Views: 25,690
Rating: undefined out of 5
Keywords: esp32 bluetooth, esp32 tutorial, bluetooth, bluetooth tutorial, dronebot workshop esp32, bluetooth esp32, esp32 bluetooth classic, esp32 BLE
Id: 0Q_4q1zU6Zc
Channel Id: undefined
Length: 38min 0sec (2280 seconds)
Published: Sun May 26 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.