stm32f103c8t6使用ESP8266模块station模式,其中ESP8266作为服务器,手机为客户端,请用标准库完整写出服务器的代码
时间: 2024-05-05 13:21:03 浏览: 63
以下是使用标准库完整写出的stm32f103c8t6作为ESP8266模块的station模式服务器代码,其中ESP8266作为服务器,手机为客户端。
```
#include "stm32f10x.h"
#include <stdio.h>
#include <string.h>
#define MAX_RECV_LEN 64
#define MAX_SEND_LEN 64
USART_InitTypeDef USART_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
void USART1_Configuration(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_USART1, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
USART_InitStructure.USART_BaudRate = 115200;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_Init(USART1, &USART_InitStructure);
USART_Cmd(USART1, ENABLE);
}
void GPIO_Configuration(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
}
void delay_ms(u16 nms)
{
u32 i;
for (i = 0; i < nms; i++)
{
u16 j = 1000;
while (j--)
;
}
}
void USART_SendString(USART_TypeDef *USARTx, char *str)
{
while (*str)
{
while (USART_GetFlagStatus(USARTx, USART_FLAG_TC) == RESET)
;
USART_SendData(USARTx, *str++);
}
}
int main(void)
{
USART1_Configuration();
GPIO_Configuration();
USART_SendString(USART1, "AT+CWMODE=1\r\n");
delay_ms(500);
USART_SendString(USART1, "AT+CIPMUX=1\r\n");
delay_ms(500);
USART_SendString(USART1, "AT+CIPSERVER=1,8080\r\n");
delay_ms(500);
while (1)
{
if (USART_GetFlagStatus(USART1, USART_FLAG_RXNE) == SET)
{
char c = USART_ReceiveData(USART1);
if (c == '+')
{
char recv_str[MAX_RECV_LEN + 1];
memset(recv_str, 0, sizeof(recv_str));
int recv_len = 0;
while (1)
{
c = USART_ReceiveData(USART1);
if (c == '\r')
continue;
if (c == '\n')
break;
if (recv_len >= MAX_RECV_LEN)
break;
recv_str[recv_len++] = c;
}
if (recv_len > 0)
{
recv_str[recv_len] = '\0';
printf("Recv: %s\n", recv_str);
if (strstr(recv_str, "+IPD,"))
{
int comma_pos = 0;
int data_len = 0;
for (int i = 0; i < recv_len; i++)
{
if (recv_str[i] == ',')
{
comma_pos = i;
break;
}
}
sscanf(&recv_str[comma_pos + 1], "%d", &data_len);
if (data_len > 0)
{
char send_str[MAX_SEND_LEN + 1];
memset(send_str, 0, sizeof(send_str));
sprintf(send_str, "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n<html><body><h1>Hello STM32!</h1></body></html>");
printf("Send: %s\n", send_str);
USART_SendString(USART1, "AT+CIPSEND=0,");
USART_SendString(USART1, send_str);
USART_SendString(USART1, "\r\n");
}
}
}
}
}
}
}
```
以上代码中,首先初始化了USART和GPIO,然后通过USART向ESP8266发送了一些指令,使其进入station模式,并开启了服务器。之后进入主循环,通过USART接收到ESP8266发送的数据,根据数据中是否包含IPD来判断是否有客户端连接。如果有,就准备好HTTP响应并发送给客户端,完成一次请求响应。
阅读全文