nodemcu 例程,获取温湿度

作者:shuxiansheng | 更新时间:2020-03-01 | 浏览量:981

wifi.setmode(wifi.STATION)
cfg = {}
cfg.ssid =
cfg.pwd =
wifi.sta.config(cfg)
wifi.sta.connect(cfg)
                
device_id =
Product_id=
AuthInfo=
UPbroker =
UPport =
time1 = tmr.create()              
time2 = tmr.create()     
Temp_Humi_Pin = 5      
UPbroker =     
UPport =
     
ip = wifi.sta.getip()     
    if wifi.sta.getip() == nil then 
        print("Disconnect,Waiting For IP...")
    else
        time1:stop()     
        print("连接成功")
       
      
        client_TempHumiLed = mqtt.Client("device_id",120,"Product_id","AuthInfo",false)
        client_TempHumiLed:connect(UPbroker,UPport,0,0,function(client)
            print(" connect success!")
        end)
       
        time2:alarm(1500, tmr.ALARM_AUTO, Up_AC_Message)
    end
end
function Up_AC_Message()
    status,temp,humi,temp_dec,humi_dec = dht.read11(Temp_Humi_Pin)
    weather = {}                                                
    weather.temperature = temp    
    weather.humidity = humi    
    ok,weather_message = pcall(sjson.encode, weather)
    header = string.char(3, 0, string.len(weather_message))..weather_message
    client_TempHumiLed:publish("$dp",header,0,0,function(client)
        print("PUBLISH weather SUCCESS!")
    end)
end
time1:alarm(1000, tmr.ALARM_AUTO, Reconnect)  

评论:共3条

天从人愿 评论于:2020-03-03 09:25:08
还可以
aaronwang 评论于:2020-03-06 18:07:10
谢谢大佬
deng 评论于:2020-05-31 07:26:47
大佬,有空能帮忙解答一下吗?自己填写的那部分还不太懂,可以的话加一下好友,谢谢!969439474
返回顶部