README.md
1# BearPi-HM_Nano开发板智慧烟感案例开发
2本示例将演示如何在BearPi-HM_Nano开发板上使用MQTT协议连接华为IoT平台,使用E53_SF1 智慧烟感扩展板与 BearPi-HM_Nano 开发板实现智慧烟感的案例,设备安装如下图所示。
3
4
5
6
7## 软件设计
8
9
10
11### 连接平台
12在连接平台前需要设置获取CONFIG_APP_DEVICEID、CONFIG_APP_DEVICEPWD、CONFIG_APP_SERVERIP、CONFIG_APP_SERVERPORT,通过oc_mqtt_profile_connect()函数连接平台。
13```c
14 WifiConnect(CONFIG_WIFI_SSID, CONFIG_WIFI_PWD);
15 dtls_al_init();
16 mqtt_al_init();
17 oc_mqtt_init();
18
19 g_app_cb.app_msg = queue_create("queue_rcvmsg",10,1);
20 if(g_app_cb.app_msg == NULL){
21 printf("Create receive msg queue failed");
22
23 }
24 oc_mqtt_profile_connect_t connect_para;
25 (void) memset( &connect_para, 0, sizeof(connect_para));
26
27 connect_para.boostrap = 0;
28 connect_para.device_id = CONFIG_APP_DEVICEID;
29 connect_para.device_passwd = CONFIG_APP_DEVICEPWD;
30 connect_para.server_addr = CONFIG_APP_SERVERIP;
31 connect_para.server_port = CONFIG_APP_SERVERPORT;
32 connect_para.life_time = CONFIG_APP_LIFETIME;
33 connect_para.rcvfunc = msg_rcv_callback;
34 connect_para.security.type = EN_DTLS_AL_SECURITY_TYPE_NONE;
35 //连接平台
36 ret = oc_mqtt_profile_connect(&connect_para);
37 if((ret == (int)en_oc_mqtt_err_ok)){
38 g_app_cb.connected = 1;
39 printf("oc_mqtt_profile_connect succeed!\r\n");
40 }
41 else
42 {
43 printf("oc_mqtt_profile_connect failed!\r\n");
44 }
45```
46
47### 推送数据
48
49当需要上传数据时,需要先拼装数据,让后通过oc_mqtt_profile_propertyreport上报数据。代码示例如下:
50
51```c
52static void deal_report_msg(report_t *report)
53{
54 oc_mqtt_profile_service_t service;
55 oc_mqtt_profile_kv_t smoke_value;
56 oc_mqtt_profile_kv_t beep;
57
58 if (g_app_cb.connected != 1) {
59 return;
60 }
61
62 service.event_time = NULL;
63 service.service_id = "Smoke";
64 service.service_property = &smoke_value;
65 service.nxt = NULL;
66
67 smoke_value.key = "Smoke_Value";
68 smoke_value.value = &report->smokevalue;
69 smoke_value.type = EN_OC_MQTT_PROFILE_VALUE_STRING;
70 smoke_value.nxt = &beep;
71
72 beep.key = "BeepStatus";
73 beep.value = g_app_cb.beep ? "ON" : "OFF";
74 beep.type = EN_OC_MQTT_PROFILE_VALUE_STRING;
75 beep.nxt = NULL;
76 //上报数据
77 oc_mqtt_profile_propertyreport(NULL,&service);
78 return;
79}
80```
81### 命令接收
82
83华为IoT平台支持下发命令,命令是用户自定义的。接收到命令后会将命令数据发送到队列中,CloudMainTaskEntry函数中读取队列数据并调用deal_cmd_msg函数进行处理,代码示例如下:
84
85```c
86
87//use this function to push all the message to the buffer
88static int msg_rcv_callback(oc_mqtt_profile_msgrcv_t *msg)
89{
90 int ret = 0;
91 char *buf;
92 int buf_len;
93 app_msg_t *app_msg;
94
95 if ((msg == NULL) || (msg->request_id == NULL) || (msg->type != EN_OC_MQTT_PROFILE_MSG_TYPE_DOWN_COMMANDS)) {
96 return ret;
97 }
98
99 buf_len = sizeof(app_msg_t) + strlen(msg->request_id) + 1 + msg->msg_len + 1;
100 buf = malloc(buf_len);
101 if (buf == NULL) {
102 return ret;
103 }
104 app_msg = (app_msg_t *)buf;
105 buf += sizeof(app_msg_t);
106
107 app_msg->msg_type = en_msg_cmd;
108 app_msg->msg.cmd.request_id = buf;
109 buf_len = strlen(msg->request_id);
110 buf += buf_len + 1;
111 memcpy_s(app_msg->msg.cmd.request_id, buf_len, msg->request_id, buf_len);
112 app_msg->msg.cmd.request_id[buf_len] = '\0';
113
114 buf_len = msg->msg_len;
115 app_msg->msg.cmd.payload = buf;
116 memcpy_s(app_msg->msg.cmd.payload, buf_len, msg->msg, buf_len);
117 app_msg->msg.cmd.payload[buf_len] = '\0';
118
119 ret = osMessageQueuePut(g_app_cb.app_msg, &app_msg, 0U, CONFIG_QUEUE_TIMEOUT);
120 if (ret != 0) {
121 free(app_msg);
122 }
123
124 return ret;
125}
126
127static void oc_cmdresp(cmd_t *cmd, int cmdret)
128{
129 oc_mqtt_profile_cmdresp_t cmdresp;
130 ///< do the response
131 cmdresp.paras = NULL;
132 cmdresp.request_id = cmd->request_id;
133 cmdresp.ret_code = cmdret;
134 cmdresp.ret_name = NULL;
135 (void)oc_mqtt_profile_cmdresp(NULL, &cmdresp);
136}
137
138///< COMMAND DEAL
139#include <cJSON.h>
140static void deal_cmd_msg(cmd_t *cmd)
141{
142 cJSON *obj_root;
143 cJSON *obj_cmdname;
144 cJSON *obj_paras;
145 cJSON *obj_para;
146
147 int cmdret = 1;
148 obj_root = cJSON_Parse(cmd->payload);
149 if (obj_root == NULL) {
150 oc_cmdresp(cmd, cmdret);
151 }
152
153 obj_cmdname = cJSON_GetObjectItem(obj_root, "command_name");
154 if (obj_cmdname == NULL) {
155 cJSON_Delete(obj_root);
156 }
157 if (strcmp(cJSON_GetStringValue(obj_cmdname), "Smoke_Control_Beep") == 0) {
158 obj_paras = cJSON_GetObjectItem(obj_root, "paras");
159 if (obj_paras == NULL) {
160 cJSON_Delete(obj_root);
161 }
162 obj_para = cJSON_GetObjectItem(obj_paras, "Beep");
163 if (obj_para == NULL) {
164 cJSON_Delete(obj_root);
165 }
166 ///< operate the LED here
167 if (strcmp(cJSON_GetStringValue(obj_para), "ON") == 0) {
168 g_app_cb.beep = 1;
169 BeepStatusSet(ON);
170 printf("Beep On!\r\n");
171 } else {
172 g_app_cb.beep = 0;
173 BeepStatusSet(OFF);
174 printf("Beep Off!\r\n");
175 }
176 cmdret = 0;
177 oc_cmdresp(cmd, cmdret);
178 }
179
180 return;
181}
182```
183
184
185
186
187
188## 编译调试
189
190
191### 登录
192
193设备接入华为云平台之前,需要在平台注册用户账号,华为云地址:<https://www.huaweicloud.com/>
194
195在华为云首页单击产品,找到IoT物联网,单击设备接入IoTDA 并单击立即使用,如下图所示。
196
197
198
199
200
201### 创建产品
202
203在设备接入页面可看到总览界面,展示了华为云平台接入的协议与域名信息,根据需要选取MQTT通讯必要的信息备用,如下图所示。
204
205接入协议(端口号):MQTT 1883
206
207域名:iot-mqtts.cn-north-4.myhuaweicloud.com
208
209
210
211选中侧边栏产品页,单击右上角“创建产品”,在页面中选中所属资源空间,并且按要求填写产品名称,选中MQTT协议,数据格式为JSON,并填写厂商名称,在下方模型定义栏中选择所属行业以及添加设备类型,并单击右下角“确定”,如下图所示。
212
213
214
215
216
217创建完成后,在产品页会自动生成刚刚创建的产品,单击“查看”可查看创建的具体信息,如下图所示。
218
219
220
221
222单击产品详情页的自定义模型,在弹出页面中新增服务,如下图所示。
223
224服务ID:`Smoke`(必须一致)
225
226服务类型:`Senser`(可自定义)
227
228
229在“Smoke”的下拉菜单下点击“添加属性”填写相关信息“Smoke_Value”,“BeepStatus”,如下图所示。
230
231
232
233
234
235
236
237在“Smoke”的下拉菜单下点击“添加命令”填写相关信息,如下图所示。
238
239命令名称:`Smoke_Control_Beep`
240
241参数名称:`Beep`
242
243数据类型:`string`
244
245长度:`3`
246
247枚举值:`ON,OFF`
248
249
250
251
252#### 注册设备
253
254在侧边栏中单击“设备”,进入设备页面,单击右上角“注册设备”,勾选对应所属资源空间并选中刚刚创建的产品,注意设备认证类型选择“秘钥”,按要求填写秘钥,如下图所示。
255
256
257
258记录下设备ID和设备密钥,如下图所示。
259
260
261注册完成后,在设备页面单击“所有设备”,即可看到新建的设备,同时设备处于未激活状态,如下图所示。
262
263
264
265
266### 修改代码中设备信息
267修改`iot_cloud_oc_sample.c`中第31行附近的wifi的ssid和pwd,以及设备的DEVICEID和DEVICEPWD(这两个参数是在平台注册设备时产生的),如下图所示。
268
269
270
271### 修改 BUILD.gn 文件
272
273修改 `device\board\bearpi\bearpi_hm_nano\app`路径下 BUILD.gn 文件,指定 `cloud_oc_smoke` 参与编译。
274
275```r
276"D7_iot_cloud_oc_smoke:cloud_oc_smoke",
277# "D8_iot_cloud_oc_light:cloud_oc_light",
278# "D9_iot_cloud_oc_manhole_cover:cloud_oc_manhole_cover",
279# "D10_iot_cloud_oc_infrared:cloud_oc_infrared",
280# "D11_iot_cloud_oc_agriculture:cloud_oc_agriculture",
281# "D12_iot_cloud_oc_gps:cloud_oc_gps",
282```
283### 测试
284示例代码编译烧录代码后,按下开发板的RESET按键,平台上的设备显示为在线状态,如下图所示。
285
286
287
288点击设备右侧的“查看”,进入设备详情页面,可看到上报的数据。
289
290在华为云平台设备详情页,单击“命令”,选择同步命令下发,选中创建的命令属性,单击“确定”,即可发送下发命令控制设备
291
292