94 lines
2.6 KiB
C
94 lines
2.6 KiB
C
#include "EC801E.h"
|
||
#include "stdio.h"
|
||
#include "usart.h"
|
||
#include "string.h"
|
||
#include "cJSON.h"
|
||
#include "uart_dev.h"
|
||
#include "anemometer_dev.h"
|
||
|
||
//控制上电并开机
|
||
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发送JSON数据
|
||
void MQTT_Trans_Json()
|
||
{
|
||
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", "占位");
|
||
cJSON_AddStringToObject(JsonRoot, "frameType", "占位");
|
||
cJSON_AddNumberToObject(JsonRoot, "timeStamp", 1722844604);
|
||
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_AddItemToArray(DataArray, cJSON_CreateNumber(g_stMcs_Para.min_wind_direction));
|
||
|
||
// 对象转字符串+发送
|
||
cjson_str = cJSON_Print(JsonRoot);
|
||
uart_sendstr(g_term_uart_handle, cjson_str);
|
||
vPortFree(cjson_str);
|
||
|
||
cJSON_Delete(JsonRoot);
|
||
}
|
||
|
||
// MQTT发送数据命令
|
||
void MQTT_Trans_Command()
|
||
{
|
||
|
||
} |