七,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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM