【ESP32】蓝牙模块开发入门



一、实验材料

  • ESP32
  • uPyCraft
  • 蓝牙调试器(直接在华为应用商城搜索下载即可)
  • flash_download_tools(用于更新esp32固件)

二、实验步骤

1. 烧录esp32固件

2. esp32代码

main.py:

import ubluetooth as bt
from ble.tools import BLETools
from ble.const import BLEConst

__UART_UUID = bt.UUID("6E400001-B5A3-F393-E0A9-E50E24DCCA9E")
__RX_UUID = bt.UUID("6E400002-B5A3-F393-E0A9-E50E24DCCA9E")
__TX_UUID = bt.UUID("6E400003-B5A3-F393-E0A9-E50E24DCCA9E")

__UART_SERVICE = (
	__UART_UUID,
	(
		(__TX_UUID, bt.FLAG_NOTIFY,),
		(__RX_UUID, bt.FLAG_WRITE,),
	),
)


class BLEUART:
	def __init__(self, ble, rx_callback=None, name="MSTIFIY", rxbuf=100):
		self.__ble = ble
		self.__rx_cb = rx_callback
		self.__conn_handle = None

		self.__write = self.__ble.gatts_write
		self.__read = self.__ble.gatts_read
		self.__notify = self.__ble.gatts_notify

		self.__ble.active(False)
		print("activating ble...")
		self.__ble.active(True)
		print("ble activated")

		self.__ble.config(rxbuf=rxbuf)
		self.__ble.irq(self.__irq)
		self.__register_services()

		self.__adv_payload = BLETools.advertising_generic_payload(
			services=(__UART_UUID,),
			appearance=BLEConst.Appearance.GENERIC_COMPUTER,
		)
		self.__resp_payload = BLETools.advertising_resp_payload(
			name=name
		)

		self.__advertise()

	def __register_services(self):
		(
			(
				self.__tx_handle,
				self.__rx_handle,
			),
		) = self.__ble.gatts_register_services((__UART_SERVICE,))

	def __advertise(self, interval_us=500000):
		self.__ble.gap_advertise(None)
		self.__ble.gap_advertise(interval_us, adv_data=self.__adv_payload, resp_data=self.__resp_payload)
		print("advertising...")

	def __irq(self, event, data):
		if event == BLEConst.IRQ.IRQ_CENTRAL_CONNECT:
			self.__conn_handle, addr_type, addr, = data
			print("[{}] connected, handle: {}".format(BLETools.decode_mac(addr), self.__conn_handle))

			self.__ble.gap_advertise(None)
		elif event == BLEConst.IRQ.IRQ_CENTRAL_DISCONNECT:
			self.__conn_handle, _, addr, = data
			print("[{}] disconnected, handle: {}".format(BLETools.decode_mac(addr), self.__conn_handle))

			self.__conn_handle = None
			self.__advertise()
		elif event == BLEConst.IRQ.IRQ_GATTS_WRITE:
			conn_handle, value_handle = data
      
			if conn_handle == self.__conn_handle and value_handle == self.__rx_handle:
				if self.__rx_cb:
					self.__rx_cb(self.__read(self.__rx_handle))

	def send(self, data):
		"""
		将数据写入本地缓存,并推送到中心设备
		"""
		self.__write(self.__tx_handle, data)

		if self.__conn_handle is not None:
			self.__notify(self.__conn_handle, self.__tx_handle, data)


def demo():

	def rx_callback(data):
		print("rx received: {}".format(data))
		uart.send("this is mstifiy")

	ble = bt.BLE()
	uart = BLEUART(ble, rx_callback)


demo()
  • 配置好uPyCraft中的串口和开发板型号,然后点击连接--->运行--->

3. 蓝牙调试

  • esp32程序运行后,蓝牙开始广播
    在这里插入图片描述

  • 打开手机端的蓝牙调试助手,搜索到名称为MSTIFIY的蓝牙设备并连接

  • 手机发送数据

    在这里插入图片描述

OK,这样手机和esp32之间的蓝牙通信就实现了~


遇到的问题和总结

  • 蓝牙调试助手连不上esp32的蓝牙

    检查一下名为MSTIFIY的蓝牙UUID设置是否正确

    正确的配置如上图所示

  • 各种运行报出的奇奇怪怪的错误

    esp32断电重插,或者端口断开重连,程序停止重新运行都可以尝试。。。

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐