当前位置:网站首页>ESP8266 AP_ UDP_ Client
ESP8266 AP_ UDP_ Client
2022-07-27 01:28:00 【Paradise_ Violet】
Catalog
Preface
We will 8266 stay ap Mode plus creation udp signal communication , also 8266 Logically, as a client , The network debugging assistant acts as a server
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(30000,0); // 30 Second timing ( once )
}OS_Timer_1_Init_JX(30000,0); // 30 Second timing ( once )
timing 30s Is to create for the network debugging assistant udp signal communication , And reserve time for setting up the server
In the timing callback function , Inquire about AP In mode ESP8266 Of IP Address , Successful acquisition IP Address , Then turn off the timer , Initialize the network connection (UDP signal communication )
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 (UDP signal communication )
}
}Here you need to set the target IP/ port (ESP8266 As Client, You need to know in advance Server Of IP/ port )
// Initialize the network connection (UDP signal communication )
//=============================================================================================================
void ICACHE_FLASH_ATTR ESP8266_NetCon_Init_JX()
{
// Structure assignment
//--------------------------------------------------------------------------
ST_NetCon.type = ESPCONN_UDP; // Set the communication protocol to UDP
ST_NetCon.proto.udp = (esp_udp *)os_zalloc(sizeof(esp_udp)); // Open up memory
// Here you need to set the target IP/ port (ESP8266 As Client, You need to know in advance Server Of IP/ port )
//--------------------------------------------------------------------------
ST_NetCon.proto.udp->local_port = 8266 ; // Set local port
ST_NetCon.proto.udp->remote_port = 8888; // Set the target port
ST_NetCon.proto.udp->remote_ip[0] = 192; // Set the target IP Address
ST_NetCon.proto.udp->remote_ip[1] = 168;
ST_NetCon.proto.udp->remote_ip[2] = 4;
ST_NetCon.proto.udp->remote_ip[3] = 2;
//u8 remote_ip[4] = {192,168,4,2}; // The goal is IP Address
//os_memcpy(ST_NetCon.proto.udp->remote_ip,remote_ip,4); // Copy memory
espconn_regist_sentcb(&ST_NetCon,ESP8266_WIFI_Send_Cb_JX); // Register the callback function for sending network data successfully
espconn_regist_recvcb(&ST_NetCon,ESP8266_WIFI_Recv_Cb_JX); // Register the callback function for successful network data reception
espconn_create(&ST_NetCon); // initialization UDP signal communication
espconn_send(&ST_NetCon,"Hello,I am ESP8266",os_strlen("Hello,I am ESP8266")); // Active direction Server Initiate communication
} That is to say, when we 8266 As a client , Need to pre assign the server ip Address and port number , To actively send communication to the server
边栏推荐
猜你喜欢

Flinksql window triggered in advance

ESP8266 STA_Server

【Oracle】获取最近工作日及前N个工作日

Finding the greatest common divisor
![【unity】Unity界面scene视图[1]](/img/5a/c34ff09ef1ddba4b65c7873775c251.png)
【unity】Unity界面scene视图[1]

Jenkins--基础--5.1--系统配置--插件管理

As 5g becomes more and more popular, what positive effects will our lives be affected

Jenkins--基础--5.2--系统配置--系统配置

LAMP+DISCUZ论坛

Unity ugui text text box adaptation
随机推荐
6. The world cup is coming
Database interim (II)
Unity UGUI Text文本框自适应
markdown
Solve the problem that CUDA cannot accelerate GPU in pytoch
报错信息 WARNING: IPv4 forwarding is disabled. Networking will not work.
MakeFile
07 - setup and attack of log server
什么是数字经济,它是如何改变商业模式的?
Come and help you understand the mobile Internet in a minute
ESP8266 STA_UDP_Client
Unity a user-friendly UI grayscale shader
Unity screenshot widget
Unity Line接入
Spark accumulator
c语言实现动态顺序表的增删查改
Unity 一个好用的UI灰度Shader
基本的DOS命令
力扣刷题量300记录帖
RS485 signal measurement