前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >六,ESP8266 TCP Client(基于Lua脚本语言)五,ESP8266 TCP服务器多连接

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

作者头像
杨奉武
发布2018-04-18 16:14:20
1.4K0
发布2018-04-18 16:14:20
举报
文章被收录于专栏:知识分享

今天不知道是不是让我姐挺失望.......很多时候都不知道自己努力的方向对不对,,以后能不能带给家人最美好的期盼......

Init.lua 没啥改变,,就改了一下加载Client.lua

代码语言:javascript
复制
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, 3000, 0, function()
    dofile("Client.lua")
end)

新建了一个Client.lua

代码语言:javascript
复制
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)

ClientConnectedFlage = 0
TcpConnect = nil
tmr.alarm(1, 1000, 1, function()

    if  ClientConnectedFlage == 0 then
        Client = net.createConnection(net.TCP, 0) 
        Client:connect(8080,"192.168.1.103")

        Client:on("receive", function(Client, data) 
            uart.write(0,data)
        end)
        
        Client:on("connection", function(sck, c) 
            ClientConnectedFlage = 1
            TcpConnect = Client
            print("Link OK")
            tmr.stop(1)

            Client:on("disconnection", function(sck, c) 
                ClientConnectedFlage = 0
                TcpConnect = nil
                tmr.start(1)
            end)
        end)

        if  ClientConnectedFlage == 0 then
            print("Link Error")
        end
    end
end)


uart.on("data",0,function(data) 
    if  TcpConnect ~= nil then
        TcpConnect:send(data)
    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)

现在测试

现在创建服务器

发送数据测试

现在断开连接

第一次没有截到网络调试助手的图,模块就连接上了,,所以又断开了一次

 现在就用这个读取AD的电压值

命令和回复呢咱就这样规定

现在的init.lua

代码语言:javascript
复制
gpio.mode(4,gpio.OUTPUT)
gpio.mode(2,gpio.OUTPUT)
gpio.write(4,1)

if  adc.force_init_mode(adc.INIT_ADC) then
    node.restart()
    return
end

tmr.alarm(0, 1000, 1, function()
    gpio.write(4,1-gpio.read(4))
end)

tmr.alarm(1, 3000, 0, function()
    dofile("Client.lua")
end)

  现在的Client.lua

代码语言:javascript
复制
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)

ClientConnectedFlage = 0
TcpConnect = nil
tmr.alarm(1, 1000, 1, function()

    if  ClientConnectedFlage == 0 then
        Client = net.createConnection(net.TCP, 0) 
        Client:connect(8080,"192.168.1.103")

        Client:on("receive", function(Client, data) 
            uart.write(0,data)
            ReadAd(data)
        end)
        
        Client:on("connection", function(sck, c) 
            ClientConnectedFlage = 1
            TcpConnect = Client
            print("Link OK")
            tmr.stop(1)

            Client:on("disconnection", function(sck, c) 
                ClientConnectedFlage = 0
                TcpConnect = nil
                tmr.start(1)
            end)
        end)

        if  ClientConnectedFlage == 0 then
            print("Link Error")
        end
    end
end)


function ReadAd(data)
    if  data == "++MD9" then
        ad = adc.read(0)
        if  TcpConnect ~= nil then
            TcpConnect:send("++MDAD="..ad)
        end
    end
end



uart.on("data",0,function(data) 
    if  TcpConnect ~= nil then
        TcpConnect:send(data)
    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)

现在测试

 对了测试的电压是0-1V    然后分辨率是  1024

我现在接到3.3上

如果数据向加CRC校验,看这篇文章的最后

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

五,ESP8266 TCP服务器多连接

TCP 就说道这里       

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2017-09-16 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 五,ESP8266 TCP服务器多连接
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档