Post your best Lua script examples here

User avatar
By claudiuchiru
#11173 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)
Last edited by claudiuchiru on Thu Mar 05, 2015 11:23 am, edited 1 time in total.
User avatar
By claudiuchiru
#11342 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)
User avatar
By freedom2000
#11572 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