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
- Committer:
- vpcola
- Date:
- 2017-03-27
- Revision:
- 25:326f00faa092
- Parent:
- 23:06fac173529e
- Child:
- 26:4b21de8043a5
File content as of revision 25:326f00faa092:
/******************************************************************************* * Copyright (c) 2014, 2015 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 * Ian Craggs - make sure QoS2 processing works, and add device headers *******************************************************************************/ /** This is a sample program to illustrate the use of the MQTT Client library on the mbed platform. The Client class requires two classes which mediate access to system interfaces for networking and timing. As long as these two classes provide the required public programming interfaces, it does not matter what facilities they use underneath. In this program, they use the mbed system libraries. */ //#define MQTTCLIENT_QOS2 1 #include "mbed.h" #include "rtos.h" #include "easy-connect.h" #include "MQTTThreadedClient.h" Serial pc(USBTX, USBRX, 115200); Thread msgSender; static const char * clientID = "mbed-sample"; static const char * userID = "mbedhacks"; static const char * password = "qwer123"; static const char * topic_1 = "mbed-sample"; static const char * topic_2 = "test"; int arrivedcount = 0; void messageArrived(MessageData& md) { Message &message = md.message; printf("Arrived Callback 1 : qos %d, retained %d, dup %d, packetid %d\r\n", message.qos, message.retained, message.dup, message.id); printf("Payload [%.*s]\r\n", message.payloadlen, (char*)message.payload); ++arrivedcount; } class CallbackTest { public: CallbackTest() : arrivedcount(0) {} void messageArrived(MessageData& md) { Message &message = md.message; printf("Arrived Callback 2 : qos %d, retained %d, dup %d, packetid %d\r\n", message.qos, message.retained, message.dup, message.id); printf("Payload [%.*s]\r\n", message.payloadlen, (char*)message.payload); ++arrivedcount; } private: int arrivedcount; }; int main(int argc, char* argv[]) { float version = 0.6; CallbackTest testcb; printf("HelloMQTT: version is %.2f\r\n", version); NetworkInterface* network = easy_connect(true); if (!network) { return -1; } MQTTThreadedClient mqtt(network); const char* hostname = "mqtt.mbedhacks.com"; // const char* hostname = "192.168.0.7"; int port = 1883; printf("Connecting to %s:%d\r\n", hostname, port); MQTTPacket_connectData data = MQTTPacket_connectData_initializer; data.MQTTVersion = 3; data.clientID.cstring = (char *) clientID; data.username.cstring = (char *) userID; data.password.cstring = (char *) password; data.keepAliveInterval = 100; // default is 60 int rc = mqtt.connect(hostname, port, data); if (rc != 0) printf("rc from TCP connect is %d\r\n", rc); if ((rc = mqtt.subscribe(topic_1, QOS0, messageArrived)) != 0) printf("rc from MQTT subscribe 1 is %d\r\n", rc); if ((rc = mqtt.subscribe(topic_2, QOS0, &testcb, &CallbackTest::messageArrived)) != 0) printf("rc from MQTT subscribe 2 is %d\r\b", rc); // Start the data producer msgSender.start(mbed::callback(&mqtt, &MQTTThreadedClient::startListener)); int i = 0; while(true) { PubMessage message; message.qos = QOS0; message.id = 123; strcpy(&message.topic[0], topic_1); sprintf(&message.payload[0], "Testing %d", i); message.payloadlen = strlen((const char *) &message.payload[0]); mqtt.publish(message); i++; //TODO: Nothing here yet ... Thread::wait(6000); } }