当前位置:网站首页>ESP8266 AP_ TCP_ Server
ESP8266 AP_ TCP_ Server
2022-07-27 01:28:00 【Paradise_ Violet】
Catalog
stay user_init After initialization , take ESP8266_AP Pattern , after 1s Clock repetition timing
In initializing the network connection (TCP signal communication )
Callback function that successfully receives network data
Callback function for successfully sending network data
TCP Callback function for successful disconnection
TCP Callback function when the connection is abnormally disconnected
Preface
8266 stay ap Create... In mode tcp server
stay user_init After initialization , take ESP8266_AP Pattern , after 1s Clock repetition timing
void ICACHE_FLASH_ATTR user_init(void)
{
uart_init(115200,115200); // Initialize the serial port baud rate
os_delay_us(10000); // Wait for the serial port to stabilize
os_printf("\r\n=================================================\r\n");
os_printf("\t Project:\t%s\r\n", ProjectName);
os_printf("\t SDK version:\t%s", system_get_sdk_version());
os_printf("\r\n=================================================\r\n");
// OLED Display initialization
//--------------------------------------------------------
OLED_Init(); // OLED initialization
OLED_ShowString(0,0,"ESP8266 = AP"); // ESP8266 Pattern
OLED_ShowString(0,2,"IP:"); // ESP8266_IP Address
OLED_ShowString(0,4,"Remote = STA"); // Remote host mode
OLED_ShowString(0,6,"IP:"); // Remote host IP Address
//--------------------------------------------------------
LED_Init_JX(); // LED initialization
ESP8266_AP_Init_JX(); // initialization ESP8266_AP Pattern
OS_Timer_1_Init_JX(1000,1); // 1 Second timing ( repeat )
}In the timing callback function , Inquire about AP In mode ESP8266 Of IP Address ,ESP8266 Successful acquisition IP After the address , off timer , Initialize the network connection (TCP signal communication )
// Callback function of software timing
//=============================================================================================================
void ICACHE_FLASH_ATTR OS_Timer_1_cb(void)
{
struct ip_info ST_ESP8266_IP; // IP Information structure
u8 ESP8266_IP[4]; // Save in dotted decimal form IP
wifi_get_ip_info(SOFTAP_IF,&ST_ESP8266_IP); // Inquire about AP In mode ESP8266 Of IP Address
if(ST_ESP8266_IP.ip.addr != 0 ) // ESP8266 Successful acquisition IP Address
{
ESP8266_IP[0] = ST_ESP8266_IP.ip.addr; // dotted decimal IP The first number of <==> addr Lower eight
ESP8266_IP[1] = ST_ESP8266_IP.ip.addr>>8; // dotted decimal IP The second number of <==> addr Lower eight
ESP8266_IP[2] = ST_ESP8266_IP.ip.addr>>16; // dotted decimal IP The third number of <==> addr Second highest eight
ESP8266_IP[3] = ST_ESP8266_IP.ip.addr>>24; // dotted decimal IP The fourth number of <==> addr Top eight digit
// Show ESP8266 Of IP Address
//-----------------------------------------------------------------------------------------------
os_printf("ESP8266_IP = %d.%d.%d.%d\n",ESP8266_IP[0],ESP8266_IP[1],ESP8266_IP[2],ESP8266_IP[3]);
OLED_ShowIP(24,2,ESP8266_IP); // OLED Show ESP8266 Of IP Address
//-----------------------------------------------------------------------------------------------
os_timer_disarm(&OS_Timer_1); // off timer
ESP8266_NetCon_Init_JX(); // Initialize the network connection (TCP signal communication )
}
}In initializing the network connection (TCP signal communication )
Structure assignment , Set the communication protocol to TCP, And for TCP Structure requests memory , Then register the connection success callback function 、 Exception disconnect callback function , After creating TCP_server, Establish listening , Set the timeout disconnection time
// Initialize the network connection (TCP signal communication )
//=============================================================================================================
void ICACHE_FLASH_ATTR ESP8266_NetCon_Init_JX()
{
// Structure assignment
//--------------------------------------------------------------------------
ST_NetCon.type = ESPCONN_TCP ; // Communication protocol :TCP
ST_NetCon.proto.tcp = (esp_tcp *)os_zalloc(sizeof(esp_tcp)); // Application memory
// There is no need to set goals here IP/ port (ESP8266 As Server, There is no need to know in advance Client Of IP/ port )
//--------------------------------------------------------------------------
ST_NetCon.proto.tcp->local_port = 8266 ; // Set local port
//ST_NetCon.proto.tcp->remote_port = 8888; // Set the target port
//ST_NetCon.proto.tcp->remote_ip[0] = 192; // Set the target IP Address
//ST_NetCon.proto.tcp->remote_ip[1] = 168;
//ST_NetCon.proto.tcp->remote_ip[2] = 4;
//ST_NetCon.proto.tcp->remote_ip[3] = 2;
// Successfully registered connection callback function 、 Exception disconnect callback function
//--------------------------------------------------------------------------------------------------
espconn_regist_connectcb(&ST_NetCon, ESP8266_TCP_Connect_Cb_JX); // register TCP Callback function for successful connection establishment
espconn_regist_reconcb(&ST_NetCon, ESP8266_TCP_Break_Cb_JX); // register TCP Callback function with abnormally disconnected connection
// establish TCP_server, Establish listening
//----------------------------------------------------------
espconn_accept(&ST_NetCon); // establish TCP_server, Establish listening
// Please be there. espconn_accept after , Before the connection is established , Call this interface
// If the timeout is set to 0,ESP8266_TCP_server It will never disconnect those who have not communicated with it TCP_client, It is not recommended to use .
//---------------------------------------------------------------------------------------------------
espconn_regist_time(&ST_NetCon, 300, 0); // Set the timeout disconnection time . Company = second , Maximum =7200
}espconn_regist_time(&ST_NetCon, 300, 0); // Set the timeout disconnection time . Company = second , Maximum =7200
Parameter 1 is the pointer of the network connection structure , Parameter 2 is the timeout time, in seconds , Parameter 3 indicates whether it is all tcp Connect , Or just now tcp Connect , Note here that if the timeout is set to 0,ESP8266_TCP_server It will never disconnect those who have not communicated with it TCP_client, It is not recommended to use .
When we successfully created server after , We 8266 Just waiting for tcp_client Propose to him tcp Connect
If tcp If the connection is successfully established , Will enter ESP8266_TCP_Connect_Cb_JX(void *arg) This callback function , Its parameter is that the pointer of the network connection structure is (void*) type , It is necessary to replace the mandatory station with the pointer of the network connection structure (struct espconn *) type
// TCP Callback function for successful connection establishment
//====================================================================================================================
void ICACHE_FLASH_ATTR ESP8266_TCP_Connect_Cb_JX(void *arg)
{
espconn_regist_sentcb((struct espconn *)arg, ESP8266_WIFI_Send_Cb_JX); // Register the callback function for sending network data successfully
espconn_regist_recvcb((struct espconn *)arg, ESP8266_WIFI_Recv_Cb_JX); // Register the callback function for successful network data reception
espconn_regist_disconcb((struct espconn *)arg,ESP8266_TCP_Disconnect_Cb_JX); // Registration successfully disconnected TCP Connected callback function
os_printf("\n--------------- ESP8266_TCP_Connect_OK ---------------\n");
}Callback function that successfully receives network data
Parameters 1: Network transmission structure espconn The pointer 、 Parameters 2: Network transmission data pointer 、 Parameters 3: Data length
void ICACHE_FLASH_ATTR ESP8266_WIFI_Recv_Cb_JX(void * arg, char * pdata, unsigned short len)
{
struct espconn * T_arg = arg; // Cache network connection structure pointer
// Set according to the data LED Liang Liang / destroy
//-------------------------------------------------------------------------------
if(pdata[0] == 'k' || pdata[0] == 'K') LED_ON; // The initial is 'k'/'K', Light on
else if(pdata[0] == 'g' || pdata[0] == 'G') LED_OFF; // The initial is 'g'/'G', The light goes out
os_printf("\nESP8266_Receive_Data = %s\n",pdata); // The serial port prints the received data
/*
// Get remote information
//------------------------------------------------------------------------------------
remot_info * P_port_info = NULL; // Define the remote connection information pointer
if(espconn_get_connection_info(T_arg, &P_port_info, 0)==ESPCONN_OK) // Get remote information
{
T_arg->proto.tcp->remote_port = P_port_info->remote_port; // Get the opposite port number
T_arg->proto.tcp->remote_ip[0] = P_port_info->remote_ip[0]; // Get each other's IP Address
T_arg->proto.tcp->remote_ip[1] = P_port_info->remote_ip[1];
T_arg->proto.tcp->remote_ip[2] = P_port_info->remote_ip[2];
T_arg->proto.tcp->remote_ip[3] = P_port_info->remote_ip[3];
//os_memcpy(T_arg->proto.tcp->remote_ip,P_port_info->remote_ip,4); // Memory copy
}
*/
//--------------------------------------------------------------------
OLED_ShowIP(24,6,T_arg->proto.tcp->remote_ip); // Displays the remote host IP Address
//--------------------------------------------------------------------
//【TCP Communication is connection oriented , When responding to the remote host, you can directly use T_arg The structure pointer points to IP Information 】
//-----------------------------------------------------------------------------------------------
espconn_send(T_arg,"ESP8266_WIFI_Recv_OK",os_strlen("ESP8266_WIFI_Recv_OK")); // Send a reply to the other party
}TCP Communication is connection oriented , When responding to the remote host, you can directly use T_arg The structure pointer points to IP Information
espconn_send(T_arg,"ESP8266_WIFI_Recv_OK",os_strlen("ESP8266_WIFI_Recv_OK")); Send a reply to the other party
Callback function for successfully sending network data
void ICACHE_FLASH_ATTR ESP8266_WIFI_Send_Cb_JX(void *arg)
{
os_printf("\nESP8266_WIFI_Send_OK\n");
}TCP Callback function for successful disconnection
void ICACHE_FLASH_ATTR ESP8266_TCP_Disconnect_Cb_JX(void *arg)
{
os_printf("\nESP8266_TCP_Disconnect_OK\n");
}TCP Callback function when the connection is abnormally disconnected
void ICACHE_FLASH_ATTR ESP8266_TCP_Break_Cb_JX(void *arg,sint8 err)
{
os_printf("\nESP8266_TCP_Break\n");
}

send out K,led Lit and 8266 Successfully received characters K, And successfully to tcp_client The reply
Explain our tcp The communication settings have been set successfully , also 8266 Also successfully created tcp_server, The network debugging assistant acts as tcp_client Has successfully and 8266 Established tcp Connect , And the two sides communicated .
边栏推荐
- ESP8266 STA_TCP_Client
- Scoring system based on 485 bus
- Some simple extension methods commonly used by unity
- ESP8266 STA_Server
- #高级语言 各种开发软件介绍
- [CTF attack and defense world] questions about backup in the web area
- Unity Twitter登录接入
- ESP8266-----SNTP获取网络时间
- Basic DOS commands
- The setup of KEIL development environment is delivered to the installation package
猜你喜欢

Li Hongyi machine learning (2021 Edition)_ P5-6: small gradient processing

4.root用户登录
![[unity] unity interface scene view [1]](/img/5a/c34ff09ef1ddba4b65c7873775c251.png)
[unity] unity interface scene view [1]

Create MDK project

Li Hongyi machine learning (2017 Edition)_ P5: error

When a transaction encounters a distributed lock

c语言实现扫雷游戏:

Iptables detailed explanation and practical cases

c语言实现动态顺序表的增删查改

Esp8266 connects to the IOT of Lexin cloud platform_ Demo
随机推荐
做题笔记1
c语言实现三子棋游戏
Jenkins--基础--02--安装
if 与 else if 的区别
4. European Champions League
ESP8266 AP_TCP_Server
Promoting practice with competition - Reflection on the 303th weekly match
Surrounded area
物联网平台介绍
软件测试面试题之软件基础
最大公约数的求法
Shortcut key introduction
Finding the greatest common divisor
Data storage summary of C language
C语言之数据存储汇总
Unity uses navmesh to realize simple rocker function
Longest common substring
快来:鼓励高校毕业生返乡创业就业,助力乡村振兴
markdown
Jenkins -- Basic -- 5.2 -- system configuration -- system configuration