七,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(,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脚本语言)的更多相关文章
- 三,ESP8266 SPI(基于Lua脚本语言)
https://www.cnblogs.com/yangfengwu/p/7520260.html 重点是说SPI通信协议,,,, 不要害怕协议因为协议是人规定的,,刚好我也是人......规定的协议 ...
- ESP8266使用详解--基于Lua脚本语言
这些天,,,,今天终于看到了希望,,,天道酬勤 先说实现的功能...让ESP8266连接无线网,然后让它建立服务器,,我的客户端连接上以后,发给客户端发数据模块打印到串口,,往ESP8266串口里发数 ...
- 一,ESP8266下载和刷固件(基于Lua脚本语言)
用自己的小板测试...... 安排上呢 一, ESP8266下载和刷固件(Lua开发----体验一下lua开发的魅力所在) 二, 控制一个灯亮灭 三, TCP服务器 四, TCP客户端 五, UDP ...
- 八,ESP8266 文件保存数据(基于Lua脚本语言)
https://www.cnblogs.com/yangfengwu/p/7533845.html 应该是LUA介绍8266的最后一篇,,,,,,下回是直接用SDK,,然后再列个12345...... ...
- 二,ESP8266 GPIO和SPI和定时器和串口(基于Lua脚本语言)
https://www.cnblogs.com/yangfengwu/p/7514336.html 我们写lua用这个软件 如果点击的时候提示安装,,安装就行,,如果没有提示呢可以,按照下面链接的提示 ...
- 五,ESP8266 TCP服务器多连接(基于Lua脚本语言)
https://www.cnblogs.com/yangfengwu/p/7524326.html 一些时间去准备朋友的元器件了... 接着写,,争取今天写完所有的文章,,因为答应了朋友下周5之前要做 ...
- 六,ESP8266 TCP Client(基于Lua脚本语言)
今天不知道是不是让我姐挺失望.......很多时候都不知道自己努力的方向对不对,,以后能不能带给家人最美好的期盼...... Init.lua 没啥改变,,就改了一下加载Client.lua gpio ...
- 四,ESP8266 TCP服务器(基于Lua脚本语言)
我要赶时间赶紧写完所有的内容....朋友的东西答应的还没做完呢!!!!!!!没想到又来了新的事情,,....... 配置模块作为TCP服务器然后呢咱们连接服务器发指令控制LED亮灭 控制的指令呢咱就配 ...
- 九,ESP8266 判断是断电上电(强制硬件复位)之后运行的内部程序还是内部软件复位之后运行的程序(基于Lua脚本语言)
现在我有一个需求,WIFI模块控制一个继电器,我要做的是如果内部程序跑乱了,造成了内部程序复位重启,那么控制继电器的状态不能改变 如果是设备断电了,然后又来电了,我需要的是继电器一定要是断开才好.不能 ...
随机推荐
- python中matplotlib.pyplot中cm的属性
https://matplotlib.org/gallery/color/colormap_reference.html
- 【读书笔记】iOS-网络-优化请求性能
一,度量网络性能 1,网络带宽 用于描述无线网络性能的最常见度量指标就是带宽.在数字无线通信中,网络带宽可以描述为两个端点之间的通信通道每秒钟可以传输的位数.现代无线网络所能提供的理论带宽是很高的.不 ...
- JavaSE——TCP协议网络编程(一)
实现客户端与服务端的连接: 创建TCP服务端的 ServerSocket ServerSocket :此类实现服务器套接字.服务器套接字请求通过网络传入,基于该请求执行某些操作,然后可能向请求者返回结 ...
- <Android 应用 之路> 干货集中营 ~ GankIOClient
简介 利用干货集中营 , daimajia和他的小伙伴给我们开发者带来的福利开发的一个干货集中营客户端,主要功能在阅读干货上. Github地址: https://github.com/onlylov ...
- openCV 视频分解及合成
1. 视频分解 import cv2 # ************************** # 分解视频 cap=cv2.VideoCapture('1.mp4')#获取一个视频cap isOpe ...
- Reactjs-generator-cli 一款基于Ink构建用于快速搭建React应用的CLI scaffolding工具
Reactjs-generator-cli 一款基于Ink构建用于快速搭建React应用的CLI scaffolding工具 A simple CLI for scaffolding React.js ...
- 记一次服务器迁移后的nginx启动问题
背景 服务器A准备下线,故直接将上面的所有应用/资料打包迁移到服务器B.包括搭建的nginx,迁移到B服务器后,楼主偷懒,就想着直接./nginx启动,过程遇到如下问题. ./nginx ./ngin ...
- leveldb源码分析--BloomFilter
bloomfilter是leveldb中的一大性能利器,所以为了文章的表现完整性这里新启这么一篇文章.leveldb中的bloomfilter的实现在bloom.cc中,是一个较为简单的实现,所以就不 ...
- 单元测试,模拟用户Get登陆,并携带登录后的token访问接口
HttpClient _httpClient; HttpClient _businessHttpClient; private async Task<string> GetAccessTo ...
- [POWERSHELL] [.net 3.5] [Windows Server] 在Windows Server上安装.NET3.5
Install-WindowsFeature Net-Framework-Core -source \\network\share\sxs