View difference between Paste ID: GLXACx8N and BGWyrJMD
SHOW: | | - or go back to the newest paste.
1
local mqtt = require("mqtt")
2
3-
local turbine = peripheral.wrap("bottom")
3+
local induction = peripheral.wrap("bottom")
4
5-
if turbine == nil or turbine.isFormed() == false then
5+
if induction == nil or induction.isFormed() == false then
6-
    print("Unable to find turbine. Restarting in 5")
6+
    print("Unable to find induction. Restarting in 5")
7
    os.sleep(5)
8
    os.reboot()
9
end
10
11
local keep_alive = 60
12-
local baseTopic = "/ftbUniversity19/ITLandfill/fission/turbine/"
12+
local baseTopic = "/ftbUniversity19/ITLandfill/fission/induction/"
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/energy", tostring(turbine.getEnergyFilledPercentage()), false)
75+
    sendMessage("sensor/energy", tostring(induction.getEnergyFilledPercentage()), false)
76-
    sendMessage("sensor/flow/rate", tostring(turbine.getFlowRate()), false)  
76+
    sendMessage("sensor/cells/induction", tostring(induction.getInstalledCells()), false)
77-
    sendMessage("sensor/flow/max", tostring(turbine.getMaxFlowRate()), false)
77+
    sendMessage("sensor/cells/provider", tostring(induction.getInstalledProviders()), false)  
78-
    sendMessage("sensor/production", tostring(turbine.getProductionRate()), false)
78+
    sendMessage("sensor/lastInput", tostring(induction.getLastInput()), false)  
79-
    sendMessage("sensor/steam", tostring(turbine.getSteamFilledPercentage()), false)
79+
    sendMessage("sensor/lastOutput", tostring(induction.getLastOutput()), false)
80-
    sendMessage("sensor/lastSteamInput", tostring(turbine.getLastSteamInputRate()), false)
80+
81
82
parallel.waitForAny(
83
    function()
84
        -- run io loop for client until connection close
85
        -- please note that in sync mode background PINGREQ's are not available, and automatic reconnects too
86
        print("running client in synchronous input/output loop")
87
        mqtt.run_sync(client)
88
        print("done, synchronous input/output loop is stopped")
89
    end,
90
    function()
91
        while true do
92
            os.sleep(keep_alive)
93
            client:send_pingreq()
94
        end
95
    end,
96
    function()
97
        while true do
98
            os.sleep(refresh)
99
            if (pcall(sendUpdate) == false) then
100
                print("Error updating. Restarting in 5")
101
                os.sleep(5)
102
                os.reboot()
103
            end
104
        end
105
    end
106
)