mqtt - Протокол MQTT

Функції для роботи з протоколом MQTT — легковажним протоколом обміну повідомленнями для IoT-пристроїв.

Для роботи з MQTT Лілка повинна бути підключена до WiFi (див. wifi - Робота з WiFi-мережами).

Приклад підключення до MQTT-брокера, підписки на топік та публікації повідомлень:

 1wifi.connect("MyWifi", "password")
 2
 3local client, err = mqtt.connect({
 4    host = "broker.hivemq.com",
 5    port = 1883,
 6    client_id = "lilka-test",
 7})
 8if client == nil then
 9    error("Не вдалося підключитись: " .. err)
10end
11
12mqtt.subscribe(client, "lilka/in")
13
14function lilka.update(delta)
15    local msg = mqtt.receive(client)
16    if msg then
17        print("Отримано:", msg.topic, msg.payload)
18    end
19
20    local state = controller.get_state()
21    if state.a.just_pressed then
22        mqtt.publish(client, "lilka/out", "Привіт!")
23    end
24    if state.b.just_pressed then
25        util.exit()
26    end
27end
28
29function lilka.draw()
30    display.fill_screen(display.color565(0, 0, 0))
31    display.set_cursor(0, 16)
32    display.print("MQTT: підключено")
33end
class mqtt
static connect(options)

Creates an MQTT client and connects to the broker.

Options: host (string, required) — broker hostname or IP port (integer, optional) — broker port (default: 1883) client_id (string, optional) username (string, optional) password (string, optional) keepalive (integer, optional) — keepalive interval in seconds (default: 60)

Parameters:

options (table)

Returns:

client MQTT client handle, or nil on error

Return type:

MqttClient or nil

Returns:

? errmsg Error message on failure

Return type:

str

static connected(client)

Returns true if the client is currently connected to the broker.

Parameters:

client (MqttClient)

Return type:

boolean

static subscribe(client, topic, qos?)

Subscribes to a topic.

Parameters:
  • client (MqttClient)

  • topic (str) – MQTT topic filter (wildcards # and + are supported)

  • qos? (integer) – QoS level 0, 1, or 2 (default: 0)

Returns:

ok true on success, or nil on error

Return type:

boolean or nil

Returns:

? errmsg Error message on failure

Return type:

str

static unsubscribe(client, topic)

Unsubscribes from a topic.

Parameters:
static publish(client, topic, payload, qos?, retain?)

Publishes a message.

Parameters:
  • client (MqttClient)

  • topic (str)

  • payload (str) – Message payload

  • qos? (integer) – QoS level 0, 1, or 2 (default: 0)

  • retain? (boolean) – Retain flag (default: false)

Returns:

ok true on success, or nil on error

Return type:

boolean or nil

Returns:

? errmsg Error message on failure

Return type:

str

static receive(client)

Returns the next pending incoming message without blocking.

Returns nil when the receive queue is empty.

Parameters:

client (MqttClient)

Returns:

message

Return type:

MqttMessage or nil

static disconnect(client)

Disconnects from the broker and releases all resources.

Parameters:

client (MqttClient)

class MqttClient
class MqttMessage
topic: str

Topic the message was published on

payload: str

Message payload (may be empty)