本示例将在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指示灯与电机