一、目的
这一节我们学习如何使用我们的ESP32开发板通过蓝牙和手机进行通信。
二、环境
ESP32 + 手机(笔者用的小米10) + Thonny IDE
三、蓝牙介绍
这个知识大家自行百度吧,这里不再赘述什么是蓝牙和蓝牙的历史,以及相关的专业知识。
四、手机蓝牙调试APP
用的最多的是这三个:nRF Connect、BLE调试助手、LightBlue
笔者用的是:BLE调试助手

为什么用这个?因为小米手机应用商店直接可以安装,其他两个没搜到,哈哈。。。大家按需安装哈!
nRF Connect GitHub 地址:
五、调试代码
大家先把下面的代码,拷贝到Thonny IDE中,保存到ESP32开发板中。
- from machine import Pin
- from machine import Timer
- from time import sleep_ms
- import bluetooth
-
- BLE_MSG = "" # 定义一个空的变量
-
-
- class ESP32_BLE():
- def __init__(self, name):
- self.led = Pin(2, Pin.OUT) # 创建LED对象,此LED为板载的蓝色LED,它用的是GPIO口2
- self.timer1 = Timer(0) # 定时器,ESP32有4个硬件定时器,此处使用的是0
- self.name = name # 传进来的参数name
- self.ble = bluetooth.BLE() # 创建BLE对象
- self.ble.active(True) # 启动蓝牙
- self.ble.config(gap_name=name) # 配置蓝牙,给蓝牙起个名字
- self.disconnected() # 调用函数执行计时器
- self.ble.irq(self.ble_irq) #蓝牙调用中断函数。当手机发送数据给ESP32, ESP32蓝牙收到数据后自动执行此中断
- self.register() # 注册
- self.advertiser() # 广播
-
- def connected(self):
- self.led.value(1) # 值为1表示蓝色LED亮
- self.timer1.deinit() # 取消计时器
-
- def disconnected(self):
- # 初始化定时器,设定周期100ms,模式为周期性的,回调函数
- self.timer1.init(period=100, mode=Timer.PERIODIC, callback=lambda t: self.led.value(not self.led.value()))
-
- def ble_irq(self, event, data):
- global BLE_MSG
- if event == 1: #_IRQ_CENTRAL_CONNECT 手机已连接此设备
- self.connected() # 调用函数,实现蓝牙没连上时闪烁,连上后长亮的效果
- elif event == 2: #_IRQ_CENTRAL_DISCONNECT 手机已断开此设备
- self.advertiser() # 调用函数,进行蓝牙广播
- self.disconnected() # 蓝牙断开连接后,蓝色LED又会闪烁
- elif event == 3: #_IRQ_GATTS_WRITE 手机发送数据给此设备
- buffer = self.ble.gatts_read(self.rx) # 接收来自手机的数据
- BLE_MSG = buffer.decode('UTF-8').strip() # 将受到的数据安装utf-8进行解码,所以手机发送数据时应该为utf-8编码
-
- def register(self):
- service_uuid = '6E400001-B5A3-F393-E0A9-E50E24DCCA9E'
- reader_uuid = '6E400002-B5A3-F393-E0A9-E50E24DCCA9E'
- sender_uuid = '6E400003-B5A3-F393-E0A9-E50E24DCCA9E'
-
- # 蓝牙是通过服务来工作的
- services = (
- # 定义一个控制灯的服务
- ( # 这个括号里的代表一个服务
- bluetooth.UUID(service_uuid), # 服务的ID
- (
- (bluetooth.UUID(sender_uuid), bluetooth.FLAG_NOTIFY), # 服务类型:通知
- (bluetooth.UUID(reader_uuid), bluetooth.FLAG_WRITE), # 服务类型:写入
- )
- ),
- # 下面也可以定义别的服务
- # -- 其他服务
- )
-
- ((self.tx, self.rx,), ) = self.ble.gatts_register_services(services)
-
- def send(self, data):
- self.ble.gatts_notify(0, self.tx, data + '\n') # 蓝牙发送通知信息
-
- def advertiser(self): # 当断开连接时,开始广播
- name = bytes(self.name, 'UTF-8') # 将设备名字转为utf-8格式
- adv_data = bytearray('\x02\x01\x02') + bytearray((len(name) + 1, 0x09)) + name
- self.ble.gap_advertise(100, adv_data)
- print(adv_data)
- print("\r\n")
-
-
- def buttons_irq(pin): # 创建BOOT按键函数
- led.value(not led.value())
- print('LED is ON.' if led.value() else 'LED is OFF')
- ble.send('LED is ON.' if led.value() else 'LED is OFF')
-
-
- if __name__ == "__main__":
- ble = ESP32_BLE("ESP32 BLE") # 创建一个对象,带1个实参,后面用来给BLE起名字用
-
- but = Pin(0, Pin.IN) # 创建对象,ESP32开发板的USB接口的右边的BOOT按键的GPIO口为0
- but.irq(trigger=Pin.IRQ_FALLING, handler=buttons_irq) # 设置中断
-
- led = Pin(2, Pin.OUT) # 创建蓝色LED对象
-
- while True:
- if BLE_MSG == 'led state': # 如果ESP32收到手机发来的数据
- print("Query: " + BLE_MSG) # 打印出读取到的信息
- BLE_MSG = ""
- print('LED is on\n' if led.value() else 'LED is off\n') # 如果灯亮输出LED is on.
- sleep_ms(8000) # 设置为5秒,大家发送查询命令后,手机赶紧切换到通知页面,等待接收ESP32发送的通知信息
- ble.send('LED state is on' if led.value() else 'LED state is off') # 蓝牙发送led数据给手机,如果灯亮发给手机“LED is on.”
- elif BLE_MSG == 'led on': # 如果ESP32收到手机发来的数据
- print("Command: " + BLE_MSG) # 打印出读取到的信息
- BLE_MSG = ""
- if led.value() != 1:
- led.value(1)
- print('LED state is on\n' if led.value() else 'LED state is off\n')
- elif BLE_MSG == 'led off': # 如果ESP32收到手机发来的数据
- print("Command: " + BLE_MSG) # 打印出读取到的信息
- BLE_MSG = ""
- if led.value() != 0:
- led.value(0)
- print('LED state is on\n' if led.value() else 'LED state is off\n')
-
- sleep_ms(100)
六、调试效果
我们在Thonny中运行代码,会看到ESP32 的蓝色LED灯开始闪烁


Thonny IDE 打印信息:

然后我们在手机上打开BLE调试助手,连接我们的开发板

然后看到显示“连接成功”


此时ESP32开发板的蓝色LED灯停止闪烁 ,变为长亮

然后我们就可以读取一些信息,比如获得开发板的名字:

然后我们就可以控制开发板的蓝色LED灯,查询灯的状态和控制灯的亮灭

然后我们切换到通知页面,代码会返回查询的结果:


关灯:

开灯:

然后我们在Thonny IDE中看到如下打印信息:
