Introduction: How to Use Bluetooth(BLE) With ESP32

About: DIY Electronics and Tech. Check out my YouTube channel : https://youtube.com/livesparks Follow me on Twitter : https://twitter.com/LiveSparking

#####NOTE#####

The method shown in this Instructables is old and outdated. Watch this video for the latest method.

###############


While the ESP32 boasts an extensive feature list (Wikipedia), the biggest feature that draws the eye is the built-in bluetooth v4.2 with BLE support. But that statement can be a misleading, while the hardware is there, the software support for using Bluetooth is missing. (It's under development)

For those who don't know what BLE is, it stands for Bluetooth Low Energy. It's a bluetooth protocol that boosts considerably lower power consumption compared to "Classic" Bluetooth.

In this Instructables I'll show you how you can use the limited (software) resources at your disposal to send data from an ESP32 board to your phone over BLE.

A few Disclaimers...

A few things of note I should mention before you proceed further with this guide..

Note 1 : When I say that Bluetooth support is not yet available, I mean in the arduino development environment.

The support very well may be available in the official SDK as well as the IDF but I haven't found any competent instructions for those.

Note 2 : The method that I am using is very much a workaround and not how BLE is ideally supposed to work. The library supports for creating services in BLE is not here for the arduino environment.

The only thing you can do reliably is creating a Beacon that advertises it's name. A YouTube comment put it very elegantly : "LOL, a brutal hack. But what can you do?"

Note 3 : The only kind of data that you can send is string.

You might very well decide to code and decode other kinds data to and from string but that is beyond the scope of this guide.

Step 1: The Example Build

If you are just here for the explanation then you can skip this step but going through it might give you a bit more clarity..

First I am going to build an example project and then I’ll try to explain how it works and how you can replicate the Bluetooth communication. We'll be building a portable temperature sensor that sends a live feed to your smartphone. The build can run for days on a single li-po and easily demonstrates the benefits of BLE.

You can use the module as a coaster to insulate your drinks and get a notification as soon as your beverage reaches the optimal temperature. Because it would be a travesty if you had to sip a tea that was a degree above 40.

The only hardware you need is a ESP32 board and a digital temperature sensor. My board supports external battery so I am using a 3.7v li-po that I salvaged from some old device to make this project portable.

Hardware

The connections for the temperature sensor are simple. The red wire connects to 3.3v, black connects to ground(gnd), yellow connects to GPIO 2 which on my board is marked as D9. Connect a 4.7k ohm resistor between the red and yellow wire. I am not using the resistor, instead i am using a 3 pin plug-able terminal that came with my sensor. It has a built-in pull up resistor.

The li-po should really be connected using a JST connector but I couldn't be bothered to buy one so I just shoehorned in some (female to female) jumper cables into the connector and soldered another pair to the battery terminals. This makes for a reliable connection and a makeshift power switch.

Now as a project enclosure, I am using a Styrofoam disk that has been cut out from a larger sheet. This makes for a great insulator. Another much smaller disk is stuck on top but a bit to the side. This is for wrapping the extra length of wires so they are not dangling all over the place. After a generous employment of your glue and a small hole for the sensor probe, you are ready to proceed to the software.

Software

If you don't already have the arduino IDE installed then go to this link to download it. The arduino software by default doesn't come with the board definitions for the various ESP32 boards. To get them to go this link and download the files in a zip. You need to unzip them to this location:

C:/Users/<user-name>/Documents/Arduino/hardware/espserrif/ESP32 

Where <user-name> is your user name on your PC. Make sure that the various files are available under the ESP32 folder and are not under another folder.

Now if you start the arduino software and go to tools->boards you should see various ESP32 boards when you scroll down.

Most digital temperature sensors use the OneWire protocol to communicate with the micro controllers so we need to get the library. Go to sketch->include library->manage libraries and search for onewire and install the library that is by way too many authors. You don't need the onewire hub. Ignore it.

Now you can download and open the code attached to this step (temperature-example.ino).

If you are using a different sensor from me then you will have to change the code accordingly. Replace the code under getTemp(). Just return the final temperature in the form of

Return <variable>; 

Where is the float containing the temperature.

Plug in the board, select the correct board and port from under tools and hit upload.

If the code refuses to upload, disconnect the sensor and connect the GPIO 0 to ground. Restore the connections after the upload.

Your ESP should now be screaming the temperature of you coffee to the world but you need someone who can understand it.

The Android App

Sorry iPhone users (...not really).

Download the apk from here and install it. When you start the app you would be greeted with a very simple interface.

If you get a error message saying ‘advertising not supported’, click OK and ignore it but if you get a message that ‘BLE is not supported’ then your phone doesn't have Bluetooth 4.0 or higher and wouldn't be able to run the app.

Make sure that Bluetooth on your phone is on and click ‘Start Scanning’, as long as the esp is in range and powered on you should be receiving some temperature value.

Possible errors:

  • -1000 :: This means that your device couldn't find the ESP. make sure that Bluetooth is on and try restarting the ESP.
  • SNA :: This means that your phone is receiving data from the ESP but the ESP itself couldn't find a temperature sensor on the GPIO 2. Make sure that the connections are secure, see if you are using a resistor of the correct value. Double check with your board schematics that you are connected to the GPIO 2 and not a pin marked as D2.

Step 2: The Explanation

How BLE normally works is that a device advertises it's name like a beacon, this name can be seen by anyone and is used to identify the device. Then the device can create various services that are seen by others when they connect to it. These services can have different streams of data.

For eg. A device named ‘Weather Station’ can have serviced under it like ‘Temperature’, ‘Humidity’ and ‘Wind’. When another BLE device like your smartphone scans for devices, it would see the Weather Station and when it connects to it, it would be able to receive the data streams under corresponding to the different services.

The libraries (for the ESP32) that are currently available to us allow us to create a beacon that others can discover but that's the extent of it. We can't create services nor can any device connect to it.

So how I am sending data without creating services is by employing a technique similar to the WiFi protocol called Beacon Stuffing. This means that I am including data to be sent within the beacon name itself. This enables me to advertise data without the requiring other devices to connect to the beacon.

ble.begin(beaconMsg);  //beaconMsg is the advertised name

We are using the SimpleBLE library to create a beacon with it's name in the format of ESP. Where ‘ESP’ always remains unchanged at the beginning of the name and is replaced with the latest data returned by the getValue() function every 100 milliseconds.

float getValue(){return sensorValue;}

The android app looks for BLE device names starting with ‘ESP’, once found, it splits the name and only displays the data from the end.

The communication is only one way, the app doesn't send anything back.

Step 3: Conclusion

At the end of the day, this method is no replacement for a properly implemented BLE library but it might be enough to tide some projects over until complete BLE support comes to Arduino. Hopefully this Instructables was of some help to you.

A BIG Thanks to DFRobot.com for sending me these products:

I recently found this library. The ReadME claims that you can connect to other BLE devices to receive data(Can't advertise yourself). I haven't tried it but you can check it out if you are interested.

You can check the project video at : YouTube