Advertisement
ale_ben

MQTT-boiler

Apr 18th, 2024 (edited)
1,532
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local mqtt = require("mqtt")
  2.  
  3. local boiler = peripheral.wrap("bottom")
  4.  
  5. if boiler == nil or boiler.isFormed() == false then
  6.     print("Unable to find boiler. Restarting in 5")
  7.     os.sleep(5)
  8.     os.reboot()
  9. end
  10.  
  11. local keep_alive = 60
  12. local baseTopic = "/ftbUniversity19/ITLandfill/fission/boiler/"
  13. local refresh = 10
  14.  
  15. local connected = false
  16.  
  17. -- create mqtt client
  18. local client = mqtt.client {
  19.     -- NOTE: this broker is not working sometimes; comment username = "..." below if you still want to use it
  20.     uri = "ws://test.mosquitto.org:8080",
  21.     clean = true,
  22.     keep_alive = keep_alive
  23. }
  24.  
  25. print("created MQTT client", client)
  26.  
  27. client:on {
  28.     connect = function(connack)
  29.         if connack.rc ~= 0 then
  30.             print("connection to broker failed:", connack:reason_string(), connack)
  31.             return
  32.         end
  33.         print("connected:", connack) -- successful connection
  34.         connected = true      
  35.         -- subscribe to test topic and publish message after it
  36.         assert(client:subscribe { topic = "/hfdghwl/#", qos = 1, callback = function(suback)
  37.             print("subscribed:", suback)
  38.  
  39.         end })
  40.     end,
  41.  
  42.     message = function(msg)
  43.         assert(client:acknowledge(msg))
  44.  
  45.         print("received:", msg)
  46.  
  47.         if msg.payload == "disconnect" then
  48.             print("disconnecting...")
  49.             assert(client:disconnect())
  50.         end
  51.     end,
  52.  
  53.     error = function(err)
  54.         print("MQTT client error:", err)
  55.     end,
  56.  
  57.     close = function()
  58.         print("MQTT conn closed")
  59.     end
  60. }
  61.  
  62. function sendMessage(topic, message, retain)
  63.     if connected then    
  64.         assert(client:publish {
  65.             topic = baseTopic..topic,
  66.             payload = message,
  67.             retain = retain
  68.         })
  69.     else
  70.         print("Not connected")
  71.     end    
  72. end
  73.  
  74. function sendUpdate()
  75.     sendMessage("sensor/boil/capacity", tostring(boiler.getBoilCapacity()), false)
  76.     sendMessage("sensor/boil/rate", tostring(boiler.getBoilRate()), false)              
  77.     sendMessage("sensor/cooledCoolant", tostring(boiler.getCooledCoolantFilledPercentage()), false)
  78.     sendMessage("sensor/heatedCoolant", tostring(boiler.getHeatedCoolantFilledPercentage()), false)
  79.     sendMessage("sensor/boil/maxRate", tostring(boiler.getMaxBoilRate()), false)
  80.     sendMessage("sensor/steam", tostring(boiler.getSteamFilledPercentage()), false)
  81.     sendMessage("sensor/water", tostring(boiler.getWaterFilledPercentage()), false)
  82.     sendMessage("sensor/temperature", tostring(boiler.getTemperature()), false)
  83. end
  84.  
  85. parallel.waitForAny(
  86.     function()
  87.         -- run io loop for client until connection close
  88.         -- please note that in sync mode background PINGREQ's are not available, and automatic reconnects too
  89.         print("running client in synchronous input/output loop")
  90.         mqtt.run_sync(client)
  91.         print("done, synchronous input/output loop is stopped")
  92.     end,
  93.     function()
  94.         while true do
  95.             os.sleep(keep_alive)
  96.             client:send_pingreq()
  97.         end
  98.     end,
  99.     function()
  100.         while true do
  101.             os.sleep(refresh)
  102.             if (pcall(sendUpdate) == false) then
  103.                 print("Error updating. Restarting in 5")
  104.                 os.sleep(5)
  105.                 os.reboot()
  106.             end
  107.         end
  108.     end
  109. )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement