( Kodest | 2016. 02. 29., h – 20:50 )

A dht már hetek óta stabil, az adc-re kötött fotoellenállás szintén. Pár napja a relé is gond nélkül megy. Nem a nodemcu boarddal csináltam csak egy natúr esp12e van összeforrasztva.


wifi.setmode(wifi.STATION)
wifi.setphymode(wifi.PHYMODE_N)
wifi.sta.config("apname", "appasswd")

relay = gpio.LOW
gpio.mode(8, gpio.OUTPUT)

m = mqtt.Client("mymqttendpoint", 120, "", "")
conn = false

function tick()
if wifi.sta.status() ~= 5 then
conn = false
return
end
if not conn then
m:connect("test.mosquitto.org", 1883)
return
end
s, t, h, td, hd = dht.readxx(4)
if s == dht.OK then
m:publish("mysensor/esp1/temperature", t.."."..td, 0, 0)
m:publish("mysensor/esp1/humidity", h.."."..hd, 0, 0)
end
m:publish("mysensor/esp1/light", adc.read(0)..".0", 0, 0)
end

m:on("connect", function(c)
conn = true
m:subscribe("mysensor/esp1/relay", 0)
tick()
end)

m:on("offline", function(c)
m:close()
conn = false
end)

m:on("message", function(c, topic, data)
if data == "on" then
relay = gpio.HIGH
elseif data == "off" then
relay = gpio.LOW
elseif data == "toggle" then
if relay == gpio.LOW then
relay = gpio.HIGH
else
relay = gpio.LOW
end
end
gpio.write(8, relay)
end)

tmr.alarm(0, 10000, tmr.ALARM_AUTO, tick)