init

Dependencies:   mbed C12832 EthernetInterface MQTT mbed-rtos picojson

Committer:
co838_mgl6
Date:
Thu May 05 12:05:16 2016 +0000
Revision:
1:1e45dd2c91fb
first step

Who changed what in which revision?

UserRevisionLine numberNew contents of line
co838_mgl6 1:1e45dd2c91fb 1 /*******************************************************************************
co838_mgl6 1:1e45dd2c91fb 2 * Copyright (c) 2014 IBM Corp.
co838_mgl6 1:1e45dd2c91fb 3 *
co838_mgl6 1:1e45dd2c91fb 4 * All rights reserved. This program and the accompanying materials
co838_mgl6 1:1e45dd2c91fb 5 * are made available under the terms of the Eclipse Public License v1.0
co838_mgl6 1:1e45dd2c91fb 6 * and Eclipse Distribution License v1.0 which accompany this distribution.
co838_mgl6 1:1e45dd2c91fb 7 *
co838_mgl6 1:1e45dd2c91fb 8 * The Eclipse Public License is available at
co838_mgl6 1:1e45dd2c91fb 9 * http://www.eclipse.org/legal/epl-v10.html
co838_mgl6 1:1e45dd2c91fb 10 * and the Eclipse Distribution License is available at
co838_mgl6 1:1e45dd2c91fb 11 * http://www.eclipse.org/org/documents/edl-v10.php.
co838_mgl6 1:1e45dd2c91fb 12 *
co838_mgl6 1:1e45dd2c91fb 13 * Contributors:
co838_mgl6 1:1e45dd2c91fb 14 * Ian Craggs - initial API and implementation and/or initial documentation
co838_mgl6 1:1e45dd2c91fb 15 *******************************************************************************/
co838_mgl6 1:1e45dd2c91fb 16
co838_mgl6 1:1e45dd2c91fb 17 /**
co838_mgl6 1:1e45dd2c91fb 18 This is a sample program to illustrate the use of the MQTT Client library
co838_mgl6 1:1e45dd2c91fb 19 on Linux. The Client class requires two classes which mediate
co838_mgl6 1:1e45dd2c91fb 20 access to system interfaces for networking and timing. As long as these two
co838_mgl6 1:1e45dd2c91fb 21 classes provide the required public programming interfaces, it does not matter
co838_mgl6 1:1e45dd2c91fb 22 what facilities they use underneath. In this program, they use the Linux
co838_mgl6 1:1e45dd2c91fb 23 system libraries.
co838_mgl6 1:1e45dd2c91fb 24
co838_mgl6 1:1e45dd2c91fb 25 */
co838_mgl6 1:1e45dd2c91fb 26
co838_mgl6 1:1e45dd2c91fb 27 #if defined(LINUX)
co838_mgl6 1:1e45dd2c91fb 28
co838_mgl6 1:1e45dd2c91fb 29 #include "LinuxMQTT.h"
co838_mgl6 1:1e45dd2c91fb 30 #include "LinuxIPStack.h"
co838_mgl6 1:1e45dd2c91fb 31 #include "MQTTClient.h"
co838_mgl6 1:1e45dd2c91fb 32
co838_mgl6 1:1e45dd2c91fb 33 #include <sys/types.h>
co838_mgl6 1:1e45dd2c91fb 34 #include <sys/socket.h>
co838_mgl6 1:1e45dd2c91fb 35 #include <sys/param.h>
co838_mgl6 1:1e45dd2c91fb 36 #include <sys/time.h>
co838_mgl6 1:1e45dd2c91fb 37 #include <sys/select.h>
co838_mgl6 1:1e45dd2c91fb 38 #include <netinet/in.h>
co838_mgl6 1:1e45dd2c91fb 39 #include <netinet/tcp.h>
co838_mgl6 1:1e45dd2c91fb 40 #include <arpa/inet.h>
co838_mgl6 1:1e45dd2c91fb 41 #include <netdb.h>
co838_mgl6 1:1e45dd2c91fb 42 #include <stdio.h>
co838_mgl6 1:1e45dd2c91fb 43 #include <unistd.h>
co838_mgl6 1:1e45dd2c91fb 44 #include <errno.h>
co838_mgl6 1:1e45dd2c91fb 45 #include <fcntl.h>
co838_mgl6 1:1e45dd2c91fb 46
co838_mgl6 1:1e45dd2c91fb 47 #include <stdlib.h>
co838_mgl6 1:1e45dd2c91fb 48 #include <string.h>
co838_mgl6 1:1e45dd2c91fb 49 #include <signal.h>
co838_mgl6 1:1e45dd2c91fb 50
co838_mgl6 1:1e45dd2c91fb 51 #define DEFAULT_STACK_SIZE -1
co838_mgl6 1:1e45dd2c91fb 52
co838_mgl6 1:1e45dd2c91fb 53
co838_mgl6 1:1e45dd2c91fb 54 int arrivedcount = 0;
co838_mgl6 1:1e45dd2c91fb 55
co838_mgl6 1:1e45dd2c91fb 56 void messageArrived(MQTT::Message* message)
co838_mgl6 1:1e45dd2c91fb 57 {
co838_mgl6 1:1e45dd2c91fb 58 printf("Message %d arrived: qos %d, retained %d, dup %d, packetid %d\n",
co838_mgl6 1:1e45dd2c91fb 59 ++arrivedcount, message->qos, message->retained, message->dup, message->id);
co838_mgl6 1:1e45dd2c91fb 60 printf("Payload %.*s\n", message->payloadlen, (char*)message->payload);
co838_mgl6 1:1e45dd2c91fb 61 }
co838_mgl6 1:1e45dd2c91fb 62
co838_mgl6 1:1e45dd2c91fb 63
co838_mgl6 1:1e45dd2c91fb 64 int connect(MQTT::Client<IPStack, Countdown>::connectionLostInfo* info)
co838_mgl6 1:1e45dd2c91fb 65 {
co838_mgl6 1:1e45dd2c91fb 66 const char* hostname = "localhost"; //"m2m.eclipse.org";
co838_mgl6 1:1e45dd2c91fb 67 int port = 1883;
co838_mgl6 1:1e45dd2c91fb 68 printf("Connecting to %s:%d\n", hostname, port);
co838_mgl6 1:1e45dd2c91fb 69 int rc = info->network->connect(hostname, port);
co838_mgl6 1:1e45dd2c91fb 70 if (rc != 0)
co838_mgl6 1:1e45dd2c91fb 71 printf("rc from TCP connect is %d\n", rc);
co838_mgl6 1:1e45dd2c91fb 72
co838_mgl6 1:1e45dd2c91fb 73 MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
co838_mgl6 1:1e45dd2c91fb 74 data.MQTTVersion = 3;
co838_mgl6 1:1e45dd2c91fb 75 data.clientID.cstring = (char*)"mbed-icraggs";
co838_mgl6 1:1e45dd2c91fb 76 rc = info->client->connect(&data);
co838_mgl6 1:1e45dd2c91fb 77 if (rc != 0)
co838_mgl6 1:1e45dd2c91fb 78 printf("rc from MQTT connect is %d\n", rc);
co838_mgl6 1:1e45dd2c91fb 79
co838_mgl6 1:1e45dd2c91fb 80 return rc;
co838_mgl6 1:1e45dd2c91fb 81 }
co838_mgl6 1:1e45dd2c91fb 82
co838_mgl6 1:1e45dd2c91fb 83
co838_mgl6 1:1e45dd2c91fb 84 int main(int argc, char* argv[])
co838_mgl6 1:1e45dd2c91fb 85 {
co838_mgl6 1:1e45dd2c91fb 86 IPStack ipstack = IPStack();
co838_mgl6 1:1e45dd2c91fb 87 float version = 0.3;
co838_mgl6 1:1e45dd2c91fb 88 const char* topic = "mbed-sample";
co838_mgl6 1:1e45dd2c91fb 89
co838_mgl6 1:1e45dd2c91fb 90 printf("Version is %f\n", version);
co838_mgl6 1:1e45dd2c91fb 91
co838_mgl6 1:1e45dd2c91fb 92 MQTT::Client<IPStack, Countdown> client = MQTT::Client<IPStack, Countdown>(ipstack);
co838_mgl6 1:1e45dd2c91fb 93
co838_mgl6 1:1e45dd2c91fb 94 client.setConnectionLostHandler(connect);
co838_mgl6 1:1e45dd2c91fb 95
co838_mgl6 1:1e45dd2c91fb 96 MQTT::Client<IPStack, Countdown>::connectionLostInfo info = {&client, &ipstack};
co838_mgl6 1:1e45dd2c91fb 97 int rc = connect(&info);
co838_mgl6 1:1e45dd2c91fb 98
co838_mgl6 1:1e45dd2c91fb 99 rc = client.subscribe(topic, MQTT::QOS2, messageArrived);
co838_mgl6 1:1e45dd2c91fb 100 if (rc != 0)
co838_mgl6 1:1e45dd2c91fb 101 printf("rc from MQTT subscribe is %d\n", rc);
co838_mgl6 1:1e45dd2c91fb 102
co838_mgl6 1:1e45dd2c91fb 103 MQTT::Message message;
co838_mgl6 1:1e45dd2c91fb 104
co838_mgl6 1:1e45dd2c91fb 105 // QoS 0
co838_mgl6 1:1e45dd2c91fb 106 char buf[100];
co838_mgl6 1:1e45dd2c91fb 107 sprintf(buf, "Hello World! QoS 0 message from app version %f", version);
co838_mgl6 1:1e45dd2c91fb 108 message.qos = MQTT::QOS0;
co838_mgl6 1:1e45dd2c91fb 109 message.retained = false;
co838_mgl6 1:1e45dd2c91fb 110 message.dup = false;
co838_mgl6 1:1e45dd2c91fb 111 message.payload = (void*)buf;
co838_mgl6 1:1e45dd2c91fb 112 message.payloadlen = strlen(buf)+1;
co838_mgl6 1:1e45dd2c91fb 113 rc = client.publish(topic, &message);
co838_mgl6 1:1e45dd2c91fb 114 while (arrivedcount == 0)
co838_mgl6 1:1e45dd2c91fb 115 client.yield(100);
co838_mgl6 1:1e45dd2c91fb 116
co838_mgl6 1:1e45dd2c91fb 117 // QoS 1
co838_mgl6 1:1e45dd2c91fb 118 printf("Now QoS 1\n");
co838_mgl6 1:1e45dd2c91fb 119 sprintf(buf, "Hello World! QoS 1 message from app version %f", version);
co838_mgl6 1:1e45dd2c91fb 120 message.qos = MQTT::QOS1;
co838_mgl6 1:1e45dd2c91fb 121 message.payloadlen = strlen(buf)+1;
co838_mgl6 1:1e45dd2c91fb 122 rc = client.publish(topic, &message);
co838_mgl6 1:1e45dd2c91fb 123 while (arrivedcount == 1)
co838_mgl6 1:1e45dd2c91fb 124 client.yield(100);
co838_mgl6 1:1e45dd2c91fb 125
co838_mgl6 1:1e45dd2c91fb 126 // QoS 2
co838_mgl6 1:1e45dd2c91fb 127 sprintf(buf, "Hello World! QoS 2 message from app version %f", version);
co838_mgl6 1:1e45dd2c91fb 128 message.qos = MQTT::QOS2;
co838_mgl6 1:1e45dd2c91fb 129 message.payloadlen = strlen(buf)+1;
co838_mgl6 1:1e45dd2c91fb 130 rc = client.publish(topic, &message);
co838_mgl6 1:1e45dd2c91fb 131 while (arrivedcount == 2)
co838_mgl6 1:1e45dd2c91fb 132 client.yield(100);
co838_mgl6 1:1e45dd2c91fb 133
co838_mgl6 1:1e45dd2c91fb 134 rc = client.unsubscribe(topic);
co838_mgl6 1:1e45dd2c91fb 135 if (rc != 0)
co838_mgl6 1:1e45dd2c91fb 136 printf("rc from unsubscribe was %d\n", rc);
co838_mgl6 1:1e45dd2c91fb 137
co838_mgl6 1:1e45dd2c91fb 138 rc = client.disconnect();
co838_mgl6 1:1e45dd2c91fb 139 if (rc != 0)
co838_mgl6 1:1e45dd2c91fb 140 printf("rc from disconnect was %d\n", rc);
co838_mgl6 1:1e45dd2c91fb 141
co838_mgl6 1:1e45dd2c91fb 142 ipstack.disconnect();
co838_mgl6 1:1e45dd2c91fb 143
co838_mgl6 1:1e45dd2c91fb 144 printf("Finishing with %d messages received\n", arrivedcount);
co838_mgl6 1:1e45dd2c91fb 145
co838_mgl6 1:1e45dd2c91fb 146 return 0;
co838_mgl6 1:1e45dd2c91fb 147 }
co838_mgl6 1:1e45dd2c91fb 148
co838_mgl6 1:1e45dd2c91fb 149 #endif