I recently wanted to make a heat pad that I could control with my phone.

At first, I thought about using wifi to control mi home, so that I could use voice assistant. But then I thought about it, and I found that the situation was wrong. If you use wifi, that means you can only use it indoors.

So, in the end, I decided to go straight to Bluetooth.

Hardware selection

Although bluetooth connection is selected, in order to facilitate wifi expansion in the future, ESP32 is selected for the hardware. It also has the function of wifi and Bluetooth connection, and the code is compatible with Arduino, which is very convenient to use.

Bluetooth connection

  1. The initial idea is to generate the TWO-DIMENSIONAL code of the MAC address of the hardware, and the mobile phone software can scan the two-dimensional code to obtain the MAC address, connect and send instructions such as temperature setting.
  2. Turns out, it looks like you can connect to the device directly using the device name, so you can set all your hardware to the same device name without having to use the QR code, which is nice.
  3. Finally, I saw a way of bluetooth broadcasting when I was looking up information, but I haven’t done the experiment yet. I can try it if I have the chance in the future.

Temperature control mode

The temperature can be read by using a temperature-sensitive resistor.

  1. The simplest temperature control can be directly controlled by a relay switch. Set the upper and lower temperature ranges to stop heating until the upper range is reached, and restart the heating when the lower range is reached.
  2. The higher order is to use PWM to adjust the power of the heating resistance, the closer to the target temperature, the smaller the power, so that the smooth temperature curve can be achieved. Even worse, the PID closed-loop control algorithm can be superimposed on the previous error, real-time adjustment.

Mobile phone software

Since I don’t know how to make Android software, NOW I just use an APP of “Bluetooth Serial Port” to directly send instructions and control the hardware.

Later still want to learn Android, make a set of shelves out.

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"

#if! defined(CONFIG_BT_ENABLED) || ! defined(CONFIG_BLUEDROID_ENABLED)
#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it
#endif

BluetoothSerial SerialBT;
char START_FLAG = '$';
char END_FLAG = The '#';
int TEMPERATURE_MIN = 0;
int TEMPERATURE_MAX = 50;

void setup(a) {
  Serial.begin(115200);
  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++) { SerialBT.write(msg[i]); }}int NONE = 0;
int START = 1;
int pre_status = NONE;

int num = 0;
void loop(a) {
  if (SerialBT.available()) {
    char msg_char = SerialBT.read();
    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");
        SerialBT_sendMsg(msg);
      }
      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"));
  }

  
    

  delay(20);
}
Copy the code