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

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

 

posted on 2017-09-16 16:40  广源时代  阅读(4782)  评论(1编辑  收藏  举报

导航

支付宝 QQ群