Osamu Koizumi / Mbed OS HelloMQTT

Dependencies:   MQTT

Fork of HelloMQTT by Osamu Koizumi

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /*******************************************************************************
00002  * Copyright (c) 2014, 2015 IBM Corp.
00003  *
00004  * All rights reserved. This program and the accompanying materials
00005  * are made available under the terms of the Eclipse Public License v1.0
00006  * and Eclipse Distribution License v1.0 which accompany this distribution.
00007  *
00008  * The Eclipse Public License is available at
00009  *    http://www.eclipse.org/legal/epl-v10.html
00010  * and the Eclipse Distribution License is available at
00011  *   http://www.eclipse.org/org/documents/edl-v10.php.
00012  *
00013  * Contributors:
00014  *    Ian Craggs - initial API and implementation and/or initial documentation
00015  *    Ian Craggs - make sure QoS2 processing works, and add device headers
00016  *******************************************************************************/
00017 
00018  /**
00019   This is a sample program to illustrate the use of the MQTT Client library
00020   on the mbed platform.  The Client class requires two classes which mediate
00021   access to system interfaces for networking and timing.  As long as these two
00022   classes provide the required public programming interfaces, it does not matter
00023   what facilities they use underneath. In this program, they use the mbed
00024   system libraries.
00025 
00026  */
00027 
00028 #define MQTTCLIENT_QOS2 1
00029 
00030 #include "easy-connect.h"
00031 #include "MQTTNetwork.h"
00032 #include "MQTTmbed.h"
00033 #include "MQTTClient.h"
00034 #include "MQTT_server_setting.h"
00035 #include "mbed-trace/mbed_trace.h"
00036 
00037 // Number of messages received from server.
00038 volatile unsigned int arrivedcount = 0;
00039 
00040 /*
00041  * Callback function called when a message arrived from server.
00042  */
00043 void messageArrived(MQTT::MessageData& md)
00044 {
00045     MQTT::Message &message = md.message;
00046     printf("! Message arrived: qos %d, retained %d, dup %d, packetid %d\r\n",
00047             message.qos, message.retained, message.dup, message.id);
00048     printf("! Payload %.*s\r\n", message.payloadlen, (char*)message.payload);
00049     ++arrivedcount;
00050 }
00051 
00052 
00053 int main(int argc, char* argv[])
00054 {
00055     mbed_trace_init();
00056 
00057     NetworkInterface* network = NULL;
00058     MQTTNetwork* mqttNetwork = NULL;
00059     MQTT::Client<MQTTNetwork, Countdown>* mqttClient = NULL;
00060     
00061     const float version = 0.7;
00062     const char* topic = "mbed-test";
00063     bool isSubscribed = false;
00064 
00065     printf("HelloMQTT: version is %.2f\r\n", version);
00066     printf("\r\n");
00067 
00068     printf("Opening network interface...\r\n");
00069     {
00070         network = easy_connect(false);    // If true, prints out connection details.
00071         if (!network) {
00072             printf("Unable to open network interface.\r\n");
00073             return -1;
00074         }
00075     }
00076     printf("Network interface opened successfully.\r\n");
00077     printf("\r\n");
00078 
00079     printf("Connecting to host %s:%d ...\r\n", MQTT_SERVER_HOST_NAME, MQTT_SERVER_PORT);
00080     {
00081         mqttNetwork = new MQTTNetwork(network);
00082 #if MBED_CONF_APP_USE_TLS == 1
00083         int rc = mqttNetwork->connect(MQTT_SERVER_HOST_NAME, MQTT_SERVER_PORT, SSL_CA_PEM,
00084                 SSL_CLIENT_CERT_PEM, SSL_CLIENT_PRIVATE_KEY_PEM);
00085 #else
00086         int rc = mqttNetwork->connect(MQTT_SERVER_HOST_NAME, MQTT_SERVER_PORT);
00087 #endif /* MBED_CONF_APP_USE_TLS */
00088         if (rc != MQTT::SUCCESS){
00089             printf("ERROR: rc from TCP connect is %d\r\n", rc);
00090             goto ERROR;
00091         }
00092     }
00093     printf("Connection established.\r\n");
00094     printf("\r\n");
00095 
00096     printf("MQTT client is trying to connect the server ...\r\n");
00097     {
00098         MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
00099         data.MQTTVersion = 3;
00100         data.clientID.cstring = (char*)"mbed-sample";
00101         data.username.cstring = (char*)"testuser";
00102         data.password.cstring = (char*)"testpassword";
00103 
00104         mqttClient = new MQTT::Client<MQTTNetwork, Countdown>(*mqttNetwork);
00105         int rc = mqttClient->connect(data);
00106         if (rc != MQTT::SUCCESS) {
00107             printf("ERROR: rc from MQTT connect is %d\r\n", rc);
00108             goto ERROR;
00109         }
00110     }
00111     printf("Client connected.\r\n");
00112     printf("\r\n");
00113 
00114     printf("Client is trying to subscribe a topic \"%s\".\r\n", topic);
00115     {
00116         int rc = mqttClient->subscribe(topic, MQTT::QOS2, messageArrived);
00117         if (rc != MQTT::SUCCESS) {
00118             printf("ERROR: rc from MQTT subscribe is %d\r\n", rc);
00119             goto ERROR;
00120         }
00121         isSubscribed = true;
00122     }
00123     printf("Client has subscribed a topic \"%s\".\r\n", topic);
00124     printf("\r\n");
00125 
00126     printf("Client publishes messages ...\r\n");
00127     {
00128         MQTT::Message message;
00129         message.retained = false;
00130         message.dup = false;
00131 
00132         const size_t buf_size = 100;
00133         char *buf = new char[buf_size];
00134         message.payload = (void*)buf;
00135 
00136         const MQTT::QoS q[] = {MQTT::QOS0, MQTT::QOS1, MQTT::QOS2};
00137         for(unsigned int i=0; i < (sizeof(q)/sizeof(q[0])); i++) {
00138             message.qos = q[i];
00139             message.id = i;
00140             sprintf(buf, "Hello World!  QoS %d message from app version %f\r\n", i, version);
00141             message.payloadlen = strlen(buf)+1;
00142             // Publish a message.
00143             printf("Publishing message QoS %d.\r\n", i);
00144             int rc = mqttClient->publish(topic, message);
00145             if(rc != MQTT::SUCCESS) {
00146                 printf("ERROR: rc from MQTT publish is %d\r\n", rc);
00147                 goto ERROR;
00148             }
00149             printf("QoS %d message published.\r\n", i);
00150             while (arrivedcount < (i+1)){
00151                 mqttClient->yield(100);
00152             }
00153         }
00154         delete[] buf;
00155     }
00156     printf("Version %.2f: finish %d msgs\r\n", version, arrivedcount);
00157 
00158 ERROR:
00159     if(mqttClient) {
00160         if(isSubscribed) {
00161             mqttClient->unsubscribe(topic);
00162             mqttClient->setMessageHandler(topic, 0);
00163         }
00164         if(mqttClient->isConnected()) 
00165             mqttClient->disconnect();
00166         delete mqttClient;
00167     }
00168     if(mqttNetwork) {
00169         mqttNetwork->disconnect();
00170         delete mqttNetwork;
00171     }
00172     if(network) {
00173         network->disconnect();
00174         // network is not created by new.
00175     }
00176 
00177     exit(0);
00178 }