Advertisement
pastukhov

Untitled

Jan 26th, 2015
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.21 KB | None | 0 0
  1. pinblue = 3
  2. pinred = 4
  3.  
  4. gpio.mode(pinblue,gpio.OUTPUT)
  5. gpio.mode(pinred,gpio.OUTPUT)
  6. gpio.write(pinblue,gpio.LOW)
  7. gpio.write(pinred,gpio.LOW)
  8.  
  9. chipid = node.chipid()
  10.  
  11. m = mqtt.Client(chipid, 120, "esp", "esp")
  12.  
  13. m:lwt("/lwt", wifi.sta.getmac(), 0, 0)
  14.  
  15. m:on("offline", function(con)
  16.      print ("reconnecting...")
  17.      print(node.heap())
  18.      tmr.alarm(1, 10000, 0, function()
  19.           m:connect("192.168.100.45", 1883, 0)
  20.      end)
  21. end)
  22.  
  23. -- on publish message receive event
  24. m:on("message", function(conn, topic, data)
  25.   print(topic .. ":" )
  26.   if data ~= nil then
  27.     print(data)
  28.   end
  29.   if string.match(topic,'red') then
  30.      pin = pinblue
  31.   end
  32.   if string.match(topic,'blue') then
  33.      pin = pinred
  34.   end
  35.   if  data == "On" then
  36.      gpio.write(pin,gpio.HIGH)
  37.   end
  38.   if  data == "Off" then
  39.      gpio.write(pin,gpio.LOW)
  40.   end
  41.  
  42. end)
  43.  
  44. tmr.alarm(0, 1000, 1, function()
  45.  if wifi.sta.status() == 5 then
  46.      tmr.stop(0)
  47.      m:connect("92.168.100.45", 1883, 0, function(conn)
  48.           print("connected")
  49.           m:subscribe("actions/#",0, function(conn)
  50.           m:publish("esp8266/".. chipid ,chipid,0,0, function(conn) print("sent") end)
  51.           end)
  52.      end)
  53.  end
  54. end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement