#include #include #include #include #include #include #include "E53_IA1.h" #include "lcd.h" #include #include #include #define cn_endpoint_id "BearPi_0001" #define cn_app_server "119.3.250.80" #define cn_app_port "5683" #define cn_app_Agriculture 0x0 #define cn_app_Agriculture_Control_Light 0x1 #define cn_app_response_Agriculture_Control_Light 0x2 #define cn_app_Agriculture_Control_Motor 0x3 #define cn_app_response_Agriculture_Control_Motor 0x4 #pragma pack(1) // 各类信息 typedef struct { int8_t messageId; int8_t Temperature; // 温度 int8_t Humidity; // 湿度 uint16_t Luminance; // 光照强度 } tag_app_Agriculture; typedef struct { int8_t messageId; uint16_t mid; int8_t errcode; int8_t Light_State; // 灯状态 } tag_app_Response_Agriculture_Control_Light; typedef struct { int8_t messageId; uint16_t mid; int8_t errcode; int8_t Motor_State; // 马达状态 } tag_app_Response_Agriculture_Control_Motor; typedef struct { int8_t messageId; uint16_t mid; char Light[3]; // 灯是否打开(ON, OFF) } tag_app_Agriculture_Control_Light; typedef struct { uint8_t messageId; uint16_t mid; char Motor[3]; // 马达是否打开(ON, OFF) } tag_app_Agriculture_Control_Motor; #pragma pack() E53_IA1_Data_TypeDef E53_IA1_Data; #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 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; tag_app_Response_Agriculture_Control_Light Response_Agriculture_Control_Light; tag_app_Response_Agriculture_Control_Motor Response_Agriculture_Control_Motor; tag_app_Agriculture_Control_Light *Agriculture_Control_Light; tag_app_Agriculture_Control_Motor *Agriculture_Control_Motor; 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_Agriculture_Control_Light: Agriculture_Control_Light = (tag_app_Agriculture_Control_Light *)s_rcv_buffer; printf("Agriculture_Control_Light:msgid:%d mid:%d", Agriculture_Control_Light->messageId, ntohs(Agriculture_Control_Light->mid)); if (Agriculture_Control_Light->Light[0] == 'O' && Agriculture_Control_Light->Light[1] == 'N') { HAL_GPIO_WritePin(IA1_Light_GPIO_Port, IA1_Light_Pin, GPIO_PIN_SET); Response_Agriculture_Control_Light.messageId = cn_app_response_Agriculture_Control_Light; Response_Agriculture_Control_Light.mid = Agriculture_Control_Light->mid; Response_Agriculture_Control_Light.errcode = 0; Response_Agriculture_Control_Light.Light_State = 1; oc_lwm2m_report((char *)&Response_Agriculture_Control_Light, sizeof(Response_Agriculture_Control_Light), 1000); } if (Agriculture_Control_Light->Light[0] == 'O' && Agriculture_Control_Light->Light[1] == 'F' && Agriculture_Control_Light->Light[2] == 'F') { HAL_GPIO_WritePin(IA1_Light_GPIO_Port, IA1_Light_Pin, GPIO_PIN_RESET); Response_Agriculture_Control_Light.messageId = cn_app_response_Agriculture_Control_Light; Response_Agriculture_Control_Light.mid = Agriculture_Control_Light->mid; Response_Agriculture_Control_Light.errcode = 0; Response_Agriculture_Control_Light.Light_State = 0; oc_lwm2m_report((char *)&Response_Agriculture_Control_Light, sizeof(Response_Agriculture_Control_Light), 1000); } break; case cn_app_Agriculture_Control_Motor: Agriculture_Control_Motor = (tag_app_Agriculture_Control_Motor *)s_rcv_buffer; printf("Agriculture_Control_Motor:msgid:%d mid:%d", Agriculture_Control_Motor->messageId, ntohs(Agriculture_Control_Motor->mid)); if (Agriculture_Control_Motor->Motor[0] == 'O' && Agriculture_Control_Motor->Motor[1] == 'N') { HAL_GPIO_WritePin(IA1_Motor_GPIO_Port, IA1_Motor_Pin, GPIO_PIN_SET); Response_Agriculture_Control_Motor.messageId = cn_app_response_Agriculture_Control_Motor; Response_Agriculture_Control_Motor.mid = Agriculture_Control_Motor->mid; Response_Agriculture_Control_Motor.errcode = 0; Response_Agriculture_Control_Motor.Motor_State = 1; oc_lwm2m_report((char *)&Response_Agriculture_Control_Motor, sizeof(Response_Agriculture_Control_Motor), 1000); } if (Agriculture_Control_Motor->Motor[0] == 'O' && Agriculture_Control_Motor->Motor[1] == 'F' && Agriculture_Control_Motor->Motor[2] == 'F') { HAL_GPIO_WritePin(IA1_Motor_GPIO_Port, IA1_Motor_Pin, GPIO_PIN_RESET); Response_Agriculture_Control_Motor.messageId = cn_app_response_Agriculture_Control_Motor; Response_Agriculture_Control_Motor.mid = Agriculture_Control_Motor->mid; Response_Agriculture_Control_Motor.errcode = 0; Response_Agriculture_Control_Motor.Motor_State = 0; oc_lwm2m_report((char *)&Response_Agriculture_Control_Motor, sizeof(Response_Agriculture_Control_Motor), 1000); } break; default: break; } } } return ret; } static int app_report_task_entry() { int ret = -1; oc_config_param_t oc_param; tag_app_Agriculture Agriculture; (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; ret = oc_lwm2m_config(&oc_param); if (0 != ret) { return ret; } // 主要运行代码 while (1) { Agriculture.messageId = cn_app_Agriculture; Agriculture.Temperature = (int8_t)E53_IA1_Data.Temperature; Agriculture.Humidity = (int8_t)E53_IA1_Data.Humidity; Agriculture.Luminance = htons((uint16_t)E53_IA1_Data.Lux); oc_lwm2m_report((char *)&Agriculture, sizeof(Agriculture), 1000); osal_task_sleep(2 * 1000); } return ret; } static int app_collect_task_entry() { Init_E53_IA1(); while (1) { E53_IA1_Read_Data(); printf("\r\n******************************Lux Value is %d\r\n", (int)E53_IA1_Data.Lux); printf("\r\n******************************Humidity is %d\r\n", (int)E53_IA1_Data.Humidity); printf("\r\n******************************Temperature is %d\r\n", (int)E53_IA1_Data.Temperature); LCD_ShowString(10, 140, 200, 16, 16, "Temperature:"); LCD_ShowNum(140, 140, (int)E53_IA1_Data.Temperature, 5, 16); LCD_ShowString(10, 170, 200, 16, 16, "Humidity:"); LCD_ShowNum(140, 170, (int)E53_IA1_Data.Humidity, 5, 16); LCD_ShowString(10, 200, 200, 16, 16, "Luminance:"); LCD_ShowNum(140, 200, (int)E53_IA1_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, 40, 200, 16, 24, "Agriculture Demo"); LCD_ShowString(10, 80, 200, 16, 16, "NCDP_IP:"); LCD_ShowString(80, 80, 200, 16, 16, cn_app_server); LCD_ShowString(10, 110, 200, 16, 16, "NCDP_PORT:"); LCD_ShowString(100, 110, 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); return 0; }