串口2发送AT指令连接阿里云库函数代码
时间: 2023-06-27 10:05:19 浏览: 194
这是一个基于STM32的串口2发送AT指令连接阿里云的示例代码,你可以根据自己的需求进行修改:
```
#include "stm32f4xx_hal.h"
UART_HandleTypeDef huart2;
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_USART2_UART_Init(void);
void Aliyun_Connect(void) {
char cmd[100];
HAL_UART_Transmit(&huart2, (uint8_t *) "AT\r\n", 4, 1000);
HAL_Delay(200);
HAL_UART_Transmit(&huart2, (uint8_t *) "ATE0\r\n", 6, 1000);
HAL_Delay(200);
HAL_UART_Transmit(&huart2, (uint8_t *) "AT+CPIN?\r\n", 10, 1000);
HAL_Delay(200);
HAL_UART_Transmit(&huart2, (uint8_t *) "AT+CREG?\r\n", 10, 1000);
HAL_Delay(200);
HAL_UART_Transmit(&huart2, (uint8_t *) "AT+CGATT?\r\n", 11, 1000);
HAL_Delay(200);
sprintf(cmd, "AT+QIFGCNT=0\r\n");
HAL_UART_Transmit(&huart2, (uint8_t *) cmd, strlen(cmd), 1000);
HAL_Delay(200);
sprintf(cmd, "AT+QICSGP=1,\"CMNET\"\r\n");
HAL_UART_Transmit(&huart2, (uint8_t *) cmd, strlen(cmd), 1000);
HAL_Delay(200);
sprintf(cmd, "AT+QIACT\r\n");
HAL_UART_Transmit(&huart2, (uint8_t *) cmd, strlen(cmd), 1000);
HAL_Delay(2000);
sprintf(cmd, "AT+QMTOPEN=0,\"a1XXXXXXXXX.iot-as-mqtt.cn-shanghai.aliyuncs.com\",1883\r\n");
HAL_UART_Transmit(&huart2, (uint8_t *) cmd, strlen(cmd), 1000);
HAL_Delay(2000);
sprintf(cmd, "AT+QMTCONN=0,\"XXXXXX|securemode=3,signmethod=hmacsha1|\"\r\n");
HAL_UART_Transmit(&huart2, (uint8_t *) cmd, strlen(cmd), 1000);
HAL_Delay(2000);
}
int main(void) {
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_USART2_UART_Init();
Aliyun_Connect();
while (1) {
}
}
void SystemClock_Config(void) {
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
__HAL_RCC_PWR_CLK_ENABLE();
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
RCC_OscInitStruct.PLL.PLLM = 16;
RCC_OscInitStruct.PLL.PLLN = 336;
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV4;
RCC_OscInitStruct.PLL.PLLQ = 7;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) {
Error_Handler();
}
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != HAL_OK) {
Error_Handler();
}
}
static void MX_USART2_UART_Init(void) {
huart2.Instance = USART2;
huart2.Init.BaudRate = 115200;
huart2.Init.WordLength = UART_WORDLENGTH_8B;
huart2.Init.StopBits = UART_STOPBITS_1;
huart2.Init.Parity = UART_PARITY_NONE;
huart2.Init.Mode = UART_MODE_TX_RX;
huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
huart2.Init.OverSampling = UART_OVERSAMPLING_16;
if (HAL_UART_Init(&huart2) != HAL_OK) {
Error_Handler();
}
}
static void MX_GPIO_Init(void) {
__HAL_RCC_GPIOH_CLK_ENABLE();
__HAL_RCC_GPIOA_CLK_ENABLE();
GPIO_InitTypeDef GPIO_InitStruct = {0};
GPIO_InitStruct.Pin = GPIO_PIN_0;
GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
HAL_GPIO_Init(GPIOH, &GPIO_InitStruct);
GPIO_InitStruct.Pin = GPIO_PIN_2 | GPIO_PIN_3;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF7_USART2;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
}
void Error_Handler(void) {
while (1) {
}
}
```
需要注意的是,这里使用的是AT指令连接阿里云,而不是MQTT或者其他协议,如果你需要使用其他协议,请查询阿里云官方文档并修改代码。另外,这里的代码仅供参考,请根据自己的具体情况进行修改和调试。
阅读全文