Sample MQTT program - simple send and receive

Dependencies:   C12832 MQTT

Dependents:   MQTT_G_SENSOR

This program and the MQTT libraries it uses are part of the EclipseTM Paho project; specifically the embedded client.

This example and API are working, but are still in progress. Please give us your feedback.

HelloMQTT is an example of using the MQTT API. The MQTT API is portable across network interface stacks. MQTT is designed to be used with TCP/IP, but any transport with similar characteristics should be suitable.

HelloMQTT uses the EthernetInterface to show how this works. The MQTT library contains an MQTTEthernet.h header, which is a wrapper around the mbed ethernet interface. To use the MQTT API with ethernet, include the following two headers:

#include "MQTTEthernet.h"
#include "MQTTClient.h"

then instantiate an MQTT client like this:

MQTTEthernet ipstack = MQTTEthernet();             
MQTT::Client<MQTTEthernet, Countdown> client = MQTT::Client<MQTTEthernet, Countdown>(ipstack);

Countdown is a timer class supplied in the MQTT library that is specific to mbed, so you should not need to change that.

If you want to use the MQTT API with a different network stack, create an interface header similar to MQTTEthernet.h. The class you create must have the following two methods at a minimum:

    int read(char* buffer, int len, int timeout);
    int write(char* buffer, int len, int timeout);

where the timeout is in milliseconds, and the return value is the number of characters read from or written to the network, or -1 on failure.


Jan Jongboom
3 months ago

File content as of revision 20:49c9daf2b0ff:

    "config": {
            "help": "options are ETHERNET,WIFI_ESP8266,MESH_LOWPAN_ND,MESH_THREAD",
            "value": "ETHERNET"
        "mesh_radio_type": {
            "help": "options are ATMEL, MCR20",
            "value": "ATMEL"
        "esp8266-tx": {
            "help": "Pin used as TX (connects to ESP8266 RX)",
            "value": "D1"
        "esp8266-rx": {
            "help": "Pin used as RX (connects to ESP8266 TX)",
            "value": "D0"
        "esp8266-ssid": {
            "value": "\"SSID\""
        "esp8266-password": {
            "value": "\"Password\""
        "esp8266-debug": {
            "value": true
        "lcd-mosi": {
            "value": "D11",
            "macro_name": "LCD_MOSI"
        "lcd-sck": {
            "value": "D13",
            "macro_name": "LCD_SCK"
        "lcd-miso": {
            "value": "D12",
            "macro_name": "LCD_MISO"
        "lcd-a0": {
            "value": "D7",
            "macro_name": "LCD_A0"
        "lcd-ncs": {
            "value": "D10",
            "macro_name": "LCD_NCS"
    "target_overrides": {
        "*": {
            "target.features_add": ["NANOSTACK", "LOWPAN_ROUTER", "COMMON_PAL"],
            "mbed-mesh-api.6lowpan-nd-channel-page": 0,
            "mbed-mesh-api.6lowpan-nd-channel": 12,
            "mbed-trace.enable": 0
        "HEXIWEAR": {
            "esp8266-tx": "PTD3",
            "esp8266-rx": "PTD2"
        "NUCLEO_F401RE": {
            "esp8266-tx": "D8",
            "esp8266-rx": "D2"
        "NUCLEO_F411RE": {
            "esp8266-tx": "D8",
            "esp8266-rx": "D2"