美文网首页NODEMCU
六,ESP8266 TCP Client(基于Lua脚本语言)

六,ESP8266 TCP Client(基于Lua脚本语言)

作者: 杨奉武 | 来源:发表于2017-10-14 18:15 被阅读196次

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

    Init.lua 没啥改变,,就改了一下加载Client.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,3000,0, function()

    dofile("Client.lua")

    end)

    新建了一个Client.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.autoconnect(1)

    ClientConnectedFlage=0TcpConnect=nil

    tmr.alarm(1,1000,1, function()ifClientConnectedFlage ==0then

    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=1TcpConnect=Client

    print("Link OK")

    tmr.stop(1)

    Client:on("disconnection", function(sck, c)

    ClientConnectedFlage=0TcpConnect=nil

    tmr.start(1)

    end)

    end)ifClientConnectedFlage ==0then

    print("Link Error")

    end

    end

    end)

    uart.on("data",0,function(data)ifTcpConnect ~=nil then

    TcpConnect:send(data)

    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)

    现在测试

    现在创建服务器

    发送数据测试

    现在断开连接

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

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

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

    现在的init.lua

    gpio.mode(4,gpio.OUTPUT)

    gpio.mode(2,gpio.OUTPUT)

    gpio.write(4,1)ifadc.force_init_mode(adc.INIT_ADC) then

    node.restart()returnend

    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

    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=0TcpConnect=nil

    tmr.alarm(1,1000,1, function()ifClientConnectedFlage ==0then

    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=1TcpConnect=Client

    print("Link OK")

    tmr.stop(1)

    Client:on("disconnection", function(sck, c)

    ClientConnectedFlage=0TcpConnect=nil

    tmr.start(1)

    end)

    end)ifClientConnectedFlage ==0then

    print("Link Error")

    end

    end

    end)

    function ReadAd(data)ifdata =="++MD9"then

    ad= adc.read(0)ifTcpConnect ~=nil then

    TcpConnect:send("++MDAD="..ad)

    end

    end

    end

    uart.on("data",0,function(data)ifTcpConnect ~=nil then

    TcpConnect:send(data)

    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)

    现在测试

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

    我现在接到3.3上

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

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

    五,ESP8266 TCP服务器多连接

    TCP 就说道这里

    相关文章

      网友评论

      • loser未来的:杨哥你好,小弟最近也在搞8266 在查看你的教程后收益匪浅,但也发现你的代码存在一些不合理的地方,wifi.eventmon.register(wifi.eventmon.STA_GOT_IP, function(T)。你注册了一个wifi获得ip的监听事件,也就说在8266获得IP之后就回调这个函数,所以你的代码在之前连接TCP的动作时无用的!也就是为什莫会出现连接失败的报错,正确的顺序时应该在获得ip之后才开始连接TCP的 ,也就时在连接TCP的服务应该在这个回调函数之内进行,才能避免多次打印连接失败,更多的相关代码可以看一下我的代码。
        杨奉武:@loser未来的 也有可能是用手机连接WIFI模块的无线然后建立TCP服务,就不连接路由器了

      本文标题:六,ESP8266 TCP Client(基于Lua脚本语言)

      本文链接:https://www.haomeiwen.com/subject/omlfuxtx.html