当前位置:网站首页>ESP8266 AP_ UDP_ Server
ESP8266 AP_ UDP_ Server
2022-07-27 01:28:00 【Paradise_ Violet】
Catalog
1. Definition espconn Type structure ( Network connection structure )
Next we need to udp Assignment of communication structure
3. register / Define callback function
4. call UDP initialization API
Callback function for successfully sending network data
udp signal communication 8266 As a server, wait for the client to actively initiate communication with him
1. Definition espconn Type structure ( Network connection structure )
struct espconn ST_NetCon; // notes : Must be defined as a global variable , The kernel will use this variable ( Memory )
struct espconn {
/** type of the espconn (TCP, UDP) */
enum espconn_type type; // Type of network connection tcp/udp
/** current state of the espconn */
enum espconn_state state; // Status of network connection
union {
esp_tcp *tcp;
esp_udp *udp;
} proto;
/** A callback function that is informed about events for this espconn */
espconn_recv_callback recv_callback; // Callback function
espconn_sent_callback sent_callback; // Callback function
uint8 link_cnt; // Number of connections
void *reverse; //void* The pointer
};
typedef struct _esp_udp {
int remote_port; // Remote host port number
int local_port; //8266 Port number
uint8 local_ip[4]; //8266ip Address
uint8 remote_ip[4]; // Remote host ip Address
} esp_udp;2. Next, we need to assign values to the network connection structure , Here we set it to udp There are two ways to communicate
Law 1 : Create a udp Communication structure , And will udp The pointer of the communication structure is assigned to the network connection structure to store upd Members of the structure pointer
esp_udp ST_UDP; // UDP Communication structure ST_NetCon.proto.udp = &ST_UDP;
Law two : ST_NetCon.proto.udp = (esp_udp *)os_zalloc(sizeof(esp_udp)); // Application memory
sizeof(esp_udp) : Calculation udp Size of communication structureos_zalloc(sizeof(esp_udp)): Distribute udp Communication structure size memory
(esp_udp *) : Force the first address of this memory to udp Communication structure pointer type
Then assign the value to the network connection structure to store udp Members of the communication structure
•void * os zalloc (sizet size)
Apply for a piece of specified size ( Parameter determination ) Of memory , And return the first address of this memory .
Be careful : After applying for this memory , Cannot be assigned to other variables . If there are too many operations to apply for memory , Memory will run out .
Use 【os_free( Pointer parameter )】 To free up this memory .
Next we need to udp Assignment of communication structure
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.udp->local_port = espconn_port(); // obtain 8266 Available ports ST_NetCon.proto.udp->local_port = 8266 ; // Set local port // hold 8266 Port set to 8266 //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;
3. register / Define callback function
ESP8266_WIFI_Recv_Cb_JX(void * arg, char * pdata, unsigned short len)
Parameter one void* The pointer , Parameter 2 network transmission data pointer , Parameter 3 is data length
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// 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 remot_info * P_port_info = NULL; // Remote connection information structure pointer // Set according to the data LED Liang Liang / destroy Control the light on and off according to the first character of the received data //------------------------------------------------------------------------------- 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 【UDP Communication is connectionless , When responding to the remote host, you need to obtain the other party's IP/ Port information 】 //------------------------------------------------------------------------------------ if(espconn_get_connection_info(T_arg, &P_port_info, 0)==ESPCONN_OK) // Get remote information { T_arg->proto.udp->remote_port = P_port_info->remote_port; // Get the opposite port number T_arg->proto.udp->remote_ip[0] = P_port_info->remote_ip[0]; // Get each other IP Address T_arg->proto.udp->remote_ip[1] = P_port_info->remote_ip[1]; T_arg->proto.udp->remote_ip[2] = P_port_info->remote_ip[2]; T_arg->proto.udp->remote_ip[3] = P_port_info->remote_ip[3]; //os_memcpy(T_arg->proto.udp->remote_ip,P_port_info->remote_ip,4); // Memory copy } //-------------------------------------------------------------------- OLED_ShowIP(24,6,T_arg->proto.udp->remote_ip); // Displays the remote host IP Address //-------------------------------------------------------------------- espconn_send(T_arg,"ESP8266_WIFI_Recv_OK",os_strlen("ESP8266_WIFI_Recv_OK")); // Send a reply to the other party }espconn_get_connection_info(T_arg, &P_port_info, 0)
Parameter one : Successfully received the pointer passed by the parameter of the network data callback function
Parameter two : Is the pointer of the remote connection information structure pointer
Parameter 3 : Set to 0
T_arg->proto.udp->remote_ip[0] = P_port_info->remote_ip[0]; // Get each other IP Address T_arg->proto.udp->remote_ip[1] = P_port_info->remote_ip[1]; T_arg->proto.udp->remote_ip[2] = P_port_info->remote_ip[2]; T_arg->proto.udp->remote_ip[3] = P_port_info->remote_ip[3];You can use this API To replace the top four lines os_memcpy(T_arg->proto.udp->remote_ip,P_port_info->remote_ip,4); // Memory copy
os_memcpy(void *des, void *src, size_t n)
From the original memory pointer ( Parameter two ) Point , The length will be specified ( Parameter 3 ) data , Copy to target memory pointer ( Parameter one ) Point
void *des Target memory block pointer
void *src Source memory block pointer
size_n Copy length
4. call UDP initialization API
espconn_create(&ST_NetCon); // initialization UDP signal communication
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");
}
边栏推荐
猜你喜欢

The MySQL character set is set to UTF-8, but the console still has the problem of Chinese garbled code

Li Hongyi machine learning (2021 Edition)_ P7-9: training skills

最大公约数的求法

ESP8266 STA_TCP_Client

物联网改善我们生活的 6 种方式

Unity uses navmesh to realize simple rocker function

C language to realize the three chess game

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

Esp8266 --- JSON data exchange format

MySQL关闭连接事务自动提交的问题
随机推荐
Unity Line接入
The setup of KEIL development environment is delivered to the installation package
Come and help you understand the Internet of things in three minutes
#快捷键介绍
梦想的旅程
Jenkins--基础--02--安装
Adding, deleting, checking and modifying dynamic sequence table with C language
集中式版本控制工具代码合并问题
Pit encountered by AssetBundle
顺序表之OJ题
Jenkins--基础--5.3--系统配置--全局安全配置
Jenkins--基础--04--安装中文插件
力扣刷题量300记录帖
Database interim (II)
MySQL关闭连接事务自动提交的问题
Introduction to mathematical modeling - from real objects to mathematical modeling [2]
ESP8266---JSON数据交换格式
7. Formula F1 champion
Unity 使用NavMesh实现简易的摇杆功能
Li Hongyi machine learning (2017 Edition)_ P3-4: Regression