ESP8266学习之路 十 (UDP服务器通信)

lua开发8266API手册:https://nodemcu.readthedocs.io/en/master/​​​​​​​

一.  UDP服务器

此模式只比客户端模式下多了个 监听函数 获取客户端的信息 其余和客户端模式一样
只有客户端给服务器发送数据时才会建立通信通道连接

1.  单连接

udpserver.lua文件:

ConnectIP = ""			 --客户端连接时获取客户端的ip
ConnectPort = 0          --客户端连接时获取客户端的端口
ListenPort = 8080		 --服务器监听端口 ,也是服务器接收数据的端口

wifi.setmode(wifi.STATIONAP)

apcfg={}
apcfg.ssid="ESP8266WIFI"
apcfg.pwd="12345678"
wifi.ap.config(apcfg) 

stacfg={}
stacfg.ssid="TP-Link"
stacfg.pwd="12345678"
wifi.sta.config(stacfg)
wifi.sta.autoconnect(1)
 
UdpSocket = net.createUDPSocket()
UdpSocket:listen(ListenPort) --服务器监听端口,接收数据端口

UdpSocket:on("receive", function(socket, data, port, ip)
         print("\r\nport:"..port.."---ip:"..ip.."\r\n") 
         ConnectPort = port		--获取客户端的端口
         ConnectIP = ip			--获取客户端的ip
        uart.write(0,data) 
end)

uart.on("data",0,function(data) 
    if UdpSocket~=nil then		--只有有客户端连接时才会发送数据
        if ConnectPort~=0 then --判断是否有客户端连接的端口
            UdpSocket:send(ConnectPort, ConnectIP, data)
        end
    end
end,0)

 

 

当UDP服务器给客户端发消息时也是只发送给一个客户端

 

2.多连接

wifi.setmode(wifi.STATIONAP)

cfg={}
cfg.ssid="ESP8266WIFI"
cfg.pwd="12345678"
wifi.ap.config(cfg)

apcfg={}
apcfg.ssid="TP-Link"
apcfg.pwd="12345678"
wifi.sta.config(apcfg)
wifi.sta.autoconnect(1)

ConnectIP = "192.168.4.2"   --默认通信ip
ConnectPort = 8080				--默认通信端口

UdpSocket = net.createUDPSocket()   
UdpSocket:listen(ConnectPort)   --监听端口

UdpSocketTable={}   --存放UDP连接的socket
UdpIPTable={}		--存放UDP连接的ip
UdpPortTable={}		--存放UDP连接的端口
UdpConnectCnt = 0	--连接的个数
UdpCanConnect = 0	--新连接和默认连接

UdpSocket:on("receive", function(socket, data, port, ip)
    UdpCanConnect = 1
    for i=0,2 do
        if  UdpIPTable[i] == ip and UdpPortTable[i] == port  then
            UdpCanConnect = 0
        end
    end

    if  ip == ConnectIP and port == ConnectPort  then
        UdpCanConnect = 0
    end

    if  UdpCanConnect == 1 then --存放新连接的客户端
        UdpSocketTable[UdpConnectCnt] = socket
        UdpIPTable[UdpConnectCnt] = ip 
        UdpPortTable[UdpConnectCnt] = port
        print("\r\n"..UdpConnectCnt.."-Connect")
        UdpConnectCnt = UdpConnectCnt + 1
    end
    
    if  UdpConnectCnt == 3 then
        UdpConnectCnt = 0
    end
    uart.write(0,data)
end)

uart.on("data",0,function(data) 
    if  UdpSocket ~= nil then --默认连接的客户端
        UdpSocket:send(ConnectPort,ConnectIP,data)
    end
    
    for i=0,2 do	--新连接的客户端
        if  UdpSocketTable[i] ~= nil then
            UdpSocketTable[i]:send(UdpPortTable[i],UdpIPTable[i],data) 
        end
    end
        
end, 0)

 

UDP服务器发送消息时 连接的客户端都可以接收到数据

 

 

  • 1
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
ESP8266作为UDP连接服务器时,需要进行以下设置: 1. 将ESP8266设置为AP模式,即作为一个独立的无线网络。可以通过设置SSID(WiFi名称)、密码、加密方式等参数来配置AP模式。 2. 使用espconn库中的函数进行UDP通信的初始化,例如espconn_create()函数。 具体的代码示例如下: ```c #include "ip_addr.h" #include "espconn.h" #define ESP8266_AP_SSID "LOVEYOU" #define ESP8266_AP_PASS "Be Happy" // 初始化AP模式,设置WiFi名和密码 void AP_mode_init() { struct softap_config ap_config; wifi_set_opmode(0x02); // 设置为AP模式,并保存到Flash os_memset(&ap_config, 0, sizeof(struct softap_config)); // AP参数结构体 = 0 os_strcpy(ap_config.ssid, ESP8266_AP_SSID); // 设置SSID(将字符串复制到ssid数组) os_strcpy(ap_config.password, ESP8266_AP_PASS); // 设置密码(将字符串复制到password数组) ap_config.ssid_len = os_strlen(ESP8266_AP_SSID); // 设置ssid长度(和SSID的长度一致) ap_config.channel = 1; // 通道号1~13 ap_config.authmode = AUTH_WPA2_PSK; // 设置加密模式 ap_config.ssid_hidden = 0; // 不隐藏SSID ap_config.max_connection = 4; // 最大连接数 ap_config.beacon_interval = 100; // 信标间隔时槽100~60000 ms wifi_softap_set_config(&ap_config); // 设置soft-AP,并保存到Flash } // 初始化UDP通信 void init_UDP_communication() { // 在这里进行UDP通信的初始化操作 // 例如使用espconn_create()函数创建一个UDP连接 } // 主函数 void main() { AP_mode_init(); // 初始化AP模式 init_UDP_communication(); // 初始化UDP通信 } ``` 以上代码是一个简的示例,通过设置ESP8266为AP模式并初始化UDP通信,可以将ESP8266作为UDP连接服务器。 #### 引用[.reference_title] - *1* *2* *3* [ESP8266_AP模式、UDP服务器or客户端](https://blog.csdn.net/E2242/article/details/124904269)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值