【ESP8266之LUA开发】七、建立UDP通信,加入空闲中断

关于UDP的一些基础知识

  • UDP通信支持一路默认,3路动态连接,共可连接4个

  • UDP是一种面向无连接的即时通信方式,即时通信意味着通信完毕即是销毁。

  • 模块发数据,要先设置一个默认通信的IPPort

  • ServerClient之分

  • 可实现同时多个UDP通信

8266UDP Server

init.lua

gpio.mode(4,gpio.OUTPUT)
gpio.mode(2,gpio.OUTPUT)
gpio.write(4,1)

if  adc.force_init_mode(adc.INIT_ADC) then
    node.restart()
    return
end

tmr.alarm(0, 1000, 1, function()
    gpio.write(4,1-gpio.read(4))
end)

tmr.alarm(1, 3000, 0, function()
    dofile("wifi.lua")
end)

wifi.lua

wifi.setmode(wifi.STATIONAP)

cfg={}
cfg.ssid="Hellow8266"
cfg.pwd="11223344"
wifi.ap.config(cfg)

apcfg={}
apcfg.ssid="qqqqq"
apcfg.pwd="11223344"
wifi.sta.config(apcfg)
wifi.sta.autoconnect(1)

ConnectIP = "192.168.1.103"
ConnectPort = 8080

UdpSocket = net.createUDPSocket()   
UdpSocket:listen(ConnectPort)

UdpSocketTable={}
UdpIPTable={}
UdpPortTable={}
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)


printip = 0
wifi.eventmon.register(wifi.eventmon.STA_DISCONNECTED, function(T)
    printip = 0
end)


wifi.eventmon.register(wifi.eventmon.STA_GOT_IP, function(T)
   if printip == 0 then
      print("+IP"..T.IP)
   end
   printip = 1
end)

UdpCanConnect – 等于1表示有新的连接加入!
ConnectIP 则是本机分配的IP地址,查看方法可移步<这里>
socket Tool中的 对方IP 指的是8266分配的IP地址!
print("\r\n"..UdpConnectCnt.."-Connect") - 没有回车换行会挤到一起

程序解读

UdpSocket:on("receive", function(socket, data, port, ip)中做了判断8266接收哪个UDPClient发来的信息,只要一个新的UDPClient(不和默认的,不和表中的一样)和模块建立连接,那就存储那个UDPClientsocketipport到表中,然后打印data到串口。

如果经判断表中已经有个这个udp的信息,那个直接就打印data到串口。

uart.on是串口经过8266向已经连接的UDP Client广播data

这里写图片描述
这里写图片描述

这里写图片描述

这里写图片描述

关于为什么会是1然后是许多个1,显然是空闲中断在使坏!

看下广播的结果~
这里写图片描述

这里写图片描述
这里写图片描述
这里写图片描述
这里写图片描述
这里写图片描述


加入空闲中断后的写法,可同时参考<这里>

init.lua

同上

wifi.lua

wifi.setmode(wifi.STATIONAP)

cfg={}
cfg.ssid="Hellow8266"
cfg.pwd="11223344"
wifi.ap.config(cfg)

apcfg={}
apcfg.ssid="qqqqq"
apcfg.pwd="11223344"
wifi.sta.config(apcfg)
wifi.sta.autoconnect(1)

ConnectIP = "192.168.1.103"
ConnectPort = 8080

UdpSocket = net.createUDPSocket()   
UdpSocket:listen(ConnectPort)

UdpSocketTable={}
UdpIPTable={}
UdpPortTable={}
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)

UartReadCnt=0
UartReadCntCopy=0
UartReadData=""
UartReadDataCopy=""

uart.on("data",0,function(data)
	UartReadCnt = UartReadCnt + 1
    UartReadData = UartReadData..data
end, 0)
	
tmr.alarm(2, 5, 1, function()
    if  UartReadCnt ~=0 then
        if  UartReadCnt == UartReadCntCopy  then
            UartReadCnt = 0
            UartReadCntCopy = 0
            UartReadDataCopy = UartReadData
            UartReadData=""
            NetSend(UartReadDataCopy)
        else
            UartReadCntCopy = UartReadCnt  
        end         
    end

end)
	
function NetSend(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	


printip = 0
wifi.eventmon.register(wifi.eventmon.STA_DISCONNECTED, function(T)
    printip = 0
end)


wifi.eventmon.register(wifi.eventmon.STA_GOT_IP, function(T)
   if printip == 0 then
      print("+IP"..T.IP)
   end
   printip = 1
end)

这里写图片描述

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ReCclay

如果觉得不错,不妨请我喝杯咖啡

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值