#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 parse_json(uint8_t *json_buff); //控制上电并开机 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+QMTCFG=\"session\",0,0\r\n"); 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) - 2; i++)// 雨量光辐射还是空气 { cJSON_AddItemToArray(DataArray, cJSON_CreateNumber(((float)((int )(ptr[i] * 100 + 0.5)))/100.0));// 四舍五入两位小数 } // 对象转字符串 cjson_str = cJSON_Print(JsonRoot); str_len = strlen(cjson_str) + 2 + 4; 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; } //时间获取完成变量,用于控制是否开始MQTT信息接收 uint8_t time_get_ok = 0; // 生成时间戳 int 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++] != '"' && uart_dev_char_present(g_ec801_uart_handle)); // 丢掉前面的 memcpy(time, time + index - 1, index); index = 1; // "前面是时间 do{ time[index] = uart_dev_in_char(g_ec801_uart_handle); }while(time[index++] != '"' && uart_dev_char_present(g_ec801_uart_handle)); // 字符提取成int sscanf(time, "\"%d/%d/%d,%d:%d:%d\"", &year, &month, &day, &hour, &minute, &second); if(year) { time_get_ok = 1; } // 生成时间戳 g_time_stamp = fml_time_to_stamp(year, month, day, hour, minute, second); return year; } #define JSON_BUFFER_SIZE 200 // 解析收到的4g模块数据 void parse_4g_receive_data() { uint8_t temp_buff[JSON_BUFFER_SIZE]; int jsonBufferIndex = 0; // 索引 char c = 0; int inJson = 0; if(uart_dev_char_present(g_ec801_uart_handle)){ memset(temp_buff, '\0', sizeof(temp_buff));//每次接受前清空一下BUFF for(jsonBufferIndex = 0; uart_dev_char_present(g_ec801_uart_handle);) { c = uart_dev_in_char(g_ec801_uart_handle); if (c == '{') { inJson = 1; // 进入JSON字符串 jsonBufferIndex = 0; // 重置JSON缓冲区索引 temp_buff[jsonBufferIndex++] = c; } else if (c == '}' && inJson) { temp_buff[jsonBufferIndex++] = c; //重置索引与标志 jsonBufferIndex = 0; inJson = 0; } else if (inJson) { // 如果在JSON字符串内部,则存储字符 if (jsonBufferIndex < JSON_BUFFER_SIZE - 1) { // 保留一个位置给字符串结束符 temp_buff[jsonBufferIndex++] = c; } }else { jsonBufferIndex++;//一直没有{可以继续检索 } } // temp_buff为接收到的JSON // term_printf(temp_buff); // 不为空再解析 if(temp_buff[0] != '\0') { parse_json(temp_buff); } } } void parse_json(uint8_t *json_buff) { cJSON* cjson_root = cJSON_Parse(json_buff); if(cjson_root == NULL) { term_printf("parse fail.\n"); return; } cJSON* cjson_id = cJSON_GetObjectItem(cjson_root, "deviId"); cJSON* cjson_type = cJSON_GetObjectItem(cjson_root, "frameType"); cJSON* cjson_version = cJSON_GetObjectItem(cjson_root, "version"); cJSON* cjson_response = cJSON_GetObjectItem(cjson_root, "response"); cJSON* cjson_time = cJSON_GetObjectItem(cjson_root, "timeStamp"); // 取出数据 char *temp_id = cjson_id -> valuestring; char *temp_type = cjson_type -> valuestring; int temp_version = cjson_version -> valueint; int temp_response = cjson_response -> valueint; int temp_time = cjson_time -> valueint; // term_printf("deviId=%s\n frameType=%s\n version=%d\n response=%d\n timeStamp=%d\n", temp_id, temp_type, temp_version, temp_response, temp_time); cJSON_Delete(cjson_root); // 数据处理 }