#5 — The ESP32 with Bluetooth

Wireless data transfer level 1, here we go!

Carissa Aurelia
I learn ESP32 (and you should too).
8 min readFeb 28, 2020

--

Bluetooth earphones. Photo by Zarak Khan on Unsplash

You know the famed Airpods by Apple (I mean who doesn’t in this century anyway lol, unless… 🙃🙃). And soon after Apple launches it, many brands are on the race to create the most sophisticated wireless earphones. Samsung have their Galaxy earbuds. The Chinese company Xiaomi have their Redmi AirDots that comes in many series. And I have one, little ESP32 board, which has a built-in Bluetooth module :)

Aaaand nope. We’re not going to create any earphones in this project (are we?). But instead, we’ll create something even cooler with the ESP32 Bluetooth module. Are you ready?

ESP32 Bluetooth module

Bluetooth + ESP32. Source: https://colourtvservicebbsr.blogspot.com/2018/08/home-automation-using-bluetooth-of-esp32.html

The ESP32 board has 2 built-in Bluetooth module: the classic Bluetooth and the Bluetooth Low Energy or BLE. Bluetooth classic is simply the Bluetooth you interact with every day. Your smartphone has this kind of Bluetooth. Your PC/laptop likely has it too (unless you’re using a very, very old one). And your wireless earphones definitely has it. They all use the same standard serial protocol and functions for sending data wirelessly.

The Bluetooth Low Energy (BLE). Source: https://www.slashgear.com/bluetooth-le-audio-official-and-the-uses-will-surprise-you-06605380/

BLE is a different case. According to randomnerdtutorials.com, it is a power-conserving variant of Bluetooth. It’s used for short-distance transmission of small data amounts. Unlike Bluetooth, when not in use, BLE will enter a sleep mode. This greatly reduces its power consumption, 100x less than the classic Bluetooth. In everyday use, you can find the BLE being utilised in healthcare, fitness, tracking, beacon, and home automation. Simple example including the smartwatch that can send you notifications that you get on your smartphone. There are two types of devices in BLE: the server and the client. The server broadcasts/advertises its existence so it can be found by other devices. The client scans nearby devices and when it founds the desired device, it’ll establish a connection and receive the sent data. This is called point-to-point communication.

Using an I2C LCD, a BMP180 sensor, an android phone, and the ESP32 Bluetooth module, I’ll create a unique chain of communication. The phone will be able to pair to the ESP32 Bluetooth to send a message, which will be displayed on the LCD screen. Meanwhile, the BMP180 will return sensor readings and, via Bluetooth, displays them on the phone screen. And then I’ll demonstrate BLE by point-to-point communication using two ESP32 boards. Cool ‘right?

1. The Ingredients (and yada, yada)

The Ingredients.

With the same “ingredients” as my 4th project in the ESP32 blog series (read here), I’ll create the same circuit but with an entirely different purpose. Shortly, I’ll be using my laptop, a 38 pins ESP32 board, a breadboard, a MicroUSB to USB cable, a few male-to-female dan male-to-male jumper wires, a 16x2 I2C LCD, and a BMP180 sensor. Wiring out everything as I did in the 4th project should do just fine. And again, never forget to double-check the wirings! Nobody wants to end up with that smoky smell of burnt electronics in the air 😅😅.

Wired components.

2. Code it out

It’s time to do the coding! Make sure you’ve obtained both the BMP180 and the I2C LCD device addresses before proceeding (read here for detailed instructions).

Basically, I want the screen to display messages that I send via the serial Bluetooth terminal on my android phone (I’m working on a Samsung Galaxy A7 2018). To do so, I’ll create a character variable that receives the input message character by character and appends it to a string variable when I press enter (aka the \n character). Then it’ll output the message using the print() command from the LiquidCrystal_I2C library and flushes the string variable and clears LCD screen for the next input. Next, I want to display sensor readings in an interval of 10 seconds. Using a function called millis() , which basically returns the number of milliseconds passed since the board began running the current program, I can tell if it’s been 10 seconds since the last sensor readings output to the screen.

Don’t get your head in a twist though. I have the code ready down here :)

Change the device addresses based on your own device addresses and you should be good to go. Aside from that, you can also change the Bluetooth device name by editing the “KevinIsBali” part in the code (Fyi, Kevin (

) is my partner in this project! He’s from Bali, Indonesia, and he’s very proud of it!)

Upload the code to the board (and be extremely patient with uploading, as usual) and ignore the warning that appears. Then press the ESP32’s enable button for the Bluetooth to start. You can see if the Bluetooth is ready by opening the serial monitor window at a baud rate of 9600.

Bluetooth is ready!

3. Installing the serial Bluetooth terminal

Moving on, I’m going to install an application on my phone called “Serial Bluetooth Terminal”. The app will act as a GUI where I can write the message to be displayed on the LCD and to receive sensor readings. You can download it in the Android Play Store.

The Serial Bluetooth Terminal app.

4. Setting up and making things work

Next up, I’m going to pair my phone’s Bluetooth with the ESP32 Bluetooth. Turn on the phone’s Bluetooth and head on to itssettings page. Because I named my ESP32 Bluetooth “KevinIsBali”, I’ll look for that device to pair.

Pairing the devices.

Then open the Serial Bluetooth Terminal app and click the hamburger button (on the top left side of the screen) > Devices. We’ll see the “KevinIsBali” device in the list of paired devices. Select it and the terminal will settle a connection to the ESP32 board via Bluetooth.

Selecting the device (left) and connecting the devices (right).

Optionally, when you’ve selected a device, you can connect/disconnect it at whim using the cable button on the top right of the screen (left of the trash button).

And it’s all set! The sensor readings will come in an interval of 10 seconds. You can try typing and sending a message and the LCD screen will display it.

Sensor readings are being displayed every 10 seconds (left) and how ‘YASS QUEEN’ is being displayed in the LCD (right).

5. Bonus: BLE test

In this section, I’ll need two ESP32 boards; one for the server, one for the scanner. I work together with Adriel and Adrian’s group so I don’t have to buy another ESP32 board. I upload the BLE_server code on my ESP32 and they upload the BLE_scan code on their ESP32. Both codes can be found in the Arduino IDE by clicking File > Examples > ESP32 BLE Arduino. I personalised the code by changing the “Long name works now” part of the code into “BLECACADANBALI” so that their ESP32 scanner can easily recognise my ESP32 server.

My ESP32 feat. Adriel and Adrian’s ESP32 :)

After uploading both codes to the respective ESP32 boards, open the serial monitor at baud rate 115200 and press both boards’ enable buttons. The scanner should see the server device being advertised.

Scanner ESP32 serial monitor screen (left) and server ESP32 serial monitor screen (right).

6. Lessons learnt

You probably noticed that the serial monitor screen for the Bluetooth classic project is spewing some strange, unknown symbols before displaying “The device started, now you can pair it with bluetooth!”. The screenshot of the screen I put above captured it perfectly (or if you don’t want to scroll all the way up, you can see it down here). This happens whenever I pressed the ESP32's enable button after uploading the code. That’s the result of the baud rate settings.

Strange symbols in the serial monitor screen (highlighted with yellow).

In a nutshell, baud rate states the rate in bits per second at which a piece of information is transferred on the communication channel. Different devices have different baud rates. A baud rate of 9600 means that a device is capable of transferring a maximum of 9600 bits per second information. If you set the baud rate too high/too low than the devices’ supposed rate, the data won’t be properly transferred and, hence, won’t be properly displayed as well.

At first, I set the baud rate for the Bluetooth classic code to 115200 baud. This causes some erratic behaviour on the LCD screen (such as displaying reverse question marks and missing characters) whenever I try to display a message sent from the serial Bluetooth terminal. A few good minutes of googling helps me to figure out what’s wrong. It turns out that the ESP32 Bluetooth and the I2C LCD have different baud rates which, as I said, causes whatever message I sent through Bluetooth to be improperly displayed. With trial and error, I find 9600 baud is the suitable rate for messages to be displayed on the LCD, at the cost of strange characters appearing on the serial monitor.

There is still one strange character that keeps appearing even after I changed the baud rate, which is the 4 horizontal lines at the end of every message. I suppose that it was the representation of the \n symbol. I haven’t been able to find any satisfying answers on the internet regarding that symbol — if anybody is able to explain what that symbol really is, you can leave a comment down below :)

--

--