Sample MQTT program - simple send and receive

Dependencies:   C12832 MQTT

Fork of HelloMQTT by MQTT

Committer:
icraggs
Date:
Tue Oct 03 22:56:42 2017 +0000
Revision:
23:85b0a1df7d75
Parent:
21:a68bd76740f9
Update client id and hostname

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 23:85b0a1df7d75 46 #define MQTT_DEBUG 1
icraggs 0:0cae29831d01 47
Jan Jongboom 20:49c9daf2b0ff 48 #include "easy-connect.h"
Jan Jongboom 20:49c9daf2b0ff 49 #include "MQTTNetwork.h"
Jan Jongboom 20:49c9daf2b0ff 50 #include "MQTTmbed.h"
icraggs 2:638c854c0695 51 #include "MQTTClient.h"
icraggs 2:638c854c0695 52
icraggs 2:638c854c0695 53 int arrivedcount = 0;
icraggs 2:638c854c0695 54
icraggs 8:a3e3113054a1 55
icraggs 9:5beb8609e9f7 56 void messageArrived(MQTT::MessageData& md)
icraggs 2:638c854c0695 57 {
icraggs 9:5beb8609e9f7 58 MQTT::Message &message = md.message;
Jan Jongboom 20:49c9daf2b0ff 59 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 60 logMessage("Payload %.*s\r\n", message.payloadlen, (char*)message.payload);
icraggs 2:638c854c0695 61 ++arrivedcount;
icraggs 2:638c854c0695 62 }
icraggs 0:0cae29831d01 63
icraggs 2:638c854c0695 64
icraggs 2:638c854c0695 65 int main(int argc, char* argv[])
Jan Jongboom 20:49c9daf2b0ff 66 {
Jan Jongboom 20:49c9daf2b0ff 67 float version = 0.6;
icraggs 2:638c854c0695 68 char* topic = "mbed-sample";
Jan Jongboom 20:49c9daf2b0ff 69
Jan Jongboom 20:49c9daf2b0ff 70 logMessage("HelloMQTT: version is %.2f\r\n", version);
Jan Jongboom 20:49c9daf2b0ff 71
Jan Jongboom 20:49c9daf2b0ff 72 NetworkInterface* network = easy_connect(true);
Jan Jongboom 20:49c9daf2b0ff 73 if (!network) {
Jan Jongboom 20:49c9daf2b0ff 74 return -1;
Jan Jongboom 20:49c9daf2b0ff 75 }
Jan Jongboom 20:49c9daf2b0ff 76
Jan Jongboom 20:49c9daf2b0ff 77 MQTTNetwork mqttNetwork(network);
Jan Jongboom 20:49c9daf2b0ff 78
Jan Jongboom 21:a68bd76740f9 79 MQTT::Client<MQTTNetwork, Countdown> client(mqttNetwork);
Jan Jongboom 20:49c9daf2b0ff 80
icraggs 23:85b0a1df7d75 81 const char* hostname = "iot.eclipse.org";
icraggs 4:e4c690c45021 82 int port = 1883;
Jan Jongboom 20:49c9daf2b0ff 83 logMessage("Connecting to %s:%d\r\n", hostname, port);
Jan Jongboom 20:49c9daf2b0ff 84 int rc = mqttNetwork.connect(hostname, port);
icraggs 4:e4c690c45021 85 if (rc != 0)
Jan Jongboom 20:49c9daf2b0ff 86 logMessage("rc from TCP connect is %d\r\n", rc);
Jan Jongboom 20:49c9daf2b0ff 87
Jan Jongboom 20:49c9daf2b0ff 88 MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
icraggs 23:85b0a1df7d75 89 char clientid[30];
icraggs 23:85b0a1df7d75 90 sprintf(clientid, "mbed-sample-%d", rand());
icraggs 23:85b0a1df7d75 91 logMessage("Client id is %s\r\n", clientid);
icraggs 23:85b0a1df7d75 92 data.clientID.cstring = clientid;
icraggs 10:086a9314e8a5 93 data.username.cstring = "testuser";
icraggs 10:086a9314e8a5 94 data.password.cstring = "testpassword";
icraggs 16:28d062c5522b 95 if ((rc = client.connect(data)) != 0)
Jan Jongboom 20:49c9daf2b0ff 96 logMessage("rc from MQTT connect is %d\r\n", rc);
Jan Jongboom 20:49c9daf2b0ff 97
icraggs 19:7f7aba7a4a8b 98 if ((rc = client.subscribe(topic, MQTT::QOS2, messageArrived)) != 0)
Jan Jongboom 20:49c9daf2b0ff 99 logMessage("rc from MQTT subscribe is %d\r\n", rc);
icraggs 2:638c854c0695 100
icraggs 2:638c854c0695 101 MQTT::Message message;
icraggs 0:0cae29831d01 102
icraggs 2:638c854c0695 103 // QoS 0
icraggs 2:638c854c0695 104 char buf[100];
Jan Jongboom 20:49c9daf2b0ff 105 sprintf(buf, "Hello World! QoS 0 message from app version %f\r\n", version);
icraggs 2:638c854c0695 106 message.qos = MQTT::QOS0;
icraggs 2:638c854c0695 107 message.retained = false;
icraggs 2:638c854c0695 108 message.dup = false;
icraggs 2:638c854c0695 109 message.payload = (void*)buf;
icraggs 2:638c854c0695 110 message.payloadlen = strlen(buf)+1;
icraggs 16:28d062c5522b 111 rc = client.publish(topic, message);
icraggs 10:086a9314e8a5 112 while (arrivedcount < 1)
icraggs 2:638c854c0695 113 client.yield(100);
Jan Jongboom 20:49c9daf2b0ff 114
icraggs 2:638c854c0695 115 // QoS 1
Jan Jongboom 20:49c9daf2b0ff 116 sprintf(buf, "Hello World! QoS 1 message from app version %f\r\n", version);
icraggs 2:638c854c0695 117 message.qos = MQTT::QOS1;
icraggs 2:638c854c0695 118 message.payloadlen = strlen(buf)+1;
icraggs 16:28d062c5522b 119 rc = client.publish(topic, message);
icraggs 10:086a9314e8a5 120 while (arrivedcount < 2)
icraggs 2:638c854c0695 121 client.yield(100);
Jan Jongboom 20:49c9daf2b0ff 122
icraggs 2:638c854c0695 123 // QoS 2
Jan Jongboom 20:49c9daf2b0ff 124 sprintf(buf, "Hello World! QoS 2 message from app version %f\r\n", version);
icraggs 2:638c854c0695 125 message.qos = MQTT::QOS2;
icraggs 2:638c854c0695 126 message.payloadlen = strlen(buf)+1;
icraggs 16:28d062c5522b 127 rc = client.publish(topic, message);
icraggs 10:086a9314e8a5 128 while (arrivedcount < 3)
icraggs 2:638c854c0695 129 client.yield(100);
Jan Jongboom 20:49c9daf2b0ff 130
icraggs 17:0811bdbdd78a 131 if ((rc = client.unsubscribe(topic)) != 0)
Jan Jongboom 20:49c9daf2b0ff 132 logMessage("rc from unsubscribe was %d\r\n", rc);
Jan Jongboom 20:49c9daf2b0ff 133
icraggs 8:a3e3113054a1 134 if ((rc = client.disconnect()) != 0)
Jan Jongboom 20:49c9daf2b0ff 135 logMessage("rc from disconnect was %d\r\n", rc);
Jan Jongboom 20:49c9daf2b0ff 136
Jan Jongboom 20:49c9daf2b0ff 137 mqttNetwork.disconnect();
Jan Jongboom 20:49c9daf2b0ff 138
Jan Jongboom 20:49c9daf2b0ff 139 logMessage("Version %.2f: finish %d msgs\r\n", version, arrivedcount);
Jan Jongboom 20:49c9daf2b0ff 140
icraggs 0:0cae29831d01 141 return 0;
icraggs 0:0cae29831d01 142 }