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. Bash:字符串操作

    参考:http://blog.csdn.net/finewings/article/details/5718133 字符串提取 去掉指定前缀 1. ${varible#pattern}         ...

  2. Code Signal_练习题_stringsRearrangement

    Given an array of equal-length strings, check if it is possible to rearrange the strings in such a w ...

  3. nodejs 知识总结

    作者QQ:1095737364    QQ群:123300273     欢迎加入! 1.添加模块:保存到package.json文件中; # npm install vue --save    注意 ...

  4. 洛谷P3313 [SDOI2014]旅行(树链剖分 动态开节点线段树)

    题意 题目链接 Sol 树链剖分板子 + 动态开节点线段树板子 #include<bits/stdc++.h> #define Pair pair<int, int> #def ...

  5. js-ES6学习笔记-module(3)

    1.如果想设置跨模块的常量(即跨多个文件),或者说一个值要被多个模块共享,可以采用下面的写法. // constants.js 模块 export const A = 1; export const ...

  6. 【代码笔记】iOS-4个可以单独点击的button

    一,效果图. 二,工程图. 三,代码. ViewController.m #import "ViewController.h" @interface ViewController ...

  7. jQuery中hover方法和toggle方法使用指南

    jQuery提供一些方法(如:toggle)将两种事件效果合并到一起,比如:mouseover.mouseout:keyup.keydown等 1.hover函数 hover(over,out)一个模 ...

  8. CSS实现英文或拼音单词首字母大写

    CSS实现英文或拼音单词首字母大写,只需要在css样式中加入: text-transform: capitalize 即可. 测试代码如下: <!doctype html> <htm ...

  9. 机器学习实战(Machine Learning in Action)学习笔记————04.朴素贝叶斯分类(bayes)

    机器学习实战(Machine Learning in Action)学习笔记————04.朴素贝叶斯分类(bayes) 关键字:朴素贝叶斯.python.源码解析作者:米仓山下时间:2018-10-2 ...

  10. MySQL常用查询语句积累

    >>MySQL某列插入递增值 SET @i := 100; UPDATE auge_item_classification SET c_code=(@i:=(@i+1)); >> ...