初中生
最后登录1970-1-1
在线时间 小时
注册时间2023-5-26
|
# 前言
本文将使用板载的ESP8266来进行网络连接控制。
# 1 硬件连接
如下图,有三根控制线用来控制ESP8266,其中:
- P601和P602是串口线,用来控制单片机与ESP8266的通信
- P115是普通IO口,控制ESP8266复位,如果需要ESP8266正常工作,需要保持高电平
# 2 RASC配置
RASC配置需要配置ESP8266使用到的引脚以及串口,配置串口如下,使用SCI串口9。
配置ESP8266连接的复位端口P115,配置默认输出高电平。
切换到Stack界面,配置UART9相关参数。
UART9详细配置如下:
# 3 代码开发
ESP32的驱动代码如下:
调用Esp8266_Init用来初始化串口9
调用Esp8266_Test来进行ESP8266连接测试。
需要修改下面的宏值,用来连接wifi与服务器。
- /*
- @hehung
- email: 1398660197@qq.com
- wechat: hehung95
- reproduced and please indicate the source @hehung
- */
- #include "app_common.h"
- #include "app_wifi_esp8266.h"
-
- #define ESP8266_DEBUG
- #ifdef ESP8266_DEBUG
- #define LOG(fmt, ... ) printf ( fmt, ##__VA_ARGS__ )
- #else
- #define LOG(fmt, ... )
- #endif
- #define HOTSPOT_ID "hehungHome" // Wifi hotspot name
- #define HOTSPOT_PASSWORD "1234543210" // Wifi hostpot password
- #define SeverIP "192.168.6.100" // Server IP
- #define SeverPort "8080" // Server Port
- #define Esp8266_ModuleEnable() \
- R_IOPORT_PinWrite(&g_ioport_ctrl, BSP_IO_PORT_01_PIN_15, BSP_IO_LEVEL_HIGH) // Enable ESP8266
- #define Esp8266_ModuleDisable() \
- R_IOPORT_PinWrite(&g_ioport_ctrl, BSP_IO_PORT_01_PIN_15, BSP_IO_LEVEL_LOW) // Disable ESP8266
- /* Fault Status, On Red LED */
- #define Esp8266_ErrorAlarm() R_PORT4->PODR ^= 1<<(BSP_IO_PORT_04_PIN_00 & 0xFF); \
- R_BSP_SoftwareDelay(500, BSP_DELAY_UNITS_MILLISECONDS);
- /* Clear Uart9 buffer */
- #define Clear_Buff() memset(At_Rx_Buff ,0 ,sizeof(At_Rx_Buff)); \
- Uart9_Num = 0;
- static _Bool uart9_receive_flag = false;
- static _Bool uart9_show_flag = false; //控制UART9收发数据显示标志
- /*用来接收UART9数据的缓冲区*/
- static char At_Rx_Buff[256];
- static uint8_t Uart9_Num = 0;
- static void Esp8266_AT_Send(char * cmd);
- /*自动配置ESP8266函数*/
- void Esp8266_Test(void)
- {
-
- LOG("\r\nInitialize ESP8266...\r\n");
- Esp8266_Init();
-
- LOG("\r\nSetting Station Mode...\r\n");
- Esp8266_SetStationMode();
-
- LOG("\r\nWIFI connecting...\r\n");
- Esp8266_StationConnect(HOTSPOT_ID ,HOTSPOT_PASSWORD ,20);
- Esp8266_SetLinkMode(0);
-
- LOG("\r\nServer Connnecting...\r\n");
- Esp8266_ServerConnect(SeverIP ,SeverPort ,20);
-
- LOG("\r\nTransparent Mode Configuring...\r\n");
- Esp8266_SetTransparentMode();
- Esp8266_SetSendData();
-
- }
- /*ESP8266 (SPI9 UART) 初始化函数*/
- void Esp8266_Init(void)
- {
- fsp_err_t err = FSP_SUCCESS;
-
- err = R_SCI_UART_Open(&g_uart9_ctrl, &g_uart9_cfg);
- assert(FSP_SUCCESS == err);
- }
- /*向ESP8266发送AT指令函数*/
- static void Esp8266_AT_Send(char * cmd)
- {
- /*向ESP8266(UART9)发送指令*/
- R_SCI_UART_Write(&g_uart9_ctrl, (uint8_t *)cmd, strlen(cmd));
-
- /*AT指令发送完成标志*/
- uart9_receive_flag = false;
- }
- /*设置ESP8266为 STA 模式*/
- void Esp8266_SetStationMode(void)
- {
- Esp8266_AT_Send("AT+CWMODE=1\r\n");
-
- /*等待设置完成*/
- while (!uart9_receive_flag)
- {
- if (strstr(At_Rx_Buff , "OK\r\n"))
- {
- LOG("\r\nESP8266 has been in Ststion Mode\r\n");
- Clear_Buff(); //清除缓冲区数据
- }
- }
- }
- /*设置ESP8266为 AP 模式*/
- void ESP8266_SetApMode(void)
- {
- Esp8266_AT_Send ( "AT+CWMODE=2\r\n" );
- /*等待设置完成*/
- while ( !uart9_receive_flag )
- {
- if (strstr( At_Rx_Buff , "OK\r\n" ))
- {
- LOG("\r\nESP8266 has been in AP Mode\r\n");
- Clear_Buff(); //清除缓冲区数据
- }
- }
- }
- /*设置ESP8266为 STA + AP 模式*/
- void Esp8266_SetStationAndApMode(void)
- {
- Esp8266_AT_Send ( "AT+CWMODE=3\r\n" );
- /*等待设置完成*/
- while ( !uart9_receive_flag )
- {
- if (strstr( At_Rx_Buff , "OK\r\n" ))
- {
- LOG("\r\nESP8266 has been in AP+Station Mode\r\n");
- Clear_Buff(); //清除缓冲区数据
- }
- }
- }
- /*ESP8266连接WiFi函数,timeout:期望最大连接时间*/
- void Esp8266_StationConnect(char * id , char * password , uint8_t timeout)
- {
- char JoinAP_AT[256];
-
- uint8_t i;
-
- sprintf( JoinAP_AT , "AT+CWJAP="%s","%s"\r\n" , id , password);
-
- Esp8266_AT_Send( JoinAP_AT );
- /*判断连接是否设置成功,失败则打印错误信息*/
- while ( !uart9_receive_flag )
- {
- for(i = 0; i <= timeout; i++)
- {
- if ( strstr( At_Rx_Buff , "OK\r\n" ) )
- {
- LOG("\r\nWifi Conncted\r\n");
- Clear_Buff(); //清除缓冲区数据
- break;
- }
- if ( strstr( At_Rx_Buff , "ERROR\r\n" ) ) //根据ESP8266的固件版本不同有不同的响应,一般为“FAIL”或“ERROR”
- {
- if( strstr( At_Rx_Buff , "+CWJAP:1\r\n" ))
- LOG("\r\nWifi connect timeout\r\n");
-
- if( strstr( At_Rx_Buff , "+CWJAP:2\r\n" ))
- LOG("\r\nWifi password error\r\n");
-
- if( strstr( At_Rx_Buff , "+CWJAP:3\r\n" ))
- LOG("\r\nNo target hotspot found\r\n");
-
- if( strstr( At_Rx_Buff , "+CWJAP:4\r\n" ))
- LOG("\r\nWifi Connected Fail\r\n");
-
- while(1)
- {
- Esp8266_ErrorAlarm();
- } //LED灯警告错误,红灯闪烁
- }
- if ( i == timeout )
- {
- LOG("\r\nWifi Connect Timeout\r\n");
- while(1)
- {
- Esp8266_ErrorAlarm();
- } //LED灯警告错误,红灯闪烁
- }
- R_BSP_SoftwareDelay(1, BSP_DELAY_UNITS_SECONDS);
- }
- }
- }
- /*设置连接模式为单连接或者多连接*/
- void Esp8266_SetLinkMode(uint8_t mode)
- {
- switch (mode)
- {
- case 0 :
- Esp8266_AT_Send("AT+CIPMUX=0\r\n"); //设置为单连接模式,透传只有在单连接模式下才能进行
- break;
- case 1 :
- Esp8266_AT_Send("AT+CIPMUX=1\r\n"); //设置为多连接模式,服务器只有在多连接模式下才能打开
- break;
- }
-
- /*等待设置完成*/
- while ( !uart9_receive_flag )
- {
- if (strstr( At_Rx_Buff , "OK\r\n" ))
- {
- if( mode )
- LOG("\r\nESP8266 has been in Multi-connection Mode\r\n");
- else
- LOG("\r\nESP8266 has been in Single-connection Mode\r\n");
- Clear_Buff(); //清除缓冲区数据
- }
- }
- }
- /*ESP8266连接服务器函数,timeout:期望最大连接时间*/
- void Esp8266_ServerConnect( char * server_id , char * port , uint8_t timeout )
- {
- char JoinServer_AT[256];
-
- uint8_t i;
-
- sprintf( JoinServer_AT , "AT+CIPSTART="TCP","%s",%s\r\n" , server_id , port );
-
- Esp8266_AT_Send( JoinServer_AT );
-
- /*判断连接是否设置成功,失败则打印错误信息*/
- while ( !uart9_receive_flag )
- {
- for(i = 0; i <= timeout; i++)
- {
- if (strstr( At_Rx_Buff , "OK\r\n" ))
- {
- LOG("\r\nServer Connect Successfully\r\n");
- Clear_Buff(); //清除缓冲区数据
- break;
- }
- if (strstr( At_Rx_Buff , "ERROR\r\n" ))
- {
- LOG("\r\nServer Connect Failed\r\n");
- while(1)
- {
- Esp8266_ErrorAlarm();
- } //LED灯警告错误,红灯闪烁
- }
- if ( i == timeout )
- {
- LOG("\r\nServer Connect Timeout\r\n");
- while(1)
- {
- Esp8266_ErrorAlarm();
- } //LED灯警告错误,红灯循环闪烁
- }
- R_BSP_SoftwareDelay(1, BSP_DELAY_UNITS_SECONDS);
- }
- }
- }
- /*设置ESP8266为透传模式*/
- void Esp8266_SetTransparentMode(void)
- {
- Esp8266_AT_Send ( "AT+CIPMODE=1\r\n" );
- /*等待设置完成*/
- while ( !uart9_receive_flag )
- {
- if (strstr( At_Rx_Buff , "OK\r\n" ))
- {
- LOG("\r\nESP8266 has in Transparent Mode\r\n");
- Clear_Buff(); //清除缓冲区数据
- }
- }
- }
- /*设置ESP8266为发送数据模式*/
- void Esp8266_SetSendData(void)
- {
- Esp8266_AT_Send ( "AT+CIPSEND\r\n" );
-
- /*等待设置完成*/
- while ( !uart9_receive_flag )
- {
- if (strstr( At_Rx_Buff , "OK\r\n" ))
- {
- LOG("\r\nESP8266 has been in Transparent Mode\r\n\r\n>");
- uart9_show_flag = true;
- Clear_Buff(); //清除缓冲区数据
- }
- }
- }
- /*Wifi串口回调函数*/
- void uart9_notification(uart_callback_args_t * p_args)
- {
- switch(p_args->event)
- {
- case UART_EVENT_RX_CHAR:
-
- At_Rx_Buff[Uart9_Num++] = (uint8_t ) p_args->data; //将UART9收到的数据放到Buff缓冲区中
-
- /*进入透传模式后打开串口调试助手收发数据显示*/
- //if( uart9_show_flag )
- R_SCI_UART_Write(&g_uart4_ctrl, (uint8_t *)&(p_args->data), 1);
-
- break;
-
- case UART_EVENT_TX_COMPLETE:
- {
- uart9_receive_flag = true; //ESP8266回应完成标志
-
- break;
- }
- default:
- break;
- }
- }
复制代码
|
-
|