/*
* 函数名:ESP8266_StaTcpClient_ApTcpServer
* 描述 :WF-ESP8266模块进行STA(TCP Client)+AP(TCP Server)测试
* 输入 :无
* 返回 : 无
* 调用 :被外部调用
*/
void ESP8266_StaTcpClient_ApTcpServer ( void )
{
char cStrInput [100] = { 0 }, * pStrDelimiter [3], * pBuf, * pStr;
u8 uc = 0;
u32 ul = 0;
ESP8266_Choose ( ENABLE );
ESP8266_AT_Test ();
ESP8266_Net_Mode_Choose ( STA_AP );
PC_Usart ( "\r\n请输入要创建的WiFi的名称、加密方式和密钥,加密方式的编号为:\
\r\n0 = OPEN\
\r\n1 =WEP\
\r\n2 = WPA_PSK\
\r\n3 = WPA2_PSK\
\r\n4 = WPA_WPA2_PSK\
\r\n输入格式为:名称字符+英文逗号+加密方式编号+英文逗号+密钥字符+空格,点击发送\r\n" );
scanf ( "%s", cStrInput );
PC_Usart ( "\r\n稍等片刻 ……\r\n" );
pBuf = cStrInput;
uc = 0;
while ( ( pStr = strtok ( pBuf, "," ) ) != NULL )
{
pStrDelimiter [ uc ++ ] = pStr;
pBuf = NULL;
}
ESP8266_BuildAP ( pStrDelimiter [0], pStrDelimiter [2], pStrDelimiter [1] );
ESP8266_Cmd ( "AT+RST", "OK", "ready", 2500 ); //*
ESP8266_Cmd ( "AT+CWLAP", "OK", 0, 5000 );
do
{
PC_Usart ( "\r\n请输入要连接的WiFi名称和密钥,输入格式为:名称字符+英文逗号+密钥字符+空格,点击发送\r\n" );
scanf ( "%s", cStrInput );
PC_Usart ( "\r\n稍等片刻 ……\r\n" );
pBuf = cStrInput;
uc = 0;
while ( ( pStr = strtok ( pBuf, "," ) ) != NULL )
{
pStrDelimiter [ uc ++ ] = pStr;
pBuf = NULL;
}
} while ( ! ESP8266_JoinAP ( pStrDelimiter [0], pStrDelimiter [1] ) );
ESP8266_Enable_MultipleId ( ENABLE );
PC_Usart ( "\r\n请输入服务器要开启的端口号和超时时间(0~28800,单位:秒),输入格式为:端口号字符+英文逗号+超时时间字符+空格,点击发送\r\n" );
scanf ( "%s", cStrInput );
PC_Usart ( "\r\n稍等片刻 ……\r\n" );
pBuf = cStrInput;
uc = 0;
while ( ( pStr = strtok ( pBuf, "," ) ) != NULL )
{
pStrDelimiter [ uc ++ ] = pStr;
pBuf = NULL;
}
ESP8266_StartOrShutServer ( ENABLE, pStrDelimiter [0], pStrDelimiter [1] );
do
{
PC_Usart ( "\r\n请在电脑上将网络调试助手以TCP Server连接网络,并输入电脑的IP和端口号,输入格式为:电脑IP+英文逗号+端口号+空格,点击发送\r\n" );
scanf ( "%s", cStrInput );
PC_Usart ( "\r\n稍等片刻 ……\r\n" );
pBuf = cStrInput;
uc = 0;
while ( ( pStr = strtok ( pBuf, "," ) ) != NULL )
{
pStrDelimiter [ uc ++ ] = pStr;
pBuf = NULL;
}
} while ( ! ( ESP8266_Link_Server ( enumTCP, pStrDelimiter [0], pStrDelimiter [1], Multiple_ID_0 ) &&
ESP8266_Link_Server ( enumTCP, pStrDelimiter [0], pStrDelimiter [1], Multiple_ID_1 ) &&
ESP8266_Link_Server ( enumTCP, pStrDelimiter [0], pStrDelimiter [1], Multiple_ID_2 ) ) );
do
{
PC_Usart ( "\r\n正查询本模块IP,前一个为AP IP,后一个为STA IP……\r\n" );
ESP8266_Cmd ( "AT+CIFSR", "OK", "Link", 500 );
PC_Usart ( "\r\n请用手机连接刚才创建的WiFi,这里只连接一个手机,作为ID3,然后用手机网络调试助手以TCP Client连接刚才开启的服务器(AP IP)……\r\n" );
Delay_ms ( 20000 ) ;
} while ( ! ESP8266_Cmd ( "AT+CIPSTATUS", "+CIPSTATUS:3", 0, 500 ) );
for ( uc = 0; uc < 3; uc ++ )
{
PC_Usart ( "\r\n请输入端口ID%d要发送的字符串,输入格式为:字符串(不含空格)+空格,点击发送\r\n", uc );
scanf ( "%s", cStrInput );
ul = strlen ( cStrInput );
ESP8266_SendString ( DISABLE, cStrInput, ul, ( ENUM_ID_NO_TypeDef ) uc );
}
PC_Usart ( "\r\n请输入要向端口手机(ID3)发送的字符串,输入格式为:字符串(不含空格)+空格,点击发送\r\n" );
scanf ( "%s", cStrInput );
ul = strlen ( cStrInput );
ESP8266_SendString ( DISABLE, cStrInput, ul, Multiple_ID_3 );
PC_Usart ( "\r\n请在电脑网络调试助手或手机网络调试助手发送字符串……\r\n" );
while (1)
{
pStr = ESP8266_ReceiveString ( DISABLE );
PC_Usart ( "%s", pStr );
}
} |