<noscript id="mmkmi"><source id="mmkmi"></source></noscript>
  • <noscript id="mmkmi"><kbd id="mmkmi"></kbd></noscript>
  • <table id="mmkmi"><source id="mmkmi"></source></table>
  • Tison溫度濕度LED控制的詳細介紹(基于nodemcu編程)

    作者:zls121zls | 更新時間:2016-12-08 | 瀏覽量:5780

    控制界面:http://www.eqytg.org/Panel/index/id/134.html

    詳細介紹:http://www.eqytg.org/info/822.html

    一:準備工作

    環境搭建:

    1、PC端軟件,需要安裝java   ESPlorer

    2、固件燒錄工具:esp8266 flash升級燒寫燒錄工具v2.1綠色版

    3、燒寫教程:參考difiot

    4、以上所有資料訪問百度云盤

     

    基于nodemuc編程API說明中文版本

    https://github.com/nodemcu/nodemcu-firmware/wiki/nodemcu_api_cn

    NodeMCU所有資料和源碼

    https://github.com/nodemcu

    基礎編程手冊

    http://manual.luaer.cn/

     

    二:硬件介紹

    三:相關源碼

    軟件管腳解讀:NODEMCU對應TISON開發板ESP8266管腳關系,如下圖

    TISON開發板
    NODEMCU引腳  TISON開發板 輸入輸出 開發板標識 連接關系
    IO6 GPIO12 輸入 photoR 連接Tison ESP8266光敏管
    IO6 GPIO12 輸出 REALY 連接Tison ESP8266繼電器
    IO6 GPIO12 輸入輸出 DHT 連接Tison ESP8266溫濕度傳感器
    IO7 GPIO13 輸出 LEDB 連接Tison ESP8266開發板藍色LED,藍色燈亮起進入ESPTOUCH模式,配置完成關閉
    IO5 GPIO14 輸出 LEDR 連接Tison ESP8267開發板紅色LED,紅色燈亮起表示故障,故障排除關閉
    IO8 GPIO15 輸出 LEDG 連接Tison ESP8268開發板綠色LED,綠色燈亮起進入AIRKISS模式,配置完成關閉

     

    --init.lua
    print("set up wifi mode")
    wifi.setmode(wifi.STATION)
    wifi.sta.config("JSZLZXBGS","jszlzx123")
    --here SSID and PassWord should be modified according your wireless router
    wifi.sta.connect()
    tmr.alarm(1, 1000, 1, function()
        if wifi.sta.getip()== nil then
            print("IP unavaiable, Waiting...")
        else
            tmr.stop(1)
            print("IP:"..wifi.sta.getip())
            dofile("runtime.lua")     
        end
    end)

     

    dofile("config.lua")
    dofile("Dht.lua")
    dofile("SendData.lua")

    bLive=0
    bOnLine=0

    cu = net.createConnection(net.TCP)
    cu:connect(config.port, config.host)

    cu:on("receive", function(cu, c) 
        print(c)
        r = cjson.decode(c)
        --如果存活標記為1,置為0
        if r.M=="isOL" then
            bLive=0 
        end
        tmr.alarm(1, 10000, 1, function()
            --checkin和心跳
            dofile("checkIn.lua")
            --在線后再讀取數據,發送年,接收命令
            if bOnLine==1 then
                print("start send data")
                --讀取濕度      
                Humi,Temp=ReadDHT(config.DHTPin)         
                --發送數據
                sendToBigiot(cu,Humi,Temp)
            end
            --執行命令
            dofile("sayCommand.lua")
            
        end)
    end)

    --modify DEVICEID1 INPUTID APIKEY DEVICEID2
    config={
    host = host or "www.eqytg.org",
    port = port or 8181,
    DEVICEID = "822",
    UID=" ",
    TempID=" ",
    HumiID=" ",
    APIKEY = " ",
    DHTPin= 6,
    LEDPin= 8
    }

    --收到連接正常,發送checkin
    if r.M == "WELCOME TO BIGIOT" then
        ok, s = pcall(cjson.encode, {M="checkin",ID=config.DEVICEID,K=config.APIKEY})
        if ok then
          print(s)
        else
          print("failed to encode!")
        end
        cu:send( s.."\n" )
        bLive=0
        --定時心跳,防止掉線
        tmr.alarm(2, 40000, 1, function()
            --如果標記為1,表示未收到上次的心跳返回,重啟
            if bLive==3 then
                node.restart()
            end
            ok, ticket = pcall(cjson.encode, {M="isOL",ID="D"..config.DEVICEID})
            print(ticket)
            cu:send(ticket.."\n" )
            --發送后將標記置為1
            bLive=bLive+1        
        end)
    end
    if r.M=="checkinok" then
        bOnLine=1
    end


    function ReadDHT(pin)
        dhstatus, temp, humi, temp_dec, humi_dec = dht.read11(pin)
        print(dhstatus)
        if dhstatus == dht.OK then         
            print(string.format("DHT Temperature:%d.%01d;Humidity:%d.%01d\r\n",
            math.floor(temp),
            temp_dec,
            math.floor(humi),
            humi_dec
            ))
            --轉換實際溫度
            realTemp=math.floor(temp)
            --轉換實際濕度
            realhumi=math.floor(humi)
            
            return realhumi,realTemp
            
        elseif status == dht.ERROR_CHECKSUM then
            print( "DHT Checksum error." )
        elseif status == dht.ERROR_TIMEOUT then
            print( "DHT timed out." )
        end
    end

    function sendToBigiot(cu,humi,temp)
        print(humi)
        print(temp)
         
        if humi==nil then 
            humi=0
        end
        
        if temp==nil then
            temp=0
        end
        --上報濕度
        local v = {[config.TempID]=string.format("%d", math.floor(temp)),[config.HumiID]=string.format("%d",math.floor(humi))}              
        ok3, s3 = pcall(cjson.encode, {M="update",ID=config.DEVICEID,V=v})
        print("send data:"..s3)
        cu:send( s3.."\n")
    end

     --如果是say代表命令

    LEDG = 5
    LEDR = 7
    LEDB = 8

    gpio.mode(LEDG,gpio.OUTPUT)
    gpio.mode(LEDR,gpio.OUTPUT)
    gpio.mode(LEDB,gpio.OUTPUT)


    gpio.write(LEDG, gpio.LOW)
    gpio.write(LEDR, gpio.HIGH)
    gpio.write(LEDB, gpio.HIGH)
     
    if r.M == "say" then     
        local commander=r.C 
        if commander == "play" then     
            gpio.write(LEDG, gpio.LOW) 
            gpio.write(LEDR, gpio.LOW)
            gpio.write(LEDB, gpio.LOW) 
            ok, played = pcall(cjson.encode, {M="say",ID=config.UID,C="LED turn on!"})
            cu:send( played.."\n" )
        elseif commander == "stop" then     
            gpio.write(LEDG, gpio.HIGH)
            gpio.write(LEDR, gpio.HIGH)
            gpio.write(LEDB, gpio.HIGH)
            ok, stoped = pcall(cjson.encode, {M="say",ID=config.UID,C="LED turn off!"})
            cu:send( stoped.."\n" )  
        end  
    end

     

    四:相關資料參考

    經典的NodeMCU lua教程,值得好好看看 

    http://www.electrodragon.com/w/ESP8266_NodeMCU_Lua

    http://www.nodemcu.com/index_cn.html

    http://nodemcu.readthedocs.io/en/master/

    http://www.electrodragon.com/smartconfig-nodemcu/

    http://www.zhihu.com/question/36288709

    http://blog.csdn.net/leytton/article/details/51723221

    http://www.tinylab.org/nodemcu-kickstart/

    http://my.oschina.net/u/2306127/blog/402931

    http://www.esp8266.com/viewforum.php?f=17

    http://nodemcu-dev.doit.am/index.html

    https://github.com/SmartArduino/Doit_Cloud

    http://www.wifimcu.com

    http://nodemcu.doit.am/

    http://www.esp8266.com

    http://git.oschina.net/HEKRCLOUD/hekr-esp8266-sdk-ra

    NodeMCU固件編譯環境搭建

    http://blog.csdn.net/free0loop/article/details/48518729


    評論:共4條

    貝殼物聯 評論于:2016-10-22 14:55:14
    灰常詳細,謝謝分享-_-!!!
    coldfish 評論于:2016-11-18 10:45:22
    好貼,大有用處
    沃斯托克 評論于:2017-07-08 11:35:36
    你好,根據你的代碼含cjson的固件可以實現,現在新生成的含sjon的固件,把代碼中cjson改為sjson,有錯誤,找了很多天,不知道什么原因,能否幫我解答下,謝謝
    沃斯托克 評論于:2017-07-08 11:37:01
    固件生成網址https://nodemcu-build.com/
    返回頂部
    <noscript id="mmkmi"><source id="mmkmi"></source></noscript>
  • <noscript id="mmkmi"><kbd id="mmkmi"></kbd></noscript>
  • <table id="mmkmi"><source id="mmkmi"></source></table>
  • 三上悠亚在线观看