ESP32经典蓝牙(比如HC05模块)的基本使用
一、基本功能与简单介绍
ESP32的经典蓝牙功能使用
- ESP32(通过串口)连接电脑
- ESP32(通过蓝牙)连接手机
- 电脑发送数据给ESP32,ESP32会将数据转发给手机。
- 手机发送数据给ESP32,ESP32会见数据转发给电脑。
二、主编辑器代码
#include <Arduino.h>
#include <BluetoothSerial.h>
BluetoothSerial SerialBT;
void setup()
{
Serial.begin(115200);
SerialBT.begin("DDG_xiaomi8"); // 如果没有参数传入则默认是蓝牙名称是: "ESP32"
SerialBT.setPin("1234"); // 蓝牙连接的配对码
Serial.printf("BT initial ok and ready to pair. \r\n");
}
void loop()
{
if (Serial.available())
{
SerialBT.write(Serial.read());
}
if (SerialBT.available())
{
Serial.write(SerialBT.read());
}
delay(1);
}