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

https://www.cnblogs.com/yangfengwu/p/7533302.html

 

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

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

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

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

init.lua

gpio.mode(4,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("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 = 8080

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



UdpSocketTable={}
UdpIPTable={}
UdpPortTable={}
UdpConnectCnt = 0
UdpCanConnect = 0

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

    if  UdpCanConnect == 1 then
        UdpSocketTable[UdpConnectCnt] = socket
        UdpIPTable[UdpConnectCnt] = ip 
        UdpPortTable[UdpConnectCnt] = port
        print("\r\n"..UdpConnectCnt.."-Connect")
    end
    
    UdpConnectCnt = UdpConnectCnt + 1
    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)

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

这样

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 = 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)

 

 

 

 

 

 

 

 

 串口事件函数里面

 

 

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

 测试一下

 

 

 

 

 

 

 看一下是不是发给默认的

 

 

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

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

http://www.cnblogs.com/yangfengwu/p/7520260.html

二,ESP8266 GPIO和SPI和定时器和串口

现在让其余的连接上

 

 

 现在向串口写数据

 

 

 

 

 

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

 

 我们就设置模块启动的时候查看一下设置的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")
else
    print("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")
else
    print("Stationpwd: "..apcfg.pwd)
end 


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 = 0
    for i=0,2 do
        if  UdpIPTable[i] ~= ip or UdpPortTable[i] ~= port  then
            if  ip ~= ConnectIP or port ~= ConnectPort  then
                UdpCanConnect = 1
            end
        end
    end

    if  UdpCanConnect == 1 then
        UdpSocketTable[UdpConnectCnt] = socket
        UdpIPTable[UdpConnectCnt] = ip 
        UdpPortTable[UdpConnectCnt] = port
        print("\r\n"..UdpConnectCnt.."-Connect")
    end
    
    UdpConnectCnt = UdpConnectCnt + 1
    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)

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

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

https://www.cnblogs.com/yangfengwu/p/7534521.html

 

posted on 2017-09-17 01:13  广源时代  阅读(2760)  评论(1编辑  收藏  举报

导航

支付宝 QQ群