ESP32 Arduino: Serial communication over Bluetooth Hello World

The objective of this post is to explain how to get started with the BluetoothSerial ESP32 library, in order to send data to a emulated Serial connection, operating over Bluetooth classic. The tests of this ESP32 tutorial were performed using a DFRobot’s ESP-WROOM-32 device integrated in a ESP32 FireBeetle board.


Introduction

The objective of this post is to explain how to get started with the BluetoothSerial ESP32 library, in order to send data to a emulated Serial connection, operating over Bluetooth classic.

At the time of writing, the mentioned library had just been added to the ESP32 Arduino core. So, you may need to update to the latest version of the Arduino core. You can check here how to do it.

One important thing to mentioned is that this is a very high level library that will hide from us most of the Bluetooth implementation details, which is why the code we are going to develop is very simple and small.

In terms of API, it will be very similar to the regular Serial communication functions we use on the Arduino environment.

Note that the example provided here is based on the library example available on the Arduino core, which I encourage you to try.

The tests of this ESP32 tutorial were performed using a DFRobot’s ESP-WROOM-32 device integrated in a ESP32 FireBeetle board.

If you prefer, you can check the video tutorial at my Youtube channel.


The code

The first thing we need to do is including the BluetoothSerial.h library, which will expose the functionalities needed to work with serial over Bluetooth.

#include "BluetoothSerial.h"

Next we will need an object of class BluetoothSerial, which is the one we will use to initialize the Bluetooth stack on the ESP32 and to send the data.

You can check the implementation file for this class here. Under the hood, this class makes use of IDF’s Bluetooth classic API, which we have been covering in previous posts.

BluetoothSerial SerialBT;

Now that we have our Bluetooth object, we need to initialize the Bluetooth stack, so other devices can see our ESP32 and pair with it, before initializing the serial communication.

To do so, we simply need to call the begin method of the BluetoothSerial object, which will handle all of the lower level initialization for us.

This method receives as input a string with the name we want to assign to the device, which will be seen by other Bluetooth enabled devices that will discover it.

As output, it returns a Boolean value indicating if the initialization was successful or not. For now we will not look into that value, but for a robust code you should always include error checks.

We will do the mentioned initialization in the Arduino Setup function.

void setup() {
  SerialBT.begin("ESP32");
}

Now that we have initialized the device, we will periodically send a “Hello World” message on the Arduino main loop.

As can be seen in the header file of the BluetoothSerial class, it inherits from the Stream class. The Stream class inherits from the Print class, which means that we can use the println method to write to the Bluetooth serial port.

SerialBT.println("Hello World");

Between each iteration of the main loop, we will do a small 1 second delay. The full source code can be seen below.

#include "BluetoothSerial.h"

BluetoothSerial SerialBT;

void setup() {
  SerialBT.begin("ESP32");
}

void loop() {

  SerialBT.println("Hello World");
  delay(1000);
}

 

Testing the code

To test the code, simply compile it and upload it to your ESP32 using the Arduino IDE. Once it finishes, go to your computer’s Bluetooth devices and start a scan. The ESP32 should get detected, as shown in figure 1.

ESP32 Bluetooth Classic Serial Communication Hello World.png

Figure 1 – ESP32 detected as Bluetooth device on Windows 8 (menus in Portuguese).

In my case, I already had the device paired. If you haven’t yet, pair with it. Once it is paired, you should get a new COM port available on your computer. On Windows 8, you can check it at the device manager, as can be seen in figure 2.

Windows 8 Serial over Bluetooth COM at device manager.png

Figure 2 – New Bluetooth over serial COM port detected in Windows 8.

You can now go back to the Arduino IDE and this COM port should be on the list of the available COM ports that we can connect to using the Serial Monitor. Choose it and open the Serial Monitor. You can check the expected result at figure 3, which shows the message we defined in the code getting printed.

ESP32 Arduino Bluetooth Classic Hello World Serial Monitor.png

Figure 3 – Output of the program on the Arduino IDE serial monitor.


Related posts

Advertisements
This entry was posted in ESP32 and tagged , , , , , , . Bookmark the permalink.

15 Responses to ESP32 Arduino: Serial communication over Bluetooth Hello World

  1. Jeremy Griffith says:

    I love this. A couple years ago, I did something similar for a senior project. We basically wrote an Android app which would communicate with an Arduino via Bluetooth. Based on certain commands, the Arduino would drive a solenoid which would move a deadbolt in a door. Fun stuff!

    Liked by 1 person

  2. Pingback: ESP32 Arduino: Getting the Bluetooth Device Address | techtutorialsx

  3. ristomatti says:

    Thank you! I have no idea how long it would’ve taken for me to notice the new library.

    Liked by 1 person

    • antepher says:

      You’re welcome 🙂 I’ve also stumbled on it by chance, I was looking at the Arduino core PRs to check if there was something new coming and there it was. Works very well, a great job by Copercini, the author of the PR.

      Best regards,
      Nuno Santos

      Like

      • ristomatti says:

        Yep just tried it for debugging on the phone screen and it worked like a charm! I’ve been eyeballing the bluetooth examples every now and then and they’ve seemed too much trouble to investigate. This API is a great abstraction to all that madness. 🙂

        Thank you also for the other posts on ESP32/Arduino! I came across your blog just today while looking for code examples for taking advantage of the second core. as I saw it being demostrated on Andreas Spies YouTube channel. I ended up reading all the other posts also, very useful and well written posts!

        I found the info I needed on the use of the second core. I’m planning to see if it would make sense on a NRF24L01 to wifi/MQTT gateway device I’m building to route traffic from AVR/STM32 based nodes. I’m also planning to try the queues you had another article about. Basic idea would be to run the wifi/MQTT code on the default core and the RF24/RF24Network/RF24Mesh library loop process on the other. There’d be a queue for each direction (MQTT->NRF24 and NRF->MQTT).

        Liked by 1 person

        • antepher says:

          Thank you very much for the feedback 🙂

          This is indeed a very nice abstraction, specially since most of the times we only want to exchange some data using the Bluetooth interface.

          Nonetheless, the IDF API brings some more flexibility, which may be needed for more advanced use cases.

          I’m trying to cover both on tutorials, to give a better understanding of what happens under the hood and which additional functionalities we can get from using it.

          I’m glad you are finding the other posts useful 🙂 I would like to go back to some more FreeRTOS stuff, there’s still plenty of functionalities I would like to explore.

          Andreas Spies has awesome videos, I’ve been following his Youtube channel for a while and really recommend it.

          Regarding your project, it seems a very interesting one. I have a couple of NRF24L01 here but never had the chance to test them. Are they reliable? I’ve heard some good stuff about them.

          It seems a good approach, using queues. A good thing about them is that you can have a FreeRTOS task waiting on them blocked while there is no data, leaving the CPU available for other tasks.

          Let us know if it works fine 🙂

          Best regards,
          Nuno Santos

          Like

  4. Pingback: ESP32 Arduino Bluetooth over Serial: Receiving data | techtutorialsx

  5. Pingback: ESP32 Arduino Bluetooth: Finding the device with Python | techtutorialsx

  6. Ardxb says:

    Hello
    I am using Arduino 1.8.4
    I downloaded and unzipped the Arduino-esp32 directory inside library folder of Arduino 1.8.4
    I restarted the Arduino IDE
    I keep getting the error message that BluetoothSerial.h not found. The files are present.
    Need some help.
    Thanks

    Liked by 1 person

    • antepher says:

      Hi!

      Sorry for the delay. Does the rest work? If you try to use another ESP32 library such as the HTTPClient, does it work?

      I never ran into that issue, but I’ve installed / updated my libraries always using the method indicated on the GitHub page of the Arduino core, or using git directly on the command line:
      https://github.com/espressif/arduino-esp32#installation-instructions

      My recommendation is that you try an installation following that procedure, it will most likely cause you less problems when updating, for example.

      Let me know if it solves the problem.

      Best regards,
      Nuno Santos

      Like

  7. Pingback: ESP32 Arduino Bluetooth Classic: Controlling a relay remotely | techtutorialsx

  8. Rod McMahon says:

    Hi
    Could you confirm he number of bytes in a serial packet that can be sent with this simple arduino BT print command and the rate (no of times/second) that it could be sent through the ESP32.

    Liked by 1 person

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s