In this project, we will be dealing with the inbuilt Bluetooth feature in ESP32 Development board and try to build an application around it. The Bluetooth system can be divided into two different categories: Classic Bluetooth and Bluetooth Low Energy (BLE). ESP32 supports dual-mode Bluetooth, meaning that both Classic Bluetooth and BLE are supported by ESP32. Here, we will control General Purpose Input Output (GPIO) pins of the ESP32 board using Bluetooth in classic mode and a customized Android application.
For more documentation, you can refer to Espressif IoT Development Framework here. So, let’s get started.
Material required for the Bluetooth project
1. ESP32 NodeMCU (Check the datasheet, if you are using a different version.)
2. USB Type C cable to program ESP32 from a laptop or PC (most Android phones use this type of cable).
3. LED with Resistor(1K) – To test the Bluetooth
4. Breadboard – To place the components
Steps for the ESP32 support in Arduino IDE:
(Ignore this step if you already have the setup of ESP boards in Arduino IDE)
Here, to program the ESP32 we need an Integrated Development Environment and we will use Arduino IDE software. Arduino IDE is a cross-platform application. It is written in Java and coded in C/C++ with some special rules. To download the latest Arduino IDE from here.
Arduino IDE does not contain support of ESP32 family so to install the ESP-32 Board in Arduino IDE, you can refer here.
The Code for Bluetooth in ESP32
Download the Code from the link below and Open it in Arduino IDE. Let’s understand the code.
Firstly, we will declare the Bluetooth libraries in the code. All the declaration like Led pin, states and UUIDs are declared here. To have more details on UUID refer here.
Then, we will create two classes. MyServerCallbacks is created to check the server connection and MyCallbacks to perform the various functions.
Hence, this function contains the LED controlling part of the Bluetooth and prints the value received. One may also include some inputs to fetch data like temperature or humidity. The android application will give commands like “A” and “B” which will be received by the ESP-32 BLE and will generate the same results. These classes are created to call in the Void Setup() and most of the part of the code is set up here only.
All the characteristics of Bluetooth and device name will be declared here including the start of various services like UART and BLE Server.
The baud rate is by default set to 115200 and pin 5 declared as LED will be set to OUTPUT.
There is only one Output (LED) in the circuit you can add many more outputs or set inputs.
ESP32 Pin 5 -> Resistor
Resistor -> LED +ve
LED -ve -> Ground
Using the Android Application:
You can download the android application from here(.apk download link) and use it or edit this using Thunkable.
We have created this app for the ESP Bluetooth and you may use it to control the LED or Light.
Turn On your phone’s Bluetooth and then start the app. Click Connect and choose the device you named in the code.
After connecting the app interface will get active by changing its colour you can turn On and Off the Led.
Assemble everything and try this out.
Let’s try it out with a relay and control an AC bulb. For relay connections, you can refer here.