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

简介: 那天朋友问我为什么有UDP Sever 和 UDP Client   ,,我说:每个人想的不一样,设计上不一样......既然是面向无连接的,那么模块发数据就指定IP和端口号,,,为了能和多个UDP进行通信,我们知道模块的Ip和监听的端口号,,就向这个模块发数据,模块通过数据里面的IP,和端口信...

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

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

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

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

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("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一样的道理

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

 

目录
相关文章
|
6月前
|
Ubuntu 网络协议 Linux
【Lua基础入门】解密世界上最快的脚本语言
【Lua基础入门】解密世界上最快的脚本语言
114 1
|
7月前
|
存储 NoSQL Java
Lua高性能脚本语言快速入门
Lua高性能脚本语言快速入门
151 0
|
XML 存储 Java
【Lua基础 第1章】初识Lua脚本语言、数据类型、全局变量、关键字的使用
初识Lua脚本语言、数据类型、全局变量、关键字的使用
122 0
【Lua基础 第1章】初识Lua脚本语言、数据类型、全局变量、关键字的使用
|
存储 JavaScript 编译器
Lua脚本语言——Lua脚本基础语法
Lua脚本语言——Lua脚本基础语法
609 0
Lua脚本语言——Lua脚本基础语法
|
Web App开发 网络协议
八,ESP8266 文件保存数据(基于Lua脚本语言)
应该是LUA介绍8266的最后一篇,,,,,,下回是直接用SDK,,然后再列个12345.......不过要等一两个星期,先忙完朋友的事情 前面几篇 用AT指令版本的 一,  http://www.cnblogs.
1547 0
|
网络协议 网络架构
五,ESP8266 TCP服务器多连接(基于Lua脚本语言)
一些时间去准备朋友的元器件了... 接着写,,争取今天写完所有的文章,,因为答应了朋友下周5之前要做好朋友的东西 对于TCP大家在玩AT指令的时候有没有发现客户端最多连接5个,,,再连接就不行了?? 所以在用AT指令开发的时候单片机程序一定要记得清除多余的连接 现在看用LUA语言怎么做 直接先上菜 Init.
1368 0
|
网络协议 编解码
六,ESP8266 TCP Client(基于Lua脚本语言)
今天不知道是不是让我姐挺失望.......很多时候都不知道自己努力的方向对不对,,以后能不能带给家人最美好的期盼...... Init.lua 没啥改变,,就改了一下加载Client.lua   gpio.
1409 0
|
API 数据安全/隐私保护 芯片
三,ESP8266 SPI(基于Lua脚本语言)
重点是说SPI通信协议,,,, 不要害怕协议因为协议是人规定的,,刚好我也是人......规定的协议既然能成为规范让所有人所接受,那么必然有它的优势和优点,必然值得学习,, 害怕协议的人是因为当初碰到了不懂的老师,他只会告诉你这很难.
1413 0