博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
七,ESP8266-UDP(基于Lua脚本语言)
阅读量:7029 次
发布时间:2019-06-28

本文共 5717 字,大约阅读时间需要 19 分钟。

 

那天朋友问我为什么有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()    returnendtmr.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  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 = 0wifi.eventmon.register(wifi.eventmon.STA_DISCONNECTED, function(T)    printip = 0end)wifi.eventmon.register(wifi.eventmon.STA_GOT_IP, function(T)   if printip == 0 then      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 = 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 = 0wifi.eventmon.register(wifi.eventmon.STA_DISCONNECTED, function(T)    printip = 0end)wifi.eventmon.register(wifi.eventmon.STA_GOT_IP, function(T)   if printip == 0 then      print("+IP"..T.IP)   end   printip = 1end)

 

 

 

 

 

 

 

 

 串口事件函数里面

 

 

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

 测试一下

 

 

 

 

 

 

 看一下是不是发给默认的

 

 

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

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

现在让其余的连接上

 

 

 现在向串口写数据

 

 

 

 

 

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

 

 我们就设置模块启动的时候查看一下设置的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"endprint("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"endprint("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 = 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  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 = 0wifi.eventmon.register(wifi.eventmon.STA_DISCONNECTED, function(T)    printip = 0end)wifi.eventmon.register(wifi.eventmon.STA_GOT_IP, function(T)   if printip == 0 then      print("+IP"..T.IP)   end   printip = 1end)

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

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

 

转载地址:http://narxl.baihongyu.com/

你可能感兴趣的文章
记Promise得一些API
查看>>
javascript事件之调整大小(resize)事件
查看>>
20145234黄斐《Java程序设计》第六周学习总结
查看>>
【CLRS】《算法导论》读书笔记(四):栈(Stack)、队列(Queue)和链表(Linked List)...
查看>>
hibernate 和 mybatis区别
查看>>
互联网广告综述之点击率特征工程
查看>>
HDU3421 Max Sum II【序列处理】
查看>>
POJ NOI MATH-7653 地球人口承载力估计
查看>>
iOS UI高级之网络编程(HTTP协议)
查看>>
使用cocoaPods import导入时没有提示的解决办法
查看>>
iOS数据持久化存储之归档NSKeyedArchiver
查看>>
JavaScript面向对象
查看>>
Intellij修改模板代码
查看>>
2.页面布局示例笔记
查看>>
一些老游戏CPU 100%占用的解决方法
查看>>
f5基本介绍
查看>>
博前语
查看>>
Java SE核心之一:常用类,包装类,其他基本数据类型包装类。
查看>>
郑捷《机器学习算法原理与编程实践》学习笔记(第二章 中文文本分类(一))...
查看>>
python (ploit)
查看>>