七,ESP8266-UDP(基于Lua脚本语言)

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

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

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

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

init.lua

gpio.mode(4,gpio.OUTPUT)

gpio.mode(2,gpio.OUTPUT)

gpio.write(4,1)ifadc.force_init_mode(adc.INIT_ADC) then

node.restart()returnend

tmr.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=0fori=0,2doifUdpIPTable[i] ~= ip or UdpPortTable[i] ~=port  thenifip ~= ConnectIP or port ~=ConnectPort  then

UdpCanConnect=1end

end

endifUdpCanConnect ==1then

UdpSocketTable[UdpConnectCnt]=socket

UdpIPTable[UdpConnectCnt]=ip

UdpPortTable[UdpConnectCnt]=port

print("\r\n"..UdpConnectCnt.."-Connect")

end

UdpConnectCnt= UdpConnectCnt +1ifUdpConnectCnt ==3then

UdpConnectCnt=0end

uart.write(0,data)

end)

uart.on("data",0,function(data)ifUdpSocket ~=nil then

UdpSocket:send(ConnectPort,ConnectIP,data)

endfori=0,2doifUdpSocketTable[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)ifprintip ==0then

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=1fori=0,2doifUdpIPTable[i] == ip and UdpPortTable[i] ==port  then

UdpCanConnect=0end

endifip == ConnectIP and port ==ConnectPort  then

UdpCanConnect=0endifUdpCanConnect ==1then

UdpSocketTable[UdpConnectCnt]=socket

UdpIPTable[UdpConnectCnt]=ip

UdpPortTable[UdpConnectCnt]=port

print("\r\n"..UdpConnectCnt.."-Connect")

UdpConnectCnt= UdpConnectCnt +1endifUdpConnectCnt ==3then

UdpConnectCnt=0end

uart.write(0,data)

end)

uart.on("data",0,function(data)ifUdpSocket ~=nil then

UdpSocket:send(ConnectPort,ConnectIP,data)

endfori=0,2doifUdpSocketTable[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)ifprintip ==0then

print("+IP"..T.IP)

end

printip=1end)

串口事件函数里面

这样的话一个默认的,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)ifcfg.ssid ==nil then

cfg.ssid="Hellow8266"cfg.pwd="11223344"end

print("APssid:"..cfg.ssid)ifcfg.pwd ==nil then

print("APpwd: nil")elseprint("APpwd:"..cfg.pwd)

end

wifi.ap.config(cfg)

apcfg={}

apcfg= wifi.sta.getconfig(true)ifapcfg.ssid ==nil then

apcfg.ssid="qqqqq"apcfg.pwd="11223344"end

print("APssid:"..apcfg.ssid)ifapcfg.pwd ==nil then

print("Stationpwd: nil")elseprint("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=0fori=0,2doifUdpIPTable[i] ~= ip or UdpPortTable[i] ~=port  thenifip ~= ConnectIP or port ~=ConnectPort  then

UdpCanConnect=1end

end

endifUdpCanConnect ==1then

UdpSocketTable[UdpConnectCnt]=socket

UdpIPTable[UdpConnectCnt]=ip

UdpPortTable[UdpConnectCnt]=port

print("\r\n"..UdpConnectCnt.."-Connect")

end

UdpConnectCnt= UdpConnectCnt +1ifUdpConnectCnt ==3then

UdpConnectCnt=0end

uart.write(0,data)

end)

uart.on("data",0,function(data)ifUdpSocket ~=nil then

UdpSocket:send(ConnectPort,ConnectIP,data)

endfori=0,2doifUdpSocketTable[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)ifprintip ==0then

print("+IP"..T.IP)

end

printip=1end)

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

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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,491评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,856评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,745评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,196评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,073评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,112评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,531评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,215评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,485评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,578评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,356评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,215评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,583评论 3 299
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,898评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,174评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,497评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,697评论 2 335

推荐阅读更多精彩内容