HelloMQTT over TLS.

Dependencies:   MQTT

Fork of HelloMQTT by Osamu Koizumi

HelloMQTT over TLS

This program is a fork of HelloMQTT. Added TLS capability by using TLSSocket library. Tested on K64F.

This sample program connects to iot.eclipse.org:8883 by default. Verifies server identification. Subscribes a certain topic. Then publishes three messages with different QoSs, i.e. QoS0, QoS1, and QoS2.

Warning

Some brokers do not accept QoS2 and/or QoS1 message. For example, AWS IoT Message Broker doesn't accept QoS2. In such broker, this program doesn't work as is. Change QoS level.

Output from console

HelloMQTT: version is 0.70

Opening network interface...
Network interface opened successfully.

Connecting to host iot.eclipse.org:8883 ...
Connection established.

MQTT client is trying to connect the server ...
Client connected.

Client is trying to subscribe a topic "mbed-test".
Client has subscribed a topic "mbed-test".

Client publishes messages ...
Publishing message QoS 0.
QoS 0 message published.
! Message arrived: qos 0, retained 0, dup 0, packetid 6257
! Payload Hello World!  QoS 0 message from app version 0.700000

Publishing message QoS 1.
QoS 1 message published.
! Message arrived: qos 1, retained 0, dup 0, packetid 1
! Payload Hello World!  QoS 1 message from app version 0.700000

Publishing message QoS 2.
QoS 2 message published.
! Message arrived: qos 2, retained 0, dup 0, packetid 2
! Payload Hello World!  QoS 2 message from app version 0.700000

Version 0.70: finish 3 msgs

Known Issues

On K64F, when serial baud rate is changed from 9600 to 115200, program fails.

Committer:
Osamu Koizumi
Date:
Fri Jun 08 15:36:05 2018 +0900
Branch:
mutualAuth
Revision:
35:c8fd5859a455
Parent:
34:8f7a465c2192
Child:
39:1421615a44f5
Roll back easy-connect.

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
icraggs 17:0811bdbdd78a 28 #define MQTTCLIENT_QOS2 1
icraggs 0:0cae29831d01 29
Jan Jongboom 20:49c9daf2b0ff 30 #include "easy-connect.h"
Jan Jongboom 20:49c9daf2b0ff 31 #include "MQTTNetwork.h"
Jan Jongboom 20:49c9daf2b0ff 32 #include "MQTTmbed.h"
icraggs 2:638c854c0695 33 #include "MQTTClient.h"
Osamu Koizumi 24:e7cf8b02e012 34 #include "MQTT_server_setting.h"
Osamu Koizumi 34:8f7a465c2192 35 #include "mbed-trace/mbed_trace.h"
icraggs 2:638c854c0695 36
Osamu Koizumi 35:c8fd5859a455 37 #undef USE_TLS
Osamu Koizumi 35:c8fd5859a455 38 #if defined(MBED_CONF_APP_USE_TLS) && (MBED_CONF_APP_USE_TLS == 1)
Osamu Koizumi 35:c8fd5859a455 39 #define USE_TLS
Osamu Koizumi 35:c8fd5859a455 40 #endif
Osamu Koizumi 35:c8fd5859a455 41
Osamu Koizumi 24:e7cf8b02e012 42 // Number of messages received from server.
Osamu Koizumi 35:c8fd5859a455 43 volatile unsigned int arrivedcount = 0;
icraggs 2:638c854c0695 44
Osamu Koizumi 24:e7cf8b02e012 45 /*
Osamu Koizumi 24:e7cf8b02e012 46 * Callback function called when a message arrived from server.
Osamu Koizumi 24:e7cf8b02e012 47 */
icraggs 9:5beb8609e9f7 48 void messageArrived(MQTT::MessageData& md)
icraggs 2:638c854c0695 49 {
icraggs 9:5beb8609e9f7 50 MQTT::Message &message = md.message;
Osamu Koizumi 24:e7cf8b02e012 51 printf("! Message arrived: qos %d, retained %d, dup %d, packetid %d\r\n",
Osamu Koizumi 24:e7cf8b02e012 52 message.qos, message.retained, message.dup, message.id);
Osamu Koizumi 24:e7cf8b02e012 53 printf("! Payload %.*s\r\n", message.payloadlen, (char*)message.payload);
icraggs 2:638c854c0695 54 ++arrivedcount;
icraggs 2:638c854c0695 55 }
icraggs 0:0cae29831d01 56
icraggs 2:638c854c0695 57
icraggs 2:638c854c0695 58 int main(int argc, char* argv[])
Jan Jongboom 20:49c9daf2b0ff 59 {
Osamu Koizumi 34:8f7a465c2192 60 mbed_trace_init();
Osamu Koizumi 34:8f7a465c2192 61
Osamu Koizumi 24:e7cf8b02e012 62 NetworkInterface* network = NULL;
Osamu Koizumi 24:e7cf8b02e012 63 MQTTNetwork* mqttNetwork = NULL;
Osamu Koizumi 24:e7cf8b02e012 64 MQTT::Client<MQTTNetwork, Countdown>* mqttClient = NULL;
Osamu Koizumi 24:e7cf8b02e012 65
Osamu Koizumi 24:e7cf8b02e012 66 const float version = 0.7;
Osamu Koizumi 34:8f7a465c2192 67 const char* topic = "mbed-test";
Osamu Koizumi 24:e7cf8b02e012 68 bool isSubscribed = false;
Osamu Koizumi 24:e7cf8b02e012 69
Osamu Koizumi 24:e7cf8b02e012 70 printf("HelloMQTT: version is %.2f\r\n", version);
Osamu Koizumi 24:e7cf8b02e012 71 printf("\r\n");
Osamu Koizumi 24:e7cf8b02e012 72
Osamu Koizumi 24:e7cf8b02e012 73 printf("Opening network interface...\r\n");
Osamu Koizumi 24:e7cf8b02e012 74 {
Osamu Koizumi 24:e7cf8b02e012 75 network = easy_connect(false); // If true, prints out connection details.
Osamu Koizumi 24:e7cf8b02e012 76 if (!network) {
Osamu Koizumi 24:e7cf8b02e012 77 printf("Unable to open network interface.\r\n");
Osamu Koizumi 24:e7cf8b02e012 78 return -1;
Osamu Koizumi 24:e7cf8b02e012 79 }
Osamu Koizumi 24:e7cf8b02e012 80 }
Osamu Koizumi 24:e7cf8b02e012 81 printf("Network interface opened successfully.\r\n");
Osamu Koizumi 24:e7cf8b02e012 82 printf("\r\n");
Osamu Koizumi 24:e7cf8b02e012 83
Osamu Koizumi 24:e7cf8b02e012 84 printf("Connecting to host %s:%d ...\r\n", MQTT_SERVER_HOST_NAME, MQTT_SERVER_PORT);
Osamu Koizumi 24:e7cf8b02e012 85 {
Osamu Koizumi 24:e7cf8b02e012 86 mqttNetwork = new MQTTNetwork(network);
Osamu Koizumi 35:c8fd5859a455 87 #ifdef USE_TLS
Osamu Koizumi 34:8f7a465c2192 88 int rc = mqttNetwork->connect(MQTT_SERVER_HOST_NAME, MQTT_SERVER_PORT, SSL_CA_PEM,
Osamu Koizumi 34:8f7a465c2192 89 SSL_CLIENT_CERT_PEM, SSL_CLIENT_PRIVATE_KEY_PEM);
Osamu Koizumi 35:c8fd5859a455 90 #else
Osamu Koizumi 35:c8fd5859a455 91 int rc = mqttNetwork->connect(MQTT_SERVER_HOST_NAME, MQTT_SERVER_PORT);
Osamu Koizumi 35:c8fd5859a455 92 #endif /* USE_TLS */
Osamu Koizumi 24:e7cf8b02e012 93 if (rc != MQTT::SUCCESS){
Osamu Koizumi 24:e7cf8b02e012 94 printf("ERROR: rc from TCP connect is %d\r\n", rc);
Osamu Koizumi 24:e7cf8b02e012 95 goto ERROR;
Osamu Koizumi 24:e7cf8b02e012 96 }
Osamu Koizumi 24:e7cf8b02e012 97 }
Osamu Koizumi 24:e7cf8b02e012 98 printf("Connection established.\r\n");
Osamu Koizumi 24:e7cf8b02e012 99 printf("\r\n");
Osamu Koizumi 24:e7cf8b02e012 100
Osamu Koizumi 24:e7cf8b02e012 101 printf("MQTT client is trying to connect the server ...\r\n");
Osamu Koizumi 24:e7cf8b02e012 102 {
Osamu Koizumi 24:e7cf8b02e012 103 MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
Osamu Koizumi 24:e7cf8b02e012 104 data.MQTTVersion = 3;
Osamu Koizumi 24:e7cf8b02e012 105 data.clientID.cstring = (char*)"mbed-sample";
Osamu Koizumi 24:e7cf8b02e012 106 data.username.cstring = (char*)"testuser";
Osamu Koizumi 24:e7cf8b02e012 107 data.password.cstring = (char*)"testpassword";
Osamu Koizumi 24:e7cf8b02e012 108
Osamu Koizumi 24:e7cf8b02e012 109 mqttClient = new MQTT::Client<MQTTNetwork, Countdown>(*mqttNetwork);
Osamu Koizumi 24:e7cf8b02e012 110 int rc = mqttClient->connect(data);
Osamu Koizumi 24:e7cf8b02e012 111 if (rc != MQTT::SUCCESS) {
Osamu Koizumi 24:e7cf8b02e012 112 printf("ERROR: rc from MQTT connect is %d\r\n", rc);
Osamu Koizumi 24:e7cf8b02e012 113 goto ERROR;
Osamu Koizumi 24:e7cf8b02e012 114 }
Osamu Koizumi 24:e7cf8b02e012 115 }
Osamu Koizumi 24:e7cf8b02e012 116 printf("Client connected.\r\n");
Osamu Koizumi 24:e7cf8b02e012 117 printf("\r\n");
Jan Jongboom 20:49c9daf2b0ff 118
Osamu Koizumi 29:bc323243010e 119 printf("Client is trying to subscribe a topic \"%s\".\r\n", topic);
Osamu Koizumi 24:e7cf8b02e012 120 {
Osamu Koizumi 24:e7cf8b02e012 121 int rc = mqttClient->subscribe(topic, MQTT::QOS2, messageArrived);
Osamu Koizumi 24:e7cf8b02e012 122 if (rc != MQTT::SUCCESS) {
Osamu Koizumi 24:e7cf8b02e012 123 printf("ERROR: rc from MQTT subscribe is %d\r\n", rc);
Osamu Koizumi 24:e7cf8b02e012 124 goto ERROR;
Osamu Koizumi 24:e7cf8b02e012 125 }
Osamu Koizumi 24:e7cf8b02e012 126 isSubscribed = true;
Osamu Koizumi 24:e7cf8b02e012 127 }
Osamu Koizumi 29:bc323243010e 128 printf("Client has subscribed a topic \"%s\".\r\n", topic);
Osamu Koizumi 24:e7cf8b02e012 129 printf("\r\n");
Osamu Koizumi 24:e7cf8b02e012 130
Osamu Koizumi 24:e7cf8b02e012 131 printf("Client publishes messages ...\r\n");
Osamu Koizumi 24:e7cf8b02e012 132 {
Osamu Koizumi 24:e7cf8b02e012 133 MQTT::Message message;
Osamu Koizumi 24:e7cf8b02e012 134 message.retained = false;
Osamu Koizumi 24:e7cf8b02e012 135 message.dup = false;
Osamu Koizumi 24:e7cf8b02e012 136
Osamu Koizumi 24:e7cf8b02e012 137 const size_t buf_size = 100;
Osamu Koizumi 24:e7cf8b02e012 138 char *buf = new char[buf_size];
Osamu Koizumi 35:c8fd5859a455 139 message.payload = (void*)buf;
Jan Jongboom 20:49c9daf2b0ff 140
Osamu Koizumi 24:e7cf8b02e012 141 const MQTT::QoS q[] = {MQTT::QOS0, MQTT::QOS1, MQTT::QOS2};
Osamu Koizumi 24:e7cf8b02e012 142 for(unsigned int i=0; i < (sizeof(q)/sizeof(q[0])); i++) {
Osamu Koizumi 24:e7cf8b02e012 143 message.qos = q[i];
Osamu Koizumi 35:c8fd5859a455 144 message.id = i;
Osamu Koizumi 24:e7cf8b02e012 145 sprintf(buf, "Hello World! QoS %d message from app version %f\r\n", i, version);
Osamu Koizumi 24:e7cf8b02e012 146 message.payloadlen = strlen(buf)+1;
Osamu Koizumi 24:e7cf8b02e012 147 // Publish a message.
Osamu Koizumi 35:c8fd5859a455 148 printf("Publishing message QoS %d.\r\n", i);
Osamu Koizumi 24:e7cf8b02e012 149 int rc = mqttClient->publish(topic, message);
Osamu Koizumi 24:e7cf8b02e012 150 if(rc != MQTT::SUCCESS) {
Osamu Koizumi 24:e7cf8b02e012 151 printf("ERROR: rc from MQTT publish is %d\r\n", rc);
Osamu Koizumi 24:e7cf8b02e012 152 goto ERROR;
Osamu Koizumi 24:e7cf8b02e012 153 }
Osamu Koizumi 24:e7cf8b02e012 154 printf("QoS %d message published.\r\n", i);
Osamu Koizumi 35:c8fd5859a455 155 while (arrivedcount < (i+1)){
Osamu Koizumi 24:e7cf8b02e012 156 mqttClient->yield(100);
Osamu Koizumi 35:c8fd5859a455 157 }
Osamu Koizumi 24:e7cf8b02e012 158 }
Osamu Koizumi 24:e7cf8b02e012 159 delete[] buf;
Osamu Koizumi 24:e7cf8b02e012 160 }
Osamu Koizumi 24:e7cf8b02e012 161 printf("Version %.2f: finish %d msgs\r\n", version, arrivedcount);
Osamu Koizumi 24:e7cf8b02e012 162
Osamu Koizumi 24:e7cf8b02e012 163 ERROR:
Osamu Koizumi 24:e7cf8b02e012 164 if(mqttClient) {
Osamu Koizumi 24:e7cf8b02e012 165 if(isSubscribed) {
Osamu Koizumi 24:e7cf8b02e012 166 mqttClient->unsubscribe(topic);
Osamu Koizumi 24:e7cf8b02e012 167 mqttClient->setMessageHandler(topic, 0);
Osamu Koizumi 24:e7cf8b02e012 168 }
Osamu Koizumi 24:e7cf8b02e012 169 if(mqttClient->isConnected())
Osamu Koizumi 24:e7cf8b02e012 170 mqttClient->disconnect();
Osamu Koizumi 24:e7cf8b02e012 171 delete mqttClient;
Osamu Koizumi 24:e7cf8b02e012 172 }
Osamu Koizumi 24:e7cf8b02e012 173 if(mqttNetwork) {
Osamu Koizumi 24:e7cf8b02e012 174 mqttNetwork->disconnect();
Osamu Koizumi 24:e7cf8b02e012 175 delete mqttNetwork;
Osamu Koizumi 24:e7cf8b02e012 176 }
Osamu Koizumi 24:e7cf8b02e012 177 if(network) {
Osamu Koizumi 24:e7cf8b02e012 178 network->disconnect();
Osamu Koizumi 24:e7cf8b02e012 179 // network is not created by new.
Jan Jongboom 20:49c9daf2b0ff 180 }
Jan Jongboom 20:49c9daf2b0ff 181
Osamu Koizumi 24:e7cf8b02e012 182 exit(0);
icraggs 0:0cae29831d01 183 }