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

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

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

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

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

init.lua

gpio.mode(4,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)

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

UdpSocket:listen(ConnectPort)

UdpSocketTable={}

UdpIPTable={}

UdpPortTable={}

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

UdpCanConnect= 0

for i=0,2 do

if UdpIPTable[i] ~= ip or UdpPortTable[i] ~=port thenif ip ~= ConnectIP or port ~=ConnectPort then

UdpCanConnect= 1end

end

endif UdpCanConnect == 1then

UdpSocketTable[UdpConnectCnt]=socket

UdpIPTable[UdpConnectCnt]=ip

UdpPortTable[UdpConnectCnt]=port

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

end

UdpConnectCnt= UdpConnectCnt + 1

if UdpConnectCnt == 3then

UdpConnectCnt= 0end

uart.write(0,data)

end)

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

UdpSocket:send(ConnectPort,ConnectIP,data)

endfor i=0,2 do

if UdpSocketTable[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)if printip == 0then

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

end

printip= 1end)

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

这样

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

UdpSocket:listen(ConnectPort)

UdpSocketTable={}

UdpIPTable={}

UdpPortTable={}

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

UdpCanConnect= 1

for i=0,2 do

if UdpIPTable[i] == ip and UdpPortTable[i] ==port then

UdpCanConnect= 0end

endif ip == ConnectIP and port ==ConnectPort then

UdpCanConnect= 0endif UdpCanConnect == 1then

UdpSocketTable[UdpConnectCnt]=socket

UdpIPTable[UdpConnectCnt]=ip

UdpPortTable[UdpConnectCnt]=port

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

UdpConnectCnt= UdpConnectCnt + 1endif UdpConnectCnt == 3then

UdpConnectCnt= 0end

uart.write(0,data)

end)

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

UdpSocket:send(ConnectPort,ConnectIP,data)

endfor i=0,2 do

if UdpSocketTable[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)if printip == 0then

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

end

printip= 1end)

b2763b5f39168a4f9c77d8e6644e6f20.png

3021d9eb2671ca271208179f0b46a325.png

a91252f18d999f5d2c0b8daf8c041deb.png

fc8e335b834095f67f282e24c392be4f.png

f0cfdbd2578fcccdc7cc59eac87acd14.png

b0a6e3bd490d00797324dea7aedb4af9.png

串口事件函数里面

1477fd783642bd196b497d7c7830161f.png

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

测试一下

46f7072086d104d1bcc72e8a55a0b789.png

932bf73080301db76b1fb2fa03b32f69.png

3d13409629224873977273df3413c70e.png

看一下是不是发给默认的

fff795d530a24c520cd9f13e60da3040.png

8845c870c3910ba892f7f2b920d52d71.png

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

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

现在让其余的连接上

bcd5473345f951a869878db453bcf802.png

现在向串口写数据

faed5277994a6b9682a649c52e9ce9b3.png

cd0e763e34a395990c46bdc53d23ea20.png

1fc4addccba6710489cd1ddb402f2f99.png

f550d39d08d60633094bfaa08dfcdd50.png

8ddbee1e9fa5bad0cf2cbc19fc117167.png

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

3e364a9f5f33ab5d367386cca30d80c1.png

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

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

UDP.lua修改为

wifi.setmode(wifi.STATIONAP)

cfg={}

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

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

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

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

end

wifi.ap.config(cfg)

apcfg={}

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

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

print("APssid:"..apcfg.ssid)if apcfg.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= 0

for i=0,2 do

if UdpIPTable[i] ~= ip or UdpPortTable[i] ~=port thenif ip ~= ConnectIP or port ~=ConnectPort then

UdpCanConnect= 1end

end

endif UdpCanConnect == 1then

UdpSocketTable[UdpConnectCnt]=socket

UdpIPTable[UdpConnectCnt]=ip

UdpPortTable[UdpConnectCnt]=port

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

end

UdpConnectCnt= UdpConnectCnt + 1

if UdpConnectCnt == 3then

UdpConnectCnt= 0end

uart.write(0,data)

end)

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

UdpSocket:send(ConnectPort,ConnectIP,data)

endfor i=0,2 do

if UdpSocketTable[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)if printip == 0then

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

end

printip= 1end)

28ad6c6db967ed8576293b308d9a5e0a.png

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

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值