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
Tue Jan 10 18:10:17 2017 -0600
Update to mbed OS 5, use NetworkInterface for multiple connectivity methods - not just ethernet

Who changed what in which revision?

UserRevisionLine numberNew contents of line
icraggs1:a1d5c7a6acbc 1/*******************************************************************************
icraggs17:0811bdbdd78a 2 * Copyright (c) 2014, 2015 IBM Corp.
icraggs1:a1d5c7a6acbc 3 *
icraggs1:a1d5c7a6acbc 4 * All rights reserved. This program and the accompanying materials
icraggs1:a1d5c7a6acbc 5 * are made available under the terms of the Eclipse Public License v1.0
icraggs1:a1d5c7a6acbc 6 * and Eclipse Distribution License v1.0 which accompany this distribution.
icraggs1:a1d5c7a6acbc 7 *
icraggs1:a1d5c7a6acbc 8 * The Eclipse Public License is available at
icraggs1:a1d5c7a6acbc 9 *
icraggs1:a1d5c7a6acbc 10 * and the Eclipse Distribution License is available at
icraggs1:a1d5c7a6acbc 11 *
icraggs1:a1d5c7a6acbc 12 *
icraggs1:a1d5c7a6acbc 13 * Contributors:
icraggs1:a1d5c7a6acbc 14 * Ian Craggs - initial API and implementation and/or initial documentation
icraggs17:0811bdbdd78a 15 * Ian Craggs - make sure QoS2 processing works, and add device headers
icraggs1:a1d5c7a6acbc 16 *******************************************************************************/
Jan Jongboom 20:49c9daf2b0ff 17
icraggs2:638c854c0695 18 /**
icraggs2:638c854c0695 19 This is a sample program to illustrate the use of the MQTT Client library
icraggs2:638c854c0695 20 on the mbed platform. The Client class requires two classes which mediate
icraggs2:638c854c0695 21 access to system interfaces for networking and timing. As long as these two
icraggs2:638c854c0695 22 classes provide the required public programming interfaces, it does not matter
icraggs2:638c854c0695 23 what facilities they use underneath. In this program, they use the mbed
icraggs2:638c854c0695 24 system libraries.
Jan Jongboom 20:49c9daf2b0ff 25
icraggs2:638c854c0695 26 */
Jan Jongboom 20:49c9daf2b0ff 27
icraggs18:07a79d8f01c3 28 // change this to 0 to output messages to serial instead of LCD
icraggs18:07a79d8f01c3 29#define USE_LCD 1
icraggs1:a1d5c7a6acbc 30
icraggs18:07a79d8f01c3 31#if USE_LCD
icraggs17:0811bdbdd78a 32#include "C12832.h"
icraggs2:638c854c0695 33
Jan Jongboom 20:49c9daf2b0ff 34// the actual pins are defined in mbed_app.json and can be overridden per target
Jan Jongboom 20:49c9daf2b0ff 35C12832 lcd(LCD_MOSI, LCD_SCK, LCD_MISO, LCD_A0, LCD_NCS);
icraggs17:0811bdbdd78a 36
Jan Jongboom 20:49c9daf2b0ff 37#define logMessage lcd.cls();lcd.printf
Jan Jongboom 20:49c9daf2b0ff 38
Jan Jongboom 20:49c9daf2b0ff 39#else
Jan Jongboom 20:49c9daf2b0ff 40
Jan Jongboom 20:49c9daf2b0ff 41#define logMessage printf
icraggs18:07a79d8f01c3 42
icraggs18:07a79d8f01c3 43#endif
icraggs18:07a79d8f01c3 44
icraggs17:0811bdbdd78a 45#define MQTTCLIENT_QOS2 1
icraggs0:0cae29831d01 46
Jan Jongboom 20:49c9daf2b0ff 47#include "easy-connect.h"
Jan Jongboom 20:49c9daf2b0ff 48#include "MQTTNetwork.h"
Jan Jongboom 20:49c9daf2b0ff 49#include "MQTTmbed.h"
icraggs2:638c854c0695 50#include "MQTTClient.h"
icraggs2:638c854c0695 51
icraggs2:638c854c0695 52int arrivedcount = 0;
icraggs2:638c854c0695 53
icraggs8:a3e3113054a1 54
icraggs9:5beb8609e9f7 55void messageArrived(MQTT::MessageData& md)
icraggs2:638c854c0695 56{
icraggs9:5beb8609e9f7 57 MQTT::Message &message = md.message;
Jan Jongboom 20:49c9daf2b0ff 58 logMessage("Message arrived: qos %d, retained %d, dup %d, packetid %d\r\n", message.qos, message.retained, message.dup,;
Jan Jongboom 20:49c9daf2b0ff 59 logMessage("Payload %.*s\r\n", message.payloadlen, (char*)message.payload);
icraggs2:638c854c0695 60 ++arrivedcount;
icraggs2:638c854c0695 61}
icraggs2:638c854c0695 62
icraggs0:0cae29831d01 63
icraggs2:638c854c0695 64int main(int argc, char* argv[])
Jan Jongboom 20:49c9daf2b0ff 65{
Jan Jongboom 20:49c9daf2b0ff 66 float version = 0.6;
icraggs2:638c854c0695 67 char* topic = "mbed-sample";
Jan Jongboom 20:49c9daf2b0ff 68
Jan Jongboom 20:49c9daf2b0ff 69 logMessage("HelloMQTT: version is %.2f\r\n", version);
Jan Jongboom 20:49c9daf2b0ff 70
Jan Jongboom 20:49c9daf2b0ff 71 NetworkInterface* network = easy_connect(true);
Jan Jongboom 20:49c9daf2b0ff 72 if (!network) {
Jan Jongboom 20:49c9daf2b0ff 73 return -1;
Jan Jongboom 20:49c9daf2b0ff 74 }
Jan Jongboom 20:49c9daf2b0ff 75
Jan Jongboom 20:49c9daf2b0ff 76 MQTTNetwork mqttNetwork(network);
Jan Jongboom 20:49c9daf2b0ff 77
Jan Jongboom 20:49c9daf2b0ff 78 MQTT::Client<MQTTNetwork, Countdown> client = MQTT::Client<MQTTNetwork, Countdown>(mqttNetwork);
Jan Jongboom 20:49c9daf2b0ff 79
Jan Jongboom 20:49c9daf2b0ff 80 const char* hostname = "";
icraggs6:e4c690c45021 81 int port = 1883;
Jan Jongboom 20:49c9daf2b0ff 82 logMessage("Connecting to %s:%d\r\n", hostname, port);
Jan Jongboom 20:49c9daf2b0ff 83 int rc = mqttNetwork.connect(hostname, port);
icraggs6:e4c690c45021 84 if (rc != 0)
Jan Jongboom 20:49c9daf2b0ff 85 logMessage("rc from TCP connect is %d\r\n", rc);
Jan Jongboom 20:49c9daf2b0ff 86
Jan Jongboom 20:49c9daf2b0ff 87 MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
icraggs6:e4c690c45021 88 data.MQTTVersion = 3;
icraggs8:a3e3113054a1 89 data.clientID.cstring = "mbed-sample";
icraggs12:086a9314e8a5 90 data.username.cstring = "testuser";
icraggs12:086a9314e8a5 91 data.password.cstring = "testpassword";
icraggs16:28d062c5522b 92 if ((rc = client.connect(data)) != 0)
Jan Jongboom 20:49c9daf2b0ff 93 logMessage("rc from MQTT connect is %d\r\n", rc);
Jan Jongboom 20:49c9daf2b0ff 94
icraggs19:7f7aba7a4a8b 95 if ((rc = client.subscribe(topic, MQTT::QOS2, messageArrived)) != 0)
Jan Jongboom 20:49c9daf2b0ff 96 logMessage("rc from MQTT subscribe is %d\r\n", rc);
icraggs0:0cae29831d01 97
icraggs2:638c854c0695 98 MQTT::Message message;
icraggs0:0cae29831d01 99
icraggs2:638c854c0695 100 // QoS 0
icraggs2:638c854c0695 101 char buf[100];
Jan Jongboom 20:49c9daf2b0ff 102 sprintf(buf, "Hello World! QoS 0 message from app version %f\r\n", version);
icraggs2:638c854c0695 103 message.qos = MQTT::QOS0;
icraggs2:638c854c0695 104 message.retained = false;
icraggs2:638c854c0695 105 message.dup = false;
icraggs2:638c854c0695 106 message.payload = (void*)buf;
icraggs2:638c854c0695 107 message.payloadlen = strlen(buf)+1;
icraggs16:28d062c5522b 108 rc = client.publish(topic, message);
icraggs12:086a9314e8a5 109 while (arrivedcount < 1)
icraggs2:638c854c0695 110 client.yield(100);
Jan Jongboom 20:49c9daf2b0ff 111
icraggs2:638c854c0695 112 // QoS 1
Jan Jongboom 20:49c9daf2b0ff 113 sprintf(buf, "Hello World! QoS 1 message from app version %f\r\n", version);
icraggs2:638c854c0695 114 message.qos = MQTT::QOS1;
icraggs2:638c854c0695 115 message.payloadlen = strlen(buf)+1;
icraggs16:28d062c5522b 116 rc = client.publish(topic, message);
icraggs12:086a9314e8a5 117 while (arrivedcount < 2)
icraggs2:638c854c0695 118 client.yield(100);
Jan Jongboom 20:49c9daf2b0ff 119
icraggs2:638c854c0695 120 // QoS 2
Jan Jongboom 20:49c9daf2b0ff 121 sprintf(buf, "Hello World! QoS 2 message from app version %f\r\n", version);
icraggs2:638c854c0695 122 message.qos = MQTT::QOS2;
icraggs2:638c854c0695 123 message.payloadlen = strlen(buf)+1;
icraggs16:28d062c5522b 124 rc = client.publish(topic, message);
icraggs12:086a9314e8a5 125 while (arrivedcount < 3)
icraggs2:638c854c0695 126 client.yield(100);
Jan Jongboom 20:49c9daf2b0ff 127
icraggs17:0811bdbdd78a 128 if ((rc = client.unsubscribe(topic)) != 0)
Jan Jongboom 20:49c9daf2b0ff 129 logMessage("rc from unsubscribe was %d\r\n", rc);
Jan Jongboom 20:49c9daf2b0ff 130
icraggs8:a3e3113054a1 131 if ((rc = client.disconnect()) != 0)
Jan Jongboom 20:49c9daf2b0ff 132 logMessage("rc from disconnect was %d\r\n", rc);
Jan Jongboom 20:49c9daf2b0ff 133
Jan Jongboom 20:49c9daf2b0ff 134 mqttNetwork.disconnect();
Jan Jongboom 20:49c9daf2b0ff 135
Jan Jongboom 20:49c9daf2b0ff 136 logMessage("Version %.2f: finish %d msgs\r\n", version, arrivedcount);
Jan Jongboom 20:49c9daf2b0ff 137
icraggs0:0cae29831d01 138 return 0;
icraggs0:0cae29831d01 139}