-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathmqttclient.lua
More file actions
133 lines (108 loc) · 3.05 KB
/
Copy pathmqttclient.lua
File metadata and controls
133 lines (108 loc) · 3.05 KB
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
--
-- Manage and maintain a permanent connection to a MQTT broker
--
-- A connection is retried when it gets interrupted. Previous subscriptions
-- will be reregistered. Each subscription can have it's own callback
--
local module = {}
local m = nil -- the MQTT client
local subscriptions = {}
local connected = false
local callback = function() end -- function to run after connect
-- handle new connection
local function on_connect(con)
connected = true
-- (re)subscribe to to all topics
for topic, cb in pairs(subscriptions) do
m:subscribe(topic, 2)
end
-- send Birth
m:publish(G.config.MQTT.endpoint .. 'status', 'online', 1, 1)
-- custom handler
callback()
print "MQTT connected"
end
-- handle disconnects
local function on_offline(client)
connected = false
-- try to reconnect in 10 seconds
tmr.create():alarm(10 * 1000, tmr.ALARM_SINGLE, module.start)
print "MQTT disconnected"
end
-- handle errors
local function on_error(client, reason)
print("Error on MQTT connection. Reason " .. reason)
on_offline(client)
end
-- handle messages
local function on_message(con, topic, data)
if data ~= nil then
print("MQTT message received: " .. topic .. ": " .. data)
if subscriptions[topic] ~= nil then
-- call callback with message
subscriptions[topic](data)
end
end
end
-- send some data to the broker
--
-- @param string topic The (sub topic) to send the data to
-- @param mixed payload The actual payload to send
-- @param int qos optional quality of service
-- @param int retain can optionally be set to 1 to let the broker keep the value
--
function module.publish(topic, payload, qos, retain)
qos = qos or 0
retain = retain or 0
if(connected) then
m:publish(G.config.MQTT.endpoint .. topic, payload, qos, retain)
print("MQTT", topic, payload)
else
print("currently not connected to MQTT broker, cannot publish to " .. topic)
end
end
-- subscribe to topic
--
-- The given callback will be called when a message is received for
-- the topic. The message will be provided as the first parameter
--
-- @param {string} topic The subtopic to subscribe to
-- @param {function} callback Will be called for messages
--
function module.subscribe(topic, callback)
subscriptions[G.config.MQTT.endpoint .. topic] = callback
if(connected) then
m:subscribe(G.config.MQTT.endpoint .. topic, 0)
end
end
-- run the given function on connect
function module.waitThen(cb)
callback = cb
end
-- start MQTT connection
--
function module.start()
-- create client
m = mqtt.Client(
G.config.SELF,
25, -- keepalive
G.config.MQTT.user,
G.config.MQTT.pass,
1 -- clean session
)
-- set LWT
m:lwt(G.config.MQTT.endpoint .. 'status', 'offline', 1, 1)
-- Connect to broker
m:connect(
G.config.MQTT.host,
G.config.MQTT.port,
0, -- non-secure
0, -- autoreconnect NOT RECOMMENDED
on_connect,
on_error
)
-- register handlers
m:on("message", on_message);
m:on("offline", on_offline);
end
return module