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:
Mon Apr 16 18:55:41 2018 +0900
Revision:
24:e7cf8b02e012
Parent:
21:a68bd76740f9
Child:
29:bc323243010e
Added TLS support.

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