当前文档版本为 v0.4.0,您可以访问当前页面的 开发中 版本以获取最近可能的更新。

Bluetooth: Mesh Demo

1 功能概述

此sample为针对mesh组网的演示项目,可以演示测试的Mesh Feature如下

  • Friend-LPN

  • Heartbeat Publish

2 环境要求

  • board: pan1080a_afld_evb * 2(A&B),pan1020 Mesh Demo板(用于测试,其他1080板入网PanMesh也可以,记为C板)

  • uart (option): 显示串口log

  • 测试软件: PanMesh

3 编译和烧录

项目位置:zephyr\samples_panchip\bluetooth\mesh_demo

目前可使用ZAL工具或quick build脚本进行编译和下载。

脚本位置(脚本分为四种,分别配置了节点的不同模式):

  1. quick_build_samples\bluetooth\mesh_demo.bat(默认配置普通节点)

  2. quick_build_samples\bluetooth\mesh_demo_friend.bat(配置为friend节点)

  3. quick_build_samples\bluetooth\mesh_demo_low_power_node.bat(配置为低功耗节点)

  4. quick_build_samples\bluetooth\mesh_demo_heartbeat_publisher.bat(配置为heartbeat发布者)

打开脚本后默认会编译项目,编译完成时,可输入字符进行后续下载等操作:

Input the keyword to continue:
  'b' build                         编译项目
  'r' make clean and rebuild        重新编译项目
  'f' flash download                下载
  'e' erase chip                    擦除芯片
  'o' open project by VS Code       打开 `VS Code`,可查看源码,执行编译下载等
  others exit                       退出
wait input:

4 演示说明

4.1 测试Friend-LPN流程

  1. 运行脚本quick_build_samples\bluetooth\mesh_demo_firend.bat,编译后进行烧录至 pan1080a_afld_evb A

  2. 运行脚本quick_build_samples\bluetooth\mesh_demo_low_power_node.bat,编译后进行烧录至 pan1080a_afld_evb B

  3. 观察Friend-LPN建立及轮询过程

  4. 将code内的netkey&appkey提取到PanMesh(或者将PanMesh中的key更新到代码内),入网其他Mesh板,通过Mesh 网络对LPN进行开关操作,验证Friend-LPN交互流程

    friend连接建立log

    bt_mesh_friend.bt_mesh_friend_poll: Friendship established with 0x0002
    

    friend开始轮询log

    bt_mesh_friend.buf_send_end: Waiting 30000 ms for next poll
    

    lpn连接已建立log

    bt_mesh_lpn.lpn_timeout: state: established
    

    lpn轮询周期log

    bt_mesh_lpn.poll_timeout: Poll Timeout is 26790ms
    
  5. 建立连接后,通过PanMesh处理好key信息入网C板,连接C板作为proxy,通过Vendor Message界面发送给LPN一条控制消息如图

    0000代表关灯,0100代表开灯

    image

    手机端配置界面

  6. friend为lpn存储message log

    bt_mesh_friend.friend_lpn_enqueue_rx: Queued message for LPN 0x0002, queue_size 1
    
  7. 30s的friend poll到达时,friend为lpn节点发送存储消息,成功开/关灯。

4.2 测试Heartbeat publish流程

  1. 运行脚本quick_build_samples\bluetooth\mesh_demo.bat,编译后进行烧录至 pan1080a_afld_evb A

  2. 运行脚本quick_build_samples\bluetooth\mesh_demo_heartbeat_publisher.bat,编译后进行烧录至 pan1080a_afld_evb B

  3. 观察打印log如下

    板A:

    Log打印Subscribing to heartbeat messages

    不丢包情况下,每4s收取一次heartbeat publish消息

    [16:51:20.480]收←◆*** Booting Zephyr OS build zephyr-v2.7.0-220-g27db6b8446e7  ***
    Initializing...mesh_adhoc
    board_init
    Unicast address: 0x0003
    
    [16:51:20.523]收←◆RCL_CTRL = 0x00000000
    
    [16:51:20.648]收←◆Bluetooth initialized
    gen_onoff_init
    Mesh initialized
    Provisioning completed
    Configuring...
    Configuration complete
    Subscribing to heartbeat messages
    [00:00:00.001,000] <wrn> bt_hci_core: Failed to set device name (-12)
    [00:00:00.176,000] <inf> bt_hci_core: Identity: D4:4B:52:E4:BF:CD (random)
    [00:00:00.176,000] <inf> bt_hci_core: HCI: version 5.1 (0x0a) revision 0x0003, manufacturer 0x07d1
    [00:00:00.176,000] <inf> bt_hci_core: LMP: version 5.1 (0x0a) subver 0x0000
    [00:00:00.177,000] <inf> bt_mesh_main: Primary Element: 0x0003
    [00:00:00.177,000] <dbg> bt_mesh_main.bt_mesh_provision: net_idx 0x0000 flags 0x00 iv_index 0x0000
    
    [16:51:28.517]收←◆heartbeat
    board_heartbeat,hops = 1, feat = 1
    
    [16:51:32.653]收←◆heartbeat
    board_heartbeat,hops = 1, feat = 1
    
    [16:51:40.930]收←◆heartbeat
    board_heartbeat,hops = 1, feat = 1
    
    [16:51:45.153]收←◆heartbeat
    board_heartbeat,hops = 1, feat = 1
    

    板B:

    Log打印Publishing heartbeat messages

    *** Booting Zephyr OS build zephyr-v2.7.0-220-g27db6b8446e7  ***
    Initializing...mesh_adhoc
    board_init
    Unicast address: 0x000f
    RCL_CTRL = 0x00000000
    Bluetooth initialized
    gen_onoff_init
    Mesh initialized
    Provisioning completed
    Configuring...
    Publishing heartbeat messages
    Configuration complete
    [00:00:00.001,000] <wrn> bt_hci_core: Failed to set device name (-12)
    [00:00:00.175,000] <inf> bt_hci_core: Identity: FD:2D:D9:E3:14:04 (random)
    [00:00:00.175,000] <inf> bt_hci_core: HCI: version 5.1 (0x0a) revision 0x0003, manufacturer 0x07d1
    [00:00:00.175,000] <inf> bt_hci_core: LMP: version 5.1 (0x0a) subver 0x0000
    [00:00:00.176,000] <inf> bt_mesh_main: Primary Element: 0x000f
    [00:00:00.176,000] <dbg> bt_mesh_main.bt_mesh_provision: net_idx 0x0000 flags 0x00 iv_index 0x0000
    

5 开发说明

5.1 KCONFIG宏说明

  1. flash相关,不需要存储flash相关功能

# CONFIG_BT_SETTINGS=y
# CONFIG_FLASH=y
# CONFIG_FLASH_PAGE_LAYOUT=y
# CONFIG_FLASH_MAP=y
# CONFIG_NVS=y
# CONFIG_SETTINGS=y
# CONFIG_BT_MESH_RPL_STORE_TIMEOUT=600
  1. 调试LOG宏,基础宏打开,由CONFIG_BT_PAN_MESH_NODE_TYPE选择friend/lpn相关打印

CONFIG_BT_DEBUG_LOG=y
CONFIG_BT_MESH_DEBUG=y
  1. 由于此demo可以展示配置不同node功能,由zephyr\samples_panchip\bluetooth\mesh_demo\proj.conj 内的宏进行修改选择,以下四个宏四选一进行编译

CONFIG_BT_PAN_MESH_NODE_TYPE_NORMAL=y
CONFIG_BT_PAN_MESH_NODE_TYPE_FRIEND=y
CONFIG_BT_PAN_MESH_NODE_TYPE_LPN=y
CONFIG_BT_PAN_MESH_NODE_TYPE_HB_PUB=y

5.2 MAIN函数说明

main函数包括3部分

  1. flash相关的配置擦除, 包括函数ps_settings_initshort_time_multireset_bt_mesh_unprovisioning

  2. bt_enable() 初始化蓝牙

  3. bt_ready() 初始化MESH

  4. 蓝牙回调接口

    static void connected(struct bt_conn *conn, uint8_t err)
    {
    	if (err) {
    		printk("Connection failed (err 0x%02x)\n", err);
    	} else {
    		printk("Connected\n");
    	}
    }
    
    static void disconnected(struct bt_conn *conn, uint8_t reason)
    {
    	printk("Disconnected (reason 0x%02x)\n", reason);
    }
    
    BT_CONN_CB_DEFINE(conn_callbacks) = {
    	.connected = connected,
    	.disconnected = disconnected,
    };
    

5.3 ble_mesh说明

ble/mesh相关操作:

  1. mesh_demosample没有进行provisioner入网,通过err = bt_mesh_provision(net_key, net_idx, flags, iv_index, addr,dev_key);进行自入网,通过static void configure(void)绑定相关config信息,为了减少gatt对扫描的干扰

  2. struct bt_mesh_model root_models[] 配置mesh model

  3. 实现heartbeat的publish绑定和onoff model的灯控演示