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

想问一件事情--简书,,,代码有没有专门的显示的..比如说插入代码什么的????


一些时间去准备朋友的元器件了...

接着写,,争取今天写完所有的文章,,因为答应了朋友下周5之前要做好朋友的东西

对于TCP大家在玩AT指令的时候有没有发现客户端最多连接5个,,,再连接就不行了??

所以在用AT指令开发的时候单片机程序一定要记得清除多余的连接

现在看用LUA语言怎么做

直接先上菜

Init.lua

gpio.mode(4,gpio.OUTPUT)

gpio.mode(2,gpio.OUTPUT)

gpio.write(4,1)

tmr.alarm(0,1000,1, function()

gpio.write(4,1-gpio.read(4))

end)

tmr.alarm(1,1000,0, function()

dofile("wifi.lua")

end)

wifi.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.connect()

TCPSever=net.createServer(net.TCP,28800)

TcpConnectCnt=0TcpSocketTable={}

TCPSever:listen(8080,function(socket)ifTcpConnectCnt ==4thenifTcpSocketTable[0] ~=nil then

TcpSocketTable[0]:close()

TcpSocketTable[0] =nil

endelseifTcpSocketTable[TcpConnectCnt+1] ~=nil then

TcpSocketTable[TcpConnectCnt+1]:close()

TcpSocketTable[TcpConnectCnt+1] =nil

end

end

TcpSocketTable[TcpConnectCnt]=socket

print(TcpConnectCnt.."-Connect")

TcpConnectCnt= TcpConnectCnt +1ifTcpConnectCnt ==5then

TcpConnectCnt=0end

socket:on("receive",function(socket,data)

uart.write(0,data)

end)

socket:on("disconnection",function(sck,c)fori=0,4doifTcpSocketTable[i] ==sck then

TcpSocketTable[i]=nil

print(i.."-Disconnect")

end

end

end)

end)

uart.on("data",0,function(data)fori=0,5doifTcpSocketTable[i] ~=nil then

TcpSocketTable[i]:send(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)

下面看解释.............

wifi.setmode(wifi.STATIONAP)--模式AP+STATION就不说了

cfg={}

cfg.ssid="Hellow8266"cfg.pwd="11223344"wifi.ap.config(cfg)--设置模块的无线和密码

apcfg={}

apcfg.ssid="qqqqq"apcfg.pwd="11223344"wifi.sta.config(apcfg)--设置模块连接路由器的无线和密码--wifi.sta.connect()连接路由器,断开后可能就不自动连接了,可以用下面的

wifi.sta.autoconnect(1)可以用这个断开后自动连接路由器

TCPSever=net.createServer(net.TCP,28800) --创建服务器超过28800S不通信断开已有的连接

TcpConnectCnt=0--连接个数计数

TcpSocketTable={}--存储socket

TCPSever:listen(8080,function(socket)

如果0号连接就把1号关掉,,,1号连接就把2号关掉....4号连接就把0号关掉,这样子循环,

当然您会问可以连接5个,,这样子只可以连接四个了,,,为什么....因为如果连接了5个就进不

来这个监听函数了.......所以必须留下一个空位ifTcpConnectCnt ==4thenifTcpSocketTable[0] ~=nil then

TcpSocketTable[0]:close()

TcpSocketTable[0] =nil

endelseifTcpSocketTable[TcpConnectCnt+1] ~=nil then

TcpSocketTable[TcpConnectCnt+1]:close()

TcpSocketTable[TcpConnectCnt+1] =nil

end

end

TcpSocketTable[TcpConnectCnt]= socket--把连接的socket存到数组

print(TcpConnectCnt.."-Connect")--打印几号连接了  对了 .. 是连接符

TcpConnectCnt= TcpConnectCnt +1--连接个数加一ifTcpConnectCnt ==5then --归零

TcpConnectCnt=0end

socket:on("receive",function(socket,data)

uart.write(0,data) --把接收到的数据发到串口

end)

socket:on("disconnection",function(sck,c)fori=0,4do--判断是哪个断开了连接,,就把对应的socket变量置为 nilifTcpSocketTable[i] ==sck then

TcpSocketTable[i]=nil

print(i.."-Disconnect")

end

end

end)

end)

uart.on("data",0,function(data)fori=0,4do--把串口的数据发向不为 nil 的连接ifTcpSocketTable[i] ~=nil then

TcpSocketTable[i]:send(data)

end

end

end,0)--下面是连接路由器和没有连接路由器的监听函数,,好像是1S检测一次

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)

现在把程序下进去

测试软件呢在这里下载--也可以自己下载个网络调试助手

https://item.taobao.com/item.htm?spm=686.1000925.0.0.4a155084jlU4Rd&id=558508797404

连接了路由器了....

第一个连接

测试数据

再来几个连接

现在再连接一个

我现在随意断开一个,看一看串口应该打印哪一个断开了连接

现在发数据

好现在呢控制就用多个客户端控制继电器


控制的指令呢咱就配置成----

http://www.cnblogs.com/yangfengwu/p/7513097.html

这篇文章最后的指令,,毕竟最终咱们就要做成那样子.....这样的话8266就应该使用的非常顺手和轻松了

对了在

http://www.cnblogs.com/yangfengwu/p/7520260.html

http://www.cnblogs.com/yangfengwu/p/7524326.html

这篇两文章中有提及......这次是用多个TCP客户端控制,,还会加上CRC校验

可以直接用软件上的这两个按钮控制

先写不带CRC校验数据的

为了是程序好浏览,,就定义一个函数,,然后调用

现在的wifi.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.connect()

TCPSever=net.createServer(net.TCP,28800)

TcpConnectCnt=0TcpSocketTable={}

TCPSever:listen(8080,function(socket)ifTcpConnectCnt ==4thenifTcpSocketTable[0] ~=nil then

TcpSocketTable[0]:close()

TcpSocketTable[0] =nil

endelseifTcpSocketTable[TcpConnectCnt+1] ~=nil then

TcpSocketTable[TcpConnectCnt+1]:close()

TcpSocketTable[TcpConnectCnt+1] =nil

end

end

TcpSocketTable[TcpConnectCnt]=socket

print(TcpConnectCnt.."-Connect")

TcpConnectCnt= TcpConnectCnt +1ifTcpConnectCnt ==5then

TcpConnectCnt=0end

socket:on("receive",function(socket,data)

uart.write(0,data)

control(data)

end)

socket:on("disconnection",function(sck,c)fori=0,4doifTcpSocketTable[i] ==sck then

TcpSocketTable[i]=nil

print(i.."-Disconnect")

end

end

end)

end)

function control(data)ifdata =="++MD610"then

gpio.write(2,1)

print("Relay=1")

endifdata =="++MD600"then

gpio.write(2,0)

print("Relay=0")

end

end

uart.on("data",0,function(data)fori=0,4doifTcpSocketTable[i] ~=nil then

TcpSocketTable[i]:send(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)

好现在控制一下

现在用另一个断开

对了最好把信息发给网络对不对,,,控制一下最好通过网络有一个回复

现在的wifi.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.connect()

TCPSever=net.createServer(net.TCP,28800)

TcpConnectCnt=0TcpSocketTable={}

NowSocket=nil

TCPSever:listen(8080,function(socket)ifTcpConnectCnt ==4thenifTcpSocketTable[0] ~=nil then

TcpSocketTable[0]:close()

TcpSocketTable[0] =nil

endelseifTcpSocketTable[TcpConnectCnt+1] ~=nil then

TcpSocketTable[TcpConnectCnt+1]:close()

TcpSocketTable[TcpConnectCnt+1] =nil

end

end

TcpSocketTable[TcpConnectCnt]=socket

print(TcpConnectCnt.."-Connect")

TcpConnectCnt= TcpConnectCnt +1ifTcpConnectCnt ==5then

TcpConnectCnt=0end

socket:on("receive",function(socket,data)

NowSocket=socket

uart.write(0,data)

control(data)

end)

socket:on("disconnection",function(sck,c)fori=0,4doifTcpSocketTable[i] ==sck then

TcpSocketTable[i]=nil

print(i.."-Disconnect")

end

end

end)

end)

function control(data)ifdata =="++MD610"then

gpio.write(2,1)

print("Relay=1")ifNowSocket ~=nil then

NowSocket:send("Relay=1")

NowSocket=nil

end

endifdata =="++MD600"then

gpio.write(2,0)

print("Relay=0")ifNowSocket ~=nil then

NowSocket:send("Relay=0")

NowSocket=nil

end

end

end

uart.on("data",0,function(data)fori=0,4doifTcpSocketTable[i] ~=nil then

TcpSocketTable[i]:send(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)

好了现在加入CRC16校验数据

现在的wifi.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.connect()

TCPSever=net.createServer(net.TCP,28800)

TcpConnectCnt=0TcpSocketTable={}

NowSocket=nil

TCPSever:listen(8080,function(socket)ifTcpConnectCnt ==4thenifTcpSocketTable[0] ~=nil then

TcpSocketTable[0]:close()

TcpSocketTable[0] =nil

endelseifTcpSocketTable[TcpConnectCnt+1] ~=nil then

TcpSocketTable[TcpConnectCnt+1]:close()

TcpSocketTable[TcpConnectCnt+1] =nil

end

end

TcpSocketTable[TcpConnectCnt]=socket

print(TcpConnectCnt.."-Connect")

TcpConnectCnt= TcpConnectCnt +1ifTcpConnectCnt ==5then

TcpConnectCnt=0end

socket:on("receive",function(socket,data)

NowSocket=socket

uart.write(0,data)

control(data)

end)

socket:on("disconnection",function(sck,c)fori=0,4doifTcpSocketTable[i] ==sck then

TcpSocketTable[i]=nil

print(i.."-Disconnect")

end

end

end)

end)

function control(data)

local RevLen=string.len (data)

local crc= ow.crc16(string.sub(data,1,RevLen-2))

local recrc= data:byte(RevLen)

local recrc= recrc*256local recrc= recrc + data:byte(RevLen-1)ifcrc ==recrc thenifstring.sub(data,1,7) =="++MD610"then

gpio.write(2,1)

print("Relay=1")ifNowSocket ~=nil then

NowSocket:send("Relay=1")

NowSocket=nil

end

endifstring.sub(data,1,7) =="++MD600"then

gpio.write(2,0)

print("Relay=0")ifNowSocket ~=nil then

NowSocket:send("Relay=0")

NowSocket=nil

end

endelseifNowSocket ~=nil then

NowSocket:send("CRC16 Faild")

NowSocket=nil

end

end

end

uart.on("data",0,function(data)fori=0,4doifTcpSocketTable[i] ~=nil then

TcpSocketTable[i]:send(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)

他呢,,参数要填写字符串

上位机的CRC计算函数

//////////////////privateintcrc16_modbus(byte[] modbusdata,intlength)

{inti, j;intcrc =0x0;try{for(i =0; i < length; i++)

{

crc^=modbusdata[i];for(j =0; j <8; j++)

{if((crc &0x01) ==1)

{

crc= (crc >>1) ^0xa001;

}else{

crc>>=1;

}

}

}

}catch(Exception)

{throw;

}returncrc;

}

我的置高继电器函数

privatevoidbutton7_Click_1(objectsender, EventArgs e)

{byte[] sendbyte =newbyte[7];

sendbyte[0] = (byte)'+';//2Bsendbyte[1] = (byte)'+';

sendbyte[2] = (byte)'M';//4Dsendbyte[3] = (byte)'D';//44sendbyte[4] = (byte)'6';//6sendbyte[5] = (byte)'1';//1if(comboBox3.Text=="继电器")//pin2{

sendbyte[6] = (byte)'0';

}elseif(comboBox3.Text =="CS")//pin8{

sendbyte[6] = (byte)'1';

}elseif(comboBox3.Text =="MOSI")//pin7{

sendbyte[6] = (byte)'2';

}elseif(comboBox3.Text =="MISO")//pin6{

sendbyte[6] = (byte)'3';

}elseif(comboBox3.Text =="CLK")//pin5{

sendbyte[6] = (byte)'4';

}if(radioButtonNetCon.Checked)//网络发送{

TcpSendDataMethod(sendbyte);

}elseif(radioButtonSerialCon.Checked)//串口发送{

SerialSend(sendbyte);

}

}////////////privatevoidTcpSendDataMethod(byte[] byt)

{intcrc =0;byte[] sendbyte =newbyte[byt.Length +2];for(inti =0; i < byt.Length; i++)

{

sendbyte[i]=byt[i];

}

crc= crc16_modbus(byt, byt.Length);//计算CRCbyte[] Crcbyte = System.BitConverter.GetBytes(crc);//得到CRCsendbyte[sendbyte.Length-2] = Crcbyte[0];

sendbyte[sendbyte.Length-1] = Crcbyte[1];try{ networkstrem.Write(sendbyte,0, sendbyte.Length); }catch(Exception) { MessageBox.Show("请检查连接","提示!"); }

}

还要说一点让8266计算CRC,发现8266存储数据是大端模式......

好了现在测试一下用软件的按钮控制

好啦终于这完这一篇了....累..真心的累.............

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

推荐阅读更多精彩内容