【设备开发】小熊派iot开发套件(bearpi-4008云顶国际网站
智慧路灯是城市智能化道路上重要一环,智慧路灯的实施具有节约公共照明能耗、较少因照明引起的交通事故等多种社会意义。路灯也是大家在日常生活中可以强烈感知到的公共设施,更易理解其智能化的场景。
本实验基于nb-iot和wifi实现智慧路灯案例,实现实时数据的采集,实现命令下发的响应,实现端云互通。
e53_sc1扩展板采用了e53标准接口,包含了一个路灯灯珠,一个bh1750光照强度传感器,其中路灯灯珠使用普通gpio控制,bh1750使用iic接口通信。
环境准备
- 已注册华为云官方帐号。未注册可点击完成注册。
- 已完成实名制认证。未完成可在华为云上点击完成认证,否则无法使用设备接入功能。
- 已开通设备接入服务。未开通则访问,点击“免费试用”后开通该服务。
- ide: 1.45 (含扩展插件)
硬件准备
- 开发板:小熊派开发板(含nb卡、nb35-a通信扩展板、wifi通信扩展板、e53_sc1案例扩展板)
硬件连接
nb35-a通信扩展板需要安装sim卡,并注意卡的缺口朝外插入。将串口选择开关拨到mcu模式,并用usb线将开发板与电脑连接。
- nb35-a通信扩展板
- wifi通信扩展板
ide安装配置
驱动安装
解压,根据操作系统类型,双击配套的执行文件进行安装。
步骤 1 创建产品
1. 登录,单击左侧导航栏“产品”,单击页面右上角的“创建产品”。
2. 根据页面提示填写参数,然后单击“确定”,完成产品的创建。
方案①:coap协议 二进制码流 | 方案②:mqtt协议 json |
|
|
3. 单击“查看详情”,进入产品详情页。
步骤 2 上传模型文件
1. 在产品详情页“模型定义”页签,单击“上传模型文件”。
2. 单击“添加文件”,选择产品模型文件,并单击“确定”。
- 方案①:
- 方案②:
3. 单击“确认”,关闭上传成功提示窗口。
步骤 3 (可选)开发编解码插件
仅方案①需要开发编解码插件,方案②请跳过此步骤。
1. 在产品详情页“插件开发”页签,单击“图形化开发”,进入插件开发页面。
2. 在页面右上角选择“更多 > 图形化开发导入”
3. 单击“添加文件”,选择插件图形化开发包,并单击“确定”。
获取插件开发包:
4. 单击页面右上角“保存”,保存修改。
5. 单击“部署”,弹窗提示如下,单击“确认”,进行部署。
出现如下提示,表示在线插件部署成功。
步骤 4 注册设备
1. 在左侧导航栏选择“ 设备 > 所有设备”,单击右上角“注册设备”。
2. 填写参数后,单击“确定”。
- 所属资源空间:选择默认资源空间
- 所属产品:选择步骤1 创建产品已创建的产品
- 设备标识码:若是nb35-a通信扩展板,填写模组imei(通过at cgsn=1查询获取);若是wifi通信扩展板,填写自定义字符串,如vz_sc1_20220202200808
- 设备名称:自定义,如streetlight_01
- 设备认证类型:选择“密钥”
- 密钥/确认密钥:自定义,如12345678
3. 设备创建成功,单击“保存并关闭”。
下载保存的“devices-key.txt”文件中记录了设备id和密钥,请妥善保管。
步骤 5 导入项目工程
1. 获取项目工程代码包并解压(注:路径不要包含中文、空格)。获取项目工程:
2. 启动huawei liteos studio工具,单击“open project”,打开项目工程。
3. 项目工程预览如下:
步骤 6 修改配置参数
方案① nb35-a通信扩展板:
1. 打开“targets\stm32l431_bearpi\.config”文件,修改配置参数,并按“ctrl s”保存修改。
- config_uartat_baudrate:9600
- config_user_demo:oc_streetlight_template
- 启用#nb-iot后的config_boudica150_enable
- 不启用#wifi和#mqtt后的配置项(行前#表示该行内容为注释,即配置不生效)
...
config_uartat_baudrate=9600
...
config_iot_link_configfile="iot_config.h"
# demo
config_user_demo="oc_streetlight_template"
# nb-iot
config_boudica150_enable=y
# wifi
#config_oclwm2mtiny_enable=y
#config_lwm2m_al_enable=y
#config_wakaamalwm2m_enable=y
#config_tcip_al_enable=y
#config_esp8266_enable=y
#config_esp8266_ssid="iotdebug"
#config_esp8266_pwd="iotdebug"
# mqtt
#config_mqtt_al_enable=y
#config_paho_mqtt=y
#config_paho_connect_timeout=10000
#config_paho_cmd_timeout=10000
#config_paho_looptimeout=10
#config_paho_sndbuf_size=2048
#config_paho_rcvbuf_size=2048
#config_dtls_al_enable=y
#config_mbedtls_enable=y
#config_mbedtls_psk=y
2. 打开“targets\stm32l431_bearpi\iot_config.h”文件,修改配置参数,并按“ctrl s”保存修改。
- config_uartat_baudrate:9600
- config_user_demo:oc_streetlight_template
- 启用/*nb-iot*/后的config_boudica150_enable
- 不启用/*wifi*/和/*mqtt*/后的配置项(行前//表示该行内容为注释,即配置不生效)
...
#define config_uartat_baudrate 9600
...
#define config_iot_link_configfile "iot_config.h"
/*demo*/
#define config_user_demo "oc_streetlight_template"
/*nb-iot*/
#define config_boudica150_enable 1
/*wifi*/
// #define config_oclwm2mtiny_enable 1
// #define config_lwm2m_al_enable 1
// #define config_wakaamalwm2m_enable 1
// #define config_tcip_al_enable 1
// #define config_esp8266_enable 1
// #define config_esp8266_ssid "iotdebug"
// #define config_esp8266_pwd "iotdebug"
/*mqtt*/
// #define config_cjson_enable 1
// #define config_mqtt_al_enable 1
// #define config_paho_mqtt 1
// #define config_paho_connect_timeout 10000
// #define config_paho_cmd_timeout 10000
// #define config_paho_looptimeout 10
// #define config_paho_sndbuf_size 2048
// #define config_paho_rcvbuf_size 2048
// #define config_dtls_al_enable 1
// #define config_mbedtls_enable 1
// #define config_mbedtls_psk 1
3. 打开“targets\stm32l431_bearpi\demos\oc_streetlight_template\oc_streetlight_template.c”文件,修改相关参数,并按“ctrl s”保存修改。
- cn_endpoint_id:设备标识码
- cn_app_server:华为云iot平台coap协议接入地址。
- cn_app_port:华为云iot平台coap协议接入端口。
...
#define cn_endpoint_id "xxx"
#define cn_app_server "119.3.250.80"
#define cn_app_port "5683"
...
方案① wifi通信扩展板:
1. 打开“targets\stm32l431_bearpi\.config”文件,修改配置参数,并按“ctrl s”保存修改。
- config_uartat_baudrate:115200
- config_user_demo:oc_streetlight_template
- 停用#nb-iot后的config_boudica150_enable(行前加#号注释掉该行)
- 启用#wifi后的配置项(去掉行前的#号),并将config_esp8266_ssid和config_esp8266_pwd修改为自己手机热点或者路由器热点的用户名密码;
...
config_uartat_baudrate=115200
...
config_iot_link_configfile="iot_config.h"
# demo
config_user_demo="oc_streetlight_template"
# nb-iot
#config_boudica150_enable=y
# wifi
config_oclwm2mtiny_enable=y
config_lwm2m_al_enable=y
config_wakaamalwm2m_enable=y
config_tcip_al_enable=y
config_esp8266_enable=y
config_esp8266_ssid="iotdebug"
config_esp8266_pwd="iotdebug"
# mqtt
#config_mqtt_al_enable=y
#config_paho_mqtt=y
#config_paho_connect_timeout=10000
#config_paho_cmd_timeout=10000
#config_paho_looptimeout=10
#config_paho_sndbuf_size=2048
#config_paho_rcvbuf_size=2048
#config_dtls_al_enable=y
#config_mbedtls_enable=y
#config_mbedtls_psk=y
2. 打开“targets\stm32l431_bearpi\iot_config.h”文件,修改配置参数,并按“ctrl s”保存修改。
- config_uartat_baudrate:115200
- config_user_demo:oc_streetlight_template
- 停用/*nb-iot*/后的config_boudica150_enable(行前加//注释掉该行)
- 启用/*wifi*/后的配置项(去掉行前的//号),并将config_esp8266_ssid和config_esp8266_pwd修改为自己手机热点或者路由器热点的用户名密码;
...
#define config_uartat_baudrate 115200
...
#define config_iot_link_configfile "iot_config.h"
/*demo*/
#define config_user_demo "oc_streetlight_template"
/*nb-iot*/
// #define config_boudica150_enable 1
/*wifi*/
#define config_oclwm2mtiny_enable 1
#define config_lwm2m_al_enable 1
#define config_wakaamalwm2m_enable 1
#define config_tcip_al_enable 1
#define config_esp8266_enable 1
#define config_esp8266_ssid "iotdebug"
#define config_esp8266_pwd "iotdebug"
/*mqtt*/
// #define config_cjson_enable 1
// #define config_mqtt_al_enable 1
// #define config_paho_mqtt 1
// #define config_paho_connect_timeout 10000
// #define config_paho_cmd_timeout 10000
// #define config_paho_looptimeout 10
// #define config_paho_sndbuf_size 2048
// #define config_paho_rcvbuf_size 2048
// #define config_dtls_al_enable 1
// #define config_mbedtls_enable 1
// #define config_mbedtls_psk 1
3. 打开“targets\stm32l431_bearpi\demos\oc_streetlight_template\oc_streetlight_template.c”文件,修改相关参数,并按“ctrl s”保存修改。
- cn_endpoint_id:设备标识码
- cn_app_server:华为云iot平台coap协议接入地址。
- cn_app_port:华为云iot平台coap协议接入端口。
...
#define cn_endpoint_id "xxx"
#define cn_app_server "119.3.250.80"
#define cn_app_port "5683"
...
步骤 7 编译烧录
1. 单击进行编译,单击进行烧录。
如果修改了.config文件和iot_config.h文件,请单击全量重新编译,否则修改点可能不会生效。
2. 单击打开串口终端,选择端口,设置波特率115200,开启串口,可以在接收区查看到打印的日志信息。
3. 在lcd屏上,可以看到实时数据显示。
步骤 8 调试运行
1. 在物联网平台中,可以看到设备实时上报属性数据。
2. 在物联网平台中下发控制命令(开启路灯),查看命令下发结果及开发板的响应。
注:当前mqtt设备仅支持同步命令下发,nb设备仅支持异步命令下发。
----结束
案例程序文件:"targets\stm32l431_bearpi\demos\oc_streetlight_template\oc_streetlight_template.c"
#include
#include
#include
#include
#include
#include
#ifdef config_boudica150_enable
#include
#endif
#include "e53_sc1.h"
#include "lcd.h"
#include
#include
#define cn_endpoint_id "bearpi_0001"
#define cn_app_server "119.3.250.80"
#define cn_app_port "5683"
#define cn_app_connectivity 0
#define cn_app_lightstats 1
#define cn_app_light 2
#define cn_app_ledcmd 3
#define cn_app_cmdreply 4
#pragma pack(1)
typedef struct
{
int8_t msgid;
int16_t rsrp;
int16_t ecl;
int16_t snr;
int32_t cellid;
}app_connectivity_t;
typedef struct
{
int8_t msgid;
int16_t tog;
}app_toggle_t;
typedef struct
{
int8_t msgid;
int16_t intensity;
}app_light_intensity_t;
typedef struct
{
int8_t msgid;
uint16_t mid;
char led[3];
}app_led_cmd_t;
typedef struct
{
int8_t msgid;
uint16_t mid;
int8_t errorcode;
char curstats[3];
}app_cmdreply_t;
#pragma pack()
int *ue_stats;
int8_t key1 = 0;
int8_t key2 = 0;
int16_t toggle = 0;
int16_t lux;
e53_sc1_data_typedef e53_sc1_data;
void hal_gpio_exti_callback(uint16_t gpio_pin)
{
switch(gpio_pin)
{
case key1_pin:
key1 = 1;
printf("proceed to get ue_status!\r\n");
break;
case key2_pin:
key2 = 1;
printf("toggle led and report!\r\n");
toggle = !toggle;
hal_gpio_togglepin(sc1_light_gpio_port,sc1_light_pin);
break;
default:
break;
}
}
//if your command is very fast,please use a queue here--todo
#define cn_app_rcv_buf_len 128
static int s_rcv_buffer[cn_app_rcv_buf_len];
static int s_rcv_datalen;
static osal_semp_t s_rcv_sync;
//static void timer1_callback(void *arg)
//{
// qr_code = !qr_code;
// lcd_clear(white);
// if (qr_code == 1)
// lcd_show_image(0,0,240,239,gimage_huawei_iot_qr_code);
// else
// {
//use this function to push all the message to the buffer
static int app_msg_deal(void *usr_data, en_oc_lwm2m_msg_t type, void *data, int len)
{
unsigned char *msg;
msg = data;
int ret = -1;
if(len <= cn_app_rcv_buf_len)
{
if (msg[0] == 0xaa && msg[1] == 0xaa)
{
printf("oc respond message received! \n\r");
return ret;
}
memcpy(s_rcv_buffer,msg,len);
s_rcv_datalen = len;
(void) osal_semp_post(s_rcv_sync);
ret = 0;
}
return ret;
}
static int app_cmd_task_entry()
{
int ret = -1;
app_led_cmd_t *led_cmd;
app_cmdreply_t replymsg;
int8_t msgid;
while(1)
{
if(osal_semp_pend(s_rcv_sync,cn_osal_timeout_forever))
{
msgid = s_rcv_buffer[0] & 0x000000ff;
switch (msgid)
{
case cn_app_ledcmd:
led_cmd = (app_led_cmd_t *)s_rcv_buffer;
printf("ledcmd:msgid:%d mid:%d msg:%s \n\r",led_cmd->msgid,ntohs(led_cmd->mid),led_cmd->led);
//add command action--todo
if (led_cmd->led[0] == 'o' && led_cmd->led[1] == 'n')
{
if (toggle != 1)
{
toggle = 1;
key2 = true;
}
hal_gpio_writepin(sc1_light_gpio_port,sc1_light_pin,gpio_pin_set);
//if you need response message,do it here--todo
replymsg.msgid = cn_app_cmdreply;
replymsg.mid = led_cmd->mid;
printf("reply mid is %d. \n\r",ntohs(replymsg.mid));
replymsg.errorcode = 0;
replymsg.curstats[0] = 'o';
replymsg.curstats[1] = 'n';
replymsg.curstats[2] = ' ';
oc_lwm2m_report((char *)&replymsg,sizeof(replymsg),1000); ///< report cmd reply message
}
else if (led_cmd->led[0] == 'o' && led_cmd->led[1] == 'f' && led_cmd->led[2] == 'f')
{
if (toggle != 0)
{
toggle = 0;
key2 = true;
}
hal_gpio_writepin(sc1_light_gpio_port,sc1_light_pin,gpio_pin_reset);
//if you need response message,do it here--todo
replymsg.msgid = cn_app_cmdreply;
replymsg.mid = led_cmd->mid;
printf("reply mid is %d. \n\r",ntohs(replymsg.mid));
replymsg.errorcode = 0;
replymsg.curstats[0] = 'o';
replymsg.curstats[1] = 'f';
replymsg.curstats[2] = 'f';
oc_lwm2m_report((char *)&replymsg,sizeof(replymsg),1000); ///< report cmd reply message
}
else
break;
default:
break;
}
}
}
return ret;
}
#ifdef config_boudica150_enable
static void get_netstats()
{
ue_stats = boudica150_check_nuestats();
if (ue_stats[0] < -10) ue_stats[0] = ue_stats[0] / 10;
if (ue_stats[2] > 10) ue_stats[2] = ue_stats[2] / 10;
}
#endif
static int app_report_task_entry()
{
int ret = -1;
oc_config_param_t oc_param;
app_light_intensity_t light;
app_connectivity_t connectivity;
app_toggle_t light_status;
(void) memset(&oc_param,0,sizeof(oc_param));
oc_param.app_server.address = cn_app_server;
oc_param.app_server.port = cn_app_port;
oc_param.app_server.ep_id = cn_endpoint_id;
oc_param.boot_mode = en_oc_boot_strap_mode_factory;
oc_param.rcv_func = app_msg_deal;
// context = oc_lwm2m_config(&oc_param);
ret = oc_lwm2m_config(&oc_param);
if (0 != ret)
{
return ret;
}
//install a dealer for the led message received
while(1) //--todo ,you could add your own code here
{
if (key1 == 1)
{
#ifdef config_boudica150_enable
key1 = 0;
connectivity.msgid = cn_app_connectivity;
get_netstats();
connectivity.rsrp = htons(ue_stats[0] & 0x0000ffff);
connectivity.ecl = htons(ue_stats[1] & 0x0000ffff);
connectivity.snr = htons(ue_stats[2] & 0x0000ffff);
connectivity.cellid = htonl(ue_stats[3]);
oc_lwm2m_report((char *)&connectivity,sizeof(connectivity),1000); ///< report ue status message
#endif
}
if (key2 == 1)
{
key2 = 0;
light_status.msgid = cn_app_lightstats;
light_status.tog = htons(toggle);
oc_lwm2m_report((char *)&light_status,sizeof(light_status),1000); ///< report toggle message
}
light.msgid = cn_app_light;
light.intensity = htons((int)e53_sc1_data.lux);
oc_lwm2m_report((char *)&light,sizeof(light),1000); ///< report the light message
osal_task_sleep(2*1000);
}
return ret;
}
static int app_collect_task_entry()
{
init_e53_sc1();
while (1)
{
e53_sc1_read_data();
printf("\r\n******************************bh1750 value is %d\r\n",(int)e53_sc1_data.lux);
lcd_showstring(10, 200, 200, 16, 16, "bh1750 value is:");
lcd_shownum(140, 200, (int)e53_sc1_data.lux, 5, 16);
osal_task_sleep(2*1000);
}
return 0;
}
int standard_app_demo_main()
{
lcd_clear(black);
point_color = green;
lcd_showstring(10, 10, 200, 16, 24, "welcome to bearpi!");
lcd_showstring(15, 50, 210, 16, 24, "streetlight demo");
lcd_showstring(10, 100, 200, 16, 16, "ncdp_ip:");
lcd_showstring(80, 100, 200, 16, 16, cn_app_server);
lcd_showstring(10, 150, 200, 16, 16, "ncdp_port:");
lcd_showstring(100, 150, 200, 16, 16, cn_app_port);
osal_semp_create(&s_rcv_sync,1,0);
osal_task_create("app_collect",app_collect_task_entry,null,0x400,null,3);
osal_task_create("app_report",app_report_task_entry,null,0x1000,null,2);
osal_task_create("app_command",app_cmd_task_entry,null,0x1000,null,3);
osal_int_connect(key1_exti_irqn, 2,0,key1_irqhandler,null);
osal_int_connect(key2_exti_irqn, 3,0,key2_irqhandler,null);
return 0;
}
详细了解iot设备接入服务,请参考。
发布日期 |
文档版本 |
修订说明 |
2022/06/06 |
v1.0 |
文档首次发布 |
感谢分享
谢谢分享
感谢精彩分享
-- 高级云网管
谢谢分享
感谢分享