-
Notifications
You must be signed in to change notification settings - Fork 0
/
r3mqtt.lua
56 lines (47 loc) · 994 Bytes
/
r3mqtt.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
local mc
local msgH = {}
local function onConnect(client)
for t, _ in pairs(msgH) do
client:subscribe(t,0,nil)
end
end
local function onMessage(client, topic, data)
local f = msgH[topic]
if f then
f(data)
end
end
local rct = tmr.create()
rct:register(10 * 1000, tmr.ALARM_SEMI, connect)
local function onMqttError(client, reason)
rct:start()
end
local function close()
if mc then
mc:close()
mc = nil
end
end
local function connect()
close()
mc = mqtt.Client("PipeLEDs",30,nil,nil,1)
mc:on("connect", onConnect)
mc:on("message", onMessage)
mc:on("connfail", onMqttError)
mc:connect("mqtt.realraum.at",1883,false,true,onMqttError)
end
local function online()
return not not mc
end
local function setHandler(t, f)
msgH[t] = f
if mc then
mc:subscribe(t ,0,nil)
end
end
return {
close = close,
connect = connect,
setHandler = setHandler,
online = online
}