Advertisement
pastukhov

Untitled

Apr 23rd, 2015
254
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.34 KB | None | 0 0
  1. red = 2
  2. green = 4
  3. blue = 3
  4.  
  5. chipid = node.chipid()
  6.  
  7. pwm.setup(red,1000,0)
  8. pwm.setup(green,1000,0)
  9. pwm.setup(blue,1000,0)
  10. pwm.start(red)
  11. pwm.start(green)
  12. pwm.start(blue)
  13.  
  14. function led(r,g,b)
  15.      pwm.setduty(red,g)
  16.      pwm.setduty(green,b)
  17.      pwm.setduty(blue,r)
  18. end
  19. function fromJson(MSG)
  20.    local hashMap = {}
  21.    for k,v in string.gmatch(MSG,'"(%w)":(%d+)') do
  22.       hashMap[k] = v;
  23. --      print(k .."\t" .. v)
  24.    end
  25.    return hashMap
  26. end
  27.  
  28. m = mqtt.Client(chipid, 120, "esp", "esp")
  29.  
  30. m:lwt("lwt/".. chipid, chipid .." gone offline", 0, 0)
  31.  
  32. m:on("offline", function(con)
  33.      print ("reconnecting...")
  34.      tmr.alarm(1, 10000, 0, function()
  35.           m:connect("192.168.100.122", 1883, 0)
  36.      end)
  37. end)
  38.  
  39. -- on publish message receive event
  40. m:on("message", function(conn, topic, data)
  41.   if data ~= nil then
  42.    print(data)
  43.    local rgb = fromJson(data)
  44. --     print(rgb.r)
  45.    led(rgb.r*4,rgb.g*4,rgb.b*4)
  46.   end
  47.  
  48. end)
  49.  
  50. tmr.alarm(0, 1000, 1, function()
  51.  if wifi.sta.status() == 5 then
  52.      tmr.stop(0)
  53.      m:connect("92.168.100.122", 1883, 0, function(conn)
  54. --          print("connected")
  55.           m:subscribe("actions/esp8266/#",0, function(conn)
  56.           m:publish("events/esp8266/".. chipid ,chipid .. " become online",0,0, function(conn) print("connected") end)
  57.           end)
  58.      end)
  59.  end
  60. end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement