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

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

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

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

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

init.lua

gpio.mode(,gpio.OUTPUT)
gpio.write(,) if adc.force_init_mode(adc.INIT_ADC) then
node.restart()
return
end tmr.alarm(, , , function()
gpio.write(,-gpio.read())
end) tmr.alarm(, , , function()
dofile("UDP.lua")
end)

UDP.lua

wifi.setmode(wifi.STATIONAP)

cfg={}
cfg.ssid="Hellow8266"
cfg.pwd=""
wifi.ap.config(cfg) apcfg={}
apcfg.ssid="qqqqq"
apcfg.pwd=""
wifi.sta.config(apcfg)
wifi.sta.autoconnect() ConnectIP = "192.168.1.103"
ConnectPort = UdpSocket = net.createUDPSocket()
UdpSocket:listen(ConnectPort) UdpSocketTable={}
UdpIPTable={}
UdpPortTable={}
UdpConnectCnt =
UdpCanConnect = UdpSocket:on("receive", function(socket, data, port, ip)
UdpCanConnect =
for i=, do
if UdpIPTable[i] ~= ip or UdpPortTable[i] ~= port then
if ip ~= ConnectIP or port ~= ConnectPort then
UdpCanConnect =
end
end
end if UdpCanConnect == then
UdpSocketTable[UdpConnectCnt] = socket
UdpIPTable[UdpConnectCnt] = ip
UdpPortTable[UdpConnectCnt] = port
print("\r\n"..UdpConnectCnt.."-Connect")
end UdpConnectCnt = UdpConnectCnt +
if UdpConnectCnt == then
UdpConnectCnt =
end
uart.write(,data)
end) uart.on("data",,function(data)
if UdpSocket ~= nil then
UdpSocket:send(ConnectPort,ConnectIP,data)
end for i=, do
if UdpSocketTable[i] ~= nil then
UdpSocketTable[i]:send(UdpPortTable[i],UdpIPTable[i],data)
end
end end, ) printip =
wifi.eventmon.register(wifi.eventmon.STA_DISCONNECTED, function(T)
printip =
end) wifi.eventmon.register(wifi.eventmon.STA_GOT_IP, function(T)
if printip == then
print("+IP"..T.IP)
end
printip =
end)

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

这样

UDP.lua

wifi.setmode(wifi.STATIONAP)

cfg={}
cfg.ssid="Hellow8266"
cfg.pwd=""
wifi.ap.config(cfg) apcfg={}
apcfg.ssid="qqqqq"
apcfg.pwd=""
wifi.sta.config(apcfg)
wifi.sta.autoconnect() ConnectIP = "192.168.1.103"
ConnectPort = UdpSocket = net.createUDPSocket()
UdpSocket:listen(ConnectPort) UdpSocketTable={}
UdpIPTable={}
UdpPortTable={}
UdpConnectCnt =
UdpCanConnect = UdpSocket:on("receive", function(socket, data, port, ip)
UdpCanConnect =
for i=, do
if UdpIPTable[i] == ip and UdpPortTable[i] == port then
UdpCanConnect =
end
end if ip == ConnectIP and port == ConnectPort then
UdpCanConnect =
end if UdpCanConnect == then
UdpSocketTable[UdpConnectCnt] = socket
UdpIPTable[UdpConnectCnt] = ip
UdpPortTable[UdpConnectCnt] = port
print("\r\n"..UdpConnectCnt.."-Connect")
UdpConnectCnt = UdpConnectCnt +
end if UdpConnectCnt == then
UdpConnectCnt =
end
uart.write(,data)
end) uart.on("data",,function(data)
if UdpSocket ~= nil then
UdpSocket:send(ConnectPort,ConnectIP,data)
end for i=, do
if UdpSocketTable[i] ~= nil then
UdpSocketTable[i]:send(UdpPortTable[i],UdpIPTable[i],data)
end
end end, ) printip =
wifi.eventmon.register(wifi.eventmon.STA_DISCONNECTED, function(T)
printip =
end) wifi.eventmon.register(wifi.eventmon.STA_GOT_IP, function(T)
if printip == then
print("+IP"..T.IP)
end
printip =
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=""
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=""
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() ConnectIP = "192.168.1.103"
ConnectPort = UdpSocket = net.createUDPSocket()
UdpSocket:listen(ConnectPort) UdpSocketTable={}
UdpIPTable={}
UdpPortTable={}
UdpConnectCnt =
UdpCanConnect = UdpSocket:on("receive", function(socket, data, port, ip)
UdpCanConnect =
for i=, do
if UdpIPTable[i] ~= ip or UdpPortTable[i] ~= port then
if ip ~= ConnectIP or port ~= ConnectPort then
UdpCanConnect =
end
end
end if UdpCanConnect == then
UdpSocketTable[UdpConnectCnt] = socket
UdpIPTable[UdpConnectCnt] = ip
UdpPortTable[UdpConnectCnt] = port
print("\r\n"..UdpConnectCnt.."-Connect")
end UdpConnectCnt = UdpConnectCnt +
if UdpConnectCnt == then
UdpConnectCnt =
end
uart.write(,data)
end) uart.on("data",,function(data)
if UdpSocket ~= nil then
UdpSocket:send(ConnectPort,ConnectIP,data)
end for i=, do
if UdpSocketTable[i] ~= nil then
UdpSocketTable[i]:send(UdpPortTable[i],UdpIPTable[i],data)
end
end end, ) printip =
wifi.eventmon.register(wifi.eventmon.STA_DISCONNECTED, function(T)
printip =
end) wifi.eventmon.register(wifi.eventmon.STA_GOT_IP, function(T)
if printip == then
print("+IP"..T.IP)
end
printip =
end)

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

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

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

七,ESP8266-UDP(基于Lua脚本语言)的更多相关文章

  1. 三,ESP8266 SPI(基于Lua脚本语言)

    https://www.cnblogs.com/yangfengwu/p/7520260.html 重点是说SPI通信协议,,,, 不要害怕协议因为协议是人规定的,,刚好我也是人......规定的协议 ...

  2. ESP8266使用详解--基于Lua脚本语言

    这些天,,,,今天终于看到了希望,,,天道酬勤 先说实现的功能...让ESP8266连接无线网,然后让它建立服务器,,我的客户端连接上以后,发给客户端发数据模块打印到串口,,往ESP8266串口里发数 ...

  3. 一,ESP8266下载和刷固件(基于Lua脚本语言)

    用自己的小板测试...... 安排上呢 一, ESP8266下载和刷固件(Lua开发----体验一下lua开发的魅力所在) 二, 控制一个灯亮灭 三, TCP服务器 四, TCP客户端 五, UDP ...

  4. 八,ESP8266 文件保存数据(基于Lua脚本语言)

    https://www.cnblogs.com/yangfengwu/p/7533845.html 应该是LUA介绍8266的最后一篇,,,,,,下回是直接用SDK,,然后再列个12345...... ...

  5. 二,ESP8266 GPIO和SPI和定时器和串口(基于Lua脚本语言)

    https://www.cnblogs.com/yangfengwu/p/7514336.html 我们写lua用这个软件 如果点击的时候提示安装,,安装就行,,如果没有提示呢可以,按照下面链接的提示 ...

  6. 五,ESP8266 TCP服务器多连接(基于Lua脚本语言)

    https://www.cnblogs.com/yangfengwu/p/7524326.html 一些时间去准备朋友的元器件了... 接着写,,争取今天写完所有的文章,,因为答应了朋友下周5之前要做 ...

  7. 六,ESP8266 TCP Client(基于Lua脚本语言)

    今天不知道是不是让我姐挺失望.......很多时候都不知道自己努力的方向对不对,,以后能不能带给家人最美好的期盼...... Init.lua 没啥改变,,就改了一下加载Client.lua gpio ...

  8. 四,ESP8266 TCP服务器(基于Lua脚本语言)

    我要赶时间赶紧写完所有的内容....朋友的东西答应的还没做完呢!!!!!!!没想到又来了新的事情,,....... 配置模块作为TCP服务器然后呢咱们连接服务器发指令控制LED亮灭 控制的指令呢咱就配 ...

  9. 九,ESP8266 判断是断电上电(强制硬件复位)之后运行的内部程序还是内部软件复位之后运行的程序(基于Lua脚本语言)

    现在我有一个需求,WIFI模块控制一个继电器,我要做的是如果内部程序跑乱了,造成了内部程序复位重启,那么控制继电器的状态不能改变 如果是设备断电了,然后又来电了,我需要的是继电器一定要是断开才好.不能 ...

随机推荐

  1. 从零开始学习html(十一)CSS盒模型——上

    一.元素分类 在CSS中,html中的标签元素大体被分为三种不同的类型:块状元素.内联元素(又叫行内元素)和内联块状元素. 常用的块状元素有: <div>.<p>.<h1 ...

  2. ubuntu执行sudo apt-get update 时出现的错误及解决办法

    一.错误描述 W: GPG error: http://ppa.launchpad.net/fkrull/deadsnakes/ubuntu xenial InRelease: The followi ...

  3. FI配置步骤清单.枫

    1.    说明 本版本的FI模块配置内容非常少,主要应用的是系统默认的配置参数,但能完成基本的总帐.应收.应付操作. 配置内容包含以下几部分: 1.  基本的组织结构定义及分配,以及公司代码的全局性 ...

  4. FineReport中如何制作树数据集来实现组织树报表

    1. 问题描述 FineReport,组织树报表中由id与父id来实现组织树报表,若层级数较多时,对每个单元格设置过滤条件和形态会比较繁琐,因此FineReport提供了一种特殊的数据集——树数据集, ...

  5. Oracle 参数文件及相关操作介绍

    Oracle 参数文件及相关操作介绍 by:授客 QQ:1033553122 1.服务器参数文件 服务器参数文件是一个二进制文件,作为初始化参数的存储仓库.实例运行时,可用ALTER SYSTEM来改 ...

  6. Android Studio 编译: Program type already present: XXX 解决方案

    3情况1:个例 build.gradle 中 dependencies { classpath 'com.android.tools.build:gradle:3.1.1' // } 改成 depen ...

  7. 最近见到一个用react native实现的标尺动画,不知道如何实现 帖两张图(新知食App)

    很恶心,这款App的标尺没有做兼容,我在模拟上看不到效果,无法说明我想做出的东西的效果,无奈粘一张图吧! 就是这么一个屌样子,如何实现?

  8. python 遇到的一些坑

    lst = [1, 2, 4] print lst.__iter__().next() # 打印出来的是 1 print lst.__iter__().next() # 打印出来的是 1 # 调用__ ...

  9. 在SQL Server中使用CLR调用.NET类库中的方法 (转载)

    在SQL Server中调用 .NET 类库的方法要分为下面几步来实现: 在.NET中新建一个类库项目,并在这个项目中添加一个类文件,并把要被SQL Server调用的方法定义为公有的,静态的方法. ...

  10. vscode 折叠所有区域代码的快捷键

    折叠:ctrl + L    ctrl + 0(主键盘区的0,不是小键盘区的0) 展开:ctrl + K    ctrl + J 老是忘记,在此记录