1. 程式人生 > >六,ESP8266 TCP Client

六,ESP8266 TCP Client

服務 output post ive nil 1-1 創建 end nbsp

今天不知道是不是讓我姐挺失望.......很多時候都不知道自己努力的方向對不對,,以後能不能帶給家人最美好的期盼......

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 = 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

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

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 就說道這裏

六,ESP8266 TCP Client