HelloMQTT over TLS.
Fork of HelloMQTT by
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.
main.cpp
- Committer:
- icraggs
- Date:
- 2014-02-05
- Revision:
- 1:a1d5c7a6acbc
- Parent:
- 0:0cae29831d01
- Child:
- 2:638c854c0695
File content as of revision 1:a1d5c7a6acbc:
/******************************************************************************* * Copyright (c) 2014 IBM Corp. * * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * and Eclipse Distribution License v1.0 which accompany this distribution. * * The Eclipse Public License is available at * http://www.eclipse.org/legal/epl-v10.html * and the Eclipse Distribution License is available at * http://www.eclipse.org/org/documents/edl-v10.php. * * Contributors: * Ian Craggs - initial API and implementation and/or initial documentation *******************************************************************************/ #include "mbed.h" #include "EthernetInterface.h" #include "C12832_lcd.h" #include "MQTTPacket.h" DigitalOut myled(LED2); C12832_LCD lcd; int publish() { MQTTPacket_connectData data = MQTTPacket_connectData_initializer; int rc = 0; char buf[200]; int buflen = sizeof(buf); TCPSocketConnection mysock; MQTTString topicString = MQTTString_initializer; char* payload = "I'm alive!"; int payloadlen = strlen(payload); int len = 0; mysock.connect("m2m.eclipse.org", 1883); data.clientID.cstring = "mbed test client - Ian Craggs"; data.keepAliveInterval = 20; data.cleansession = 1; data.MQTTVersion = 3; len = MQTTSerialize_connect(buf, buflen, &data); topicString.cstring = "mbed NXP LPC1768"; len += MQTTSerialize_publish(buf + len, buflen - len, 0, 0, 0, 0, topicString, payload, payloadlen); len += MQTTSerialize_disconnect(buf + len, buflen - len); rc = 0; while (rc < len) { int rc1 = mysock.send(buf, len); if (rc1 == -1) { lcd.printf("Send failed\n"); break; } else rc += rc1; } if (rc == len) lcd.printf("Send succeeded\n"); wait(0.2); return 0; } int main() { EthernetInterface eth; eth.init(); //Use DHCP eth.connect(); lcd.printf("IP Address is %s\n", eth.getIPAddress()); while(1) { myled = 1; publish(); wait(0.2); myled = 0; publish(); wait(0.2); } eth.disconnect(); }