lua udp mysql_七,ESP8266-UDP(基于Lua脚本语言)

那天朋友问我为什么有UDP Sever 和 UDP Client  ,,我说:每个人想的不一样,设计上不一样......

既然是面向无连接的,那么模块发数据就指定IP和端口号,,,为了能和多个UDP进行通信,我们知道模块的Ip和监听的端口号,,就向这个模块发数据,

模块通过数据里面的IP,和端口信息就知道了是谁发给的,,模块把Ip和端口号记录下来就能同时和好几个UDP通信了

还有一点,我们设置一个模块默认发数据的IP和端口号,,,,剩下的是记录了谁就发给谁

init.lua

8084be3afc35

gpio.mode(4,gpio.OUTPUT)

gpio.mode(2,gpio.OUTPUT)

gpio.write(4,1)ifadc.force_init_mode(adc.INIT_ADC) then

node.restart()returnend

tmr.alarm(0,1000,1, function()

gpio.write(4,1-gpio.read(4))

end)

tmr.alarm(1,3000,0, function()

dofile("UDP.lua")

end)

8084be3afc35

UDP.lua

8084be3afc35

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=8080UdpSocket=net.createUDPSocket()

UdpSocket:listen(ConnectPort)

UdpSocketTable={}

UdpIPTable={}

UdpPortTable={}

UdpConnectCnt=0UdpCanConnect=0UdpSocket:on("receive", function(socket, data, port, ip)

UdpCanConnect=0fori=0,2doifUdpIPTable[i] ~= ip or UdpPortTable[i] ~=port  thenifip ~= ConnectIP or port ~=ConnectPort  then

UdpCanConnect=1end

end

endifUdpCanConnect ==1then

UdpSocketTable[UdpConnectCnt]=socket

UdpIPTable[UdpConnectCnt]=ip

UdpPortTable[UdpConnectCnt]=port

print("\r\n"..UdpConnectCnt.."-Connect")

end

UdpConnectCnt= UdpConnectCnt +1ifUdpConnectCnt ==3then

UdpConnectCnt=0end

uart.write(0,data)

end)

uart.on("data",0,function(data)ifUdpSocket ~=nil then

UdpSocket:send(ConnectPort,ConnectIP,data)

endfori=0,2doifUdpSocketTable[i] ~=nil then

UdpSocketTable[i]:send(UdpPortTable[i],UdpIPTable[i],data)

end

end

end,0)

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

printip=0end)

wifi.eventmon.register(wifi.eventmon.STA_GOT_IP, function(T)ifprintip ==0then

print("+IP"..T.IP)

end

printip=1end)

8084be3afc35

需要修改一下:写的匆忙写错了.......

这样

UDP.lua

8084be3afc35

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=8080UdpSocket=net.createUDPSocket()

UdpSocket:listen(ConnectPort)

UdpSocketTable={}

UdpIPTable={}

UdpPortTable={}

UdpConnectCnt=0UdpCanConnect=0UdpSocket:on("receive", function(socket, data, port, ip)

UdpCanConnect=1fori=0,2doifUdpIPTable[i] == ip and UdpPortTable[i] ==port  then

UdpCanConnect=0end

endifip == ConnectIP and port ==ConnectPort  then

UdpCanConnect=0endifUdpCanConnect ==1then

UdpSocketTable[UdpConnectCnt]=socket

UdpIPTable[UdpConnectCnt]=ip

UdpPortTable[UdpConnectCnt]=port

print("\r\n"..UdpConnectCnt.."-Connect")

UdpConnectCnt= UdpConnectCnt +1endifUdpConnectCnt ==3then

UdpConnectCnt=0end

uart.write(0,data)

end)

uart.on("data",0,function(data)ifUdpSocket ~=nil then

UdpSocket:send(ConnectPort,ConnectIP,data)

endfori=0,2doifUdpSocketTable[i] ~=nil then

UdpSocketTable[i]:send(UdpPortTable[i],UdpIPTable[i],data)

end

end

end,0)

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

printip=0end)

wifi.eventmon.register(wifi.eventmon.STA_GOT_IP, function(T)ifprintip ==0then

print("+IP"..T.IP)

end

printip=1end)

8084be3afc35

8084be3afc35

8084be3afc35

8084be3afc35

8212260ccde76b66a9e9ad8290fd1752.png

8084be3afc35

8084be3afc35

串口事件函数里面

8084be3afc35

这样的话一个默认的,3个后期连接的,,一共同时可以通信4个

测试一下

8084be3afc35

8084be3afc35

8084be3afc35

看一下是不是发给默认的

8084be3afc35

8084be3afc35

关于为什么会是1然后是许多个1,,,因为串口默认的有一个数据就会进入中断...

想统一发过去...解决方法可以参考(空闲中断)

现在让其余的连接上

8084be3afc35

现在向串口写数据

8084be3afc35

8084be3afc35

8084be3afc35

8084be3afc35

8084be3afc35

看一下模块其余的一些函数

8084be3afc35

我们就设置模块启动的时候查看一下设置的wifi.ap.config      和 wifi.sta.config

如果有就设置原来保存的,,没有设置才设置成程序中的

UDP.lua修改为

8084be3afc35

wifi.setmode(wifi.STATIONAP)

cfg={}

cfg= wifi.ap.getconfig(true)ifcfg.ssid ==nil then

cfg.ssid="Hellow8266"cfg.pwd="11223344"end

print("APssid:"..cfg.ssid)ifcfg.pwd ==nil then

print("APpwd: nil")elseprint("APpwd:"..cfg.pwd)

end

wifi.ap.config(cfg)

apcfg={}

apcfg= wifi.sta.getconfig(true)ifapcfg.ssid ==nil then

apcfg.ssid="qqqqq"apcfg.pwd="11223344"end

print("APssid:"..apcfg.ssid)ifapcfg.pwd ==nil then

print("Stationpwd: nil")elseprint("Stationpwd:"..apcfg.pwd)

end

wifi.sta.config(apcfg)

wifi.sta.autoconnect(1)

ConnectIP="192.168.1.103"ConnectPort=8080UdpSocket=net.createUDPSocket()

UdpSocket:listen(ConnectPort)

UdpSocketTable={}

UdpIPTable={}

UdpPortTable={}

UdpConnectCnt=0UdpCanConnect=0UdpSocket:on("receive", function(socket, data, port, ip)

UdpCanConnect=0fori=0,2doifUdpIPTable[i] ~= ip or UdpPortTable[i] ~=port  thenifip ~= ConnectIP or port ~=ConnectPort  then

UdpCanConnect=1end

end

endifUdpCanConnect ==1then

UdpSocketTable[UdpConnectCnt]=socket

UdpIPTable[UdpConnectCnt]=ip

UdpPortTable[UdpConnectCnt]=port

print("\r\n"..UdpConnectCnt.."-Connect")

end

UdpConnectCnt= UdpConnectCnt +1ifUdpConnectCnt ==3then

UdpConnectCnt=0end

uart.write(0,data)

end)

uart.on("data",0,function(data)ifUdpSocket ~=nil then

UdpSocket:send(ConnectPort,ConnectIP,data)

endfori=0,2doifUdpSocketTable[i] ~=nil then

UdpSocketTable[i]:send(UdpPortTable[i],UdpIPTable[i],data)

end

end

end,0)

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

printip=0end)

wifi.eventmon.register(wifi.eventmon.STA_GOT_IP, function(T)ifprintip ==0then

print("+IP"..T.IP)

end

printip=1end)

8084be3afc35

8084be3afc35

Station 模式的路由器的ssid和pwd一样的道理

完成一篇..................

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值