七,UDP
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
七,UDP的更多相关文章
- Netty4.x中文教程系列(七)UDP协议
将近快一年时间没有更新Netty的博客.一方面原因是因为项目进度的问题.另外一方面是博主有一段时间去熟悉Unity3D引擎. 本章节主要记录博主自己Netty的UDP协议使用. 1. 构建UDP服务端 ...
- 驱动力—— 通信引擎(上)—— ESFramework 4.0 进阶(03)
在ESFramework 4.0 进阶(02)-- 核心:消息处理的骨架流程一文中我们详细介绍了ESFramework中消息处理的骨架流程,并且我们已经知道,ESFramework中的所有通信引擎使用 ...
- 网络七层模型及TCP、UDP,一次HTTP请求都发生了什么
一.七层网络模型 http协议运行在应用层 二.TCP-UDP TCP.UDP协议的区别 一次Http 请求,这个过程都发生了什么 TCP 协议如何保证可靠传输 HTTP和HTTPS的区别 TCP ...
- OSI七层和TCP/IP四层的关系、TCP与UDP、HTTP、Socket
HTTP(应用层协议):超文本传输协议,HTTP协议是建立在TCP协议之上的一种应用. HTTP协议详细解释 2Http详解 TCP(面向连接的传输层协议):transmission control ...
- Day09: socket网络编程-OSI七层协议,tcp/udp套接字,tcp粘包问题,socketserver
今日内容:socket网络编程 1.OSI七层协议 2.基于tcp协议的套接字通信 3.模拟ssh远程执行命令 4.tcp的粘包问题及解决方案 5.基于udp协议的套接字 ...
- 网络编程基础socket 重要中:TCP/UDP/七层协议
计算机网络的发展及基础网络概念 问题:网络到底是什么?计算机之间是如何通信的? 早期 : 联机 以太网 : 局域网与交换机 广播 主机之间“一对所有”的通讯模式,网络对其中每一台主机发出的信号都进行无 ...
- Python进阶----网络通信基础 ,OSI七层协议() ,UDP和TCP的区别 , TCP/IP协议(三次握手,四次挥手)
Python进阶----网络通信基础 ,OSI七层协议() ,UDP和TCP的区别 , TCP/IP协议(三次握手,四次挥手) 一丶CS/BS 架构 C/S: 客户端/服务器 定义: ...
- day28——C/S与B/S架构、网络通信原理、osi七层协议、UDP、TCP协议、TCP的三次握手与四次挥手
day28 C/S B/S架构 C:client 客户端 B:browse浏览器 S:server 服务端 C/S C/S架构:基于客户端与服务端之间的通信 QQ.游戏.皮皮虾 优点:个性化设 ...
- 计算机网络(七),TCP与UDP的区别
七.TCP与UDP的区别 1.面向连接VS无连接 TCP面向连接而UDP面向无连接的,TCP是和单对单传送数据,UDP适合多波发布 2.可靠性 TCP利用握手,确认,重传机制提供了可靠性保证,UDP可 ...
随机推荐
- 新的表格展示利器 Bootstrap Table
1.bootstrap table简介及特征 Bootstrap Table是国人开发的一款基于 Bootstrap 的 jQuery 表格插件,通过简单的设置,就可以拥有强大的单选.多选.排序.分 ...
- 为什么是Spring Boot
原文:https://dzone.com/articles/why-springboot 作者:Siva Prasad Reddy Katamreddy 译者:Oopsguy 本文介绍将各种Sprin ...
- RabbitMQ安装以及java使用(一)
最近闲来无事,整理下基础知识,本次安装 1.RabbitMQ版本是3.6.10 2.操作系统是centOS 7 64位 虚拟机IP:192.168.149.133 1.安装更新系统环境依赖 yum ...
- C互质个数
C互质个数 Time Limit:1000MS Memory Limit:65536K Total Submit:55 Accepted:27 Description 贝贝.妞妞和康康都长大了,如今 ...
- "=="和equals方法究竟有什么区别?
(单独把一个东西说清楚,然后再说清楚另一个,这样,它们的区别自然就出来了,混在一起说,则很难说清楚) ==操作符专门用来比较两个变量的值是否相等,也就是用于比较变量所对应的内存中所存储的数值是否相同, ...
- C/C++ 知识点---LIB和DLL的区别与使用(网摘)
LIB和DLL的区别与使用 原文出处:[远风工作室] 共有两种库:一种是LIB包含了函数所在的DLL文件和文件中函数位置的信息(入口),代码由运行时加载在进程空间中的DLL提供,称为动态链接库dyna ...
- imshow显示超大图像
在matlab做图像处理时,有些图片比较大,或者自己的显示器比较小,又要求查看完整的图片怎么办呢? 如果使用imshow直接显示,则显然没法达到要求.最好的办法还是滚动条: hFig = figure ...
- sublime text 的小细节设置,让你的代码更优美
这些属性都可以在 首选项>设置-默认 里修改下面也会介绍几个比较常用的几个插件 字体大小: "font_size": 17 高亮编辑中的一行 "highlight_ ...
- 【Weblogic】启动命令nohup解析
nohup ./startWebLogic.sh >out.log 2>&1 & 解析 其中 0.1.2分别代表如下含义: 0 – stdin (standard inpu ...
- Andrew Ng机器学习课程笔记--week5(上)
Neural Networks: Learning 内容较多,故分成上下两篇文章. 一.内容概要 Cost Function and Backpropagation Cost Function Bac ...