-->
Page 1 of 2

MQTT on firmware 12.02 with 3.1.1 broker- light with PWM

PostPosted: Tue Mar 03, 2015 9:33 am
by claudiuchiru
You must have 3.1.1 broker not 3.1.

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)

Re: MQTT on firmware 12.02 width 3.1.1 broker- light with P

PostPosted: Wed Mar 04, 2015 6:00 pm
by rutierut
Thank you so much for this, was looking for an example off nodemcu and mqtt, tnx!

Re: MQTT on firmware 12.02 width 3.1.1 broker- light with P

PostPosted: Thu Mar 05, 2015 1:22 am
by claudiuchiru
Update 5.03.2015

on reconnect subscribe to topic ...



wifi.setmode(wifi.STATION)
wifi.sta.config("WIFI","PASSWORD")

gpio.mode(4,gpio.OUTPUT)
gpio.write(4,gpio.LOW)
setpower=0

m = net.createConnection(net.TCP, 0)
m = mqtt.Client("ESP1", 120, "", "")
m:lwt("/lwt", "offline", 0, 0)

m:on("offline", function(con)
print ("reconnecting...")
tmr.alarm(1, 40000, 1, function()
m:connect("192.168.3.1", 1883, 0)
print(wifi.sta.getip())
m:subscribe("lumina",0, function(conn)
print("subscribed")

end)
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.1", 1883, 0, function(conn)
print("connected")

m:subscribe("lumina",0, function(conn)
print("subscribed")
end)

end)
end
end)

Re: MQTT on firmware 12.02 with 3.1.1 broker- light with PW

PostPosted: Sun Mar 08, 2015 4:58 am
by freedom2000
Hi,

Thank you for this good example.
I have created an account on CloudMQTT and twiked the code --> it works very well.

I have then tried to encapsulate this "MQTT.lua" fil einto an init.lua to have have re run if any crash occurs (which was the case when connection breaks).

Here is my code

Code: Select all-- init.lua
wifi.setmode(wifi.STATION)
wifi.sta.config("MySSID","MyPassword")
tmr.delay(5000000)
print(wifi.sta.getip())
dofile('MQTT.lua')


This code never works it crashes on the dofile('MQTT.lua') statement

Do you have any idea why ?

Thanks
JP