|
| 1 | +# arduino-mqtt |
| 2 | + |
| 3 | +[](https://travis-ci.org/256dpi/arduino-mqtt) |
| 4 | +[](https://github.com/256dpi/arduino-mqtt/releases) |
| 5 | + |
| 6 | +This library bundles the [lwmqtt](https://github.com/256dpi/lwmqtt) MQTT 3.1.1 client and adds a thin wrapper to get an Arduino like API. |
| 7 | + |
| 8 | +Download the latest version from the [release](https://github.com/256dpi/arduino-mqtt/releases) section. Or even better use the builtin Library Manager in the Arduino IDE and search for "MQTT". |
| 9 | + |
| 10 | +The library is also available on [PlatformIO](https://platformio.org/lib/show/617/MQTT). You can install it by running: `pio lib install "MQTT"`. |
| 11 | + |
| 12 | +## Compatibility |
| 13 | + |
| 14 | +The following examples show how you can use the library with various Arduino compatible hardware: |
| 15 | + |
| 16 | +- [Arduino Yun & Yun-Shield](https://github.com/256dpi/arduino-mqtt/blob/master/examples/ArduinoYun/ArduinoYun.ino) ([SSL](https://github.com/256dpi/arduino-mqtt/blob/master/examples/ArduinoYun_SSL/ArduinoYun_SSL.ino)) |
| 17 | +- [Arduino Ethernet Shield](https://github.com/256dpi/arduino-mqtt/blob/master/examples/ArduinoEthernetShield/ArduinoEthernetShield.ino) |
| 18 | +- [Arduino WiFi Shield](https://github.com/256dpi/arduino-mqtt/blob/master/examples/ArduinoWiFiShield/ArduinoWiFiShield.ino) |
| 19 | +- [Adafruit HUZZAH ESP8266](https://github.com/256dpi/arduino-mqtt/blob/master/examples/AdafruitHuzzahESP8266/AdafruitHuzzahESP8266.ino) ([SSL](https://github.com/256dpi/arduino-mqtt/blob/master/examples/AdafruitHuzzahESP8266_SSL/AdafruitHuzzahESP8266_SSL.ino)) |
| 20 | +- [Arduino/Genuino WiFi101 Shield](https://github.com/256dpi/arduino-mqtt/blob/master/examples/ArduinoWiFi101/ArduinoWiFi101.ino) ([SSL](https://github.com/256dpi/arduino-mqtt/blob/master/examples/ArduinoWiFi101_SSL/ArduinoWiFi101_SSL.ino)) |
| 21 | +- [Arduino MKR GSM 1400](https://github.com/256dpi/arduino-mqtt/blob/master/examples/ArduinoMKRGSM1400/ArduinoMKRGSM1400.ino) ([SSL](https://github.com/256dpi/arduino-mqtt/blob/master/examples/ArduinoMKRGSM1400_SSL/ArduinoMKRGSM1400_SSL.ino)) |
| 22 | +- [ESP32 Development Board](https://github.com/256dpi/arduino-mqtt/blob/master/examples/ESP32DevelopmentBoard/ESP32DevelopmentBoard.ino) ([SSL](https://github.com/256dpi/arduino-mqtt/blob/master/examples/ESP32DevelopmentBoard_SSL/ESP32DevelopmentBoard_SSL.ino)) |
| 23 | + |
| 24 | +Other shields and boards should also work if they provide a [Client](https://www.arduino.cc/en/Reference/ClientConstructor) based network implementation. |
| 25 | + |
| 26 | +## Notes |
| 27 | + |
| 28 | +- The maximum size for packets being published and received is set by default to 128 bytes. To change the buffer sizes, you need to use `MQTTClient client(256)` instead of just `MQTTClient client` on the top of your sketch. The passed value denotes the read and write buffer size. |
| 29 | + |
| 30 | +- On the ESP8266 it has been reported that an additional `delay(10);` after `client.loop();` fixes many stability issues with WiFi connections. |
| 31 | + |
| 32 | +- To use the library with shiftr.io, you need to provide the token key (username) and token secret (password) as the second and third argument to `client.connect(name, key, secret)`. |
| 33 | + |
| 34 | +## Example |
| 35 | + |
| 36 | +The following example uses an Arduino MKR1000 to connect to shiftr.io. You can check on your device after a successful connection here: https://shiftr.io/try. |
| 37 | + |
| 38 | +```c++ |
| 39 | +#include <SPI.h> |
| 40 | +#include <WiFi101.h> |
| 41 | +#include <MQTT.h> |
| 42 | + |
| 43 | +const char ssid[] = "ssid"; |
| 44 | +const char pass[] = "pass"; |
| 45 | + |
| 46 | +WiFiClient net; |
| 47 | +MQTTClient client; |
| 48 | + |
| 49 | +unsigned long lastMillis = 0; |
| 50 | + |
| 51 | +void connect() { |
| 52 | + Serial.print("checking wifi..."); |
| 53 | + while (WiFi.status() != WL_CONNECTED) { |
| 54 | + Serial.print("."); |
| 55 | + delay(1000); |
| 56 | + } |
| 57 | + |
| 58 | + Serial.print("\nconnecting..."); |
| 59 | + while (!client.connect("arduino", "try", "try")) { |
| 60 | + Serial.print("."); |
| 61 | + delay(1000); |
| 62 | + } |
| 63 | + |
| 64 | + Serial.println("\nconnected!"); |
| 65 | + |
| 66 | + client.subscribe("/hello"); |
| 67 | + // client.unsubscribe("/hello"); |
| 68 | +} |
| 69 | + |
| 70 | +void messageReceived(String &topic, String &payload) { |
| 71 | + Serial.println("incoming: " + topic + " - " + payload); |
| 72 | +} |
| 73 | + |
| 74 | +void setup() { |
| 75 | + Serial.begin(115200); |
| 76 | + WiFi.begin(ssid, pass); |
| 77 | + |
| 78 | + // Note: Local domain names (e.g. "Computer.local" on OSX) are not supported by Arduino. |
| 79 | + // You need to set the IP address directly. |
| 80 | + client.begin("broker.shiftr.io", net); |
| 81 | + client.onMessage(messageReceived); |
| 82 | + |
| 83 | + connect(); |
| 84 | +} |
| 85 | + |
| 86 | +void loop() { |
| 87 | + client.loop(); |
| 88 | + |
| 89 | + if (!client.connected()) { |
| 90 | + connect(); |
| 91 | + } |
| 92 | + |
| 93 | + // publish a message roughly every second. |
| 94 | + if (millis() - lastMillis > 1000) { |
| 95 | + lastMillis = millis(); |
| 96 | + client.publish("/hello", "world"); |
| 97 | + } |
| 98 | +} |
| 99 | +``` |
| 100 | +
|
| 101 | +## API |
| 102 | +
|
| 103 | +Initialize the object using the hostname of the broker, the brokers port (default: `1883`) and the underlying Client class for network transport: |
| 104 | +
|
| 105 | +```c++ |
| 106 | +void begin(const char hostname[], Client &client); |
| 107 | +void begin(const char hostname[], int port, Client &client); |
| 108 | +``` |
| 109 | + |
| 110 | +- Specify port `8883` when using SSL clients for secure connections. |
| 111 | +- Local domain names (e.g. `Computer.local` on OSX) are not supported by Arduino. You need to set the IP address directly. |
| 112 | + |
| 113 | +The hostname and port can also be changed after calling `begin()`: |
| 114 | + |
| 115 | +```c++ |
| 116 | +void setHost(const char hostname[]); |
| 117 | +void setHost(const char hostname[], int port); |
| 118 | +``` |
| 119 | +
|
| 120 | +Set a will message (last testament) that gets registered on the broker after connecting: |
| 121 | +
|
| 122 | +```c++ |
| 123 | +void setWill(const char topic[]); |
| 124 | +void setWill(const char topic[], const char payload[]); |
| 125 | +void setWill(const char topic[], const char payload[], bool retained, int qos); |
| 126 | +void clearWill(); |
| 127 | +``` |
| 128 | + |
| 129 | +Register a callback to receive messages: |
| 130 | + |
| 131 | +```c++ |
| 132 | +void onMessage(MQTTClientCallbackSimple); |
| 133 | +// Callback signature: void messageReceived(String &topic, String &payload) {} |
| 134 | + |
| 135 | +void onMessageAdvanced(MQTTClientCallbackAdvanced); |
| 136 | +// Callback signature: void messageReceived(MQTTClient *client, char[] topic, char payload[], int payload_length) {} |
| 137 | +``` |
| 138 | +
|
| 139 | +- The set callback is mostly called during a call to `loop()` but may also be called during a call to `subscribe()`, `unsubscribe()` or `publish() // QoS > 0` if messages have been received before receiving the required acknowledgement. Therefore, it is strongly recommended to not call `subscribe()`, `unsubscribe()` or `publish() // QoS > 0` directly in the callback. |
| 140 | +
|
| 141 | +Set more advanced options: |
| 142 | +
|
| 143 | +```c++ |
| 144 | +void setOptions(int keepAlive, bool cleanSession, int timeout); |
| 145 | +``` |
| 146 | + |
| 147 | +- The `keepAlive` option controls the keep alive interval (default: 10). |
| 148 | +- The `cleanSession` option controls the session retention on the broker side (default: true). |
| 149 | +- The `timeout` option controls the default timeout for all commands in milliseconds (default: 1000). |
| 150 | + |
| 151 | +Connect to broker using the supplied client id and an optional username and password: |
| 152 | + |
| 153 | +```c++ |
| 154 | +bool connect(const char clientId[]); |
| 155 | +bool connect(const char clientId[], const char username[]); |
| 156 | +bool connect(const char clientId[], const char username[], const char password[]); |
| 157 | +``` |
| 158 | +
|
| 159 | +- This functions returns a boolean that indicates if the connection has been established successfully. |
| 160 | +
|
| 161 | +Publishes a message to the broker with an optional payload: |
| 162 | +
|
| 163 | +```c++ |
| 164 | +bool publish(const String &topic); |
| 165 | +bool publish(const char topic[]); |
| 166 | +bool publish(const String &topic, const String &payload); |
| 167 | +bool publish(const String &topic, const String &payload, bool retained, int qos); |
| 168 | +bool publish(const char topic[], const String &payload); |
| 169 | +bool publish(const char topic[], const String &payload, bool retained, int qos); |
| 170 | +bool publish(const char topic[], const char payload[]); |
| 171 | +bool publish(const char topic[], const char payload[], bool retained, int qos); |
| 172 | +bool publish(const char topic[], const char payload[], int length); |
| 173 | +bool publish(const char topic[], const char payload[], int length, bool retained, int qos); |
| 174 | +``` |
| 175 | + |
| 176 | +Subscribe to a topic: |
| 177 | + |
| 178 | +```c++ |
| 179 | +bool subscribe(const String &topic); |
| 180 | +bool subscribe(const String &topic, int qos); |
| 181 | +bool subscribe(const char topic[]); |
| 182 | +bool subscribe(const char topic[], int qos); |
| 183 | +``` |
| 184 | +
|
| 185 | +Unsubscribe from a topic: |
| 186 | +
|
| 187 | +```c++ |
| 188 | +bool unsubscribe(const String &topic); |
| 189 | +bool unsubscribe(const char topic[]); |
| 190 | +``` |
| 191 | + |
| 192 | +Sends and receives packets: |
| 193 | + |
| 194 | +```c++ |
| 195 | +bool loop(); |
| 196 | +``` |
| 197 | + |
| 198 | +- This function should be called in every `loop`. |
| 199 | + |
| 200 | +Check if the client is currently connected: |
| 201 | + |
| 202 | +```c++ |
| 203 | +bool connected(); |
| 204 | +``` |
| 205 | + |
| 206 | +Access low-level information for debugging: |
| 207 | + |
| 208 | +```c++ |
| 209 | +lwmqtt_err_t lastError(); |
| 210 | +lwmqtt_return_code_t returnCode(); |
| 211 | +``` |
| 212 | + |
| 213 | +- The error codes can be found [here](https://github.com/256dpi/lwmqtt/blob/master/include/lwmqtt.h#L11). |
| 214 | +- The return codes can be found [here](https://github.com/256dpi/lwmqtt/blob/master/include/lwmqtt.h#L243). |
| 215 | + |
| 216 | +Disconnect from the broker: |
| 217 | + |
| 218 | +```c++ |
| 219 | +bool disconnect(); |
| 220 | +``` |
| 221 | + |
| 222 | +## Release Management |
| 223 | + |
| 224 | +- Update version in `library.properties`. |
| 225 | +- Create release on GitHub. |
0 commit comments