ECE59500_ESP8266_K64_MQTT_Pub_Sub_client

Dependencies:   mbed ESP8266Interface MbedJSONValue mbed-rtos MQTT JSON

Committer:
geky
Date:
Fri Jun 05 15:15:24 2015 +0000
Revision:
17:92a64d43ee61
Parent:
16:28d062c5522b
Child:
18:76d0899bc3ce
Modified to use ESP8266

Who changed what in which revision?

UserRevisionLine numberNew contents of line
icraggs 1:a1d5c7a6acbc 1 /*******************************************************************************
icraggs 1:a1d5c7a6acbc 2 * Copyright (c) 2014 IBM Corp.
icraggs 1:a1d5c7a6acbc 3 *
icraggs 1:a1d5c7a6acbc 4 * All rights reserved. This program and the accompanying materials
icraggs 1:a1d5c7a6acbc 5 * are made available under the terms of the Eclipse Public License v1.0
icraggs 1:a1d5c7a6acbc 6 * and Eclipse Distribution License v1.0 which accompany this distribution.
icraggs 1:a1d5c7a6acbc 7 *
icraggs 1:a1d5c7a6acbc 8 * The Eclipse Public License is available at
icraggs 1:a1d5c7a6acbc 9 * http://www.eclipse.org/legal/epl-v10.html
icraggs 1:a1d5c7a6acbc 10 * and the Eclipse Distribution License is available at
icraggs 1:a1d5c7a6acbc 11 * http://www.eclipse.org/org/documents/edl-v10.php.
icraggs 1:a1d5c7a6acbc 12 *
icraggs 1:a1d5c7a6acbc 13 * Contributors:
icraggs 1:a1d5c7a6acbc 14 * Ian Craggs - initial API and implementation and/or initial documentation
icraggs 1:a1d5c7a6acbc 15 *******************************************************************************/
geky 17:92a64d43ee61 16
geky 17:92a64d43ee61 17 /**
geky 17:92a64d43ee61 18 This is a sample program to illustrate the use of the MQTT Client library
geky 17:92a64d43ee61 19 on the mbed platform. The Client class requires two classes which mediate
geky 17:92a64d43ee61 20 access to system interfaces for networking and timing. As long as these two
geky 17:92a64d43ee61 21 classes provide the required public programming interfaces, it does not matter
geky 17:92a64d43ee61 22 what facilities they use underneath. In this program, they use the mbed
geky 17:92a64d43ee61 23 system libraries.
geky 17:92a64d43ee61 24
geky 17:92a64d43ee61 25 */
icraggs 1:a1d5c7a6acbc 26
icraggs 2:638c854c0695 27
geky 17:92a64d43ee61 28 #include "MQTTESP8266.h"
icraggs 2:638c854c0695 29 #include "MQTTClient.h"
icraggs 2:638c854c0695 30
icraggs 2:638c854c0695 31 int arrivedcount = 0;
icraggs 2:638c854c0695 32
icraggs 8:a3e3113054a1 33
icraggs 9:5beb8609e9f7 34 void messageArrived(MQTT::MessageData& md)
icraggs 2:638c854c0695 35 {
icraggs 9:5beb8609e9f7 36 MQTT::Message &message = md.message;
icraggs 9:5beb8609e9f7 37 printf("Message arrived: qos %d, retained %d, dup %d, packetid %d\n", message.qos, message.retained, message.dup, message.id);
icraggs 9:5beb8609e9f7 38 printf("Payload %.*s\n", message.payloadlen, (char*)message.payload);
icraggs 2:638c854c0695 39 ++arrivedcount;
icraggs 2:638c854c0695 40 }
icraggs 0:0cae29831d01 41
icraggs 2:638c854c0695 42 int main(int argc, char* argv[])
geky 17:92a64d43ee61 43 {
geky 17:92a64d43ee61 44 printf("Starting\n");
geky 17:92a64d43ee61 45 MQTTESP8266 ipstack(D1, D0, D10, "ssid", "password");
icraggs 9:5beb8609e9f7 46 float version = 0.47;
icraggs 2:638c854c0695 47 char* topic = "mbed-sample";
geky 17:92a64d43ee61 48
sam_grove 5:4a257f6ac09a 49 printf("Version is %f\n", version);
geky 17:92a64d43ee61 50
geky 17:92a64d43ee61 51 MQTT::Client<MQTTESP8266, Countdown> client = MQTT::Client<MQTTESP8266, Countdown>(ipstack);
geky 17:92a64d43ee61 52
geky 17:92a64d43ee61 53 char* hostname = "192.168.1.20";
icraggs 6:e4c690c45021 54 int port = 1883;
icraggs 6:e4c690c45021 55 int rc = ipstack.connect(hostname, port);
icraggs 6:e4c690c45021 56 if (rc != 0)
geky 17:92a64d43ee61 57 printf("rc from TCP connect is %d\n", rc);
geky 17:92a64d43ee61 58
geky 17:92a64d43ee61 59 MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
icraggs 6:e4c690c45021 60 data.MQTTVersion = 3;
icraggs 8:a3e3113054a1 61 data.clientID.cstring = "mbed-sample";
icraggs 12:086a9314e8a5 62 data.username.cstring = "testuser";
icraggs 12:086a9314e8a5 63 data.password.cstring = "testpassword";
icraggs 16:28d062c5522b 64 if ((rc = client.connect(data)) != 0)
geky 17:92a64d43ee61 65 printf("rc from MQTT connect is %d\n", rc);
geky 17:92a64d43ee61 66
geky 17:92a64d43ee61 67 if ((rc = client.subscribe("mbed-sample", MQTT::QOS1, messageArrived)) != 0)
geky 17:92a64d43ee61 68 printf("rc from MQTT subscribe is %d\n", rc);
icraggs 2:638c854c0695 69
icraggs 2:638c854c0695 70 MQTT::Message message;
icraggs 0:0cae29831d01 71
icraggs 2:638c854c0695 72 // QoS 0
icraggs 2:638c854c0695 73 char buf[100];
icraggs 2:638c854c0695 74 sprintf(buf, "Hello World! QoS 0 message from app version %f\n", version);
icraggs 2:638c854c0695 75 message.qos = MQTT::QOS0;
icraggs 2:638c854c0695 76 message.retained = false;
icraggs 2:638c854c0695 77 message.dup = false;
icraggs 2:638c854c0695 78 message.payload = (void*)buf;
icraggs 2:638c854c0695 79 message.payloadlen = strlen(buf)+1;
icraggs 16:28d062c5522b 80 rc = client.publish(topic, message);
icraggs 12:086a9314e8a5 81 while (arrivedcount < 1)
icraggs 2:638c854c0695 82 client.yield(100);
geky 17:92a64d43ee61 83
icraggs 2:638c854c0695 84 // QoS 1
icraggs 2:638c854c0695 85 sprintf(buf, "Hello World! QoS 1 message from app version %f\n", version);
icraggs 2:638c854c0695 86 message.qos = MQTT::QOS1;
icraggs 2:638c854c0695 87 message.payloadlen = strlen(buf)+1;
icraggs 16:28d062c5522b 88 rc = client.publish(topic, message);
icraggs 12:086a9314e8a5 89 while (arrivedcount < 2)
icraggs 2:638c854c0695 90 client.yield(100);
geky 17:92a64d43ee61 91
icraggs 2:638c854c0695 92 // QoS 2
icraggs 2:638c854c0695 93 sprintf(buf, "Hello World! QoS 2 message from app version %f\n", version);
icraggs 2:638c854c0695 94 message.qos = MQTT::QOS2;
icraggs 2:638c854c0695 95 message.payloadlen = strlen(buf)+1;
icraggs 16:28d062c5522b 96 rc = client.publish(topic, message);
icraggs 12:086a9314e8a5 97 while (arrivedcount < 3)
icraggs 2:638c854c0695 98 client.yield(100);
geky 17:92a64d43ee61 99
icraggs 12:086a9314e8a5 100 // n * QoS 2
geky 17:92a64d43ee61 101 for (int i = 1; i <= 10; ++i) {
icraggs 12:086a9314e8a5 102 sprintf(buf, "Hello World! QoS 2 message number %d from app version %f\n", i, version);
icraggs 12:086a9314e8a5 103 message.qos = MQTT::QOS2;
icraggs 12:086a9314e8a5 104 message.payloadlen = strlen(buf)+1;
icraggs 16:28d062c5522b 105 rc = client.publish(topic, message);
icraggs 12:086a9314e8a5 106 while (arrivedcount < i + 3)
icraggs 12:086a9314e8a5 107 client.yield(100);
icraggs 12:086a9314e8a5 108 }
geky 17:92a64d43ee61 109
icraggs 8:a3e3113054a1 110 if ((rc = client.unsubscribe(topic)) != 0)
sam_grove 5:4a257f6ac09a 111 printf("rc from unsubscribe was %d\n", rc);
geky 17:92a64d43ee61 112
icraggs 8:a3e3113054a1 113 if ((rc = client.disconnect()) != 0)
sam_grove 5:4a257f6ac09a 114 printf("rc from disconnect was %d\n", rc);
geky 17:92a64d43ee61 115
icraggs 2:638c854c0695 116 ipstack.disconnect();
sam_grove 5:4a257f6ac09a 117 printf("Finishing with %d messages received\n", arrivedcount);
geky 17:92a64d43ee61 118
icraggs 0:0cae29831d01 119 return 0;
icraggs 0:0cae29831d01 120 }