https://www.cnblogs.com/yangfengwu/p/7524326.html
一些時間去准備朋友的元器件了...
接着寫,,爭取今天寫完所有的文章,,因為答應了朋友下周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 = 0 TcpSocketTable={} TCPSever:listen(8080,function(socket) if TcpConnectCnt == 4 then if TcpSocketTable[0] ~= nil then TcpSocketTable[0]:close() TcpSocketTable[0] = nil end else if TcpSocketTable[TcpConnectCnt+1] ~= nil then TcpSocketTable[TcpConnectCnt+1]:close() TcpSocketTable[TcpConnectCnt+1] = nil end end TcpSocketTable[TcpConnectCnt] = socket print(TcpConnectCnt.."-Connect") TcpConnectCnt = TcpConnectCnt + 1 if TcpConnectCnt == 5 then TcpConnectCnt = 0 end socket:on("receive",function(socket,data) uart.write(0,data) end) socket:on("disconnection",function(sck,c) for i=0,4 do if TcpSocketTable[i] == sck then TcpSocketTable[i] = nil print(i.."-Disconnect") end end end) end) uart.on("data",0,function(data) for i=0,5 do if TcpSocketTable[i] ~= nil then TcpSocketTable[i]:send(data) end end end, 0) printip = 0 wifi.eventmon.register(wifi.eventmon.STA_DISCONNECTED, function(T) printip = 0 end) wifi.eventmon.register(wifi.eventmon.STA_GOT_IP, function(T) if printip == 0 then print("+IP"..T.IP) end printip = 1 end)
下面看解釋.............
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個就進不 來這個監聽函數了.......所以必須留下一個空位 if TcpConnectCnt == 4 then if TcpSocketTable[0] ~= nil then TcpSocketTable[0]:close() TcpSocketTable[0] = nil end else if TcpSocketTable[TcpConnectCnt+1] ~= nil then TcpSocketTable[TcpConnectCnt+1]:close() TcpSocketTable[TcpConnectCnt+1] = nil end end TcpSocketTable[TcpConnectCnt] = socket--把連接的socket存到數組 print(TcpConnectCnt.."-Connect")--打印幾號連接了 對了 .. 是連接符 TcpConnectCnt = TcpConnectCnt + 1--連接個數加一 if TcpConnectCnt == 5 then --歸零 TcpConnectCnt = 0 end socket:on("receive",function(socket,data) uart.write(0,data) --把接收到的數據發到串口 end) socket:on("disconnection",function(sck,c) for i=0,4 do --判斷是哪個斷開了連接,,就把對應的socket變量置為 nil if TcpSocketTable[i] == sck then TcpSocketTable[i] = nil print(i.."-Disconnect") end end end) end) uart.on("data",0,function(data) for i=0,4 do --把串口的數據發向不為 nil 的連接 if TcpSocketTable[i] ~= nil then TcpSocketTable[i]:send(data) end end end, 0) --下面是連接路由器和沒有連接路由器的監聽函數,,好像是1S檢測一次 printip = 0 wifi.eventmon.register(wifi.eventmon.STA_DISCONNECTED, function(T) printip = 0 end) wifi.eventmon.register(wifi.eventmon.STA_GOT_IP, function(T) if printip == 0 then print("+IP"..T.IP) end printip = 1 end)
現在把程序下進去
測試軟件呢在這里下載--也可以自己下載個網絡調試助手
https://item.taobao.com/item.htm?spm=686.1000925.0.0.4a155084jlU4Rd&id=558508797404
連接了路由器了....
第一個連接
測試數據
再來幾個連接
現在再連接一個
我現在隨意斷開一個,看一看串口應該打印哪一個斷開了連接
現在發數據
好啦終於這完這一篇了....累..真心的累.............
https://www.cnblogs.com/yangfengwu/p/7533302.html