Sample MQTT program - simple send and receive

Dependencies:   C12832 MQTT

Dependents:   MQTT_G_SENSOR

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers linux-main.cpp Source File

linux-main.cpp

00001 /*******************************************************************************
00002  * Copyright (c) 2014 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  *******************************************************************************/
00016   
00017  /**
00018   This is a sample program to illustrate the use of the MQTT Client library
00019   on Linux.  The Client class requires two classes which mediate
00020   access to system interfaces for networking and timing.  As long as these two
00021   classes provide the required public programming interfaces, it does not matter
00022   what facilities they use underneath. In this program, they use the Linux
00023   system libraries.
00024  
00025  */
00026 
00027 #if defined(LINUX)
00028 
00029 #include "LinuxMQTT.h"
00030 #include "LinuxIPStack.h"
00031 #include "MQTTClient.h"
00032 
00033 #include <sys/types.h>
00034 #include <sys/socket.h>
00035 #include <sys/param.h>
00036 #include <sys/time.h>
00037 #include <sys/select.h>
00038 #include <netinet/in.h>
00039 #include <netinet/tcp.h>
00040 #include <arpa/inet.h>
00041 #include <netdb.h>
00042 #include <stdio.h>
00043 #include <unistd.h>
00044 #include <errno.h>
00045 #include <fcntl.h>
00046 
00047 #include <stdlib.h>
00048 #include <string.h>
00049 #include <signal.h>
00050 
00051 #define DEFAULT_STACK_SIZE -1
00052 
00053 
00054 int arrivedcount = 0;
00055 
00056 void messageArrived(MQTT::Message* message)
00057 {
00058     printf("Message %d arrived: qos %d, retained %d, dup %d, packetid %d\n", 
00059         ++arrivedcount, message->qos, message->retained, message->dup, message->id);
00060     printf("Payload %.*s\n", message->payloadlen, (char*)message->payload);
00061 }
00062 
00063 
00064 int connect(MQTT::Client<IPStack, Countdown>::connectionLostInfo* info)
00065 {
00066     const char* hostname = "localhost"; //"m2m.eclipse.org";
00067     int port = 1883;
00068     printf("Connecting to %s:%d\n", hostname, port);
00069     int rc = info->network->connect(hostname, port);
00070     if (rc != 0)
00071         printf("rc from TCP connect is %d\n", rc);
00072  
00073     MQTTPacket_connectData data = MQTTPacket_connectData_initializer;       
00074     data.MQTTVersion = 3;
00075     data.clientID.cstring = (char*)"mbed-icraggs";
00076     rc = info->client->connect(&data);
00077     if (rc != 0)
00078         printf("rc from MQTT connect is %d\n", rc);
00079     
00080     return rc;
00081 }
00082 
00083 
00084 int main(int argc, char* argv[])
00085 {   
00086     IPStack ipstack = IPStack();
00087     float version = 0.3;
00088     const char* topic = "mbed-sample";
00089     
00090     printf("Version is %f\n", version);
00091               
00092     MQTT::Client<IPStack, Countdown> client = MQTT::Client<IPStack, Countdown>(ipstack);
00093     
00094     client.setConnectionLostHandler(connect);
00095 
00096     MQTT::Client<IPStack, Countdown>::connectionLostInfo info = {&client, &ipstack};
00097     int rc = connect(&info);
00098     
00099     rc = client.subscribe(topic, MQTT::QOS2, messageArrived);   
00100     if (rc != 0)
00101         printf("rc from MQTT subscribe is %d\n", rc);
00102 
00103     MQTT::Message message;
00104 
00105     // QoS 0
00106     char buf[100];
00107     sprintf(buf, "Hello World!  QoS 0 message from app version %f", version);
00108     message.qos = MQTT::QOS0;
00109     message.retained = false;
00110     message.dup = false;
00111     message.payload = (void*)buf;
00112     message.payloadlen = strlen(buf)+1;
00113     rc = client.publish(topic, &message);
00114     while (arrivedcount == 0)
00115         client.yield(100);
00116         
00117     // QoS 1
00118     printf("Now QoS 1\n");
00119     sprintf(buf, "Hello World!  QoS 1 message from app version %f", version);
00120     message.qos = MQTT::QOS1;
00121     message.payloadlen = strlen(buf)+1;
00122     rc = client.publish(topic, &message);
00123     while (arrivedcount == 1)
00124         client.yield(100);
00125         
00126     // QoS 2
00127     sprintf(buf, "Hello World!  QoS 2 message from app version %f", version);
00128     message.qos = MQTT::QOS2;
00129     message.payloadlen = strlen(buf)+1;
00130     rc = client.publish(topic, &message);
00131     while (arrivedcount == 2)
00132         client.yield(100);
00133     
00134     rc = client.unsubscribe(topic);
00135     if (rc != 0)
00136         printf("rc from unsubscribe was %d\n", rc);
00137     
00138     rc = client.disconnect();
00139     if (rc != 0)
00140         printf("rc from disconnect was %d\n", rc);
00141     
00142     ipstack.disconnect();
00143     
00144     printf("Finishing with %d messages received\n", arrivedcount);
00145     
00146     return 0;
00147 }
00148 
00149 #endif