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