View difference between Paste ID: sAyTp5Q4 and p8NcuwNR
SHOW: | | - or go back to the newest paste.
1
local mqtt = require("mqtt")
2
3-
local reactor = peripheral.wrap("bottom")
3+
local boiler = peripheral.wrap("bottom")
4
5-
if reactor == nil or reactor.isFormed() == false then
5+
if boiler == nil or boiler.isFormed() == false then
6-
    print("Unable to find reactor. Restarting in 5")
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/reactor/"
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 = baseTopic .. "command/#", qos = 1, callback = function(suback)
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-
        
51+
52-
        local command = string.sub(msg.topic, string.len(baseTopic .. "command/"))
52+
53-
        print("Command topic: " .. command)
53+
54-
        print("Command: " .. msg.payload)
54+
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/status", tostring(reactor.getStatus()), false)
79+
    sendMessage("sensor/boil/maxRate", tostring(boiler.getMaxBoilRate()), false)
80-
    sendMessage("sensor/temperature", tostring(reactor.getTemperature()), false)              
80+
    sendMessage("sensor/steam", tostring(boiler.getSteamFilledPercentage()), false)
81-
    sendMessage("sensor/damage", tostring(reactor.getDamagePercent()), false)
81+
    sendMessage("sensor/water", tostring(boiler.getWaterFilledPercentage()), false)
82-
    sendMessage("sensor/coolant", tostring(reactor.getCoolantFilledPercentage()), false)
82+
    sendMessage("sensor/temperature", tostring(boiler.getTemperature()), false)
83-
    sendMessage("sensor/heatedCoolant", tostring(reactor.getHeatedCoolantFilledPercentage()), false)
83+
84-
    sendMessage("sensor/fuel", tostring(reactor.getFuelFilledPercentage()), false)
84+
85-
    sendMessage("sensor/waste", tostring(reactor.getWasteFilledPercentage()), false)
85+
86-
   
86+
87-
    --local burnRate = "{\"max\":"..tostring(reactor.getMaxBurnRate()) ..", \"set\":" .. tostring(reactor.getBurnRate()) .. ",\"val\":" .. tostring(reactor.getActualBurnRate()) .. "}"
87+
88-
    sendMessage("sensor/burnRate/set", tostring(reactor.getBurnRate()), false)
88+
89-
    sendMessage("sensor/burnRate/actual", tostring(reactor.getActualBurnRate()), false)
89+
90-
    sendMessage("sensor/burnRate/max", tostring(reactor.getMaxBurnRate()), false)
90+
91-
    sendMessage("sensor/burnRate/percent", tostring(reactor.getActualBurnRate()/reactor.getMaxBurnRate()), false)
91+
92-
    --sendMessage("sensor/burnRate", burnRate, false)
92+
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
)