Adventures in Embedded System Part 6 — Out of the Blue(tooth)

Previously

Akbar Rizki Maulana
3 min readMar 2, 2020

In last week’s assignments, which was a roller coaster, my friends and I explored the wonders of writing (more like remixing)our own code for the OLED display + weather sensor project combination. We thought that was a tough one. Oh boy, it had nothing on this one

The Assignment

This week’s assignment, we finally get to try the built-in Bluetooth of ESP32. Do you ever wonder why its called Bluetooth, even though there is nothing blue in it nor it contains teeth?

“ The name Bluetooth is an Anglicised version of the Scandinavian Blåtand/Blåtann (Old Norse blátǫnn), the epithet of the tenth-century king Harald Bluetooth who united dissonant Danish tribes into a single kingdom. The implication is that Bluetooth unites communication protocols.” — Wikipedia

Moving on, let’s get started!

This assignment actually consists of 2 projects, one super easy and the other one (deceptively) easy. Don’t forget to download the Serial Bluetooth Terminal in the Play Store. We use that app to connect our (Android) phones with the ESP32 so we can send/receive packets of data. The super easy project can be easily found in the Random Nerd Tutorials website down below, although we only used the Serial-to-Serial Bluetooth one:
https://randomnerdtutorials.com/esp32-bluetooth-classic-arduino-ide

Put the code in the Arduino IDE, compile, then upload it to the ESP32. Then open the Serial Monitor and wait until the words “The device started, now you can pair it with bluetooth!” appear on it. After that, open your phone and open the Serial Bluetooth Terminal and connect your phone with the ESP32. Now, you can finally communicate between you phone and the Serial Monitor through the ESP32.

Here’s a video of this code’s demonstration.

https://www.youtube.com/watch?v=eEcwg6nCsrQ

Now, we can get to the second project. This one is a bit trickier, but pretty similar to the second project in the link above. In “Exchange Data Using Bluetooth Serial”, they use a DS18B20 temperature sensor to sense the surrounding temperature and send the data to the phone. In our second project however, we use the previous OLED display + weather sensor configuration to show the sensor’s findings to our phone and print the words we input into our phone to the OLED Display.

Obviously, there are no pre-made codes so we have to get a bit creative. But we figured it out! Basically for the void setup(), we copy-paste the code from the OLED-Sensor project. For the void loop() part, we can divide the code into two parts, the first part is for displaying the weather sensor outputs on the phone and the second part is for receiving string input from the phone and display it on the OLED Display.

For the first part, you can simply use SerialBT.println() just like a normal println. That took me like 10 minutes + help from a friend to find out, which is a bit embarrassing. You can get the readings by using bmp.readTemperature, bmp.readPressure, and bmp.readAltitude. For the second part, you need to declare a variable with the String primitive type (let’s call this variable “message”). This variable will be the placeholder for the soon-to-be-displayed words. Then use SerialBT.read() to read the input in our phone, assign the input to another variable (such as incomingChar). After that, do a concatenation which basically means combining two or more String values into one, and by that I mean something like message = message + incomingChar. After that you can make the words appear on the OLED by using display.println(message).

Sorry if that’s a bit confusing, perhaps this video can help you all.

Here’s a Serial Bluetooth to OLED Display Demonstration.

Here’s a Weather Sensor Output on Phone Demonstration.

--

--