ECE59500_ESP8266_K64_MQTT_Pub_Sub_client

Dependencies:   mbed ESP8266Interface MbedJSONValue mbed-rtos MQTT JSON

Committer:
mbedAustin
Date:
Tue Jun 09 17:57:43 2015 +0000
Revision:
18:76d0899bc3ce
Parent:
17:92a64d43ee61
Child:
19:9dfb938f07eb
double checked everything working. Pub/sub to same topic for self echoing.

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
mbedAustin 18:76d0899bc3ce 33 // callback for subscribe topic
mbedAustin 18:76d0899bc3ce 34 void subscribeCallback(MQTT::MessageData& md)
icraggs 2:638c854c0695 35 {
icraggs 9:5beb8609e9f7 36 MQTT::Message &message = md.message;
mbedAustin 18:76d0899bc3ce 37 printf("Message received: 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
mbedAustin 18:76d0899bc3ce 42 // main function that sets up the subscription and makes a couple publishes
icraggs 2:638c854c0695 43 int main(int argc, char* argv[])
geky 17:92a64d43ee61 44 {
geky 17:92a64d43ee61 45 printf("Starting\n");
mbedAustin 18:76d0899bc3ce 46 MQTTESP8266 ipstack(D1, D0, D10, "demossid","passphrase"); // change to match your wifi access point
icraggs 9:5beb8609e9f7 47 float version = 0.47;
icraggs 2:638c854c0695 48 char* topic = "mbed-sample";
geky 17:92a64d43ee61 49
sam_grove 5:4a257f6ac09a 50 printf("Version is %f\n", version);
geky 17:92a64d43ee61 51
geky 17:92a64d43ee61 52 MQTT::Client<MQTTESP8266, Countdown> client = MQTT::Client<MQTTESP8266, Countdown>(ipstack);
geky 17:92a64d43ee61 53
mbedAustin 18:76d0899bc3ce 54 char* hostname = "85.119.83.194"; // test.mosquitto.org
icraggs 6:e4c690c45021 55 int port = 1883;
icraggs 6:e4c690c45021 56 int rc = ipstack.connect(hostname, port);
icraggs 6:e4c690c45021 57 if (rc != 0)
geky 17:92a64d43ee61 58 printf("rc from TCP connect is %d\n", rc);
geky 17:92a64d43ee61 59
geky 17:92a64d43ee61 60 MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
icraggs 6:e4c690c45021 61 data.MQTTVersion = 3;
mbedAustin 18:76d0899bc3ce 62 data.clientID.cstring = "mbed-clientID";
icraggs 12:086a9314e8a5 63 data.username.cstring = "testuser";
icraggs 12:086a9314e8a5 64 data.password.cstring = "testpassword";
icraggs 16:28d062c5522b 65 if ((rc = client.connect(data)) != 0)
geky 17:92a64d43ee61 66 printf("rc from MQTT connect is %d\n", rc);
geky 17:92a64d43ee61 67
mbedAustin 18:76d0899bc3ce 68 if ((rc = client.subscribe(topic, MQTT::QOS1, subscribeCallback)) != 0)
mbedAustin 18:76d0899bc3ce 69 printf("Recv'd from MQTT subscribe is %d\n", rc);
icraggs 2:638c854c0695 70
icraggs 2:638c854c0695 71 MQTT::Message message;
icraggs 0:0cae29831d01 72
icraggs 2:638c854c0695 73 // QoS 0
icraggs 2:638c854c0695 74 char buf[100];
icraggs 2:638c854c0695 75 sprintf(buf, "Hello World! QoS 0 message from app version %f\n", version);
icraggs 2:638c854c0695 76 message.qos = MQTT::QOS0;
icraggs 2:638c854c0695 77 message.retained = false;
icraggs 2:638c854c0695 78 message.dup = false;
icraggs 2:638c854c0695 79 message.payload = (void*)buf;
icraggs 2:638c854c0695 80 message.payloadlen = strlen(buf)+1;
icraggs 16:28d062c5522b 81 rc = client.publish(topic, message);
icraggs 12:086a9314e8a5 82 while (arrivedcount < 1)
icraggs 2:638c854c0695 83 client.yield(100);
geky 17:92a64d43ee61 84
icraggs 2:638c854c0695 85 // QoS 1
icraggs 2:638c854c0695 86 sprintf(buf, "Hello World! QoS 1 message from app version %f\n", version);
icraggs 2:638c854c0695 87 message.qos = MQTT::QOS1;
icraggs 2:638c854c0695 88 message.payloadlen = strlen(buf)+1;
icraggs 16:28d062c5522b 89 rc = client.publish(topic, message);
icraggs 12:086a9314e8a5 90 while (arrivedcount < 2)
icraggs 2:638c854c0695 91 client.yield(100);
geky 17:92a64d43ee61 92
icraggs 2:638c854c0695 93 // QoS 2
icraggs 2:638c854c0695 94 sprintf(buf, "Hello World! QoS 2 message from app version %f\n", version);
icraggs 2:638c854c0695 95 message.qos = MQTT::QOS2;
icraggs 2:638c854c0695 96 message.payloadlen = strlen(buf)+1;
icraggs 16:28d062c5522b 97 rc = client.publish(topic, message);
icraggs 12:086a9314e8a5 98 while (arrivedcount < 3)
icraggs 2:638c854c0695 99 client.yield(100);
geky 17:92a64d43ee61 100
icraggs 12:086a9314e8a5 101 // n * QoS 2
geky 17:92a64d43ee61 102 for (int i = 1; i <= 10; ++i) {
icraggs 12:086a9314e8a5 103 sprintf(buf, "Hello World! QoS 2 message number %d from app version %f\n", i, version);
icraggs 12:086a9314e8a5 104 message.qos = MQTT::QOS2;
icraggs 12:086a9314e8a5 105 message.payloadlen = strlen(buf)+1;
icraggs 16:28d062c5522b 106 rc = client.publish(topic, message);
icraggs 12:086a9314e8a5 107 while (arrivedcount < i + 3)
icraggs 12:086a9314e8a5 108 client.yield(100);
icraggs 12:086a9314e8a5 109 }
geky 17:92a64d43ee61 110
icraggs 8:a3e3113054a1 111 if ((rc = client.unsubscribe(topic)) != 0)
sam_grove 5:4a257f6ac09a 112 printf("rc from unsubscribe was %d\n", rc);
geky 17:92a64d43ee61 113
icraggs 8:a3e3113054a1 114 if ((rc = client.disconnect()) != 0)
sam_grove 5:4a257f6ac09a 115 printf("rc from disconnect was %d\n", rc);
geky 17:92a64d43ee61 116
icraggs 2:638c854c0695 117 ipstack.disconnect();
sam_grove 5:4a257f6ac09a 118 printf("Finishing with %d messages received\n", arrivedcount);
geky 17:92a64d43ee61 119
icraggs 0:0cae29831d01 120 return 0;
icraggs 0:0cae29831d01 121 }