Arduino PubSubClient | Arduino Client for MQTT
Description
The PubSubClient for the Arduino open-source electronics platform has been available since 2009. At the time, Arduino had recently released its first Ethernet Shield and it seemed a natural fit to run use MQTT.
With such a constrained environment, it was important to keep the library as small as possible. This could be achieved by only implementing the features of the protocol that made sense. In particular, it only supports Clean Sessions and does not support QoS 2 messages as there is such limited memory and no standard persistence mechanism.
The Arduino platform defines a standard api for network client libraries to implement. By allowing sketches to pass in any implementation of the API, the PubSubClient is able to support a wide range of Arduino-compatible hardware out of the box.
It has been used in a number of production systems and has recently been updated to support MQTT 3.1.1.
There are other constants defined it that file to control the version of MQTT used and the keepalive time, as well as constants that reflect the different connection states the client can be in.
Information
Arduino PubSubClient | |
---|---|
Language | C++ / Arduino |
License | MIT |
Website | http://pubsubclient.knolleary.net |
API Style | Blocking |
Features
Feature | |
---|---|
MQTT 3.1 | Yes |
MQTT 3.1.1 | Yes |
LWT | Yes |
SSL/TLS | Yes |
Automatic Reconnect | No |
QoS 0 | Yes |
QoS 1 | Yes |
QoS 2 | No |
Authentication | Yes |
Throttling | No |
Usage
Installation
The library can be installed into the Arduino IDE using the built-in Library Manager.
Open the Library Manager by selecting Sketch -> Include Library -> Manage Libraries…
Search for “PubSubClient” Click the “Install” button
The latest release can also be downloaded directly from GitHub
Maximum Message Size As part of minimising its footprint, it limits the size of any MQTT packet it can send or receive to 128 bytes. If you want to send or receive messages larger than this, you must change the value of MQTT_MAX_PACKET_SIZE
in PubSubClient.h
. The library allocates this much memory in its internal buffer, which reduces the memory available to the sketch itself.
Connect
The following example assumes you are using the standard Ethernet Shield. For other hardware types, refer to its documentation on how to initialise the appropriate network client.
|
|
The client connects with a default keepalive timer of 15 seconds. This can be configured by changing the value of MQTT_KEEPALIVE in PubSubClient.h.
If the call to mqttClient.connect returns false, the connection has failed for some reason. A call to mqttClient.state()
will provide more information. PubSubClient.h defines a number of constants that can be used to determine why the connection failed – for example, whether it was a network issue or the server rejected the connection with a known reason code.
Once connected, the mqttClient.loop()
function must be called regularly. This allows the client to maintain the connection and check for any incoming messages.
Connect with MQTT 3.1 or MQTT 3.1.1
In order to minimise the size of the library, the choice of MQTT version must be done at compile time. The version is chosen by changing the value of the MQTT_VERSION in PubSubClient.h – it defaults to MQTT 3.1.1:
// MQTT_VERSION : Pick the version
//#define MQTT_VERSION MQTT_VERSION_3_1
#define MQTT_VERSION MQTT_VERSION_3_1_1
Connect with LWT
|
|
Connect with Username / Password
|
|
Publish
The client only supports publishing at QoS 0:
|
|
The function will return true if the message was successfully published to the server. It will return false if:
- the client was not currently connected to the server, or
- the resulting MQTT packet to exceeded the libraries maximum packet size
Publish a retained Message
|
|
Subscribe
In order to subscribe to messages, a callback function must be set on the client. This is done using the setCallback function:
|
|
Then, once the client is connected, it can subscribe to a topic:
|
|
The call to subscribe will return true if the subscribe packet was successfully sent to the server – it does not block until the acknowledgment is received from the server.
It will return false if:
- the client was not currently connected to the server,
- an invalid qos was specified, or
- the topic was too long and caused the MQTT packet to exceed the libraries maximum packet size
If you want to publish a message from within the message callback function, it is necessary to make a copy of the topic and payload values as the client uses the same internal buffer for inbound and outbound messages:
|
|
Unsubscribe
|
|
As with the call to subscribe, this function will return true if the unsubscribe packet was successfully sent to the server – it does not block until the acknowledgment is received from the server.
It will return false if:
- the client was not currently connected to the server, or
- the topic was too long and caused the MQTT packet to exceed the libraries maximum packet size
Disconnect
|
|
This will disconnect from the broker and close the network connection. The client can be reconnected with a subsequent call to mqttClient.connect()
Full example application
The library provides a number of examples when added to the Arduino IDE. They can be accessed by selecting “File” -> “Examples” -> “PubSubClient”
Full API documentation is available here: https://pubsubclient.knolleary.net
The following is a basic example that connects to a broker, publishes a message and then subscribes to a given topic. Whenever a message is received it is printed to the Serial console.
|
|
Limitations
- It can only publish QoS 0 messages. It can subscribe at QoS 0 or QoS 1.
- The maximum message size, including header, is 128 bytes by default. This is configurable via
MQTT_MAX_PACKET_SIZE
inPubSubClient.h
. - The keepalive interval is set to 15 seconds by default. This is configurable via
MQTT_KEEPALIVE
inPubSubClient.h
. - The client uses MQTT 3.1.1 by default. It can be changed to use MQTT 3.1 by changing value of
MQTT_VERSION
inPubSubClient.h
.
Compatible Hardware
The library uses the Arduino Ethernet Client api for interacting with the underlying network hardware. This means it Just Works with a growing number of boards and shields, including:
- Arduino Ethernet
- Arduino Ethernet Shield
- Arduino YUN – use the included
YunClient
in place ofEthernetClient
, and be sure to do aBridge.begin()
first - Arduino WiFi Shield – if you want to send packets > 90 bytes with this shield, enable the
MQTT_MAX_TRANSFER_SIZE
define inPubSubClient.h
. - Sparkfun WiFly Shield – library
- TI CC3000 WiFi – library
- Intel Galileo/Edison
- ESP8266
- ESP32
The library cannot currently be used with hardware based on the ENC28J60 chip – such as the Nanode or the Nuelectronics Ethernet Shield. For those, there is an alternative library available.
Source – https://github.com/knolleary/pubsubclient
I hope you like this post. Do you have any questions? Leave a comment down below!
Thanks for reading. If you like this post probably you might like my next ones, so please support me by subscribing my blog.
Recommended:
- MQTT General Questions
- Adafruit IO ? | Adafruit IO Arduino Library | Adafruit MQTT
- MQTT – Publish/Subscribe and Message Queue
- MQTT Client Libraries
- MQTT v5.0 now an official OASIS standard
- MQTT Utility Plugins
- Using URIs to connect to a MQTT server
- HiveMQ MQTT Broker Overview | Installation
- MQTT Broker on Android | How To Run MQTT Broker in Android
- COAP vs MQTT | Difference between COAP and MQTT protocols
- ESP8266 as a MQTT Broker | How To Make ESP8266 as a MQTT Broker
- Mosquitto MQTT broker | Install Broker in AWS | Setting Up
- MQTT 5 | Overview | What’s New | MQTT Features | MQTT 5.0
- MQTT | What is MQTT | MQTT in Depth | QoS | FAQs | MQTT Introduction
- MQTT Servers/Brokers
- MQTT Products (that use MQTT)
- MQTT Tools – Web, Mobile platforms, Desktop tools, Gateways
- MQTT Public Brokers List
- How To Create Secure MQTT Broker