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 NetworkInterface APIs in mbed OS 5 to show how this works. The MQTT library contains an MQTTNetwork.h header, which is a wrapper around the mbed networking interface. To switch between connectivity methods (the default is Ethernet) the easy-connect library is provided in this example application. You can change the connectivity method in mbed_app.json.

Adding new connectivity methods to the program is trivial, as long as they implement the mbed OS 5 NetworkStack API.

Committer:
icraggs
Date:
Tue Jan 16 13:41:29 2018 +0000
Revision:
23:e38aaf532823
Parent:
21:a68bd76740f9
Update the MQTT library

Who changed what in which revision?

UserRevisionLine numberNew contents of line
icraggs 1:a1d5c7a6acbc 1 /*******************************************************************************
icraggs 17:0811bdbdd78a 2 * Copyright (c) 2014, 2015 IBM Corp.
icraggs 1:a1d5c7a6acbc 3 *
icraggs 1:a1d5c7a6acbc 4 * All rights reserved. This program and the accompanying materials
icraggs 1:a1d5c7a6acbc 5 * are made available under the terms of the Eclipse Public License v1.0
icraggs 1:a1d5c7a6acbc 6 * and Eclipse Distribution License v1.0 which accompany this distribution.
icraggs 1:a1d5c7a6acbc 7 *
icraggs 1:a1d5c7a6acbc 8 * The Eclipse Public License is available at
icraggs 1:a1d5c7a6acbc 9 * http://www.eclipse.org/legal/epl-v10.html
icraggs 1:a1d5c7a6acbc 10 * and the Eclipse Distribution License is available at
icraggs 1:a1d5c7a6acbc 11 * http://www.eclipse.org/org/documents/edl-v10.php.
icraggs 1:a1d5c7a6acbc 12 *
icraggs 1:a1d5c7a6acbc 13 * Contributors:
icraggs 1:a1d5c7a6acbc 14 * Ian Craggs - initial API and implementation and/or initial documentation
icraggs 17:0811bdbdd78a 15 * Ian Craggs - make sure QoS2 processing works, and add device headers
icraggs 1:a1d5c7a6acbc 16 *******************************************************************************/
Jan Jongboom 20:49c9daf2b0ff 17
icraggs 2:638c854c0695 18 /**
icraggs 2:638c854c0695 19 This is a sample program to illustrate the use of the MQTT Client library
icraggs 2:638c854c0695 20 on the mbed platform. The Client class requires two classes which mediate
icraggs 2:638c854c0695 21 access to system interfaces for networking and timing. As long as these two
icraggs 2:638c854c0695 22 classes provide the required public programming interfaces, it does not matter
icraggs 2:638c854c0695 23 what facilities they use underneath. In this program, they use the mbed
icraggs 2:638c854c0695 24 system libraries.
Jan Jongboom 20:49c9daf2b0ff 25
icraggs 2:638c854c0695 26 */
Jan Jongboom 20:49c9daf2b0ff 27
Jan Jongboom 21:a68bd76740f9 28 // change this to 1 to output messages to LCD instead of serial
Jan Jongboom 21:a68bd76740f9 29 #define USE_LCD 0
icraggs 1:a1d5c7a6acbc 30
icraggs 18:07a79d8f01c3 31 #if USE_LCD
icraggs 17:0811bdbdd78a 32 #include "C12832.h"
icraggs 2: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 35 C12832 lcd(LCD_MOSI, LCD_SCK, LCD_MISO, LCD_A0, LCD_NCS);
icraggs 17: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
icraggs 18:07a79d8f01c3 42
icraggs 18:07a79d8f01c3 43 #endif
icraggs 18:07a79d8f01c3 44
icraggs 17:0811bdbdd78a 45 #define MQTTCLIENT_QOS2 1
icraggs 0: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"
icraggs 2:638c854c0695 50 #include "MQTTClient.h"
icraggs 2:638c854c0695 51
icraggs 2:638c854c0695 52 int arrivedcount = 0;
icraggs 2:638c854c0695 53
icraggs 8:a3e3113054a1 54
icraggs 9:5beb8609e9f7 55 void messageArrived(MQTT::MessageData& md)
icraggs 2:638c854c0695 56 {
icraggs 9: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, message.id);
Jan Jongboom 20:49c9daf2b0ff 59 logMessage("Payload %.*s\r\n", message.payloadlen, (char*)message.payload);
icraggs 2:638c854c0695 60 ++arrivedcount;
icraggs 2:638c854c0695 61 }
icraggs 0:0cae29831d01 62
icraggs 2:638c854c0695 63
icraggs 2:638c854c0695 64 int main(int argc, char* argv[])
Jan Jongboom 20:49c9daf2b0ff 65 {
Jan Jongboom 20:49c9daf2b0ff 66 float version = 0.6;
icraggs 2: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 21:a68bd76740f9 78 MQTT::Client<MQTTNetwork, Countdown> client(mqttNetwork);
Jan Jongboom 20:49c9daf2b0ff 79
Jan Jongboom 20:49c9daf2b0ff 80 const char* hostname = "m2m.eclipse.org";
icraggs 6: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);
icraggs 6: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;
icraggs 6:e4c690c45021 88 data.MQTTVersion = 3;
icraggs 8:a3e3113054a1 89 data.clientID.cstring = "mbed-sample";
icraggs 12:086a9314e8a5 90 data.username.cstring = "testuser";
icraggs 12:086a9314e8a5 91 data.password.cstring = "testpassword";
icraggs 16: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
icraggs 19: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);
icraggs 2:638c854c0695 97
icraggs 2:638c854c0695 98 MQTT::Message message;
icraggs 0:0cae29831d01 99
icraggs 2:638c854c0695 100 // QoS 0
icraggs 2:638c854c0695 101 char buf[100];
Jan Jongboom 20:49c9daf2b0ff 102 sprintf(buf, "Hello World! QoS 0 message from app version %f\r\n", version);
icraggs 2:638c854c0695 103 message.qos = MQTT::QOS0;
icraggs 2:638c854c0695 104 message.retained = false;
icraggs 2:638c854c0695 105 message.dup = false;
icraggs 2:638c854c0695 106 message.payload = (void*)buf;
icraggs 2:638c854c0695 107 message.payloadlen = strlen(buf)+1;
icraggs 16:28d062c5522b 108 rc = client.publish(topic, message);
icraggs 12:086a9314e8a5 109 while (arrivedcount < 1)
icraggs 2:638c854c0695 110 client.yield(100);
Jan Jongboom 20:49c9daf2b0ff 111
icraggs 2:638c854c0695 112 // QoS 1
Jan Jongboom 20:49c9daf2b0ff 113 sprintf(buf, "Hello World! QoS 1 message from app version %f\r\n", version);
icraggs 2:638c854c0695 114 message.qos = MQTT::QOS1;
icraggs 2:638c854c0695 115 message.payloadlen = strlen(buf)+1;
icraggs 16:28d062c5522b 116 rc = client.publish(topic, message);
icraggs 12:086a9314e8a5 117 while (arrivedcount < 2)
icraggs 2:638c854c0695 118 client.yield(100);
Jan Jongboom 20:49c9daf2b0ff 119
icraggs 2:638c854c0695 120 // QoS 2
Jan Jongboom 20:49c9daf2b0ff 121 sprintf(buf, "Hello World! QoS 2 message from app version %f\r\n", version);
icraggs 2:638c854c0695 122 message.qos = MQTT::QOS2;
icraggs 2:638c854c0695 123 message.payloadlen = strlen(buf)+1;
icraggs 16:28d062c5522b 124 rc = client.publish(topic, message);
icraggs 12:086a9314e8a5 125 while (arrivedcount < 3)
icraggs 2:638c854c0695 126 client.yield(100);
Jan Jongboom 20:49c9daf2b0ff 127
icraggs 17: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
icraggs 8: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
icraggs 0:0cae29831d01 138 return 0;
icraggs 0:0cae29831d01 139 }