#include "EC801E.h" #include "stdio.h" #include "usart.h" #include "string.h" #include "cJSON.h" #include "uart_dev.h" #include "anemometer_dev.h" #define USE_UTC 1 uint32_t g_time_stamp; //控制上电并开机 void EC801E_Power_ON() { // PWR_KEY_4G_Pin低电平,上电自动开机 HAL_GPIO_WritePin(GPIO_4G_PWR_KEY_GPIO_Port, GPIO_4G_PWR_KEY_Pin, GPIO_PIN_SET); //上电 HAL_GPIO_WritePin(GPIO_4G_PWR_CTRL_GPIO_Port, GPIO_4G_PWR_CTRL_Pin, GPIO_PIN_SET); } //开机状态检测 //HAL_OK:正常开机 uint8_t Read_Status() { uint8_t temp_status = HAL_ERROR; temp_status = HAL_GPIO_ReadPin(GPIO_4G_STATUS_GPIO_Port, GPIO_4G_STATUS_Pin) == GPIO_PIN_SET ? HAL_OK : HAL_ERROR; return temp_status; } //串口重定向打印 size_t __write(int handle, const unsigned char * buffer, size_t size) { if(HAL_OK == HAL_UART_Transmit(&huart1,(uint8_t *)buffer,size,100000)) { return size; } else { return -1; } } // MQTT打开客户端网络.连接MQTT服务器.订阅 void MQTT_Config() { // 确保4G模块完全开机 osDelay(5000); // 打开客户端网络 uart_sendstr(g_ec801_uart_handle, "AT+QMTOPEN=0,199.7.140.10,1883\r\n"); // HAL_UART_Transmit(&huart5, (uint8_t *)"AT+QMTOPEN=0,199.7.140.10,1883\r\n", 30, 0xFFFF); // 确保打开网络完成 osDelay(5000); // 连接服务器 uart_sendstr(g_ec801_uart_handle, "AT+QMTCONN=0,Test_SUB\r\n"); // HAL_UART_Transmit(&huart5, (uint8_t *)"AT+QMTCONN=0,Test_SUB\r\n", sizeof("AT+QMTCONN=0,Test_SUB\r\n"), 0xFFFF); // 确保服务器连接完毕 osDelay(5000); // 订阅主题 uart_sendstr(g_ec801_uart_handle, "AT+QMTSUB=0,0,Test_Topic,0\r\n"); // HAL_UART_Transmit(&huart5, (uint8_t *)"AT+QMTSUB=0,0,Test_Topic,0\r\n", sizeof("AT+QMTSUB=0,0,Test_Topic,0\r\n"), 0xFFFF); } // MQTT发送数据 void MQTT_Trans_Data() { //字符串长度 uint8_t str_len = 0; char str_len_str[32]; //创建获取数据指针 float32_t *ptr = (float32_t *)&g_stMcs_Para; // 创建JSON数组及对象 char *cjson_str = NULL; cJSON * JsonRoot = cJSON_CreateObject(); cJSON * DataArray = cJSON_CreateArray(); cJSON_AddStringToObject(JsonRoot, "deviId", "item_id"); cJSON_AddStringToObject(JsonRoot, "frameType", "item_type"); cJSON_AddNumberToObject(JsonRoot, "timeStamp", g_time_stamp); cJSON_AddNumberToObject(JsonRoot, "Version", 10); cJSON_AddItemToObject(JsonRoot, "data", DataArray);//添加data数组 for(int i = 0; i < sizeof(mcs_para)/sizeof(float32_t); i++) { cJSON_AddItemToArray(DataArray, cJSON_CreateNumber(ptr[i])); } // 对象转字符串 cjson_str = cJSON_Print(JsonRoot); str_len = strlen(cjson_str) + 2; sprintf(str_len_str, "%d", str_len); // 发送发数据包命令 osDelay(2000); uart_sendstr(g_ec801_uart_handle, "AT+QMTPUBEX=0,0,0,0,Test_Topic,"); uart_sendstr(g_ec801_uart_handle, str_len_str); uart_sendstr(g_ec801_uart_handle, "\r\n"); //发送数据包 osDelay(2000); uart_sendstr(g_ec801_uart_handle, cjson_str); uart_sendstr(g_ec801_uart_handle, "\r\n"); //释放 vPortFree(cjson_str); cJSON_Delete(JsonRoot); } // 判断闰年,1闰0平 uint16_t fml_leap_year(uint16_t year) { return (((year % 4 == 0)&&(year % 100 != 0)) || (year % 400 == 0)); } //日期转时间戳 uint32_t fml_time_to_stamp(int year, int month, int day, int hour, int minute, int second) { static uint32_t dax = 0; static uint32_t day_count = 0; uint16_t leap_year_count = 0; uint16_t i; // 计算闰年数 for (i = 1970; i < year; i++) { if (fml_leap_year(i)) { leap_year_count++; } } // 计算年的总天数 day_count = leap_year_count * 366 + (year - 1970 - leap_year_count) * 365; uint8_t mouthday[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; // 计算当年到当前月的所有天数 for (i = 1; i < month; i++) { day_count += mouthday[i]; } if(fml_leap_year(year)) { day_count += 1; } // 累加计算当月的天数 day_count += (day - 1); dax = (uint32_t)(day_count * 86400) + (uint32_t)((uint32_t)hour * 3600) + (uint32_t)((uint32_t)minute * 60) + (uint32_t)second; return dax; } // 生成时间戳 void EC801_GET_Time() { int year, month, day, hour, minute, second; if(USE_UTC) { uart_sendstr(g_ec801_uart_handle, "AT+QLTS=0\r\n"); }else { uart_sendstr(g_ec801_uart_handle, "AT+QLTS=2\r\n"); } osDelay(1000); char time[100] = {0};int index = 0; // 第一个“后是时间,前面不要 do{ time[index] = uart_dev_in_char(g_ec801_uart_handle); }while(time[index++] != '"'); // 丢掉前面的 memcpy(time, time + index - 1, index); index = 1; // "前面是时间 do{ time[index] = uart_dev_in_char(g_ec801_uart_handle); }while(time[index++] != '"'); // 字符提取成int int matched = sscanf(time, "\"%d/%d/%d,%d:%d:%d\"", &year, &month, &day, &hour, &minute, &second); // 生成时间戳 g_time_stamp = fml_time_to_stamp(year, month, day, hour, minute, second); }