|
| 1 | +### MQTT protocol |
| 2 | + |
| 3 | +MQTT is a machine-to-machine (M2M)/"Internet of Things" connectivity protocol. It was designed as an extremely lightweight publish/subscribe messaging transport. For more details see [http://mqtt.org/](http://mqtt.org/). |
| 4 | + |
| 5 | +By design MQTT operates over TCP protocol, so an MQTTSN (For MQTT for Sensor Networks) was designed, which uses less resources and can operate over connectionless procols (such as UDP), but it requires a special gateway. |
| 6 | + |
| 7 | +Mbed-os only allows you to create an MQTT client device, an external broker is needed for MQTT to operate. Clients can subscribe or publish to a topic, after connecting to a broker. |
| 8 | + |
| 9 | +### API and usage |
| 10 | + |
| 11 | +Mbed-os uses [Eclipse paho project emmbedded c implementation of MQTT protocol](https://github.com/eclipse/paho.mqtt.embedded-c) and [MQTT-SN protocol](https://github.com/eclipse/paho.mqtt-sn.embedded-c/). |
| 12 | + |
| 13 | +Mbed-os supports the original paho library interface and provides its own API, which wraps around the paho library. |
| 14 | + |
| 15 | +#### New API #### |
| 16 | + |
| 17 | +The new API expects to receive a pointer to a configured and connected socket. This socket will be used for further communication. |
| 18 | + |
| 19 | +For normal, non-secure operation of the MQTT a `TCPSocket` has to be provided. |
| 20 | + |
| 21 | +For secure communication over TCP a `TLSSocket` has to be provided. |
| 22 | + |
| 23 | +For MQTT-SN communication a `UDPSocket` has to be provided. |
| 24 | + |
| 25 | +Secure MQTT-SN communication (over a `DTLSSocket`) is not yet supported. |
| 26 | + |
| 27 | +After the socket is created, an instance of class `MQTTClient` can be created with the pointer to the socket as an argument. The class will distinguish between MQTT and MQTT-SN operation based on the socket's type. |
| 28 | + |
| 29 | +Example code: |
| 30 | + |
| 31 | +```cpp |
| 32 | +#include <MQTTClientMbedOs.h> |
| 33 | + |
| 34 | +NetworkInterface *net = NetworkInterface::get_default_instance(); |
| 35 | +SocketAddress sockAddr(hostname, port); |
| 36 | +TCPSocket socket; |
| 37 | +MQTTClient client(&socket); |
| 38 | +socket.open(net); |
| 39 | +socket.connect(sockAddr); |
| 40 | +``` |
| 41 | +
|
| 42 | +The socket has to be opened and connected in order for the client to be able to interact with the broker. |
| 43 | +
|
| 44 | +#### Legacy API #### |
| 45 | +
|
| 46 | +The original paho library can be used, with mbed-os providing our own `Network` template arguments (see below). |
| 47 | +
|
| 48 | +To communicate over MQTT an instance of a template class `MQTT::Client<Network, Timer>` has to be created. mbed-os provides two Network specializations: |
| 49 | +
|
| 50 | +* `MQTTNetwork` - to communicate over mbed-os's `TCPSocket` |
| 51 | +
|
| 52 | +* `MQTTNetworkTLS` - to communicate over mbed-os's `TLSSocket` |
| 53 | +
|
| 54 | +Paho's default `Timer` implementation, called `Countdown` and available from `MQTTmbed.h` can be used. |
| 55 | +
|
| 56 | +The `MQTT::Client` constructor takes one argument which is a pointer to a successfully connected NetworkInterface. |
| 57 | +
|
| 58 | +Below is an example of how to create an instance of an MQTTClient: |
| 59 | +
|
| 60 | +```cpp |
| 61 | +#include <MQTTNetwork.h> |
| 62 | +#include <MQTTClient.h> |
| 63 | +#include <MQTTmbed.h> // Countdown |
| 64 | +
|
| 65 | +NetworkInterface *net = NetworkInterface::get_default_instance(); |
| 66 | +MQTTNetwork mqttNet(net); |
| 67 | +MQTT::Client<MQTTNetwork, Countdown> client(mqttNet); |
| 68 | +mqttNet.connect(mqtt_global::hostname, mqtt_global::port); |
| 69 | +``` |
| 70 | + |
| 71 | +The MQTTNetwork has to `connect` to a broker, given its address and port number. |
| 72 | + |
| 73 | + |
| 74 | + |
| 75 | +Apart from lanaguage details and different creation and initialization procedure, the APIs of both the legacy and new API are the same. |
| 76 | + |
| 77 | +Once connected the MQTT/MQTT-SN client is able to: |
| 78 | + |
| 79 | +* `connect` to the broker, based on a filled `MQTTPacket_connectData` structure, |
| 80 | +* `diconnect` from the broker, |
| 81 | +* `subscribe` to a topic and register a callback function to be called whenever a new message arrives, |
| 82 | +* `unsubscribe` from a topic, |
| 83 | +* `publish` messages defined with `MQTT::Message` structure to a topic, |
| 84 | +* if no operation is required, but an MQTT connection should be kept open, the client can `yield`, |
| 85 | +* `MQTTNetworkTLS` requires a certificate to be set using `set_root_cert()` before calling connect. |
0 commit comments