一. 硬件清单
1.STM32F103C8T6/STC89C52RC 2.ESP-01S模块 3.SWD或者JLINK 4.CH340串口模块 5.杜邦线若干
二. 资料清单
三. ESP-01S简介
四. 接线
因为51单片机只有一个串口,为了方便移植和测试,32也只用一个串口,32需要用两个串口的可自行更改。
五. 代码说明
以STM32F103C8T6为例
1. 串口通信配置
void USART_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
DEBUG_USART_GPIO_APBxClkCmd(DEBUG_USART_GPIO_CLK, ENABLE);
DEBUG_USART_APBxClkCmd(DEBUG_USART_CLK, ENABLE);
GPIO_InitStructure.GPIO_Pin = DEBUG_USART_TX_GPIO_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(DEBUG_USART_TX_GPIO_PORT, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = DEBUG_USART_RX_GPIO_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(DEBUG_USART_RX_GPIO_PORT, &GPIO_InitStructure);
USART_InitStructure.USART_BaudRate = DEBUG_USART_BAUDRATE;
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(DEBUG_USARTx, &USART_InitStructure);
NVIC_Configuration();
USART_ITConfig(DEBUG_USARTx, USART_IT_RXNE, ENABLE);
USART_Cmd(DEBUG_USARTx, ENABLE);
}
2. 初始化指令配置
char RST[]="AT+RST\r\n";
char UART[]="AT+UART=115200,8,1,0,0\r\n";
char CWMODE2[]="AT+CWMODE=3\r\n";
char CIPMUX[]="AT+CIPMUX=1\r\n";
char CIFSR[]="AT+CIFSR\r\n";
char CIPSERVER[]="AT+CIPSERVER=1,8080\r\n";
char CWSAP[]="AT+CWSAP=\"TEST\",\"12345678\",1,3\r\n";
void ManyConnect_AP(void)
{
Usart_SendString(DEBUG_USARTx,RST);
DelayMs(500);
while(!Usart_AT_flage);
if(strstr(Usart_Receive, "OK") )
{
Usart_SendString(DEBUG_USARTx,"RST_OK\r\n");
DelayMs(10);
Clear_Usart_Receive();
}
Clear_Usart_Receive();
Usart_SendString(DEBUG_USARTx,UART);
while(!Usart_AT_flage);
if(strstr(Usart_Receive, "OK") )
{
Usart_SendString(DEBUG_USARTx,"UART_OK\r\n");
DelayMs(10);
Clear_Usart_Receive();
}
Usart_SendString(DEBUG_USARTx,CWMODE2);
while(!Usart_AT_flage);
if(strstr(Usart_Receive, "OK") )
{
Usart_SendString(DEBUG_USARTx,"CWMODE2_OK\r\n");
DelayMs(10);
Clear_Usart_Receive();
}
Usart_SendString(DEBUG_USARTx,CWSAP);
while(!Usart_AT_flage);
if(strstr(Usart_Receive, "OK") )
{
Usart_SendString(DEBUG_USARTx,"CWSAP_OK\r\n");
DelayMs(10);
Clear_Usart_Receive();
}
Usart_SendString(DEBUG_USARTx,CIPMUX);
while(!Usart_AT_flage);
if(strstr(Usart_Receive, "OK") )
{
Usart_SendString(DEBUG_USARTx,"CIPMUX_OK\r\n");
DelayMs(10);
Clear_Usart_Receive();
}
Usart_SendString(DEBUG_USARTx,CIPSERVER);
while(!Usart_AT_flage);
if(strstr(Usart_Receive, "OK") )
{
Usart_SendString(DEBUG_USARTx,"CIPSERVER_OK\r\n");
DelayMs(10);
Clear_Usart_Receive();
Usart_Connect_Buf_Flag=1;
}
}
3. 接受数据判断与验证
int main(void)
{
unsigned char *PBuf;
DelayInit();
USART_Config();
ManyConnect_AP();
Clear_Usart_Receive();
while(1)
{
PBuf=strchr(Usart_Receive,'+');
if((PBuf[0]=='+')&&(PBuf[1]=='I')&&(PBuf[2]=='P')&&(PBuf[3]=='D')
&&(PBuf[6]==',')&&((PBuf[9]>='0')&&(PBuf[9]<='9')))
{
PBuf[10]='\r';
PBuf[11]='\n';
Usart_SendString(DEBUG_USARTx,PBuf);
DelayMs(10);
Clear_Usart_Receive();
}
}
}
4. 手机APP操作
5. 最终上位机显示
六. 资料获取
加群私聊群主可免费获得,群号:1041406448。相关模块例程也将持续更新
|