hey guys
Ravi here and this is an ESP32 it's a
pretty versatile micro controller and
comes with the support for Bluetooth Low
Energy or ble ble is very popular with
stuff like heart rate monitors
temperature and humidity sensors and
anything else that needs to communicate
in small bursts of data infrequently
over short distances why you would wanna
use it for your project instead of
something like Wi-Fi is because you can
use ble to communicate with most modern
smartphones it doesn't require you to
set up a web server you don't need to
connect to a separate network and since
ble is for Bluetooth Low Energy it's
more favorable for portable
battery-powered projects you know what
else is more favorable buying electronic
components from this video sponsor LCSC.com LCSC distributes over 200,000
products ranging from semiconductors to
resistors capacitors diodes inductors
connectors and sensors its catalog is
also integrated easyEDA so you can
order the exact component that you need
for your PCB design click the link in
the description to get eight dollars off
on your first order now I made a similar
video almost a year ago about this very
topic but that was before proper
libraries for ble for the ESP32 arduino core had come out the method that I
showed there was really jank and was
basically just a workaround but now that
the libraries are out I am going to show
you how to properly use ble for the ESP32
now we are going to go about this in a
bit of a reverse order first I am gonna
show you an example and how you can
implement it and then I am going to
teach you what you need to know to use
ble for your own projects the first
example we are going to look at is to
transmit just a number that number being
the value of a potentiometer the
connections are as simple as they could
be the first pin to 3.3 volt second pin
to GPIO 36 and the last pin to ground
the code is where it gets a bit
complicated I'll explain things in more
detail further in the video but for now
all you need to know is that
the code under setup configures and
starts the ble server and these two
lines are what send the value over ble
once the code has been uploaded you can
open the NRF connect app on your
smartphone click scan and you will see
my ESP32 listed kept connect and you
will see three services tap unknown
service and click the three downward
pointing arrow besides the unknown
characteristic you should now be seeing
a value and turning the potentiometer
should also change the values
accordingly now to actually understand
what's going on here you need to first
know a little bit about how ble works
there is a specific hierarchy that you
need to conform to in order to properly
use ble there is a ble device that
device runs a ble server that server
provides some specific services those
services have some characteristics and
each characteristic has some properties
now I know that this may sound a bit
complicated but it will become simpler
once you understand the first example
let's start from the top and work our
way down
first of all we need to initialize our
device whatever name you give the device
here will be the name that appears while
scanning then we need to configure the
ble server which is done by these two
lines the first one basically sets
everything up while in the second one we
are supplying the function that
determines what action to take
whenever different be any events happen
like what to do when a connection is
established or when some data is
received we aren't receiving any data in
this example so ignore it for now next
we configure a service by supplying a
unique ID to it this ID tells the
receiver what kind of data it is
receiving different IDs represent
different types of services for
different use cases in this example we
are just using a random ID
hence it shows up as an unknown service
similarly we supply another random ID to
configure the characteristic this ID
tells there is
or what this particular characteristic
is for example the heartrate service
would obviously have the heart rate
measurement characteristic but it also
might have the body sensor location
characteristic which would tell the
receiver from where the heart rate is
being measured while configuring a
characteristic along with the ID we also
set its properties these properties
determine what the receiver can do the
most common and obvious one is real the
receiver can read the data being
supplied by the correct restrict the
right property allows the client to send
some data back to the server the notify
property works like a messaging app
whenever there is a change in the data
of the server it notifies the client and
the value automatically gets updated the
indicate property is identical to the
notifier but here the client just lets
the server know that it has read the
change by sending an acknowledgement in
this example we are using the read and
notify properties so that any change in
the value of the potentiometer is
automatically reflected on the receivers
end next we add descriptors descriptors
are optional hints that you can attach
to characteristics to give a user a
little extra information like the range
of a temperature sensor if you are using
the notify property which we are you
must add this BLE2902 descriptor it
just keeps note of whether the
notifications are enabled or not a ble
server can broadcast data to all the
devices around it letting them know of
its name and other related information a
client uses this information to
determine whether it wants to connect to
it or not the device is essentially
advertising itself so we need to
configure this advertising in this line
then start the service and finally start
the server now in the main loop we wait
until the device is connected to a
client and then set our value and then
notify the client here the one signifies
the number of bytes we are sending the
maximum value of our potentiometer in
this example is limited to 255 which
incidentally is also the maximum value
or single byte can store one very
important thing to note here is that the
set value function can only send
8-bit integers which is equivalent to a
single character or char before sending
any data you always need to convert it
into this format and then convert it
back on the client side that was a very
basic example of how to use ble with the
ESP 32 arduino code let's do a bit more
of an advanced application we'll make a
ble thermometer I'm using the DS18B20
temperature sensor along with the firebeetle ESP32 board link to both of which
will be in the description below the
connections are exactly the same VCC to
3.3 volt ground to ground and signal pin
into GPIO 36 the code also isn't much
different but there are a few key points
that you will need to take note of this
function is just to retrieve the
temperature using the one wire protocol
the first significant change concerning
ble is with the unique IDs that we
assigned earlier these are called UUIDs
and they stand for universally unique
identifiers as I hinted earlier there
are some predefined UUIDs that have
been assigned to certain be any services
and characteristics these predefined
profiles are called GATT profiles and
they basically specify the format of
data being transferred along with what
is being transferred you can find all
the officially recognized GATT profiles
on Bluetooth.com website which I
would link in the description below in
our example the ID 081A is for
environmental sensing service while the
ID 2A6E is for the temperature
characteristic and the NRF connect app
automatically detects these services for
what they are because of theUUIDs that
we used now you send the data we have to
be a bit careful we care profile
specifies that the data must be in the
format of a single 16-bit integer and
for good reason our temperature can be
anywhere from 2 to 6 characters long
such large data can't be stored by a 8
with integer but we can only send 8-bit
integers so to work around this we store
our data in a 16-bit integer and cast it
to a 8-bit integer before sending
and specify the lengths to be two bytes
also you might have noticed that I have
multiplied the temperature by 100 this
is because the client expects an integer
and not a float the client would divided
by 100 on its end before serving the
data and there you go you have your very
own ble thermometer using the same
concept you can make your own heart rate
monitor a step counter a proximity
sensor and more but but if you want to
do something different something that is
not defined by the CAD profile well that
is exactly what we did in our first
example by using random uu IDs we
basically made our own custom profile
also you are not limited to sending an
integer you can also send strings as a
demonstration let's take a look at
another example
we'll make a status display for a switch
again most of the base code is the same
I have selected 1700 as the service ID
1A00 as a characteristic
ID there are two ways of sending text by
putting the text in quotation marks in
the set value function or you can pass
the address of a character array
just remember to cast it into a char
first this example demonstrates both
methods you are free to explore and mess
around with all these examples on your
own the app that I am using to display
this is a custom app I made in MIT App
Inventor the app can only display
strings link to download this and all
the example course that I have shown
would be in the description below now I
would recommend that if you are using a
custom profile then convert all your
data into a care array and transmit that
it's a lot easier to deal with I have
included an example below which shows
how to convert different types of data
formats into a care array for your
convenience now the only thing left is
to utilize the right function this is
easy enough just declare a function in
this format whenever some data is
received this function would run and the
receive data would be available in a
care array value do with this data as
usually
I hope that you learn something new let
me know in the comments below about any
be any project that you make leave a
like if these videos were helpful and I
will see you guys in the next one
