Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- pinblue = 3
- pinred = 4
- gpio.mode(pinblue,gpio.OUTPUT)
- gpio.mode(pinred,gpio.OUTPUT)
- gpio.write(pinblue,gpio.LOW)
- gpio.write(pinred,gpio.LOW)
- chipid = node.chipid()
- m = mqtt.Client(chipid, 120, "esp", "esp")
- m:lwt("/lwt", wifi.sta.getmac(), 0, 0)
- m:on("offline", function(con)
- print ("reconnecting...")
- print(node.heap())
- tmr.alarm(1, 10000, 0, function()
- m:connect("192.168.100.45", 1883, 0)
- end)
- end)
- -- on publish message receive event
- m:on("message", function(conn, topic, data)
- print(topic .. ":" )
- if data ~= nil then
- print(data)
- end
- if string.match(topic,'red') then
- pin = pinblue
- end
- if string.match(topic,'blue') then
- pin = pinred
- end
- if data == "On" then
- gpio.write(pin,gpio.HIGH)
- end
- if data == "Off" then
- gpio.write(pin,gpio.LOW)
- end
- end)
- tmr.alarm(0, 1000, 1, function()
- if wifi.sta.status() == 5 then
- tmr.stop(0)
- m:connect("92.168.100.45", 1883, 0, function(conn)
- print("connected")
- m:subscribe("actions/#",0, function(conn)
- m:publish("esp8266/".. chipid ,chipid,0,0, function(conn) print("sent") end)
- end)
- end)
- end
- end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement