Coding Camp - Part 12: Serial communication over Bluetooth

In this article, we will make a wireless bridge to our smartphone using the Bluetooth RFCOMM protocol stack.

(Figure 1 - Send Message to the ODROID-GO) (Figure 2 - Receive Message from the ODROID-GO)

Before we begin make sure that you've followed the guides at Getting started with Arduino and Arduino for ODROID-GO - Hello World. Additionally, you can always refer to the Arduino’s official documentation. This tells us lots of useful common functions with great instructions at https://www.arduino.cc/reference/en/. The ESP32 official programming guide is also a great place further information, most of the ESP32 specific functions are introduced at https://esp-idf.readthedocs.io/en/v3.0/. Finally, the original source of this guide can be found on the ODROID-GO wiki page at https://wiki.odroid.com/odroid_go/arduino/07_bluetooth_serial.

Bluetooth Serial

The ESP32, which is used on ODROID-GO, supports Bluetooth 4.2, so we can program Bluetooth features with helpful Arduino libraries. In this guide, we're going to use the BluetoothSerial.h library:

#include "BluetoothSerial.h"

BluetoothSerial serialBT;

void setup() {
Serial.begin(115200);

serialBT.begin("ODROID-GO");
Serial.println("The device started, now you can pair it with bluetooth!");
}

void loop() {
if (Serial.available()) {
serialBT.write(Serial.read());
}
if (serialBT.available()) {
Serial.write(serialBT.read());
}
delay(20);
}
Define an instance for BluetoothSerial as a global variable called serialBT. To set it, we use begin() function having a parameter indicates the name of the Bluetooth device for the Bluetooth scanners and name it ODROID-GO. In the loop() function, if a message from the Serial monitor available, send that message to the connected device via Bluetooth serial. On the other hand, if a message from the Bluetooth serial available, send that message to the host.

Serial Monitor

You can use a Serial monitor to watch the debug messages from the Serial port, which can be fund in the Tools → Serial Monitor menu. Alternatively, you can press CTRL-SHIFT-M to open it more quickly. To show the message properly, you should set the bandwidth to 115200 baud. This would be very helpful debugging tool for you. In this guide, we're using this tool to communicate with the connected device.

Figure 3- The Serial Monitor
Figure 3- The Serial Monitor

Connect and communicate

We tested with the Serial Bluetooth Terminal application on Android Playstore, on a Galaxy Note 8. It finds a Bluetooth device named ODROID-GO and can connect to that.

Figure 4 - Bluetooth Pairing with a Smartphone
Figure 4 - Bluetooth Pairing with a Smartphone

And in the app, select ODROID-GO on the Devices tab.

Figure 5 - Bluetooth Serial App Pairing
Figure 5 - Bluetooth Serial App Pairing

Push the connect button on the top of the screen at the Terminal tab, then these can communicate each other.

Figure 6 - Serial App After Pairing
Figure 6 - Serial App After Pairing

Figure 7 - Serial App Showing Text To and From the ODROID-GO
Figure 7 - Serial App Showing Text To and From the ODROID-GO

Of course you can do also do this using iOS or a PC/laptop if it has Bluetooth capabilities.

A completed example

The complete example is available as by clicking the Files → Examples → ODROID-GO → BT_Serial menu to import the code, then pressing CTRL-U to compile/upload.

Figure 8 - Loading the Bluetooth Example
Figure 8 - Loading the Bluetooth Example

Be the first to comment

Leave a Reply