-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmqtt.lua
More file actions
98 lines (81 loc) · 2.74 KB
/
Copy pathmqtt.lua
File metadata and controls
98 lines (81 loc) · 2.74 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
-- Simple MQTT client
-- Copyright 2019, Marcin Gil <marcin.gil--gmail.com>
-- This program is free software: you can redistribute it and/or modify
-- it under the terms of the GNU General Public License as published by
-- the Free Software Foundation, either version 3 of the License, or
-- (at your option) any later version.
-- keep alive timer in seconds
KEEP_ALIVE = 180
function MqttClient(host, port, user, password, topic, clientId, cmd_callback)
local self = {}
-- private fields
local h = host
local p = port
local t = topic
local connected = false
local callback = cmd_callback
local m = nil
local function self_register()
if callback == nil then
return
end
if not m:subscribe(clientId, 0, function(conn)
print("MQTT subscribe successful")
end) then
print("MQTT unable to subscribe")
end
end
local function on_connected(client)
connected = true
print ("MQTT Connected")
self_register()
end
local function on_disconnected(client, reason)
connected = false
print ("MQTT Offline")
tmr.create():alarm(10 * 1000, tmr.ALARM_SINGLE, self.connect)
end
local function on_message(client, topic, message)
if callback ~= nil then
callback(message)
end
end
-- setup MQTT client
print("MQTT setup for topic="..t.." clientid="..clientId.." at "..h..":"..p)
m = mqtt.Client(clientId, KEEP_ALIVE, user, password)
m:on("connect", on_connected)
m:on("offline", on_disconnected)
m:on("message", on_message)
function self.connect()
m:connect(h, p, false)
end
function self.publish(message)
-- print("MQTT publishing -"..message.."@"..t)
if not connected then
print("MQTT connecting...")
-- connect and mark connection established
-- publish upon connection
m:connect(h, p, false, function(client)
connected = true
client:publish(t, message, 0, 0, function(client)
print ("MQTT message sent")
end, function(client, reason)
connected = false
print("MQTT unable to connect due to "..reason)
end)
end)
else
print("MQTT reusing connection")
if not m:publish(t, message, 0, 0, function(client)
print("MQTT message sent")
end) then
print ("MQTT unable to publish message")
end
end
-- if not m:close() then
-- connected = false
-- print ("MQTT unable to close connection")
-- end
end
return self
end