Jack Hansdampf / mbed-mqtt-GSOE1

Dependents:   ESP8266MQTT

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers hello.cpp Source File

hello.cpp

00001 #define MQTTCLIENT_QOS2 1
00002 
00003 #include <memory.h>
00004 
00005 #include "MQTTClient.h"
00006 
00007 #define DEFAULT_STACK_SIZE -1
00008 
00009 #include "linux.cpp"
00010 
00011 int arrivedcount = 0;
00012 
00013 void messageArrived(MQTT::MessageData& md)
00014 {
00015     MQTT::Message &message = md.message;
00016 
00017     printf("Message %d arrived: qos %d, retained %d, dup %d, packetid %d\n", 
00018         ++arrivedcount, message.qos, message.retained, message.dup, message.id);
00019     printf("Payload %.*s\n", (int)message.payloadlen, (char*)message.payload);
00020 }
00021 
00022 
00023 int main(int argc, char* argv[])
00024 {   
00025     IPStack ipstack = IPStack();
00026     float version = 0.3;
00027     const char* topic = "mbed-sample";
00028     
00029     printf("Version is %f\n", version);
00030               
00031     MQTT::Client<IPStack, Countdown> client = MQTT::Client<IPStack, Countdown>(ipstack);
00032     
00033     const char* hostname = "iot.eclipse.org";
00034     int port = 1883;
00035     printf("Connecting to %s:%d\n", hostname, port);
00036     int rc = ipstack.connect(hostname, port);
00037     if (rc != 0)
00038         printf("rc from TCP connect is %d\n", rc);
00039  
00040     printf("MQTT connecting\n");
00041     MQTTPacket_connectData data = MQTTPacket_connectData_initializer;       
00042     data.MQTTVersion = 3;
00043     data.clientID.cstring = (char*)"mbed-icraggs";
00044     rc = client.connect(data);
00045     if (rc != 0)
00046         printf("rc from MQTT connect is %d\n", rc);
00047     printf("MQTT connected\n");
00048     
00049     rc = client.subscribe(topic, MQTT::QOS2, messageArrived);   
00050     if (rc != 0)
00051         printf("rc from MQTT subscribe is %d\n", rc);
00052 
00053     MQTT::Message message;
00054 
00055     // QoS 0
00056     char buf[100];
00057     sprintf(buf, "Hello World!  QoS 0 message from app version %f", version);
00058     message.qos = MQTT::QOS0;
00059     message.retained = false;
00060     message.dup = false;
00061     message.payload = (void*)buf;
00062     message.payloadlen = strlen(buf)+1;
00063     rc = client.publish(topic, message);
00064     if (rc != 0)
00065         printf("Error %d from sending QoS 0 message\n", rc);
00066     else while (arrivedcount == 0)
00067         client.yield(100);
00068         
00069     // QoS 1
00070     printf("Now QoS 1\n");
00071     sprintf(buf, "Hello World!  QoS 1 message from app version %f", version);
00072     message.qos = MQTT::QOS1;
00073     message.payloadlen = strlen(buf)+1;
00074     rc = client.publish(topic, message);
00075     if (rc != 0)
00076         printf("Error %d from sending QoS 1 message\n", rc);
00077     else while (arrivedcount == 1)
00078         client.yield(100);
00079         
00080     // QoS 2
00081     sprintf(buf, "Hello World!  QoS 2 message from app version %f", version);
00082     message.qos = MQTT::QOS2;
00083     message.payloadlen = strlen(buf)+1;
00084     rc = client.publish(topic, message);
00085     if (rc != 0)
00086         printf("Error %d from sending QoS 2 message\n", rc);
00087     while (arrivedcount == 2)
00088         client.yield(100);
00089     
00090     rc = client.unsubscribe(topic);
00091     if (rc != 0)
00092         printf("rc from unsubscribe was %d\n", rc);
00093     
00094     rc = client.disconnect();
00095     if (rc != 0)
00096         printf("rc from disconnect was %d\n", rc);
00097     
00098     ipstack.disconnect();
00099     
00100     printf("Finishing with %d messages received\n", arrivedcount);
00101     
00102     return 0;
00103 }
00104