A Threaded Secure MQTT Client example. Uses MBED TLS for SSL/TLS connection. QoS0 only for now. Example has been tested with K64F connected via Ethernet.
Fork of HelloMQTT by
main.cpp@23:06fac173529e, 2017-03-26 (annotated)
- Committer:
- vpcola
- Date:
- Sun Mar 26 04:35:46 2017 +0000
- Revision:
- 23:06fac173529e
- Parent:
- 22:826657a00c44
- Child:
- 25:326f00faa092
Code is stable now in both publish/subscribe ...
Who changed what in which revision?
User | Revision | Line number | New 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 | |
vpcola | 22:826657a00c44 | 28 | //#define MQTTCLIENT_QOS2 1 |
vpcola | 21:a7506c90aa84 | 29 | #include "mbed.h" |
vpcola | 21:a7506c90aa84 | 30 | #include "rtos.h" |
Jan Jongboom |
20:49c9daf2b0ff | 31 | #include "easy-connect.h" |
vpcola | 23:06fac173529e | 32 | #include "MQTTThreadedClient.h" |
vpcola | 23:06fac173529e | 33 | |
icraggs | 2:638c854c0695 | 34 | |
vpcola | 21:a7506c90aa84 | 35 | Serial pc(USBTX, USBRX, 115200); |
vpcola | 23:06fac173529e | 36 | Thread msgSender; |
vpcola | 23:06fac173529e | 37 | |
vpcola | 23:06fac173529e | 38 | static const char * clientID = "mbed-sample"; |
vpcola | 23:06fac173529e | 39 | static const char * userID = "mbedhacks"; |
vpcola | 23:06fac173529e | 40 | static const char * password = "qwer123"; |
vpcola | 23:06fac173529e | 41 | static const char * topic_1 = "mbed-sample"; |
vpcola | 23:06fac173529e | 42 | static const char * topic_2 = "test"; |
vpcola | 23:06fac173529e | 43 | |
icraggs | 2:638c854c0695 | 44 | int arrivedcount = 0; |
icraggs | 2:638c854c0695 | 45 | |
vpcola | 23:06fac173529e | 46 | void messageArrived(MessageData& md) |
icraggs | 2:638c854c0695 | 47 | { |
vpcola | 23:06fac173529e | 48 | Message &message = md.message; |
vpcola | 23:06fac173529e | 49 | printf("Arrived Callback 1 : qos %d, retained %d, dup %d, packetid %d\r\n", message.qos, message.retained, message.dup, message.id); |
vpcola | 22:826657a00c44 | 50 | printf("Payload [%.*s]\r\n", message.payloadlen, (char*)message.payload); |
icraggs | 2:638c854c0695 | 51 | ++arrivedcount; |
icraggs | 2:638c854c0695 | 52 | } |
icraggs | 0:0cae29831d01 | 53 | |
vpcola | 23:06fac173529e | 54 | class CallbackTest |
vpcola | 21:a7506c90aa84 | 55 | { |
vpcola | 23:06fac173529e | 56 | public: |
vpcola | 23:06fac173529e | 57 | |
vpcola | 23:06fac173529e | 58 | CallbackTest() |
vpcola | 23:06fac173529e | 59 | : arrivedcount(0) |
vpcola | 23:06fac173529e | 60 | {} |
vpcola | 23:06fac173529e | 61 | |
vpcola | 23:06fac173529e | 62 | void messageArrived(MessageData& md) |
vpcola | 21:a7506c90aa84 | 63 | { |
vpcola | 23:06fac173529e | 64 | Message &message = md.message; |
vpcola | 23:06fac173529e | 65 | printf("Arrived Callback 1 : qos %d, retained %d, dup %d, packetid %d\r\n", message.qos, message.retained, message.dup, message.id); |
vpcola | 23:06fac173529e | 66 | printf("Payload [%.*s]\r\n", message.payloadlen, (char*)message.payload); |
vpcola | 23:06fac173529e | 67 | ++arrivedcount; |
vpcola | 23:06fac173529e | 68 | } |
vpcola | 23:06fac173529e | 69 | |
vpcola | 23:06fac173529e | 70 | private: |
vpcola | 23:06fac173529e | 71 | |
vpcola | 23:06fac173529e | 72 | int arrivedcount; |
vpcola | 23:06fac173529e | 73 | }; |
icraggs | 2:638c854c0695 | 74 | |
icraggs | 2:638c854c0695 | 75 | int main(int argc, char* argv[]) |
Jan Jongboom |
20:49c9daf2b0ff | 76 | { |
Jan Jongboom |
20:49c9daf2b0ff | 77 | float version = 0.6; |
vpcola | 23:06fac173529e | 78 | CallbackTest testcb; |
Jan Jongboom |
20:49c9daf2b0ff | 79 | |
vpcola | 21:a7506c90aa84 | 80 | printf("HelloMQTT: version is %.2f\r\n", version); |
Jan Jongboom |
20:49c9daf2b0ff | 81 | |
Jan Jongboom |
20:49c9daf2b0ff | 82 | NetworkInterface* network = easy_connect(true); |
Jan Jongboom |
20:49c9daf2b0ff | 83 | if (!network) { |
Jan Jongboom |
20:49c9daf2b0ff | 84 | return -1; |
Jan Jongboom |
20:49c9daf2b0ff | 85 | } |
Jan Jongboom |
20:49c9daf2b0ff | 86 | |
vpcola | 23:06fac173529e | 87 | MQTTThreadedClient mqtt(network); |
Jan Jongboom |
20:49c9daf2b0ff | 88 | |
vpcola | 23:06fac173529e | 89 | const char* hostname = "mqtt.mbedhacks.com"; |
vpcola | 23:06fac173529e | 90 | // const char* hostname = "192.168.0.7"; |
icraggs | 6:e4c690c45021 | 91 | int port = 1883; |
vpcola | 21:a7506c90aa84 | 92 | printf("Connecting to %s:%d\r\n", hostname, port); |
vpcola | 23:06fac173529e | 93 | |
Jan Jongboom |
20:49c9daf2b0ff | 94 | |
Jan Jongboom |
20:49c9daf2b0ff | 95 | MQTTPacket_connectData data = MQTTPacket_connectData_initializer; |
icraggs | 6:e4c690c45021 | 96 | data.MQTTVersion = 3; |
vpcola | 23:06fac173529e | 97 | data.clientID.cstring = (char *) clientID; |
vpcola | 23:06fac173529e | 98 | data.username.cstring = (char *) userID; |
vpcola | 23:06fac173529e | 99 | data.password.cstring = (char *) password; |
vpcola | 23:06fac173529e | 100 | data.keepAliveInterval = 100; // default is 60 |
Jan Jongboom |
20:49c9daf2b0ff | 101 | |
vpcola | 23:06fac173529e | 102 | int rc = mqtt.connect(hostname, port, data); |
vpcola | 23:06fac173529e | 103 | if (rc != 0) |
vpcola | 23:06fac173529e | 104 | printf("rc from TCP connect is %d\r\n", rc); |
vpcola | 23:06fac173529e | 105 | |
vpcola | 23:06fac173529e | 106 | if ((rc = mqtt.subscribe(topic_1, QOS0, messageArrived)) != 0) |
vpcola | 23:06fac173529e | 107 | printf("rc from MQTT subscribe 1 is %d\r\n", rc); |
vpcola | 23:06fac173529e | 108 | |
vpcola | 23:06fac173529e | 109 | if ((rc = mqtt.subscribe(topic_2, QOS0, &testcb, &CallbackTest::messageArrived)) != 0) |
vpcola | 23:06fac173529e | 110 | printf("rc from MQTT subscribe 2 is %d\r\b", rc); |
Jan Jongboom |
20:49c9daf2b0ff | 111 | |
vpcola | 22:826657a00c44 | 112 | // Start the data producer |
vpcola | 23:06fac173529e | 113 | msgSender.start(mbed::callback(&mqtt, &MQTTThreadedClient::startListener)); |
vpcola | 21:a7506c90aa84 | 114 | |
vpcola | 23:06fac173529e | 115 | int i = 0; |
vpcola | 21:a7506c90aa84 | 116 | while(true) |
vpcola | 21:a7506c90aa84 | 117 | { |
vpcola | 23:06fac173529e | 118 | PubMessage message; |
vpcola | 23:06fac173529e | 119 | message.qos = QOS0; |
vpcola | 23:06fac173529e | 120 | message.id = 123; |
vpcola | 21:a7506c90aa84 | 121 | |
vpcola | 23:06fac173529e | 122 | strcpy(&message.topic[0], topic_1); |
vpcola | 23:06fac173529e | 123 | sprintf(&message.payload[0], "Testing %d", i); |
vpcola | 23:06fac173529e | 124 | message.payloadlen = strlen((const char *) &message.payload[0]); |
vpcola | 23:06fac173529e | 125 | mqtt.publish(message); |
vpcola | 23:06fac173529e | 126 | |
vpcola | 23:06fac173529e | 127 | i++; |
vpcola | 23:06fac173529e | 128 | //TODO: Nothing here yet ... |
vpcola | 23:06fac173529e | 129 | Thread::wait(1000); |
vpcola | 21:a7506c90aa84 | 130 | } |
Jan Jongboom |
20:49c9daf2b0ff | 131 | |
icraggs | 0:0cae29831d01 | 132 | } |