0

Bluetooth example for MRMS ESP32: Arduino, IMU, eFuse, BT, WiFi, CAN Bus (mrm-esp32)

Prerequisites

If not already done so already, install Arduino software and make basic connections, as described in page for MRMS ESP32: Arduino, IMU, eFuse, BT, WiFi, CAN Bus (mrm-esp32).

Task

A wireless communication for a mobile robot is almost mandatory and Bluetooth is a good choice. This microcontroller board features ESP32 which contains radio part with Bluetooth functionality.

Program

Download a Bluetooth terminal app in your smartphone. There are many options. Authors of BluetoothSerial library recommend https://play.google.com/store/apps/details?id=de.kai_morich.serial_bluetooth_terminal (for Android) and https://itunes.apple.com/us/app/hm10-bluetooth-serial-lite/id1030454675 (for IOS).

Here is a sketch for our demonstration:

#include "BluetoothSerial.h"

BluetoothSerial SerialBT;

void setup() {
  Serial.begin(115200); // To PC via UART/USB
  SerialBT.begin("ESP32test"); // Bluetooth device name
}

void loop() {
  if (Serial.available()) {
    uint8_t ch = Serial.read();
    SerialBT.write(ch);
  }
  
  if (SerialBT.available()) {
    uint8_t ch = SerialBT.read();
    Serial.write(ch);
  }
}
It defines SerialBT as an object of class BluetoothSerial. setup() starts serial communications, first with Your USB-connected PC, and then with ESP32's Bluetooth hardware. loop() intercepts each character sent by PC terminal application and sends it to the mobile phone (first part). The second one uses the other direction, characters arriving from the phone are being sent to the PC. Upload the sketch.

Only after uploading and running the sketch will ESP32 be visible when scanning for new Bluetooth devices. Use the phone now to scan new devices, find the one called "ESP32test", and pair the device. Open the installed Bluetooth terminal app and set it up to use the paired ESP32. If the terminal presents an option to choose between Bluetooth LE and Bluetooth Classic, use the later one. Bluetooth LE is not for data transfer of this kind.

Open Arduino's monitor, type some characters in the upper input box, and press "Send". The characters should appear in Your phone. The other direction should work, too: typing some text in phone's monitor will send it to the PC. You can use the link to issue commands to Your robot, to get its status and to get debugging information.