10、ESP8266 AP_UDP_Server

API

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

网络连接结构体

espconn

struct espconn {
    /** type of the espconn (TCP, UDP) */
    enum espconn_type type; // 网络连接类型(TCP、UDP)
    /** current state of the espconn */
    enum espconn_state state; //网络连接的状态
    union {                   //使用TCP、UDP的结构体指针
        esp_tcp *tcp;
        esp_udp *udp;
    } proto;
    /** A callback function that is informed about events for this espconn */
    espconn_recv_callback recv_callback;//接收回调函数
    espconn_sent_callback sent_callback; //发送回调函数
    uint8 link_cnt;
    void *reverse;
};


typedef struct _esp_udp {
    int remote_port; //远端端口号
    int local_port; //本机(8266)端口号
    uint8 local_ip[4]; //本机(8266)IP
	uint8 remote_ip[4];//远端IP
} esp_udp;

初始化网络连接(UDP通信)

	// ①:定义espconn型结构体(网络连接结构体
	struct espconn ST_NetCon;	// 注:必须定义为全局变量,内核将会使用此变量(内存)
	// ②:结构体赋值
	ST_NetCon.type = ESPCONN_UDP;		// 通信协议:UDP
	
	// ST_NetCon.proto.udp只是一个指针,不是真正的esp_udp型结构体变量
	ST_NetCon.proto.udp = (esp_udp *)os_zalloc(sizeof(esp_udp));	// 申请内存(参考下图)
	/* 这样也可以
	//esp_udp ST_UDP;	// UDP通信结构体
	//ST_NetCon.proto.udp = &ST_UDP;
	*/
	// 此处无需设置目标IP/端口(ESP8266作为Server,不需要预先知道Client的IP/端口)
	ST_NetCon.proto.udp->local_port  = 8266 ;		// 设置本地端口
	//ST_NetCon.proto.udp->local_port  = espconn_port();	// 也可使用espconn_port获取8266可用端口
	// ③:注册/定义回调函数
	espconn_regist_sentcb(&ST_NetCon,ESP8266_WIFI_Send_Cb_JX);	// 注册网络数据发送成功的回调函数
	espconn_regist_recvcb(&ST_NetCon,ESP8266_WIFI_Recv_Cb_JX);	// 注册网络数据接收成功的回调函数
	// ④:调用UDP初始化API
	espconn_create(&ST_NetCon);	// 初始化UDP通信

在这里插入图片描述

发送数据回调函数

在这里插入图片描述

接收数据回调函数在这里插入图片描述

发布了274 篇原创文章 · 获赞 97 · 访问量 8万+

猜你喜欢

转载自blog.csdn.net/imxlw00/article/details/104855576