Esp32 Bluetooth communication


Recently, I want to make a heating pad, which can be controlled by mobile phone.

At the beginning, I thought about using WiFi to access Mi’s home for control, so that I could use voice assistant. However, after careful consideration, it was found that the scene was not right. If you use WiFi connection, it means you can only use it indoors.

So, in the end, I decided to use Bluetooth connection directly.

Hardware selection

Although Bluetooth connection is selected, in order to expand WiFi in the future, esp32 is selected as the hardware, which has the function of WiFi and Bluetooth connection, and the code is compatible with Arduino, so it is very convenient to use.

Bluetooth connection mode

  1. The initial idea is to generate the two-dimensional code from the MAC address of the hardware, scan the two-dimensional code to obtain the MAC address of the mobile phone software, connect and send instructions such as temperature setting.
  2. Later, it was found that it seems that you can directly use the device name for Bluetooth connection. In this way, all hardware devices can be set to the same device name, and the QR code can be omitted. It’s really good.
  3. Finally, I saw a way of Bluetooth broadcasting when I looked up the data. However, there is still a future and experiment to be done. I can try it if I have the opportunity in the future.

Temperature control mode

The temperature can be read by using the thermistor.

  1. The simplest temperature control can be directly controlled by relay switch. Set the upper and lower temperature range, stop heating until the upper range, and restart heating when the temperature is lower than the lower range.
  2. The higher order is to use PWM to adjust the power of the thermistor. The closer the power is to the target temperature, the smaller the power is. In this way, the temperature curve can be smoothed. Even if not, PID closed-loop control algorithm can be used to add the previous error and adjust it in real time.

Mobile software

Because I can’t do Android Software, now I just use a “Bluetooth serial port” app to send instructions directly and control the hardware.

In the future, we still need to learn Android and make a set of shelves.

Esp32 program

//This example code is in the Public Domain (or CC0 licensed, at your option.)
//By Evandro Copercini - 2018
//This example creates a bridge between Serial and Classical Bluetooth (SPP)
//and also demonstrate that SerialBT have the same functionalities of a normal Serial

#include "BluetoothSerial.h"

#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it

BluetoothSerial SerialBT;
char START_FLAG = '$';
char END_FLAG = '#';

void setup() {
  SerialBT.begin("ESP32test"); //Bluetooth device name
  Serial.println("The device started, now you can pair it with bluetooth!");

void SerialBT_sendMsg(String msg) {
  int i = 0;
  for (i = 0; i < msg.length(); i++) {

int NONE = 0;
int START = 1;
int pre_status = NONE;

int num = 0;
void loop() {
  if (SerialBT.available()) {
    char msg_char =;
    if (msg_char == START_FLAG) {
      num = 0;
      pre_status = START;
    } else if (msg_char == END_FLAG && pre_status == START) {
      if (num >= TEMPERATURE_MIN && num <= TEMPERATURE_MAX) {
        String msg = String("set temperature to " + String(num) + "\n");
      num = 0;
      pre_status = NONE;
    } else if (isDigit(msg_char) && pre_status == START) {
      num = num * 10 + (msg_char - '0');
    } else {
      num = 0;
      pre_status = NONE;

    // SerialBT_sendMsg(String(String(msg_char) + "\n"));



Esp32 Bluetooth communication