写一个基于STM32F103C8T6与ESP8266通过USART2串口连接,STM32F103C8T6发送AT指令给ESP8266连接服务器或发送数据程序和服务器ESP8266发送数据给STM32F103C8T6
时间: 2024-05-09 12:17:06 浏览: 109
stm32实现esp8266的AT指令
5星 · 资源好评率100%
好的,我明白了。以下是基于STM32F103C8T6与ESP8266通过USART2串口连接,STM32F103C8T6发送AT指令给ESP8266连接服务器或发送数据程序和服务器ESP8266发送数据给STM32F103C8T6的参考代码:
STM32F103C8T6发送AT指令给ESP8266连接服务器或发送数据程序:
```c
#include "stm32f10x.h"
void USART2_Config(void)
{
GPIO_InitTypeDef GPIO_InitStruct;
USART_InitTypeDef USART_InitStruct;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStruct);
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_3;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStruct);
USART_InitStruct.USART_BaudRate = 9600;
USART_InitStruct.USART_WordLength = USART_WordLength_8b;
USART_InitStruct.USART_StopBits = USART_StopBits_1;
USART_InitStruct.USART_Parity = USART_Parity_No;
USART_InitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStruct.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;
USART_Init(USART2, &USART_InitStruct);
USART_Cmd(USART2, ENABLE);
}
char USART2_receive_char(){
while (USART_GetFlagStatus(USART2, USART_FLAG_RXNE) == RESET);
return USART_ReceiveData(USART2);
}
void USART2_send_char(char c){
while (USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET);
USART_SendData(USART2, c);
}
void USART2_send_string(char *str){
int i = 0;
while (str[i]){
USART2_send_char(str[i]);
i++;
}
}
int main(void)
{
USART2_Config(); // 配置USART2串口
USART2_send_string("AT+CWJAP=\"SSID\",\"PASSWORD\"\r\n"); // 连接WiFi
delay_ms(2000); // 延时2秒
USART2_send_string("AT+CIPSTART=\"TCP\",\"IP\",PORT\r\n"); // 连接服务器
delay_ms(2000); // 延时2秒
USART2_send_string("AT+CIPSEND=6\r\n"); // 发送数据前设置数据长度为6
USART2_send_string("Hello\r\n"); // 发送数据
delay_ms(2000); // 延时2秒
while(1);
}
```
ESP8266发送数据给STM32F103C8T6程序:
```c
#include "stm32f10x.h"
void USART2_Config(void)
{
GPIO_InitTypeDef GPIO_InitStruct;
USART_InitTypeDef USART_InitStruct;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStruct);
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_3;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStruct);
USART_InitStruct.USART_BaudRate = 9600;
USART_InitStruct.USART_WordLength = USART_WordLength_8b;
USART_InitStruct.USART_StopBits = USART_StopBits_1;
USART_InitStruct.USART_Parity = USART_Parity_No;
USART_InitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStruct.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;
USART_Init(USART2, &USART_InitStruct);
USART_Cmd(USART2, ENABLE);
}
char USART2_receive_char(){
while (USART_GetFlagStatus(USART2, USART_FLAG_RXNE) == RESET);
return USART_ReceiveData(USART2);
}
void USART2_send_char(char c){
while (USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET);
USART_SendData(USART2, c);
}
void USART2_send_string(char *str){
int i = 0;
while (str[i]){
USART2_send_char(str[i]);
i++;
}
}
int main(void)
{
USART2_Config(); // 配置USART2串口
while(1)
{
if (USART_GetFlagStatus(USART2, USART_FLAG_RXNE) != RESET) // 接收到数据
{
char receive_data = USART_ReceiveData(USART2);
if (receive_data == '\n') // 数据接收完毕
{
USART2_send_string("Received data: "); // 回复数据接收成功
USART2_send_string(received_data);
USART2_send_string("\r\n");
received_data = ""; // 清空接收到的数据
}
else
{
received_data += receive_data; // 继续接收数据
}
}
}
}
```
阅读全文