stm32f103c8t6驱动esp8266转串口模块(二)——模块ap模式+tcp客户端的hal库驱动代
时间: 2024-01-11 19:00:45 浏览: 93
stm32f103c8t6驱动esp8266转串口模块(二)的模块ap模式的TCP客户端的HAL库驱动代代码如下:
```c
#include <stdint.h>
#include <stdio.h>
#include "stm32f1xx_hal.h"
UART_HandleTypeDef huart1;
UART_HandleTypeDef huart2;
void MX_USART1_UART_Init(void)
{
huart1.Instance = USART1;
huart1.Init.BaudRate = 115200;
huart1.Init.WordLength = UART_WORDLENGTH_8B;
huart1.Init.StopBits = UART_STOPBITS_1;
huart1.Init.Parity = UART_PARITY_NONE;
huart1.Init.Mode = UART_MODE_TX_RX;
huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
huart1.Init.OverSampling = UART_OVERSAMPLING_16;
if (HAL_UART_Init(&huart1) != HAL_OK)
{
Error_Handler();
}
}
void MX_USART2_UART_Init(void)
{
huart2.Instance = USART2;
huart2.Init.BaudRate = 9600;
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();
}
}
void HAL_UART_MspInit(UART_HandleTypeDef* uartHandle)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
if(uartHandle->Instance==USART1)
{
__HAL_RCC_GPIOA_CLK_ENABLE();
__HAL_RCC_USART1_CLK_ENABLE();
GPIO_InitStruct.Pin = GPIO_PIN_9;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
GPIO_InitStruct.Pin = GPIO_PIN_10;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
}
else if(uartHandle->Instance==USART2)
{
__HAL_RCC_GPIOA_CLK_ENABLE();
__HAL_RCC_USART2_CLK_ENABLE();
GPIO_InitStruct.Pin = GPIO_PIN_2;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
GPIO_InitStruct.Pin = GPIO_PIN_3;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
}
}
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
// Here you can handle the received data
// For example, you can parse the incoming data, check for specific commands, etc.
HAL_UART_Receive_IT(huart, rx_buffer, rx_buffer_size); // Start listening for the next data
}
void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart)
{
// Here you can handle the completion of the transmission
}
void ESP8266_TCP_Connect(void)
{
char at_cmd[100];
sprintf(at_cmd, "AT+CWMODE=1\r\n");
HAL_UART_Transmit(&huart1, (uint8_t*)at_cmd, strlen(at_cmd), 500);
HAL_Delay(100);
sprintf(at_cmd, "AT+CWJAP=\"SSID\",\"PASSWORD\"\r\n");
HAL_UART_Transmit(&huart1, (uint8_t*)at_cmd, strlen(at_cmd), 500);
HAL_Delay(2000);
sprintf(at_cmd, "AT+CIPSTART=\"TCP\",\"DEST_IP\",DEST_PORT\r\n");
HAL_UART_Transmit(&huart1, (uint8_t*)at_cmd, strlen(at_cmd), 500);
HAL_Delay(2000);
}
int main(void)
{
HAL_Init();
MX_USART1_UART_Init();
MX_USART2_UART_Init();
HAL_UART_Receive_IT(&huart2, rx_buffer, rx_buffer_size); // Start listening for data on UART2
ESP8266_TCP_Connect();
while (1)
{
// Main program loop
}
}
```
以上代码演示了使用stm32f103c8t6驱动esp8266转串口模块(二)上的用于模块AP模式的TCP客户端的HAL库驱动代代码。此代码的主要功能是通过STM32的串口与esp8266模块进行通信,建立TCP连接并发送命令。
阅读全文