全国物联网竞赛(华为杯)华为赛道 小熊派 使用Iot studio编写/烧写代码
第一次参加物联网竞赛写一下参赛作品的完成流程
文章目录
前言
完成本次比赛作品大概的流程为
1,选择组委会发的板子这里我们组选的是小熊派开发板
2,对小熊派开发板进行编程使之能够完成数据收集处理上发和响应工作
3,在华为云平台上进行相应的设备注册和调试然后在平台上完成各个设备之间的通信
4,如果要开发APP或小程序端,还要使用华为云平台进行设备数据的订阅让设备数据发送到自己的服务器中
一、小熊派的编程
小熊派的编程软件一般为Iot studio和LiteOS Studio两者差不多
二、使用步骤
1.这里板端的开发其实是比较简单的但我们当时没有接触过的时候还是弄了很久。其实小熊派适配的传感器都是有相应的demo的我们并不需要从管脚开始编程我们可在原有的基础上进行我们想要的修改,这样代码难度就能大大的降低了
在创建项目的时候iot studio会给一些项目demo我们们选择相应的demo工程
然后找到相应的主函数如图(一般来说一个demo里面所有的demo主函数代码都有但只有相应demo的主函数代码才会被编译其他的代码不会被编译,如果要修改的话要去Makefile里去修改路径)这里不推荐这种方法要用那个工程就打开那个工程的demo就行了
2.这里是smoker的demo代码其中main函数的代码我们是不需要修改的我们要修改的地方一般是数据的处理部分(因为这里的demo都是只有数据上报和数据响应的)所有数据都是在云端处理的
#include <stdint.h>
#include <stddef.h>
#include <string.h>
#include <osal.h>
#include <oc_lwm2m_al.h>
#include <link_endian.h>
#include <boudica150_oc.h>
#include "E53_SF1.h"
#include "lcd.h"
#include <gpio.h>
#include <stm32l4xx_it.h>
#define cn_endpoint_id "SDK_LWM2M_NODTLS"
#define cn_app_server "49.4.85.232"
#define cn_app_port "5683"
typedef unsigned char int8u;
typedef char int8s;
typedef unsigned short int16u;
typedef short int16s;
typedef unsigned char int24u;
typedef char int24s;
typedef int int32s;
typedef char string;
typedef char array;
typedef char varstring;
typedef char variant;
#define cn_app_Smoke 0x8
#define cn_app_response_Smoke_Control_Beep 0xa
#define cn_app_Smoke_Control_Beep 0x9
#pragma pack(1)
typedef struct
{
int8u messageId;
int16u Smoke_Value;
} tag_app_Smoke;
typedef struct
{
int8u messageId;
int16u mid;
int8u errcode;
int8u Beep_State;
} tag_app_Response_Smoke_Control_Beep;
typedef struct
{
int8u messageId;
int16u mid;
string Beep[3];
} tag_app_Smoke_Control_Beep;
#pragma pack()
int8_t qr_code = 1;
extern const unsigned char gImage_Huawei_IoT_QR_Code[114720];
E53_SF1_Data_TypeDef E53_SF1_Data;
//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
{
POINT_COLOR = RED;
LCD_ShowString(40, 10, 200, 16, 24, "IoTCluB BearPi");
LCD_ShowString(15, 50, 210, 16, 24, "LiteOS NB-IoT 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);
}
}
//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;
osal_semp_post(s_rcv_sync);
ret = 0;
}
return ret;
}
static int app_cmd_task_entry()
{
int ret = -1;
tag_app_Response_Smoke_Control_Beep Response_Smoke_Control_Beep;
tag_app_Smoke_Control_Beep *Smoke_Control_Beep;
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_Smoke_Control_Beep:
Smoke_Control_Beep = (tag_app_Smoke_Control_Beep *)s_rcv_buffer;
printf("Smoke_Control_Beep:msgid:%d mid:%d", Smoke_Control_Beep->messageId, ntohs(Smoke_Control_Beep->mid));
/********** code area for cmd from IoT cloud **********/
if (Smoke_Control_Beep->Beep[0] == 'O' && Smoke_Control_Beep->Beep[1] == 'N')
{
E53_SF1_Beep_StatusSet(ON);
Response_Smoke_Control_Beep.messageId = cn_app_response_Smoke_Control_Beep;
Response_Smoke_Control_Beep.mid = Smoke_Control_Beep->mid;
Response_Smoke_Control_Beep.errcode = 0;
Response_Smoke_Control_Beep.Beep_State = 1;
oc_lwm2m_report((char *)&Response_Smoke_Control_Beep,sizeof(Response_Smoke_Control_Beep),1000); ///< report cmd reply message
}
if (Smoke_Control_Beep->Beep[0] == 'O' && Smoke_Control_Beep->Beep[1] == 'F' && Smoke_Control_Beep->Beep[2] == 'F')
{
E53_SF1_Beep_StatusSet(OFF);
Response_Smoke_Control_Beep.messageId = cn_app_response_Smoke_Control_Beep;
Response_Smoke_Control_Beep.mid = Smoke_Control_Beep->mid;
Response_Smoke_Control_Beep.errcode = 0;
Response_Smoke_Control_Beep.Beep_State = 0;
oc_lwm2m_report((char *)&Response_Smoke_Control_Beep,sizeof(Response_Smoke_Control_Beep),1000); ///< report cmd reply message
}
/********** code area end **********/
break;
default:
break;
}
}
}
return ret;
}
static int app_report_task_entry()
{
int ret = -1;
oc_config_param_t oc_param;
tag_app_Smoke Smoke;
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;
}
while(1) //--TODO ,you could add your own code here
{
Smoke.messageId = cn_app_Smoke;
Smoke.Smoke_Value = htons((int)E53_SF1_Data.Smoke_Value);
oc_lwm2m_report( (char *)&Smoke, sizeof(Smoke), 1000);
osal_task_sleep(2*1000);
}
return ret;
}
static int app_collect_task_entry()
{
Init_E53_SF1();
while (1)
{
E53_SF1_Read_Data();
printf("\r\n******************************Smoke Value is %d\r\n", (int)E53_SF1_Data.Smoke_Value);
if (qr_code == 0)
{
// LCD_ShowString(10, 200, 200, 16, 16, "BH1750 Value is:");
// LCD_ShowNum(140, 200, lux, 5, 16);
}
osal_task_sleep(2*1000);
}
return 0;
}
#include <stimer.h>
int standard_app_demo_main()
{
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);
stimer_create("lcdtimer",timer1_callback,NULL,8*1000,cn_stimer_flag_start);
return 0;
}
该处使用的url网络请求的数据。
代码讲解
#define cn_app_Smoke 0x8
#define cn_app_response_Smoke_Control_Beep 0xa
#define cn_app_Smoke_Control_Beep 0x9
这里宏定义的数字是在华为云平台的上注册设备的时候所定义的字段
所以自己自定义设备时要确定好这个字段的对应关系,这个字段是用来分辨服务的
比如这里通过switch语句来判断msgid是不是控制蜂鸣器的信号
以上片段在app_cmd_task_entry()函数里这是相应平台数据的处理代码这里默认是让蜂鸣器响,如果想要更多操作可以在这里修改
这是上报数据的部分(其实这里也提示了)这里默认是提交烟雾浓度信息,因为平台只能简单的处理数据(如果你想发生数据变化的斜率就得在这里改了如果没改变数据类型和数量就不用再平台的设备注册里改,否则需要进行相关的修改)
比如这个烟雾浓度可以在这里修改
这里附上我们组当时修改的smoker代码
#include <stdint.h>
#include <stddef.h>
#include <string.h>
#include"mei_qi.h"
//#include"zhao_huo.h"
#include <osal.h>
#include <oc_lwm2m_al.h>
#include <link_endian.h>
#include <boudica150_oc.h>
#include "E53_SF1.h"
#include "lcd.h"
#include <gpio.h>
#include <stm32l4xx_it.h>
#define cn_endpoint_id "SDK_LWM2M_NODTLS"
#define cn_app_server "49.4.85.232"
#define cn_app_port "5683"
typedef unsigned char int8u;
typedef char int8s;
typedef unsigned short int16u;
typedef short int16s;
typedef unsigned char int24u;
typedef char int24s;
typedef int int32s;
typedef char string;
typedef char array;
typedef char varstring;
typedef char variant;
#define cn_app_Smoke 0x8
#define cn_app_Smoke_Control_Beep 0x9
#define cn_app_response_Smoke_Control_Beep 0xa
#define cn_app_Smoke_Control_String 0xb
#define cn_app_response_Smoke_Control_String 0xc
#pragma pack(1)
typedef struct
{
int8u messageId;
int16u Smoke_Value;
} tag_app_Smoke;
typedef struct
{
int8u messageId;
int16u mid;
int8u errcode;
int8u Beep_State;
} tag_app_Response_Smoke_Control_Beep;
typedef struct
{
int8u messageId;
int16u mid;
int8u errcode;
int8u String_State;
} tag_app_Response_Smoke_Control_String;
typedef struct
{
int8u messageId;
int16u mid;
string String[3];
} tag_app_Smoke_Control_String;
typedef struct
{
int8u messageId;
int16u mid;
string Beep[3];
} tag_app_Smoke_Control_Beep;
#pragma pack()
int8_t qr_code = 0;
//extern const unsigned char gImage_Huawei_IoT_QR_Code[114720];
E53_SF1_Data_TypeDef E53_SF1_Data;
//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)
{
LCD_Clear(WHITE);
if (qr_code == 1)
{
LCD_Clear(WHITE);
LCD_Show_Image(0,0,240,240,mei_qi);
}
else
{
POINT_COLOR = BLACK;
LCD_ShowString(80, 10, 200, 16, 24, "HNU JLD");//显示字符串
LCD_ShowString(110, 50, 210, 16, 24, "CO");
LCD_ShowString(10, 100, 200, 16, 24, "NCDP_IP:");
LCD_ShowString(110, 100, 200, 16, 24, cn_app_server);//显示APP服务器服务器号
LCD_ShowString(10, 150, 200, 16, 24, "NCDP_PORT:");
LCD_ShowString(150, 150, 200, 16, 24, cn_app_port);//显示APP端口
LCD_ShowString(10, 200, 200, 16, 24, "CO Value:");
LCD_ShowNum(160, 200,(uint32_t)E53_SF1_Data.Smoke_Value,4,24);//显示APP端口
// LCD_ShowString(80, 10, 200, 16, 24, "HNU JLD");//显示字符串
// LCD_ShowString(80, 50, 210, 16, 24, "Smoke");
// LCD_ShowString(10, 100, 200, 16, 24, "NCDP_IP:");
// LCD_ShowString(110, 100, 200, 16, 24, cn_app_server);//显示APP服务器服务器号
// LCD_ShowString(10, 150, 200, 16, 24, "NCDP_PORT:");
// LCD_ShowString(150, 150, 200, 16, 24, cn_app_port);//显示APP端口
// LCD_ShowString(10, 200, 200, 16, 24, "Smoke Value:");
// LCD_ShowNum(160, 200,(uint32_t)E53_SF1_Data.Smoke_Value,4,24);//显示APP端口
}
return ;
}
//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;
osal_semp_post(s_rcv_sync);
ret = 0;
}
return ret;
}
static int app_cmd_task_entry()
{
int ret = -1;
tag_app_Response_Smoke_Control_Beep Response_Smoke_Control_Beep;
tag_app_Smoke_Control_Beep *Smoke_Control_Beep;
tag_app_Response_Smoke_Control_String Response_Smoke_Control_String;
tag_app_Smoke_Control_String *Smoke_Control_String;
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_Smoke_Control_Beep:
Smoke_Control_Beep = (tag_app_Smoke_Control_Beep *)s_rcv_buffer;
/********** code area for cmd from IoT cloud **********/
if (Smoke_Control_Beep->Beep[0] == 'O' && Smoke_Control_Beep->Beep[1] == 'N')
{
E53_SF1_Beep_StatusSet(ON);
Response_Smoke_Control_Beep.messageId = cn_app_response_Smoke_Control_Beep;
Response_Smoke_Control_Beep.mid = Smoke_Control_Beep->mid;
Response_Smoke_Control_Beep.errcode = 0;
Response_Smoke_Control_Beep.Beep_State = 1;
oc_lwm2m_report((char *)&Response_Smoke_Control_Beep,sizeof(Response_Smoke_Control_Beep),1000); ///< report cmd reply message
}
if (Smoke_Control_Beep->Beep[0] == 'O' && Smoke_Control_Beep->Beep[1] == 'F' && Smoke_Control_Beep->Beep[2] == 'F')
{
E53_SF1_Beep_StatusSet(OFF);
Response_Smoke_Control_Beep.messageId = cn_app_response_Smoke_Control_Beep;
Response_Smoke_Control_Beep.mid = Smoke_Control_Beep->mid;
Response_Smoke_Control_Beep.errcode = 0;
Response_Smoke_Control_Beep.Beep_State = 0;
oc_lwm2m_report((char *)&Response_Smoke_Control_Beep,sizeof(Response_Smoke_Control_Beep),1000); ///< report cmd reply message
}
/********** code area end **********/
break;
case cn_app_Smoke_Control_String:
Smoke_Control_String=(tag_app_Smoke_Control_String *)s_rcv_buffer;
if (Smoke_Control_String->String[0] == 'O' && Smoke_Control_String->String[1] == 'N'&& Smoke_Control_String->String[2] == 'S')
{
qr_code=1;
Response_Smoke_Control_String.messageId = cn_app_response_Smoke_Control_String;
Response_Smoke_Control_String.mid = Smoke_Control_String->mid;
Response_Smoke_Control_String.errcode = 0;
Response_Smoke_Control_String.String_State = 1;
oc_lwm2m_report((char *)&Response_Smoke_Control_String,sizeof(Response_Smoke_Control_String),1000); ///< report cmd reply message
}
if (Smoke_Control_String->String[0] == 'O' && Smoke_Control_String->String[1] == 'N'&& Smoke_Control_String->String[2] == 'B')
{
E53_SF1_Beep_StatusSet(ON);
Response_Smoke_Control_Beep.messageId = cn_app_response_Smoke_Control_Beep;
Response_Smoke_Control_Beep.mid = Smoke_Control_Beep->mid;
Response_Smoke_Control_Beep.errcode = 0;
Response_Smoke_Control_Beep.Beep_State = 1;
oc_lwm2m_report((char *)&Response_Smoke_Control_Beep,sizeof(Response_Smoke_Control_Beep),1000); ///< report cmd reply message
}
if (Smoke_Control_String->String[0] == 'O' && Smoke_Control_String->String[1] == 'F' && Smoke_Control_String->String[2] == 'F')
{
qr_code=0;
LCD_Clear(WHITE);
Response_Smoke_Control_String.messageId = cn_app_response_Smoke_Control_String;
Response_Smoke_Control_String.mid = Smoke_Control_String->mid;
Response_Smoke_Control_String.errcode = 0;
Response_Smoke_Control_String.String_State = 0;
oc_lwm2m_report((char *)&Response_Smoke_Control_String,sizeof(Response_Smoke_Control_String),1000); ///< report cmd reply message
}
break;
default:
break;
}
}
}
return ret;
}
static int app_report_task_entry()
{
int ret = -1;
oc_config_param_t oc_param;
tag_app_Smoke Smoke;
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;
}
while(1) //--TODO ,you could add your own code here
{
Smoke.messageId = cn_app_Smoke;
Smoke.Smoke_Value = htons((int)E53_SF1_Data.Smoke_Value);
oc_lwm2m_report( (char *)&Smoke, sizeof(Smoke), 1000);
osal_task_sleep(2*1000);
}
return ret;
}
static int app_collect_task_entry()
{
Init_E53_SF1();
while (1)
{
E53_SF1_Read_Data();
if((uint32_t)E53_SF1_Data.Smoke_Value>=300)//(uint32_t)E53_SF1_Data.Smoke_Value>=300
{
// E53_SF1_Beep_StatusSet(ON);
qr_code=1;
}
else{
qr_code=0;
}
printf("\r\n******************************Smoke Value is %d\r\n", (int)E53_SF1_Data.Smoke_Value);
// if (qr_code == 0)
// {
// // LCD_ShowString(10, 200, 200, 16, 16, "BH1750 Value is:");
// // LCD_ShowNum(140, 200, lux, 5, 16);
// }
osal_task_sleep(2*1000);
}
return 0;
}
#include <stimer.h>
int standard_app_demo_main()
{
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);
stimer_create("lcdtimer",timer1_callback,NULL,500,cn_stimer_flag_start);
return 0;
}
主要功能是可以在屏幕上实时展示当前的烟雾浓度值,在烟雾浓度过高时会触发蜂鸣器报警并改变图片为着火了(图片数据在头文件里这里并没有给出,这里使用的图片像素是双字节的,直接转16进制是不行的而且一个板子只能放一张全屏图片(板子容量所限))