RW007 高速 WiFi 模块使用BLE主机功能连接 BLE 从机设备示例
在本文档中将会从零开始说明如何使用 RW007
的 BLE
主机功能去连接一个BLE
串口透传设备,实现数据交互的功能。
硬件准备
- 1.
STM32F411-NUCLEO
开发板,RW007 EVB
模块 - 2.
PC
电脑 - 3.
USB
数据线 - 4.
BLE
数据透传模块(如HC-08
模块、KT6368A
蓝牙模块等) - 5.
USB-TTL
模块(如CH340
等) - 6.杜邦线若干条
软件准备
- 1.
RT-Thread Studio IDE
软件 - 2.串口调试软件(
xshell
、SSCOM
等)
步骤一:串口配置 KT6368A蓝牙模块
在这里主要使用RW007
高速 WIFI BLE
模块,主要使用BLE
的主机功能,所以需要一个BLE
的从机设备(如HC-08
模块、KT6368A
蓝牙模块等都可以),因为手上刚好有KT6368A
蓝牙BLE
模块,所以这里简单说说怎样通过串口配置KT6368A
蓝牙模块,下图是KT6368A
蓝牙模块的控制指令说明.
- 操作1:设置 BLE 蓝牙模块的名称 AT+BMBLE_TEST 输入上述命令后,会正常返回
OK
表示设置成功。 说明:这里设置 BLE 模块的名称为BLE_TEST
- 操作2:设置 BLE 蓝牙模块的 MAC 地址
AT+BNAACCEEFFDDEE
输入上述命令后,会正常返回OK
表示设置成功。 说明:这里设置 BLE 蓝牙模块的 MAC 地址为AACCEEFFDDEE
- 操作3:设置芯片复位
AT+CZ
输入上述命令后,模块会正常重启,并打印下面的信息,说明配置成功了。
郃T+VER1.6-20210114\0 TM+BLE_TEST \0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0 TN+AACCEEFFDDEE TD+KT6368A-SPP-04\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0 TS+123456
- 操作4:手机通过
BLE调试助手
扫描附近的蓝牙设备 手机上需要自行安装BLE调试助手
等蓝牙工具软件。
从手机扫描的结果可以看到,前面步骤设置的BLE_TEST
蓝牙设备,并且可以发现MAC
地址并不是设置的AACCEEFFDDEE
,关于这个问题,在蓝牙模块的说明手册上有提示,具体原因可以百度。
- 操作5:手机通过
BLE调试助手
连接蓝牙设备进行通信 通过手机与蓝牙设备进行通信,验证蓝牙设备通信功能。
到此,验证BLE
从机设备通信功能,此时只需要把蓝牙设备的串口接在需要透传的设备上即可透传数据。
步骤二:使用STM32F411-NUCLEO
通过 RW007
BLE 功能测试BLE
蓝牙模块数据传输功能
可以分为以下 3 个步骤:
- 步骤(1):参考相关文档创建使用RT-Thread Studio IDE 工程
- 步骤(2):替换
RW007
软件驱动包 - 步骤(3):通过
BLE
测试命令与BLE
设备进行通信
步骤(1):参考相关文档创建使用RT-Thread Studio IDE 工程
由于使用RT-Thread Studio IDE
如何创建使用RW007
模块的工程,官方的文档中心有提供比较详细的文档,可以参考此文档进行创建,或可以直接使用官方提供的工程,在此进行详细的描述。
步骤(2):替换 RW007
软件驱动包
可以把提供的RW007
软件驱动包,替换到创建的RT-Thread Studio
工程目录下。
步骤(3):通过 BLE
测试命令与BLE
设备进行通信
当前面两个步骤完成后,可以在STM32F411-NUCLEO
的调试串口上看到对应的BLE
测试命令,此时BLE
设备(这里使用KT6368A
蓝牙模块进行演示)通过 USB-TTL
串口与PC 进行连接。
(1)查看RW007
BLE 相关命令
msh />rw
rw007_ble
msh />rw007_ble
[rw007_ble command]
rw007_ble help
rw007_ble init central/peripheral Note: init ble mode
rw007_ble get_addr Note: get ble address
rw007_ble update_params Note: update connect parameters(no support)
rw007_ble scan Note: scan ble slave
rw007_ble stop_scan Note: stop scan
rw007_ble connect xx:xx:xx:xx:xx:xx Note: use slave addr to connect
rw007_ble disconnect [conn_handle] Note: disconnect slave
rw007_ble get_server Note: discover all server(no support)
rw007_ble get_char Note: discover all description(no support)
rw007_ble get_disc Note: discover all characteristic(no support)
rw007_ble mtu_exch Note: ble mtu exchange(no support)
rw007_ble notify Note: enable ble notify(no support)
rw007_ble write Note: ble write data(no support)
rw007_ble read Note: ble read data(no support)
rw007_ble notify_change [conn_handle] [char_value] [UUID] Note: ble notify configure by uuid
rw007_ble write_uuid [conn_handle] [UUID] [data] Note: ble write data by uuid
rw007_ble read_uuid Note: ble read data by uuid(no support)
msh />
(2) RW007
BLE 功能初始化 输入命令:
rw007_ble init central
测试示例:
msh />rw007_ble init central 263 - ble_cmd_init start ble central
注意:目前仅支持central
主机模式
(3) RW007
扫描附近的BLE
设备 输入命令:
rw007_ble scan
测试示例:
msh />rw007_ble scan
318 - ble_cmd_scan
resp_type: 1, len: 59
RW007_BLE_NTF_TYPE_DISCOVER
received advertisement; event_type=0 rssi=-46 addr_type=0 addr=ee:dd:ff:ee:cc:aa
mfg_data = 02 01 06 03 03 f0 ff
resp_type: 1, len: 83
RW007_BLE_NTF_TYPE_DISCOVER
received advertisement; event_type=4 rssi=-47 addr_type=0 addr=ee:dd:ff:ee:cc:aa
mfg_data = 1e 09 42 4c 45 5f 54 45 53 54 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
可以从扫描结果中看到mac
地址为ee:dd:ff:ee:cc:aa
的设备,这个设备为前面通过串口设置BLE
模块的MAC
地址,后面将以这个地址进行连接设备。
(4)RW007
连接BLE
蓝牙设备 通过rw007_ble connect
命令连接BLE
设备 输入命令:
rw007_ble connect ee:dd:ff:ee:cc:aa
测试示例:
msh />rw007_ble connect ee:dd:ff:ee:cc:aa
341 - ble_cmd_connect
str_addr: ee:dd:ff:ee:cc:aa
mac addr: aa:cc:ee:ff:dd:ee
msh />resp_type: 1, len: 52
RW007_BLE_NTF_TYPE_CONNECT
type: 0, status: 0, conn_handle: 1
resp_type: 1, len: 44
RW007_BLE_NTF_TYPE_CONNECT_DESC
our_id_addr: c9:47:8c:42:00:48
peer_id_addr: aa:cc:ee:ff:dd:ee
(5)RW007
向蓝牙设备发送数据 由于目前大部分BLE
设备通过UUID
进行数据的收发. 从上面的 UUID
说明可知,特征 1 的UUID
为 “FFF1”
,特征是“WRITE”
、 “NOTIFY”
,这里可以直接使用FFF1
作为测试的UUID
. 输入命令:
rw007_ble write_uuid 1 fff1 Hello,BLE_TEST
测试示例:
msh />rw007_ble write_uuid 1 fff1 Hello,BLE_TEST 434 - ble_cmd_gattc_write_by_uuid write conn_hanle:1 uuid:fff1 data:Hello,BLE_TEST write rc:0 msh />
(6)RW007
接收蓝牙设备的数据 输入命令:
rw007_ble notify_change 1 1 0xfff1
测试示例:
msh />rw007_ble notify_change 1 1 0xfff1
415 - ble_cmd_gatt_notify_change_by_uuid
到此,使用STM32F411-NUCLEO
通过 RW007
BLE 功能测试BLE
蓝牙模块数据传输功能完成。