micro_climate/Drivers/EC801E/EC801E.c

71 lines
2.0 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include "EC801E.h"
#include "stdio.h"
#include "usart.h"
#include "string.h"
#include "cJSON.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模块完全开机
HAL_Delay(5000);
// 打开客户端网络
HAL_UART_Transmit(&huart5, (uint8_t *)"AT+QMTOPEN=0,199.7.140.10,1883\r\n", sizeof("AT+QMTOPEN=0,199.7.140.10,1883\r\n"), 0xFFFF);
// 确保打开网络完成
HAL_Delay(5000);
// 连接服务器
HAL_UART_Transmit(&huart5, (uint8_t *)"AT+QMTCONN=0,Test_SUB\r\n", sizeof("AT+QMTCONN=0,Test_SUB\r\n"), 0xFFFF);
// 确保服务器连接完毕
HAL_Delay(5000);
// 订阅主题
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);
}
void MQTT_Trans_Json()
{
char *cjson_str = NULL;
cJSON * JsonRoot = cJSON_CreateObject();
cJSON_AddStringToObject(JsonRoot, "deviId", "占位");
cJSON_AddStringToObject(JsonRoot, "frameType", "占位");
cJSON_AddNumberToObject(JsonRoot, "timeStamp", 1722844604);
cJSON_AddNumberToObject(JsonRoot, "Version", 10);
cjson_str = cJSON_Print(JsonRoot);
HAL_UART_Transmit(&huart1, cjson_str, sizeof(cjson_str), 0xFFFF);
// printf("%s", cjson_str);
free(cjson_str);
cJSON_Delete(JsonRoot);
}