nodemcu-firmware/docs/modules/mqtt.md

345 lines
14 KiB
Markdown
Raw Permalink Normal View History

2016-01-10 22:52:05 +01:00
# MQTT Module
| Since | Origin / Contributor | Maintainer | Source |
| :----- | :-------------------- | :---------- | :------ |
| 2015-01-23 | [Stephen Robinson](https://github.com/esar/contiki-mqtt), [Tuan PM](https://github.com/tuanpmt/esp_mqtt) | [Vowstar](https://github.com/vowstar) | [mqtt.c](../../app/modules/mqtt.c)|
2016-01-10 22:52:05 +01:00
The client adheres to version 3.1.1 of the [MQTT](https://en.wikipedia.org/wiki/MQTT) protocol. Make sure that your broker supports and is correctly configured for version 3.1.1. The client is backwards incompatible with brokers running MQTT 3.1.
## mqtt.Client()
Creates a MQTT client.
#### Syntax
`mqtt.Client(clientid, keepalive[, username, password, cleansession, max_message_length])`
2016-01-10 22:52:05 +01:00
#### Parameters
- `clientid` client ID
- `keepalive` keepalive seconds
- `username` user name
- `password` user password
- `cleansession` 0/1 for `false`/`true`. Default is 1 (`true`).
- `max_message_length`, how large messages to accept. Default is 1024.
2016-01-10 22:52:05 +01:00
#### Returns
MQTT client
#### Notes
According to MQTT specification the max PUBLISH length is 256Mb. This is too large for NodeMCU to realistically handle. To avoid
an out-of-memory situation, there is a limit on how big messages to accept. This is controlled by the `max_message_length` parameter.
In practice, this only affects incoming PUBLISH messages since all regular control packets are small.
The default 1024 was chosen as this was the implicit limit in NodeMCU 2.2.1 and older (where this was not handled at all).
Note that "message length" refers to the full MQTT message size, including fixed & variable headers, topic name, packet ID (if applicable),
and payload. For exact details, please see [the MQTT specification](http://docs.oasis-open.org/mqtt/mqtt/v3.1.1/os/mqtt-v3.1.1-os.html#_Toc398718037).
Any message *larger* than `max_message_length` will be (partially) delivered to the `overflow` callback, if defined. The rest
of the message will be discarded. Any subsequent messages should be handled as expected.
Discarded messages will still be ACK'ed if QoS level 1 or 2 was requested, even if the application stack cannot handle them.
Heap memory will be used to buffer any message which spans more than a single TCP packet. A single allocation for the full
message will be performed when the message header is first seen, to avoid heap fragmentation.
If allocation fails, the MQTT session will be disconnected.
Naturally, messages larger than `max_message_length` will not be stored.
Note that heap allocation may occur even if the individual messages are not larger than the configured max! For example,
the broker may send multiple smaller messages in quick succession, which could go into the same TCP packet. If the last message
in the TCP packet did not fit fully, a heap buffer will be allocated to hold the incomplete message while waiting for the next TCP packet.
The typical maximum size for a message to fit into a single TCP packet is 1460 bytes, but this depends on the network's MTU
configuration, any packet fragmentation, and as described above, multiple messages in the same TCP packet.
2016-01-10 22:52:05 +01:00
#### Example
```lua
-- init mqtt client without logins, keepalive timer 120s
m = mqtt.Client("clientid", 120)
-- init mqtt client with logins, keepalive timer 120sec
2016-01-10 22:52:05 +01:00
m = mqtt.Client("clientid", 120, "user", "password")
-- setup Last Will and Testament (optional)
2019-02-17 19:26:29 +01:00
-- Broker will publish a message with qos = 0, retain = 0, data = "offline"
2016-01-10 22:52:05 +01:00
-- to topic "/lwt" if client don't send keepalive packet
m:lwt("/lwt", "offline", 0, 0)
m:on("offline", function(client) print ("offline") end)
-- on publish message receive event
2019-02-17 19:26:29 +01:00
m:on("message", function(client, topic, data)
print(topic .. ":" )
2016-01-10 22:52:05 +01:00
if data ~= nil then
print(data)
end
end)
-- on publish overflow receive event
m:on("overflow", function(client, topic, data)
print(topic .. " partial overflowed message: " .. data )
end)
2016-01-10 22:52:05 +01:00
-- for TLS: m:connect("192.168.11.118", secure-port, 1)
2020-03-15 09:34:12 +01:00
m:connect("192.168.11.118", 1883, false, function(client)
print("connected")
-- Calling subscribe/publish only makes sense once the connection
-- was successfully established. You can do that either here in the
-- 'connect' callback or you need to otherwise make sure the
-- connection was established (e.g. tracking connection status or in
-- m:on("connect", function)).
-- subscribe topic with qos = 0
client:subscribe("/topic", 0, function(client) print("subscribe success") end)
-- publish a message with data = hello, QoS = 0, retain = 0
client:publish("/topic", "hello", 0, 0, function(client) print("sent") end)
end,
function(client, reason)
First round of MQTT fixes (#3360) * mqtt: remove concept of connection timeout Just rely on the network stack to tell us when things have gone south. * mqtt: remove write-only mqtt_state.port field * mqtt: drop useless conditional * mqtt: decouple message sent flag from timer * mqtt: reconnect callback does not need to hang up The network stack has certainly done that for us at this point. Similarly, since we're about to call mqtt_socket_disconnected, don't bother unregistering the timer here, either. * mqtt: don't tick once per second Set the timer for the duration of the wait and cancel it on the other side. * mqtt: defer message queue destruction to _disconnect We're going to want to publish a disconnect message for real, so doing this in _close does no one any favors * mqtt: miscellaneous cleanups No functional change intended * mqtt: close() should send disconnect message for real This means waiting for _sent() to fire again before telling the network stack to disconnect. * mqtt: tidy connect and dns - Push the self-ref to after all allocations and error returns - Don't try to extract IPv4 from the domain string ourselves, let the resolver, since it can - Don't try to connect to localhost. That can't possibly work. * mqtt: common up some callback invocations * mqtt: don't retransmit messages on timeout There's no point in retransmitting messages on timeout; the network stack will be trying to do it for us anyway. * mqtt: remove unnecessary NULL udata checks * mqtt: hold strings in Lua, not C Eliminates a host of C-side allocations. While here, move the rest of the mqtt_connect_info structure out to its own thing, and pack some flags using a bitfield. * mqtt: mqtt_socket_on use lua_checkoption * mqtt: slightly augment debug messages These changes have made some debugging ever so slightly easier.
2021-01-05 12:07:09 +01:00
print("Connection failed reason: " .. reason)
end)
2016-01-10 22:52:05 +01:00
First round of MQTT fixes (#3360) * mqtt: remove concept of connection timeout Just rely on the network stack to tell us when things have gone south. * mqtt: remove write-only mqtt_state.port field * mqtt: drop useless conditional * mqtt: decouple message sent flag from timer * mqtt: reconnect callback does not need to hang up The network stack has certainly done that for us at this point. Similarly, since we're about to call mqtt_socket_disconnected, don't bother unregistering the timer here, either. * mqtt: don't tick once per second Set the timer for the duration of the wait and cancel it on the other side. * mqtt: defer message queue destruction to _disconnect We're going to want to publish a disconnect message for real, so doing this in _close does no one any favors * mqtt: miscellaneous cleanups No functional change intended * mqtt: close() should send disconnect message for real This means waiting for _sent() to fire again before telling the network stack to disconnect. * mqtt: tidy connect and dns - Push the self-ref to after all allocations and error returns - Don't try to extract IPv4 from the domain string ourselves, let the resolver, since it can - Don't try to connect to localhost. That can't possibly work. * mqtt: common up some callback invocations * mqtt: don't retransmit messages on timeout There's no point in retransmitting messages on timeout; the network stack will be trying to do it for us anyway. * mqtt: remove unnecessary NULL udata checks * mqtt: hold strings in Lua, not C Eliminates a host of C-side allocations. While here, move the rest of the mqtt_connect_info structure out to its own thing, and pack some flags using a bitfield. * mqtt: mqtt_socket_on use lua_checkoption * mqtt: slightly augment debug messages These changes have made some debugging ever so slightly easier.
2021-01-05 12:07:09 +01:00
m:close()
-- you can call m:connect again after the offline callback fires
2016-01-10 22:52:05 +01:00
```
# MQTT Client
## mqtt.client:close()
First round of MQTT fixes (#3360) * mqtt: remove concept of connection timeout Just rely on the network stack to tell us when things have gone south. * mqtt: remove write-only mqtt_state.port field * mqtt: drop useless conditional * mqtt: decouple message sent flag from timer * mqtt: reconnect callback does not need to hang up The network stack has certainly done that for us at this point. Similarly, since we're about to call mqtt_socket_disconnected, don't bother unregistering the timer here, either. * mqtt: don't tick once per second Set the timer for the duration of the wait and cancel it on the other side. * mqtt: defer message queue destruction to _disconnect We're going to want to publish a disconnect message for real, so doing this in _close does no one any favors * mqtt: miscellaneous cleanups No functional change intended * mqtt: close() should send disconnect message for real This means waiting for _sent() to fire again before telling the network stack to disconnect. * mqtt: tidy connect and dns - Push the self-ref to after all allocations and error returns - Don't try to extract IPv4 from the domain string ourselves, let the resolver, since it can - Don't try to connect to localhost. That can't possibly work. * mqtt: common up some callback invocations * mqtt: don't retransmit messages on timeout There's no point in retransmitting messages on timeout; the network stack will be trying to do it for us anyway. * mqtt: remove unnecessary NULL udata checks * mqtt: hold strings in Lua, not C Eliminates a host of C-side allocations. While here, move the rest of the mqtt_connect_info structure out to its own thing, and pack some flags using a bitfield. * mqtt: mqtt_socket_on use lua_checkoption * mqtt: slightly augment debug messages These changes have made some debugging ever so slightly easier.
2021-01-05 12:07:09 +01:00
Schedules a clean teardown of the connection.
MQTT requires clients to actively signal a desire to disconnect to the server
to avoid sending their LWT. Thus, the Client is not immediately reusable
after this call, but only after the "offline" callback has fired.
2016-01-10 22:52:05 +01:00
#### Syntax
`mqtt:close()`
#### Parameters
none
#### Returns
First round of MQTT fixes (#3360) * mqtt: remove concept of connection timeout Just rely on the network stack to tell us when things have gone south. * mqtt: remove write-only mqtt_state.port field * mqtt: drop useless conditional * mqtt: decouple message sent flag from timer * mqtt: reconnect callback does not need to hang up The network stack has certainly done that for us at this point. Similarly, since we're about to call mqtt_socket_disconnected, don't bother unregistering the timer here, either. * mqtt: don't tick once per second Set the timer for the duration of the wait and cancel it on the other side. * mqtt: defer message queue destruction to _disconnect We're going to want to publish a disconnect message for real, so doing this in _close does no one any favors * mqtt: miscellaneous cleanups No functional change intended * mqtt: close() should send disconnect message for real This means waiting for _sent() to fire again before telling the network stack to disconnect. * mqtt: tidy connect and dns - Push the self-ref to after all allocations and error returns - Don't try to extract IPv4 from the domain string ourselves, let the resolver, since it can - Don't try to connect to localhost. That can't possibly work. * mqtt: common up some callback invocations * mqtt: don't retransmit messages on timeout There's no point in retransmitting messages on timeout; the network stack will be trying to do it for us anyway. * mqtt: remove unnecessary NULL udata checks * mqtt: hold strings in Lua, not C Eliminates a host of C-side allocations. While here, move the rest of the mqtt_connect_info structure out to its own thing, and pack some flags using a bitfield. * mqtt: mqtt_socket_on use lua_checkoption * mqtt: slightly augment debug messages These changes have made some debugging ever so slightly easier.
2021-01-05 12:07:09 +01:00
`nil`
2016-01-10 22:52:05 +01:00
## mqtt.client:connect()
Connects to the broker specified by the given host, port, and secure options.
#### Syntax
`mqtt:connect(host[, port[, secure]][, function(client)[, function(client, reason)]])`
2016-01-10 22:52:05 +01:00
#### Parameters
- `host` host, domain or IP (string)
- `port` broker port (number), default 1883
- `secure` boolean: if `true`, use TLS. Take note of constraints documented in the [net module](net.md).
- `function(client)` callback function for when the connection was established
- `function(client, reason)` callback function for when the connection could not be established. No further callbacks should be called.
2016-01-10 22:52:05 +01:00
!!! attention
Networking rampage and accumulated fixes (#3060) * espconn: remove unused espconn code, take 1 This is the easiest part of https://github.com/nodemcu/nodemcu-firmware/issues/3004 . It removes a bunch of functions that were never called in our tree. * espconn: De-orbit espconn_gethostbyname Further work on https://github.com/nodemcu/nodemcu-firmware/issues/3004 While here, remove `mqtt`'s charming DNS-retry logic (which is neither shared with nor duplicated in other modules) and update its :connect() return value behavior and documentation. * espconn: remove scary global pktinfo A write-only global! How about that. * net: remove deprecated methods All the TLS stuff moved over there a long time ago, and net_createUDPSocket should just do what it says on the tin. * espconn_secure: remove ESPCONN_SERVER support We can barely function as a TLS client; being a TLS server seems like a real stretch. This code was never called from Lua anyway. * espconn_secure: more code removal * espconn_secure: simplify ssl options structure There is nothing "ssl_packet" about this structure. Get rid of the terrifying "pbuffer" pointer. Squash two structure types together and eliminate an unused field. * espconn_secure: refactor mbedtls_msg_info_load Split out espconn_mbedtls_parse, which we can use as part of our effort towards addressing https://github.com/nodemcu/nodemcu-firmware/issues/3032 * espconn_secure: introduce TLS cert/key callbacks The new feature part of https://github.com/nodemcu/nodemcu-firmware/issues/3032 Subsequent work will remove the old mechanism. * tls: add deprecation warnings * luacheck: net.ifinfo is a thing now * tls: remove use of espconn->reverse * mqtt: stop using espconn->reverse Instead, just place the espconn structure itself at the top of the user data. This enlarges the structure somewhat but removes one more layer of dynamic heap usage and NULL checks. While here, simplify the code a bit. * mqtt: remove redundant pointer to connect_info Everywhere we have the mqtt_state_t we also have the lmqtt_userdata. * mqtt: doc fixes * mqtt: note bug * tls: allow :on(...,nil) to unregister a callback
2020-04-07 14:06:27 +02:00
Secure (`mqtts`) connections come with quite a few limitations. Please see
the warnings in the [tls module](tls.md)'s documentation.
2016-01-10 22:52:05 +01:00
#### Returns
Networking rampage and accumulated fixes (#3060) * espconn: remove unused espconn code, take 1 This is the easiest part of https://github.com/nodemcu/nodemcu-firmware/issues/3004 . It removes a bunch of functions that were never called in our tree. * espconn: De-orbit espconn_gethostbyname Further work on https://github.com/nodemcu/nodemcu-firmware/issues/3004 While here, remove `mqtt`'s charming DNS-retry logic (which is neither shared with nor duplicated in other modules) and update its :connect() return value behavior and documentation. * espconn: remove scary global pktinfo A write-only global! How about that. * net: remove deprecated methods All the TLS stuff moved over there a long time ago, and net_createUDPSocket should just do what it says on the tin. * espconn_secure: remove ESPCONN_SERVER support We can barely function as a TLS client; being a TLS server seems like a real stretch. This code was never called from Lua anyway. * espconn_secure: more code removal * espconn_secure: simplify ssl options structure There is nothing "ssl_packet" about this structure. Get rid of the terrifying "pbuffer" pointer. Squash two structure types together and eliminate an unused field. * espconn_secure: refactor mbedtls_msg_info_load Split out espconn_mbedtls_parse, which we can use as part of our effort towards addressing https://github.com/nodemcu/nodemcu-firmware/issues/3032 * espconn_secure: introduce TLS cert/key callbacks The new feature part of https://github.com/nodemcu/nodemcu-firmware/issues/3032 Subsequent work will remove the old mechanism. * tls: add deprecation warnings * luacheck: net.ifinfo is a thing now * tls: remove use of espconn->reverse * mqtt: stop using espconn->reverse Instead, just place the espconn structure itself at the top of the user data. This enlarges the structure somewhat but removes one more layer of dynamic heap usage and NULL checks. While here, simplify the code a bit. * mqtt: remove redundant pointer to connect_info Everywhere we have the mqtt_state_t we also have the lmqtt_userdata. * mqtt: doc fixes * mqtt: note bug * tls: allow :on(...,nil) to unregister a callback
2020-04-07 14:06:27 +02:00
`nil`; use callbacks to observe the outcome.
2016-01-10 22:52:05 +01:00
#### Notes
An application should watch for connection failures and handle errors in the error callback,
in order to achieve a reliable connection to the server. For example:
```
2019-02-17 19:26:29 +01:00
function handle_mqtt_error(client, reason)
tmr.create():alarm(10 * 1000, tmr.ALARM_SINGLE, do_mqtt_connect)
end
function do_mqtt_connect()
mqtt:connect("server", function(client) print("connected") end, handle_mqtt_error)
end
```
In reality, the connected function should do something useful!
The first callback to `:connect()` aliases with the "connect" callback
available through `:on()` (the last passed callback to either of those are
used). However, if `nil` is passed to `:connect()`, any existing callback
will be preserved, rather than removed.
The second (failure) callback aliases with the "connfail" callback available
through `:on()`. (The "offline" callback is only called after an already
established connection becomes closed. If the `connect()` call fails to
establish a connection, the callback passed to `:connect()` is called and
nothing else.)
Previously, we instructed an application to pass either the *integer* 0 or
*integer* 1 for `secure`. Now, this will trigger a deprecation warning; please
use the *boolean* `false` or `true` instead.
#### Connection failure callback reason codes:
| Constant | Value | Description |
|----------|-------|-------------|
|`mqtt.CONN_FAIL_SERVER_NOT_FOUND`|-5|There is no broker listening at the specified IP Address and Port|
|`mqtt.CONN_FAIL_NOT_A_CONNACK_MSG`|-4|The response from the broker was not a CONNACK as required by the protocol|
|`mqtt.CONN_FAIL_DNS`|-3|DNS Lookup failed|
|`mqtt.CONN_FAIL_TIMEOUT_RECEIVING`|-2|Timeout waiting for a CONNACK from the broker|
|`mqtt.CONN_FAIL_TIMEOUT_SENDING`|-1|Timeout trying to send the Connect message|
|`mqtt.CONNACK_ACCEPTED`|0|No errors. _Note: This will not trigger a failure callback._|
|`mqtt.CONNACK_REFUSED_PROTOCOL_VER`|1|The broker is not a 3.1.1 MQTT broker.|
|`mqtt.CONNACK_REFUSED_ID_REJECTED`|2|The specified ClientID was rejected by the broker. (See `mqtt.Client()`)|
|`mqtt.CONNACK_REFUSED_SERVER_UNAVAILABLE`|3|The server is unavailable.|
|`mqtt.CONNACK_REFUSED_BAD_USER_OR_PASS`|4|The broker refused the specified username or password.|
|`mqtt.CONNACK_REFUSED_NOT_AUTHORIZED`|5|The username is not authorized.|
2016-01-10 22:52:05 +01:00
## mqtt.client:lwt()
Networking rampage and accumulated fixes (#3060) * espconn: remove unused espconn code, take 1 This is the easiest part of https://github.com/nodemcu/nodemcu-firmware/issues/3004 . It removes a bunch of functions that were never called in our tree. * espconn: De-orbit espconn_gethostbyname Further work on https://github.com/nodemcu/nodemcu-firmware/issues/3004 While here, remove `mqtt`'s charming DNS-retry logic (which is neither shared with nor duplicated in other modules) and update its :connect() return value behavior and documentation. * espconn: remove scary global pktinfo A write-only global! How about that. * net: remove deprecated methods All the TLS stuff moved over there a long time ago, and net_createUDPSocket should just do what it says on the tin. * espconn_secure: remove ESPCONN_SERVER support We can barely function as a TLS client; being a TLS server seems like a real stretch. This code was never called from Lua anyway. * espconn_secure: more code removal * espconn_secure: simplify ssl options structure There is nothing "ssl_packet" about this structure. Get rid of the terrifying "pbuffer" pointer. Squash two structure types together and eliminate an unused field. * espconn_secure: refactor mbedtls_msg_info_load Split out espconn_mbedtls_parse, which we can use as part of our effort towards addressing https://github.com/nodemcu/nodemcu-firmware/issues/3032 * espconn_secure: introduce TLS cert/key callbacks The new feature part of https://github.com/nodemcu/nodemcu-firmware/issues/3032 Subsequent work will remove the old mechanism. * tls: add deprecation warnings * luacheck: net.ifinfo is a thing now * tls: remove use of espconn->reverse * mqtt: stop using espconn->reverse Instead, just place the espconn structure itself at the top of the user data. This enlarges the structure somewhat but removes one more layer of dynamic heap usage and NULL checks. While here, simplify the code a bit. * mqtt: remove redundant pointer to connect_info Everywhere we have the mqtt_state_t we also have the lmqtt_userdata. * mqtt: doc fixes * mqtt: note bug * tls: allow :on(...,nil) to unregister a callback
2020-04-07 14:06:27 +02:00
Setup [Last Will and Testament](http://www.hivemq.com/blog/mqtt-essentials-part-9-last-will-and-testament).
2016-01-10 22:52:05 +01:00
As the last will is sent to the broker when connecting, `lwt()` must be called BEFORE calling `connect()`.  
Networking rampage and accumulated fixes (#3060) * espconn: remove unused espconn code, take 1 This is the easiest part of https://github.com/nodemcu/nodemcu-firmware/issues/3004 . It removes a bunch of functions that were never called in our tree. * espconn: De-orbit espconn_gethostbyname Further work on https://github.com/nodemcu/nodemcu-firmware/issues/3004 While here, remove `mqtt`'s charming DNS-retry logic (which is neither shared with nor duplicated in other modules) and update its :connect() return value behavior and documentation. * espconn: remove scary global pktinfo A write-only global! How about that. * net: remove deprecated methods All the TLS stuff moved over there a long time ago, and net_createUDPSocket should just do what it says on the tin. * espconn_secure: remove ESPCONN_SERVER support We can barely function as a TLS client; being a TLS server seems like a real stretch. This code was never called from Lua anyway. * espconn_secure: more code removal * espconn_secure: simplify ssl options structure There is nothing "ssl_packet" about this structure. Get rid of the terrifying "pbuffer" pointer. Squash two structure types together and eliminate an unused field. * espconn_secure: refactor mbedtls_msg_info_load Split out espconn_mbedtls_parse, which we can use as part of our effort towards addressing https://github.com/nodemcu/nodemcu-firmware/issues/3032 * espconn_secure: introduce TLS cert/key callbacks The new feature part of https://github.com/nodemcu/nodemcu-firmware/issues/3032 Subsequent work will remove the old mechanism. * tls: add deprecation warnings * luacheck: net.ifinfo is a thing now * tls: remove use of espconn->reverse * mqtt: stop using espconn->reverse Instead, just place the espconn structure itself at the top of the user data. This enlarges the structure somewhat but removes one more layer of dynamic heap usage and NULL checks. While here, simplify the code a bit. * mqtt: remove redundant pointer to connect_info Everywhere we have the mqtt_state_t we also have the lmqtt_userdata. * mqtt: doc fixes * mqtt: note bug * tls: allow :on(...,nil) to unregister a callback
2020-04-07 14:06:27 +02:00
The broker will publish a client's last will message once it notices that the connection to the client is broken; that occurs when...
 - The client fails to send a keepalive packet for as long as specified in `mqtt.Client()`
Networking rampage and accumulated fixes (#3060) * espconn: remove unused espconn code, take 1 This is the easiest part of https://github.com/nodemcu/nodemcu-firmware/issues/3004 . It removes a bunch of functions that were never called in our tree. * espconn: De-orbit espconn_gethostbyname Further work on https://github.com/nodemcu/nodemcu-firmware/issues/3004 While here, remove `mqtt`'s charming DNS-retry logic (which is neither shared with nor duplicated in other modules) and update its :connect() return value behavior and documentation. * espconn: remove scary global pktinfo A write-only global! How about that. * net: remove deprecated methods All the TLS stuff moved over there a long time ago, and net_createUDPSocket should just do what it says on the tin. * espconn_secure: remove ESPCONN_SERVER support We can barely function as a TLS client; being a TLS server seems like a real stretch. This code was never called from Lua anyway. * espconn_secure: more code removal * espconn_secure: simplify ssl options structure There is nothing "ssl_packet" about this structure. Get rid of the terrifying "pbuffer" pointer. Squash two structure types together and eliminate an unused field. * espconn_secure: refactor mbedtls_msg_info_load Split out espconn_mbedtls_parse, which we can use as part of our effort towards addressing https://github.com/nodemcu/nodemcu-firmware/issues/3032 * espconn_secure: introduce TLS cert/key callbacks The new feature part of https://github.com/nodemcu/nodemcu-firmware/issues/3032 Subsequent work will remove the old mechanism. * tls: add deprecation warnings * luacheck: net.ifinfo is a thing now * tls: remove use of espconn->reverse * mqtt: stop using espconn->reverse Instead, just place the espconn structure itself at the top of the user data. This enlarges the structure somewhat but removes one more layer of dynamic heap usage and NULL checks. While here, simplify the code a bit. * mqtt: remove redundant pointer to connect_info Everywhere we have the mqtt_state_t we also have the lmqtt_userdata. * mqtt: doc fixes * mqtt: note bug * tls: allow :on(...,nil) to unregister a callback
2020-04-07 14:06:27 +02:00
 - The TCP connection is properly closed (without closing the mqtt-connection before)
- The broker tries to send data to the client and the TCP connection breaks.
This means if you specified 120 as keepalive timer, just turn off the client device and the broker does not send any data to the client, the last will message will be published 120s after turning off the device.
Networking rampage and accumulated fixes (#3060) * espconn: remove unused espconn code, take 1 This is the easiest part of https://github.com/nodemcu/nodemcu-firmware/issues/3004 . It removes a bunch of functions that were never called in our tree. * espconn: De-orbit espconn_gethostbyname Further work on https://github.com/nodemcu/nodemcu-firmware/issues/3004 While here, remove `mqtt`'s charming DNS-retry logic (which is neither shared with nor duplicated in other modules) and update its :connect() return value behavior and documentation. * espconn: remove scary global pktinfo A write-only global! How about that. * net: remove deprecated methods All the TLS stuff moved over there a long time ago, and net_createUDPSocket should just do what it says on the tin. * espconn_secure: remove ESPCONN_SERVER support We can barely function as a TLS client; being a TLS server seems like a real stretch. This code was never called from Lua anyway. * espconn_secure: more code removal * espconn_secure: simplify ssl options structure There is nothing "ssl_packet" about this structure. Get rid of the terrifying "pbuffer" pointer. Squash two structure types together and eliminate an unused field. * espconn_secure: refactor mbedtls_msg_info_load Split out espconn_mbedtls_parse, which we can use as part of our effort towards addressing https://github.com/nodemcu/nodemcu-firmware/issues/3032 * espconn_secure: introduce TLS cert/key callbacks The new feature part of https://github.com/nodemcu/nodemcu-firmware/issues/3032 Subsequent work will remove the old mechanism. * tls: add deprecation warnings * luacheck: net.ifinfo is a thing now * tls: remove use of espconn->reverse * mqtt: stop using espconn->reverse Instead, just place the espconn structure itself at the top of the user data. This enlarges the structure somewhat but removes one more layer of dynamic heap usage and NULL checks. While here, simplify the code a bit. * mqtt: remove redundant pointer to connect_info Everywhere we have the mqtt_state_t we also have the lmqtt_userdata. * mqtt: doc fixes * mqtt: note bug * tls: allow :on(...,nil) to unregister a callback
2020-04-07 14:06:27 +02:00
!!! note
There is at present a bug in the NodeMCU MQTT library that results in all disconnections
appearing as unexpected disconnects -- the MQTT-level disconnection message is not set
before the TCP connection is torn down. As a result, LWT messages will almost always be
published. See https://github.com/nodemcu/nodemcu-firmware/issues/3031
2016-01-10 22:52:05 +01:00
#### Syntax
`mqtt:lwt(topic, message[, qos[, retain]])`
2016-01-10 22:52:05 +01:00
#### Parameters
- `topic` the topic to publish to (string)
- `message` the message to publish, (buffer or string)
- `qos` QoS level, default 0
- `retain` retain flag, default 0
#### Returns
`nil`
## mqtt.client:on()
Registers a callback function for an event.
#### Syntax
`mqtt:on(event, function(client[, topic[, message]]))`
2016-01-10 22:52:05 +01:00
#### Parameters
- `event` can be "connect", "connfail", "suback", "unsuback", "puback", "message", "overflow", or "offline"
- callback function. The first parameter is always the client object itself.
Any remaining parameters passed differ by event:
- If event is "message", the 2nd and 3rd parameters are received topic and
message, respectively, as Lua strings.
- If the event is "overflow", the parameters are as with "message", save
that the message string is truncated to the maximum message size.
- If the event is "connfail", the 2nd parameter will be the connection
failure code; see above.
- Other event types do not provide additional arguments. This has some
unfortunate consequences: the broker-provided subscription maximum QoS
information is lost, and the application must, if it expects per-event
acknowledgements, manage a queue or queues itself.
2016-01-10 22:52:05 +01:00
#### Returns
`nil`
## mqtt.client:publish()
Publishes a message.
#### Syntax
`mqtt:publish(topic, payload, qos, retain[, function(client)])`
2016-01-10 22:52:05 +01:00
#### Parameters
- `topic` the topic to publish to ([topic string](http://www.hivemq.com/blog/mqtt-essentials-part-5-mqtt-topics-best-practices))
- `message` the message to publish, (buffer or string)
- `qos` QoS level
- `retain` retain flag
- `function(client)` optional callback fired when PUBACK received (for QoS 1
or 2) or when message sent (for QoS 0).
#### Notes
2019-02-17 19:26:29 +01:00
When calling publish() more than once, the last callback function defined will
be called for ALL publish commands. This callback argument also aliases with
the "puback" callback for `:on()`.
2016-01-10 22:52:05 +01:00
#### Returns
`true` on success, `false` otherwise
2016-01-10 22:52:05 +01:00
## mqtt.client:subscribe()
Subscribes to one or several topics.
#### Syntax
2016-01-30 10:23:27 +01:00
`mqtt:subscribe(topic, qos[, function(client)])`
`mqtt:subscribe(table[, function(client)])`
2016-01-10 22:52:05 +01:00
#### Parameters
- `topic` a [topic string](http://www.hivemq.com/blog/mqtt-essentials-part-5-mqtt-topics-best-practices)
- `qos` QoS subscription level, default 0
- `table` array of 'topic, qos' pairs to subscribe to
- `function(client)` optional callback fired when subscription(s) succeeded.
#### Notes
When calling subscribe() more than once, the last callback function defined
will be called for ALL subscribe commands. This callback argument also aliases
with the "suback" callback for `:on()`.
2016-01-10 22:52:05 +01:00
#### Returns
`true` on success, `false` otherwise
#### Example
```lua
-- subscribe topic with qos = 0
m:subscribe("/topic",0, function(conn) print("subscribe success") end)
-- or subscribe multiple topic (topic/0, qos = 0; topic/1, qos = 1; topic2 , qos = 2)
m:subscribe({["topic/0"]=0,["topic/1"]=1,topic2=2}, function(conn) print("subscribe success") end)
2016-01-29 22:40:15 +01:00
```
2016-03-20 00:24:18 +01:00
2017-08-22 22:05:24 +02:00
!!! caution
Rather than calling `subscribe` multiple times you should use the multiple topics syntax shown in the above example if you want to subscribe to more than one topic at once.
2016-03-20 00:24:18 +01:00
## mqtt.client:unsubscribe()
Unsubscribes from one or several topics.
#### Syntax
`mqtt:unsubscribe(topic[, function(client)])`
`mqtt:unsubscribe(table[, function(client)])`
#### Parameters
- `topic` a [topic string](http://www.hivemq.com/blog/mqtt-essentials-part-5-mqtt-topics-best-practices)
- `table` array of 'topic, anything' pairs to unsubscribe from
- `function(client)` optional callback fired when unsubscription(s) succeeded.
#### Notes
When calling subscribe() more than once, the last callback function defined
will be called for ALL subscribe commands. This callback argument also aliases
with the "unsuback" callback for `:on()`.
2016-03-20 00:24:18 +01:00
#### Returns
`true` on success, `false` otherwise
#### Example
```lua
-- unsubscribe topic
m:unsubscribe("/topic", function(conn) print("unsubscribe success") end)
-- or unsubscribe multiple topic (topic/0; topic/1; topic2)
m:unsubscribe({["topic/0"]=0,["topic/1"]=0,topic2="anything"}, function(conn) print("unsubscribe success") end)
```