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)