Guest User

ESP MQTT

a guest
Sep 7th, 2015
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.67 KB | None | 0 0
  1. topic = "/dim/".. wifi.sta.getmac() .. "/"
  2. broker_addr = "mqtt-host.lan"
  3. broker_port = 1883
  4.  
  5. pwm.setup(3, 100, 001)
  6. pwm.setup(4, 100, 001)
  7. pwm.start(3)
  8. pwm.start(4)
  9.  
  10. led1_current=005
  11. led1_target=005
  12. led1_fadetimer = 0
  13. led1_diff = 0
  14. led1_step = 0
  15. led1_update_time = 20
  16.  
  17. led2_current=005
  18. led2_target=005
  19. led2_fadetimer = 0
  20. led2_diff = 0
  21. led2_step = 0
  22. led2_update_time = 20
  23.  
  24. m = mqtt.Client(wifi.sta.getmac(), 120)
  25. m:lwt("/lwt", wifi.sta.getmac(), 0, 0)
  26. m:on("offline", function(con)
  27.      print ("reconnecting...")
  28.      print(node.heap())
  29.      tmr.alarm(1, 10000, 0, function()
  30.        m:connect(broker_addr, broker_port, 0, function(conn)
  31.           print("reconnected")
  32.           m:subscribe(topic ,0, function(conn)
  33.                print("subbed to " .. topic)
  34.                m:publish(topic.."status/","leddim reconnected",0,0, function(conn) print("sent sub") end)
  35.           end)
  36.        end)
  37.      end)
  38. end)
  39. -- on publish message receive event
  40. m:on("message", function(conn, topic, data)
  41.   print(topic .. ":" )
  42.   if data ~= nil then
  43.     print(data)
  44.     payload = cjson.decode(data)
  45.  
  46.  
  47.   print("Input:"..data)
  48.  
  49.     if payload['led'] == 1 then
  50.       led1_target=tonumber(payload['l'])
  51.       print("Received LED1 Target Value: "..led1_target)
  52.  
  53.       led1_fadetimer=tonumber(payload['ft'])
  54.  
  55.       print("Received LED1 Target Value: "..led1_target)
  56.       print("Received LED1 Current Value: " .. led1_current)
  57.  
  58.       print ("Received LED1 Fadetimer: " ..led1_fadetimer)
  59.       led1_diff=(led1_target)-(led1_current)
  60.      
  61.       -- Get steps / second (on 10Hz).
  62.       led1_step = led1_diff / (led1_fadetimer/led1_update_time);
  63.    
  64.      
  65.       print("Step value: "..led1_step);
  66.  
  67.  
  68.       tmr.alarm(1, led1_update_time, 1, function()
  69.        
  70.         if led1_step > 0 and led1_current + led1_step < led1_target then
  71.            led1_current = led1_current + led1_step;
  72.         elseif led1_step < 0 and led1_current + led1_step > led1_target then
  73.           led1_current = led1_current + led1_step;
  74.         else
  75.           led1_current = led1_target;
  76.          
  77.           tmr.stop(1)
  78.         end;
  79.        
  80.         pwm.setduty(3, led1_current)
  81.        
  82.       end )
  83.     end
  84.  
  85.     if payload['led'] == 2 then
  86.    
  87.       led2_target=tonumber(payload['l'])
  88.       print("Received LED2 Target Value: "..led2_target)
  89.  
  90.       led2_fadetimer=tonumber(payload['ft'])
  91.  
  92.       print("Received LED2 Target Value: "..led2_target)
  93.       print("Received LED2 Current Value: " .. led2_current)
  94.  
  95.       print ("Received LED2 Fadetimer: " ..led2_fadetimer)
  96.       led2_diff=(led2_target)-(led2_current)
  97.      
  98.       -- Get steps / second (on 10Hz).
  99.       led2_step = led2_diff / (led2_fadetimer/led2_update_time);
  100.    
  101.      
  102.       print("Step value: "..led2_step);
  103.  
  104.  
  105.       tmr.alarm(2, led2_update_time, 1, function()
  106.        
  107.         if led2_step > 0 and led2_current + led2_step < led2_target then
  108.            led2_current = led2_current + led2_step;
  109.         elseif led2_step < 0 and led2_current + led2_step > led2_target then
  110.           led2_current = led2_current + led2_step;
  111.         else
  112.           led2_current = led2_target;
  113.           tmr.stop(2)
  114.         end;
  115.        
  116.         pwm.setduty(4, led2_current)
  117.        
  118.       end )
  119.     end
  120.  
  121.   end
  122. end)
  123.  
  124. tmr.alarm(0, 1000, 1, function()
  125.  if wifi.sta.status() == 5 then
  126.      tmr.stop(0)
  127.      m:connect(broker_addr, broker_port, 0, function(conn)
  128.           print("connected")
  129.           m:subscribe(topic ,0, function(conn)
  130.                print("subbed to " .. topic)
  131.                m:publish(topic.."status/","leddim active",0,0, function(conn) print("sent sub") end)
  132.           end)
  133.      end)
  134.  end
  135. end)
Advertisement
Add Comment
Please, Sign In to add comment