RW007系列连载1RW007-BLE主机通信使用学习示例与综合Demo
Posted RT-Thread物联网操作系统
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了RW007系列连载1RW007-BLE主机通信使用学习示例与综合Demo相关的知识,希望对你有一定的参考价值。
本期文章将分为5篇连载,前2篇将围绕模块功能详解展开,其后3篇将为大家讲解基于RW007相关DEMO的实现,文章目录如下:
步骤1:更新 “RW007” 模块的固件
由于默认RW007
模块的固件不支持BLE
主机功能,所以需要使用相关的升级工具进行升级。具体操作请参考附件中对应的文档进行操作。
步骤2:更新主机的 “RW007” 软件驱动包
由于v2.0.1
版本的RW007
软件驱动包中,不包括BLE
功能,所以需要手动更新RW007
的软件驱动包,请使用提供的软件驱动包替换到主机软件包的目录下。
步骤3:测试 “RW007” 模块的 “BLE” 功能
根据前面的操作步骤,确保RW007
模块自身的固件已经更新和RW007
驱动软件包已更新替换到使用的工程中,在提供的软件驱动包中,已经包含了部分 BLE
功能的测试命令,可以在串口调试中通过msh
命令,进行调试。
1msh />rw007_ble
2[rw007_ble command]
3
4rw007_ble help
5rw007_ble init central/peripheral Note: init ble mode
6rw007_ble get_addr Note: get ble address
7rw007_ble update_params Note: update connect parameters(no support)
8rw007_ble scan Note: scan ble slave
9rw007_ble stop_scan Note: stop scan
10rw007_ble connect xx:xx:xx:xx:xx:xx Note: use slave addr to connect
11rw007_ble disconnect [conn_handle] Note: disconnect slave
12rw007_ble get_server Note: discover all server(no support)
13rw007_ble get_char Note: discover all description(no support)
14rw007_ble get_disc Note: discover all characteristic(no support)
15rw007_ble mtu_exch Note: ble mtu exchange(no support)
16rw007_ble notify Note: enable ble notify(no support)
17rw007_ble write Note: ble write data(no support)
18rw007_ble read Note: ble read data(no support)
19rw007_ble notify_change [conn_handle] [char_value] [UUID] Note: ble notify configure by uuid
20rw007_ble write_uuid [conn_handle] [UUID] [data] Note: ble write data by uuid
21rw007_ble read_uuid Note: ble read data by uuid
22
23msh />
如上可知,有部分BLE
功能暂时还没支持的,下面将详细说一说BLE
测试命令的使用。
BLE 功能初始化
BLE
功能初始化函数,使用 BLE
功能必须调用。
1.RW007
BLE 设备初始化为主机设备。
1rw007_ble init central
测试示例
1msh />rw007_ble init central
2122 - ble_cmd_init
3start ble central
4msh />
2.RW007
BLE 设备初始化为从机设备
注意:目前不支持使用从机模式。
1rw007_ble init peripheral
BLE 功能获取本机的BLE设备地址
由于BLE设备的地址分为public
公共地址和random
随机地址。
1rw007_ble get_addr
测试示例:
1msh />rw007_ble get_addr
2273 - ble_cmd_get_addr
3msh />resp_type: 0, len: 12
4RW007_BLE_RSP_TYPE_ADDR_GET
5public_id_addr = 48:00:42:8c:47:c9
6random_id_addr = 00:04:5c:43:00:00
BLE 功能扫描设备
目前RW007
BLE 功能支持通过 scan
命令,扫描周围的BLE
设备。
1.执行扫描周围BLE
设备
1rw007_ble scan
测试示例:
1msh />rw007_ble scan
2176 - ble_cmd_scan
3msh />resp_type: 1, len: 65
4RW007_BLE_NTF_TYPE_DISCOVER
5received advertisement; event_type=0 rssi=-60 addr_type=0 addr=4c:ed:fb:00:04:b1
6resp_type: 1, len: 56
这里的 addr
地址内容可以为后面连接设备使用,由于目前没有直接显示对应 BLE 设备的名称,需要先人工确定 BLE 设备的地址。
2.停止扫描周围BLE
设备
该指令用于 RW007
模块正在执行scan
扫描操作过程中,马上停止扫描的动作,可以执行此命令。
1rw007_ble stop_scan
BLE 功能设备连接
目前 RW007
BLE 功能设备支持通过 BLE
设备的类mac
地址来连接设备,该命令仅支持主机模式去连接从机设备的类mac
地址,连接成功后会有对应的conn_handle
值,这个conn_handle
值在其他功能沿用。
1rw007_ble connect xx:xx:xx:xx:xx:xx
测试示例:
1msh />rw007_ble connect 4c:ed:fb:00:04:b1
2198 - ble_cmd_connect
3str_addr: 4c:ed:fb:00:04:b1
4mac addr: b1: 4: 0:fb:ed:4c
5msh />resp_type: 1, len: 52
6RW007_BLE_NTF_TYPE_CONNECT
7type: 0, status: 0, conn_handle: 1
8
9resp_type: 1, len: 44
10RW007_BLE_NTF_TYPE_CONNECT_DESC
11our_id_addr: c9:47:8c:42:00:48
12peer_id_addr: b1:04:00:fb:ed:4c
BLE 功能设备断开连接
目前RW007
BLE 功能设备支持主动断开从设备的连接。
1rw007_ble disconnect [conn_handle]
其中 conn_handle
与 connect
连接时的需要一致。
1msh />rw007_ble disconnect 1
2341 - ble_cmd_disconnect
3msh />resp_type: 1, len: 52
4RW007_BLE_NTF_TYPE_DISCONN
BLE 功能写数据
目前RW007
BLE 功能写数据,支持通过指定UUID
来写对应的数据。
1rw007_ble write_uuid [conn_handle] [UUID] [data]
指令说明:[conn_handle]
为 RW007
连接设备时生成的对应值,[UUID]
为对应 BLE
设备的特征参数,[data]
为需要发送的数据。
测试示例:
1msh />rw007_ble write_uuid 1 ffe1 Hello,RT-Thread....
2402 - ble_cmd_gattc_write_by_uuid
3write conn_hanle:1 uuid:ffe1 data:Hello,RT-Thread....
4write rc:0
5msh />
注意:目前新的RW007 驱动中已经支持通过 128 位的 UUID
进行写数据操作
测试示例:
1msh />rw007_ble write_uuid 1 ebe0ccbe7a0a4b0c8a1a6ff2997da3a6 00
2data: 00
3write conn_hanle:1 uuid:ebe0ccbe7a0a4b0c8a1a6ff2997da3a6
4write rc:0
说明:测试示例中的 [conn_handle]
为 1,[UUID]
为ebe0ccbe7a0a4b0c8a1a6ff2997da3a6
,[data]
为 0x00
(这里是使用 hex 值)
BLE 功能更新notify 参数
目前 RW007
BLE 功能支持修改接收 notify
参数。
1rw007_ble notify_change [conn_handle] [char_value] [UUID]
参数说明:
[conn_handle]
为 RW007
连接设备时生成的对应值
[char_value]
为配置参数,具体如下
10:disable indication¬ification
21:enable notification,disable indication
32:enable indication, disable notification
[UUID]
为对应 BLE
设备的特征参数值。
BLE 功能关于广播包类型的说明
目前 RW007
BLE 功能中可以关于广播包 event
的类型可以分为下面几种。
1/* Advertising report */
2#define BLE_HCI_ADV_RPT_EVTYPE_ADV_IND (0)
3#define BLE_HCI_ADV_RPT_EVTYPE_DIR_IND (1)
4#define BLE_HCI_ADV_RPT_EVTYPE_SCAN_IND (2)
5#define BLE_HCI_ADV_RPT_EVTYPE_NONCONN_IND (3)
6#define BLE_HCI_ADV_RPT_EVTYPE_SCAN_RSP (4)
广播包结构如下:
1广播包结构描述:
2struct ble_gap_disc_desc {
3 uint8_t event_type;
4 uint8_t length_data;
5 ble_addr_t addr;
6 int8_t rssi;
7 uint8_t *data;
8
9 /***
10 * LE direct advertising report fields; direct_addr is BLE_ADDR_ANY if
11 * direct address fields are not present.
12 */
13 ble_addr_t direct_addr;
14};
15
16通过event_type区分出不同的广播包类型,所有类型分为如下几种:
17/* Advertising report */
180 : 普通广播数据包
191 : 直接广播包
202 : 扫描请求包
213 : 不可连接广播指示
224 : 扫描响应数据包
因此,在设备的scan
扫描回应中可以通过对应的event_type
来判定是哪种广播包的类型。
在 RW007
的驱动代码中,可以通过下面的event_type
来判断是哪种类型的广播包,可以让用户自行实现对应功能。
1case RW007_BLE_NTF_TYPE_DISC:
2 {
3 rt_kprintf("RW007_BLE_NTF_TYPE_DISCOVER\\n");
4 ....
5 switch(event_type)
6 {
7 case BLE_HCI_ADV_RPT_EVTYPE_ADV_IND:
8
9 break;
10 case BLE_HCI_ADV_RPT_EVTYPE_DIR_IND:
11
12 break;
13 case BLE_HCI_ADV_RPT_EVTYPE_SCAN_IND:
14
15 break;
16 case BLE_HCI_ADV_RPT_EVTYPE_NONCONN_IND:
17
18 break;
19 case BLE_HCI_ADV_RPT_EVTYPE_SCAN_RSP:
20
21 break;
22 }
23 ....
24 }
测试示例:
1.初始化 RW007
BLE 功能为主机模式。
1msh /> rw007_ble init central
2263 - ble_cmd_init
3start ble central
4msh />
2.执行BLE
扫描指令
1msh />rw007_ble scan
2318 - ble_cmd_scan
3msh />resp_type: 1, len: 83
4RW007_BLE_NTF_TYPE_DISCOVER
5received advertisement; event_type=3 rssi=-56 addr_type=1 addr=41:ef:77:50:2b:29
6mfg_data = 1e ff 06 00 01 09 20 02 5b 5d cd 33 a4 8c c7 c5 36 ac de 12 ab 17 87 89 e1 84 e9 19 ec c4 ad
7
8resp_type: 1, len: 83
9RW007_BLE_NTF_TYPE_DISCOVER
10received advertisement; event_type=3 rssi=-65 addr_type=1 addr=3c:2e:33:ea:8d:aa
11mfg_data = 1e ff 06 00 01 09 20 02 28 4c cf 9b a7 f2 7d f6 c7 7e d9 54 ac dd 91 a6 ee 0d 22 43 e1 8f e3
12
13resp_type: 1, len: 83
14RW007_BLE_NTF_TYPE_DISCOVER
15received advertisement; event_type=3 rssi=-59 addr_type=1 addr=42:18:ab:80:5c:11
16mfg_data = 1e ff 06 00 01 09 20 02 eb bf cf 15 97 aa 50 7f a4 57 25 d9 21 35 71 eb 6f aa a1 56 5c d5 e2
3.观察event_type=
可以确定是哪种广播包类型。
1RW007_BLE_NTF_TYPE_DISCOVER
2received advertisement; event_type=0 rssi=-64 addr_type=1 addr=61:63:46:c3:49:99
3mfg_data = 02 01 1a 02 0a 0c 0b ff 4c 00 10 06 00 19 1d 6d 63 18
4
5resp_type: 1, len: 52
6RW007_BLE_NTF_TYPE_DISCOVER
7received advertisement; event_type=4 rssi=-64 addr_type=1 addr=61:63:46:c3:49:99
8mfg_data =
9
10resp_type: 1, len: 73
11RW007_BLE_NTF_TYPE_DISCOVER
12received advertisement; event_type=0 rssi=-73 addr_type=0 addr=91:21:52:00:4e:b8
13mfg_data = 14 ff 4c 00 07 0f 00 02 20 91 21 52 00 4e b8 f5 58 58 3c 39 00
BLE 功能读数据
目前 RW007
支持通过 UUID
读取数据的功能
1rw007_ble read_uuid [conn_handle] [UUID]
参数说明:
[conn_handle]
为 RW007
连接设备时生成的对应值
[UUID]
为对应 BLE
设备的特征参数。
测试示例:
1msh />rw007_ble read_uuid 1 2a24
2read conn_hanle:1 uuid:2a24
3read rc:0
4msh />ble data input packet resp_type: 0, len: 18
5RW007_BLE_RSP_TYPE_READ
6connect:1 attr_handle:14 uuid: 24 2a read data:
700000000: 4C 59 57 53 44 30 33 4D 4D 43 00 LYWSD03MMC.
说明:这里的 [conn_handle]
为 1,[UUID]
为 2a24
,读取到的数据为 BLE 设备的名称。
目前 RW007 模块已经支持读取 128 位的 UUID 的功能。
测试示例:
1msh />rw007_ble read_uuid 1 ebe0ccbe7a0a4b0c8a1a6ff2997da3a6
2read conn_hanle:1 uuid:ebe0ccbe7a0a4b0c8a1a6ff2997da3a6
3read rc:0
4msh />ble data input packet resp_type: 0, len: 22
5RW007_BLE_RSP_TYPE_READ
6connect:1 attr_handle:51 uuid: a6 a3 7d 99 f2 6f 1a 8a 0c 4b 0a 7a be cc e0 eb read data:
700000000: 00
说明:
这里的 [conn_handle]
为 1,[UUID]
为128位的 ebe0ccbe7a0a4b0c8a1a6ff2997da3a6
,读取到的数据值为 00
以上的代码可以在RW007
的软件驱动包中找到对应的位置,想深入了解的可以仔细阅读。
总结
经过前面的学习,算是摸了一遍 RW007 的 BLE 功能,个人觉得 RW007 的 BLE 主机功能还是挺有意思的,使用也比较简单,像我这种没有任何 BLE 相关知识的也能基本知道怎样通过 RW007 的 BLE 主机功能去扫描附近的蓝牙BLE 设备,如何去连接扫描到的蓝牙 BLE 设备,如何通过蓝牙 BLE 设备的 UUID 去写数据。总得来说,使用算是比较方便的,但是目前貌似仅支持BLE 的主机功能,既然是作为主机后面再继续测试一下BLE 透传数据的功能。
注意事项:
1.请确保使用的RW007
模块带BLE
功能。
2.RW007
BLE 功能是基于SPI
模式的,请确保RW007
模块自身的版本为 2.0.1
以上的版本。
????RW007资料链接:
奶牛网盘 https://realthread.cowtransfer.com/s/274a38fde4b543
????RW007购买链接:
https://item.taobao.com/item.htm?&id=587364858221
口令(复制至X宝打开):5嘻6bLxXVYVZEv嘻 https://m.tb.cn/h.4rai8N8?sm=35e672 RW007 高速WIFI模块 SPI 物联网 透传模块 无线模块
RT-Thread线下技术培训:MQTT网关
本次培训RT-Thread将以ART-Pi为硬件平台+RT-Thread物联网操作系统提供MQTT网关原型,希望帮助工程师快速熟悉基于RT-Thread的项目开发流程,熟悉MQTT网关开发中所涉及到的技术要点如Modbus、MQTT、连网、文件系统、FTP、Bootloader、OTA、裁剪优化,提升产品开发速度。
我们将到访的城市有:上海、深圳、南京、广州、郑州、北京、武汉、天津、成都、西安
长按识别上方二维码报名
你可以添加微信17775982065为好友,注明:公司+姓名,拉进 RT-Thread 官方微信交流群!
???????????? 点击阅读原文报名培训
以上是关于RW007系列连载1RW007-BLE主机通信使用学习示例与综合Demo的主要内容,如果未能解决你的问题,请参考以下文章
RW007系列综合实战2使用RW007模块连接小米蓝牙温湿度计2代
RW007系列综合实战3柿饼派上怎样更新RW007固件和驱动?