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:
34:8f7a465c2192
Updated TLSSocket library.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Osamu Koizumi 28:70c0694ae0cf 1 #ifndef _SERVER_ROT_CERT_H_
Osamu Koizumi 28:70c0694ae0cf 2 #define _SERVER_ROT_CERT_H_
Osamu Koizumi 28:70c0694ae0cf 3
Osamu Koizumi 28:70c0694ae0cf 4 const char MQTT_SERVER_HOST_NAME[] = "iot.eclipse.org";
Osamu Koizumi 28:70c0694ae0cf 5
Osamu Koizumi 28:70c0694ae0cf 6 #if MBED_CONF_APP_USE_TLS == 1
Osamu Koizumi 28:70c0694ae0cf 7 const int MQTT_SERVER_PORT = 8883;
Osamu Koizumi 28:70c0694ae0cf 8
Osamu Koizumi 34:8f7a465c2192 9 /*
Osamu Koizumi 34:8f7a465c2192 10 * Root CA certificate here in PEM format.
Osamu Koizumi 34:8f7a465c2192 11 * "-----BEGIN CERTIFICATE-----\n"
Osamu Koizumi 34:8f7a465c2192 12 * ...
Osamu Koizumi 34:8f7a465c2192 13 * "-----END CERTIFICATE-----\n";
Osamu Koizumi 34:8f7a465c2192 14 */
Osamu Koizumi 34:8f7a465c2192 15 const char SSL_CA_PEM[] =
Osamu Koizumi 28:70c0694ae0cf 16 // DST Root CA X3, which is Root CA of iot.eclipse.org
Osamu Koizumi 28:70c0694ae0cf 17 "-----BEGIN CERTIFICATE-----\n"
Osamu Koizumi 28:70c0694ae0cf 18 "MIIDSjCCAjKgAwIBAgIQRK+wgNajJ7qJMDmGLvhAazANBgkqhkiG9w0BAQUFADA/\n"
Osamu Koizumi 28:70c0694ae0cf 19 "MSQwIgYDVQQKExtEaWdpdGFsIFNpZ25hdHVyZSBUcnVzdCBDby4xFzAVBgNVBAMT\n"
Osamu Koizumi 28:70c0694ae0cf 20 "DkRTVCBSb290IENBIFgzMB4XDTAwMDkzMDIxMTIxOVoXDTIxMDkzMDE0MDExNVow\n"
Osamu Koizumi 28:70c0694ae0cf 21 "PzEkMCIGA1UEChMbRGlnaXRhbCBTaWduYXR1cmUgVHJ1c3QgQ28uMRcwFQYDVQQD\n"
Osamu Koizumi 28:70c0694ae0cf 22 "Ew5EU1QgUm9vdCBDQSBYMzCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEB\n"
Osamu Koizumi 28:70c0694ae0cf 23 "AN+v6ZdQCINXtMxiZfaQguzH0yxrMMpb7NnDfcdAwRgUi+DoM3ZJKuM/IUmTrE4O\n"
Osamu Koizumi 28:70c0694ae0cf 24 "rz5Iy2Xu/NMhD2XSKtkyj4zl93ewEnu1lcCJo6m67XMuegwGMoOifooUMM0RoOEq\n"
Osamu Koizumi 28:70c0694ae0cf 25 "OLl5CjH9UL2AZd+3UWODyOKIYepLYYHsUmu5ouJLGiifSKOeDNoJjj4XLh7dIN9b\n"
Osamu Koizumi 28:70c0694ae0cf 26 "xiqKqy69cK3FCxolkHRyxXtqqzTWMIn/5WgTe1QLyNau7Fqckh49ZLOMxt+/yUFw\n"
Osamu Koizumi 28:70c0694ae0cf 27 "7BZy1SbsOFU5Q9D8/RhcQPGX69Wam40dutolucbY38EVAjqr2m7xPi71XAicPNaD\n"
Osamu Koizumi 28:70c0694ae0cf 28 "aeQQmxkqtilX4+U9m5/wAl0CAwEAAaNCMEAwDwYDVR0TAQH/BAUwAwEB/zAOBgNV\n"
Osamu Koizumi 28:70c0694ae0cf 29 "HQ8BAf8EBAMCAQYwHQYDVR0OBBYEFMSnsaR7LHH62+FLkHX/xBVghYkQMA0GCSqG\n"
Osamu Koizumi 28:70c0694ae0cf 30 "SIb3DQEBBQUAA4IBAQCjGiybFwBcqR7uKGY3Or+Dxz9LwwmglSBd49lZRNI+DT69\n"
Osamu Koizumi 28:70c0694ae0cf 31 "ikugdB/OEIKcdBodfpga3csTS7MgROSR6cz8faXbauX+5v3gTt23ADq1cEmv8uXr\n"
Osamu Koizumi 28:70c0694ae0cf 32 "AvHRAosZy5Q6XkjEGB5YGV8eAlrwDPGxrancWYaLbumR9YbK+rlmM6pZW87ipxZz\n"
Osamu Koizumi 28:70c0694ae0cf 33 "R8srzJmwN0jP41ZL9c8PDHIyh8bwRLtTcm1D9SZImlJnt1ir/md2cXjbDaJWFBM5\n"
Osamu Koizumi 28:70c0694ae0cf 34 "JDGFoqgCWjBH4d1QB7wCCZAA62RjYJsWvIjJEubSfZGL+T0yjWW06XyxV3bqxbYo\n"
Osamu Koizumi 28:70c0694ae0cf 35 "Ob8VZRzI9neWagqNdwvYkQsEjgfbKbYK7p2CNTUQ\n"
Osamu Koizumi 28:70c0694ae0cf 36 "-----END CERTIFICATE-----\n";
Osamu Koizumi 34:8f7a465c2192 37
Osamu Koizumi 34:8f7a465c2192 38 /*
Osamu Koizumi 34:8f7a465c2192 39 * (optional) Client certificate here in PEM format.
Osamu Koizumi 34:8f7a465c2192 40 * Set NULL if you don't use.
Osamu Koizumi 34:8f7a465c2192 41 * "-----BEGIN CERTIFICATE-----\n"
Osamu Koizumi 34:8f7a465c2192 42 * ...
Osamu Koizumi 34:8f7a465c2192 43 * "-----END CERTIFICATE-----\n";
Osamu Koizumi 34:8f7a465c2192 44 */
Osamu Koizumi 34:8f7a465c2192 45 const char* SSL_CLIENT_CERT_PEM = NULL;
Osamu Koizumi 34:8f7a465c2192 46
Osamu Koizumi 34:8f7a465c2192 47
Osamu Koizumi 34:8f7a465c2192 48 /*
Osamu Koizumi 34:8f7a465c2192 49 * (optional) Client private key here in PEM format.
Osamu Koizumi 34:8f7a465c2192 50 * Set NULL if you don't use.
Osamu Koizumi 34:8f7a465c2192 51 * "-----BEGIN RSA PRIVATE KEY-----\n"
Osamu Koizumi 34:8f7a465c2192 52 * ...
Osamu Koizumi 34:8f7a465c2192 53 * "-----END RSA PRIVATE KEY-----\n";
Osamu Koizumi 34:8f7a465c2192 54 */
Osamu Koizumi 34:8f7a465c2192 55 const char* SSL_CLIENT_PRIVATE_KEY_PEM = NULL;
Osamu Koizumi 34:8f7a465c2192 56
Osamu Koizumi 28:70c0694ae0cf 57 #else
Osamu Koizumi 28:70c0694ae0cf 58 /* No TLS */
Osamu Koizumi 28:70c0694ae0cf 59 const int MQTT_SERVER_PORT = 1883;
Osamu Koizumi 28:70c0694ae0cf 60 #endif /* MBED_CONF_APP_USE_TLS == 1 */
Osamu Koizumi 28:70c0694ae0cf 61
Osamu Koizumi 28:70c0694ae0cf 62 #endif /* _SERVER_ROT_CERT */