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
    }
    

Functions

client (...) Create new MQTT client instance
get_ioloop () Returns default ioloop instance.
run_ioloop (...) Run default ioloop for given MQTT clients or functions.
validate_subscribe_topic (t) Validates a topic with wildcards.
validate_publish_topic (t) Validates a topic without wildcards.
compile_topic_pattern (t) Returns a Lua pattern from topic.
topic_match (topic, opts) Parses wildcards in a topic into a table.

Tables

mqtt Module table


Functions

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

  1. boolean false if invalid
  2. 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

  1. boolean false if invalid
  2. 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

  1. boolean false if the topic was invalid
  2. 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:

  1. 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.
  2. `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)  -- "myhome myhome"
    print(fields[2], fields.roomid)  -- "living living"
    print(fields[3], fields.varargs) -- "mainlights/brightness mainlights/brightness"
    
    print(varargst[1]) -- "mainlights"
    print(varargst[2]) -- "brightness"

Tables

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
generated by LDoc 1.5.0 Last updated 2024-10-13 22:14:17