本示例将在BearPi-HM_Nano開發闆上使用MQTT協定連接配接華為IoT平台,使用的是E53_IA1 智慧農業擴充闆與 BearPi-HM_Nano 開發闆。
E53_IA1開發闆對應方法:
#ifndef __E53_IA1_H__
#define __E53_IA1_H__
/***************************************************************
* 名 稱: GasStatus_ENUM
* 說 明:枚舉狀态結構體
***************************************************************/
typedef enum
{
OFF = 0,
ON
} E53_IA1_Status_ENUM;
/* E53_IA1傳感器資料類型定義 ------------------------------------------------------------*/
typedef struct
{
float Lux; //光照強度
float Humidity; //濕度
float Temperature; //溫度
} E53_IA1_Data_TypeDef;
/* 寄存器宏定義 --------------------------------------------------------------------*/
#define SHT30_Addr 0x44 //十進制
#define BH1750_Addr 0x23
void E53_IA1_Init(void); //初始化開發闆
void E53_IA1_Read_Data(E53_IA1_Data_TypeDef *ReadData); //讀取傳感器上的資料:溫度、濕度、光照 *ReadData 指針變量
void Light_StatusSet(E53_IA1_Status_ENUM status); //燈狀态設定 void Motor_StatusSet(E53_IA1_Status_ENUM status); //電機狀态設定
#endif /* __E53_IA1_H__ */
二、連接配接平台
### 連接配接平台
在連接配接平台前需要設定擷取CONFIG_APP_DEVICEID、CONFIG_APP_DEVICEPWD、CONFIG_APP_SERVERIP、CONFIG_APP_SERVERPORT,通過oc_mqtt_profile_connect()函數連接配接平台。
```c
WifiConnect(CONFIG_WIFI_SSID, CONFIG_WIFI_PWD);
dtls_al_init();
mqtt_al_init();
oc_mqtt_init();
g_app_cb.app_msg = queue_create("queue_rcvmsg",10,1);
if(NULL == g_app_cb.app_msg){
printf("Create receive msg queue failed");
}
oc_mqtt_profile_connect_t connect_para;
(void) memset( &connect_para, 0, sizeof(connect_para));
connect_para.boostrap = 0;
connect_para.device_id = CONFIG_APP_DEVICEID;
connect_para.device_passwd = CONFIG_APP_DEVICEPWD;
connect_para.server_addr = CONFIG_APP_SERVERIP;
connect_para.server_port = CONFIG_APP_SERVERPORT;
connect_para.life_time = CONFIG_APP_LIFETIME;
connect_para.rcvfunc = msg_rcv_callback;
connect_para.security.type = EN_DTLS_AL_SECURITY_TYPE_NONE;
ret = oc_mqtt_profile_connect(&connect_para);
if((ret == (int)en_oc_mqtt_err_ok)){
g_app_cb.connected = 1;
printf("oc_mqtt_profile_connect succed!\r\n");
}
else
{
printf("oc_mqtt_profile_connect faild!\r\n");
}
```
三、指令接收
### 推送資料
當需要上傳資料時,需要先拼裝資料,讓後通過oc_mqtt_profile_propertyreport上報資料。代碼示例如下:
```c
static void deal_report_msg(report_t *report)
{
oc_mqtt_profile_service_t service;
oc_mqtt_profile_kv_t temperature;
oc_mqtt_profile_kv_t humidity;
oc_mqtt_profile_kv_t luminance;
oc_mqtt_profile_kv_t led;
oc_mqtt_profile_kv_t motor;
service.event_time = NULL;
service.service_id = "Agriculture";
service.service_property = &temperature;
service.nxt = NULL;
temperature.key = "Temperature";
temperature.value = &report->temp;
temperature.type = EN_OC_MQTT_PROFILE_VALUE_INT;
temperature.nxt = &humidity;
humidity.key = "Humidity";
humidity.value = &report->hum;
humidity.type = EN_OC_MQTT_PROFILE_VALUE_INT;
humidity.nxt = &luminance;
luminance.key = "Luminance";
luminance.value = &report->lum;
luminance.type = EN_OC_MQTT_PROFILE_VALUE_INT;
luminance.nxt = &led;
led.key = "LightStatus";
led.value = g_app_cb.led?"ON":"OFF";
led.type = EN_OC_MQTT_PROFILE_VALUE_STRING;
led.nxt = &motor;
motor.key = "MotorStatus";
motor.value = g_app_cb.motor?"ON":"OFF";
motor.type = EN_OC_MQTT_PROFILE_VALUE_STRING;
motor.nxt = NULL;
oc_mqtt_profile_propertyreport(USERNAME,&service);
return;
}
```
代碼編譯與燒錄到開發闆
擷取資料,背景的log
華為雲上接收到開發闆的資料---LED訓示燈與電機