Tison温度湿度LED控制

Tison温度湿度LED控制的详细介绍

创作者:zls121zls | 更新日期:2020-09-22 | 在线时长:111天
Tison温度湿度LED控制,有源码哦-_-!!!

控制界面:http://www.bigiot.net/Panel/index/id/134.html

详细介绍:http://www.bigiot.net/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)

-----runtime.lua---------------------------------------------------

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

bLive=0
bOnLine=0

gpio.mode(config.LEDG,gpio.OUTPUT)
gpio.mode(config.LEDR,gpio.OUTPUT)
gpio.mode(config.LEDB,gpio.OUTPUT)
gpio.write(config.LEDG,gpio.LOW) 
gpio.write(config.LEDR,gpio.LOW) 
gpio.write(config.LEDG,gpio.LOW) 

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
    end)
    --执行命令
    dofile("sayCommand.lua")
end)

--modify DEVICEID1 INPUTID APIKEY DEVICEID2
config={
host = host or "www.bigiot.net",
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

 --sayCommand.lua----------------------------------------------------

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

if r.M == "say" then     
    local commander=r.C    
    if commander == "play" then  
        print("play:LEDG turn on!")    
        gpio.write(config.LEDG,gpio.LOW) 
        ok, played = pcall(cjson.encode, {M="say",ID=r.ID,C="LEDG turn on!"})
        cu:send( played.."\n" )
    elseif commander == "stop" then 
        print("play:LEDG turn off!")         
        gpio.write(config.LEDG,gpio.HIGH)
        ok, stoped = pcall(cjson.encode, {M="say",ID=r.ID,C="LEDG turn off!"})
        cu:send( stoped.."\n" )     
   elseif commander == "up" then 
        print("up:LEDR turn on!") 
        gpio.write(config.LEDR,gpio.LOW)
        ok, uped = pcall(cjson.encode, {M="say",ID=r.ID,C="LEDR turn off!"})
        cu:send( uped.."\n" )  
   elseif commander == "plus" then  
        print("plus:LEDR turn off!")       
        gpio.write(config.LEDR,gpio.HIGH)
        ok, plused = pcall(cjson.encode, {M="say",ID=r.ID,C="LEDR turn off!"})
        cu:send( plused.."\n" )  
   elseif commander == "pause" then  
        print("pause:LEDB turn on!") 
        gpio.write(config.LEDB,gpio.LOW)
        ok, paused = pcall(cjson.encode, {M="say",ID=config.DEVICEID,C="LEDB turn off!"})
        cu:send( paused.."\n" )  
   elseif commander == "right" then 
        print("right:LEDB turn off!")        
        gpio.write(config.LEDB,gpio.HIGH)
        ok, righted = pcall(cjson.encode, {M="say",ID=config.DEVICEID,C="LEDB turn off!"})
        cu:send( righted.."\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