1/ topic "lumina" for receive command message
2/ topic for confirmation command "lumina\raspuns"
the message must be between 2...100 for PWM , 0 and 1 its for Start / Stop command
-- init.lua
wifi.setmode(wifi.STATION)
wifi.sta.config("WIFI","PASSWORD")
gpio.mode(4,gpio.OUTPUT)
gpio.write(4,gpio.LOW)
setpower=0
m = mqtt.Client("ESP1", 120, "", "")
m:lwt("/lwt", "offline", 0, 0)
m:on("offline", function(con)
print ("reconnecting...")
print(node.heap())
tmr.alarm(1, 10000, 0, function()
m:connect("192.168.3.200", 1883, 0)
end)
end)
m:on("message", function(conn, topic, msg)
print("Recieved:" .. topic .. ":" .. msg)
m:publish("lumina/raspuns",msg,0,0)
if (msg=="1") then
print("Door Open")
pwm.close(4)
tmr.delay(1000000)
gpio.write(4,gpio.HIGH)
elseif (msg=="0") then
print("Door Closed")
pwm.close(4)
tmr.delay(1000000)
gpio.write(4,gpio.LOW)
else
pwm.close(4)
setpower=tonumber(msg)
pwm.setup(4,1000,setpower*10)
pwm.start(4)
print("PWM")
end
end)
tmr.alarm(0, 1000, 1, function()
if wifi.sta.status() == 5 then
tmr.stop(0)
m:connect("192.168.3.200", 1883, 0, function(conn)
print("connected")
m:subscribe("lumina",0, function(conn)
end)
end)
end
end)