五,ESP8266 TCP服務(wù)器多連接(基于Lua腳本語(yǔ)言)

想問(wèn)一件事情--簡(jiǎn)書(shū),,,代碼有沒(méi)有專(zhuān)門(mén)的顯示的..比如說(shuō)插入代碼什么的????


一些時(shí)間去準(zhǔn)備朋友的元器件了...

接著寫(xiě),,爭(zhēng)取今天寫(xiě)完所有的文章,,因?yàn)榇饝?yīng)了朋友下周5之前要做好朋友的東西

對(duì)于TCP大家在玩AT指令的時(shí)候有沒(méi)有發(fā)現(xiàn)客戶端最多連接5個(gè),,,再連接就不行了??

所以在用AT指令開(kāi)發(fā)的時(shí)候單片機(jī)程序一定要記得清除多余的連接

現(xiàn)在看用LUA語(yǔ)言怎么做

直接先上菜

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就不說(shuō)了

cfg={}

cfg.ssid="Hellow8266"cfg.pwd="11223344"wifi.ap.config(cfg)--設(shè)置模塊的無(wú)線和密碼

apcfg={}

apcfg.ssid="qqqqq"apcfg.pwd="11223344"wifi.sta.config(apcfg)--設(shè)置模塊連接路由器的無(wú)線和密碼--wifi.sta.connect()連接路由器,斷開(kāi)后可能就不自動(dòng)連接了,可以用下面的

wifi.sta.autoconnect(1)可以用這個(gè)斷開(kāi)后自動(dòng)連接路由器

TCPSever=net.createServer(net.TCP,28800) --創(chuàng)建服務(wù)器超過(guò)28800S不通信斷開(kāi)已有的連接

TcpConnectCnt=0--連接個(gè)數(shù)計(jì)數(shù)

TcpSocketTable={}--存儲(chǔ)socket

TCPSever:listen(8080,function(socket)

如果0號(hào)連接就把1號(hào)關(guān)掉,,,1號(hào)連接就把2號(hào)關(guān)掉....4號(hào)連接就把0號(hào)關(guān)掉,這樣子循環(huán),

當(dāng)然您會(huì)問(wèn)可以連接5個(gè),,這樣子只可以連接四個(gè)了,,,為什么....因?yàn)槿绻B接了5個(gè)就進(jìn)不

來(lái)這個(gè)監(jiān)聽(tīng)函數(shù)了.......所以必須留下一個(gè)空位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存到數(shù)組

print(TcpConnectCnt.."-Connect")--打印幾號(hào)連接了? 對(duì)了 .. 是連接符

TcpConnectCnt= TcpConnectCnt +1--連接個(gè)數(shù)加一ifTcpConnectCnt ==5then --歸零

TcpConnectCnt=0end

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

uart.write(0,data) --把接收到的數(shù)據(jù)發(fā)到串口

end)

socket:on("disconnection",function(sck,c)fori=0,4do--判斷是哪個(gè)斷開(kāi)了連接,,就把對(duì)應(yīng)的socket變量置為 nilifTcpSocketTable[i] ==sck then

TcpSocketTable[i]=nil

print(i.."-Disconnect")

end

end

end)

end)

uart.on("data",0,function(data)fori=0,4do--把串口的數(shù)據(jù)發(fā)向不為 nil 的連接ifTcpSocketTable[i] ~=nil then

TcpSocketTable[i]:send(data)

end

end

end,0)--下面是連接路由器和沒(méi)有連接路由器的監(jiān)聽(tīng)函數(shù),,好像是1S檢測(cè)一次

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)

現(xiàn)在把程序下進(jìn)去

測(cè)試軟件呢在這里下載--也可以自己下載個(gè)網(wǎng)絡(luò)調(diào)試助手

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

連接了路由器了....

第一個(gè)連接

測(cè)試數(shù)據(jù)

再來(lái)幾個(gè)連接

現(xiàn)在再連接一個(gè)

我現(xiàn)在隨意斷開(kāi)一個(gè),看一看串口應(yīng)該打印哪一個(gè)斷開(kāi)了連接

現(xiàn)在發(fā)數(shù)據(jù)

好現(xiàn)在呢控制就用多個(gè)客戶端控制繼電器


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

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

這篇文章最后的指令,,畢竟最終咱們就要做成那樣子.....這樣的話8266就應(yīng)該使用的非常順手和輕松了

對(duì)了在

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

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

這篇兩文章中有提及......這次是用多個(gè)TCP客戶端控制,,還會(huì)加上CRC校驗(yàn)

可以直接用軟件上的這兩個(gè)按鈕控制

先寫(xiě)不帶CRC校驗(yàn)數(shù)據(jù)的

為了是程序好瀏覽,,就定義一個(gè)函數(shù),,然后調(diào)用

現(xiàn)在的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)

好現(xiàn)在控制一下

現(xiàn)在用另一個(gè)斷開(kāi)

對(duì)了最好把信息發(fā)給網(wǎng)絡(luò)對(duì)不對(duì),,,控制一下最好通過(guò)網(wǎng)絡(luò)有一個(gè)回復(fù)

現(xiàn)在的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)

好了現(xiàn)在加入CRC16校驗(yàn)數(shù)據(jù)

現(xiàn)在的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)

他呢,,參數(shù)要填寫(xiě)字符串

上位機(jī)的CRC計(jì)算函數(shù)

//////////////////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;

}

我的置高繼電器函數(shù)

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)//網(wǎng)絡(luò)發(fā)送{

TcpSendDataMethod(sendbyte);

}elseif(radioButtonSerialCon.Checked)//串口發(fā)送{

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);//計(jì)算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("請(qǐng)檢查連接","提示!"); }

}

還要說(shuō)一點(diǎn)讓8266計(jì)算CRC,發(fā)現(xiàn)8266存儲(chǔ)數(shù)據(jù)是大端模式......

好了現(xiàn)在測(cè)試一下用軟件的按鈕控制

好啦終于這完這一篇了....累..真心的累.............

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 228,786評(píng)論 6 534
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡,警方通過(guò)查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 98,656評(píng)論 3 419
  • 文/潘曉璐 我一進(jìn)店門(mén),熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人,你說(shuō)我怎么就攤上這事。” “怎么了?”我有些...
    開(kāi)封第一講書(shū)人閱讀 176,697評(píng)論 0 379
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)。 經(jīng)常有香客問(wèn)我,道長(zhǎng),這世上最難降的妖魔是什么? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 63,098評(píng)論 1 314
  • 正文 為了忘掉前任,我火速辦了婚禮,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 71,855評(píng)論 6 410
  • 文/花漫 我一把揭開(kāi)白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上,一...
    開(kāi)封第一講書(shū)人閱讀 55,254評(píng)論 1 324
  • 那天,我揣著相機(jī)與錄音,去河邊找鬼。 笑死,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,322評(píng)論 3 442
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起,我...
    開(kāi)封第一講書(shū)人閱讀 42,473評(píng)論 0 289
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒(méi)想到半個(gè)月后,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 49,014評(píng)論 1 335
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 40,833評(píng)論 3 355
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 43,016評(píng)論 1 371
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,568評(píng)論 5 362
  • 正文 年R本政府宣布,位于F島的核電站,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 44,273評(píng)論 3 347
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 34,680評(píng)論 0 26
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 35,946評(píng)論 1 288
  • 我被黑心中介騙來(lái)泰國(guó)打工, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 51,730評(píng)論 3 393
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 48,006評(píng)論 2 374

推薦閱讀更多精彩內(nèi)容