Advertisement
Guest User

Untitled

a guest
Oct 21st, 2016
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.61 KB | None | 0 0
  1. function connect_to_mqtt_broker()
  2.  
  3.  
  4. print("Connecting to broker...")
  5. m:connect(BROKER, PORT, 0, 1, function(client)
  6. print("connected")
  7. print("["..tmr.time().."(s)] - Client connected to broker: "..BROKER)
  8. m:subscribe(SUB_TOPIC,0, function(conn)
  9. print("Subscribed to "..SUB_TOPIC.." topic")
  10. led(0,204,0,150)
  11. end)
  12. m:publish(PUB_TOPIC,"Hello from: "..node.chipid()..RESTART_REASON,0,0, function(conn)
  13. print("sent")
  14. end)
  15. end,
  16. function(client, reason)
  17. print("failed reason: "..reason)
  18. end)
  19.  
  20. end
  21.  
  22. ---MQTT client---
  23. print("--------------> Create mqtt clinet")
  24. --set up MQTT client
  25. -- init mqtt client with keepalive timer 120sec
  26. m = mqtt.Client("ESP"..node.chipid(), KEEP_ALIVE_TMR, USER, PASSWORD)
  27. m:lwt(PUB_TOPIC, "offline", 0, 0)
  28. m:on("offline", function(conn)
  29. print("["..tmr.time().."(s)] - Mqtt client gone offline")
  30.  
  31. end)
  32. m:on("message", function(conn, topic, data)
  33. --receive_data(data, topic)
  34. print("Data received: "..data)
  35. led(200,50,50,30)
  36. receive_data(data, topic)
  37. led(0,204,0,150)
  38. end)
  39.  
  40. m:on("offline", function(conn)
  41. print("["..tmr.time().."(s)] - Mqtt client gone offline")
  42.  
  43. end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement