Module mqtt
MQTT module
Usage:
local client = mqtt.client {
uri = "mqtts://aladdin:soopersecret@mqttbroker.com",
clean = true,
version = mqtt.v50, -- specify constant for MQTT version
}
-
client (...)
-
Create new MQTT client instance
Parameters:
Returns:
Client
new client instance
See also:
-
get_ioloop ()
-
Returns default
ioloop
instance. Shortcut to Ioloop.get.
See also:
-
run_ioloop (...)
-
Run default
ioloop
for given MQTT clients or functions.
Will not return until all clients/functions have exited.
Parameters:
- ...
MQTT clients or loop functions to add to ioloop, see Ioloop:add for details on functions.
See also:
Usage:
mqtt.run_ioloop(client1, client2, func1)
-
validate_subscribe_topic (t)
-
Validates a topic with wildcards.
Parameters:
- t
string
wildcard-topic to validate
Returns:
string
the input topic if valid
Or
-
boolean
false if invalid
-
string
error description
Usage:
local t = assert(mqtt.validate_subscribe_topic("base/+/thermostat/#"))
-
validate_publish_topic (t)
-
Validates a topic without wildcards.
Parameters:
Returns:
string
the input topic if valid
Or
-
boolean
false if invalid
-
string
error description
Usage:
local t = assert(mqtt.validate_publish_topic("base/living/thermostat/setpoint"))
-
compile_topic_pattern (t)
-
Returns a Lua pattern from topic.
Takes a wildcarded-topic and returns a Lua pattern that can be used
to validate if a received topic matches the wildcard-topic
Parameters:
Returns:
string
Lua-pattern that matches the topic and returns the captures
Or
-
boolean
false if the topic was invalid
-
string
error description
Usage:
local patt = compile_topic_pattern("homes/+/+/#")
local topic = "homes/myhome/living/mainlights/brightness"
local homeid, roomid, varargs = topic:match(patt)
-
topic_match (topic, opts)
-
Parses wildcards in a topic into a table.
Parameters:
- topic
string
incoming topic string
- opts parsing options table
- topic
string
the wild-carded topic to match against (optional if
opts.pattern
is given)
- pattern
string
the compiled pattern for the wild-carded topic (optional if
opts.topic
is given). If not given then topic will be compiled and the result will be
stored in this field for future use (cache).
- keys
array
array of field names. The order must be the same as the
order of the wildcards in
topic
Returns:
-
table
fields
: the array part will have the values of the wildcards, in
the order they appeared. The hash part, will have the field names provided
in opts.keys
, with the values of the corresponding wildcard. If a #
wildcard was used, that one will be the last in the table.
-
`varargs`:
The returned table is an array, with all segments that were
matched by the
#
wildcard (empty if there was no #
wildcard).
Or
boolean
false
if there was no match
Or
false
+err on error, eg. pattern was invalid.
Usage:
local opts = {
topic = "homes/+/+/#",
keys = { "homeid", "roomid", "varargs"},
}
local fields, varargst = topic_match("homes/myhome/living/mainlights/brightness", opts)
print(fields[1], fields.homeid) print(fields[2], fields.roomid) print(fields[3], fields.varargs)
print(varargst[1]) print(varargst[2])
-
mqtt
-
Module table
Fields:
- v311
number
MQTT v3.1.1 protocol version constant
- v50
number
MQTT v5.0 protocol version constant
- _VERSION
string
luamqtt library version string