Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- red = 2
- green = 4
- blue = 3
- chipid = node.chipid()
- pwm.setup(red,1000,0)
- pwm.setup(green,1000,0)
- pwm.setup(blue,1000,0)
- pwm.start(red)
- pwm.start(green)
- pwm.start(blue)
- function led(r,g,b)
- pwm.setduty(red,g)
- pwm.setduty(green,b)
- pwm.setduty(blue,r)
- end
- function fromJson(MSG)
- local hashMap = {}
- for k,v in string.gmatch(MSG,'"(%w)":(%d+)') do
- hashMap[k] = v;
- -- print(k .."\t" .. v)
- end
- return hashMap
- end
- m = mqtt.Client(chipid, 120, "esp", "esp")
- m:lwt("lwt/".. chipid, chipid .." gone offline", 0, 0)
- m:on("offline", function(con)
- print ("reconnecting...")
- tmr.alarm(1, 10000, 0, function()
- m:connect("192.168.100.122", 1883, 0)
- end)
- end)
- -- on publish message receive event
- m:on("message", function(conn, topic, data)
- if data ~= nil then
- print(data)
- local rgb = fromJson(data)
- -- print(rgb.r)
- led(rgb.r*4,rgb.g*4,rgb.b*4)
- end
- end)
- tmr.alarm(0, 1000, 1, function()
- if wifi.sta.status() == 5 then
- tmr.stop(0)
- m:connect("92.168.100.122", 1883, 0, function(conn)
- -- print("connected")
- m:subscribe("actions/esp8266/#",0, function(conn)
- m:publish("events/esp8266/".. chipid ,chipid .. " become online",0,0, function(conn) print("connected") end)
- end)
- end)
- end
- end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement