mqtt esp test

Dependencies:   ESP8266Interface MQTT mbed-rtos mbed-src

Fork of ESP8266_MQTT_HelloWorld by ESP8266

Committer:
fwndz
Date:
Wed Dec 21 12:56:37 2016 +0800
Revision:
21:4a7c4d6a8106
Parent:
19:1174b87e2111
Change mbed src file

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;
fwndz 19:1174b87e2111 32 Serial pc(PB_6, PB_7);;
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 {
fwndz 19:1174b87e2111 45 pc.printf("Starting\n");
fwndz 19:1174b87e2111 46 MQTTESP8266 ipstack(PA_2, PA_3, PA_7, "FearOfTheDark","PabloEscobar"); // change to match your wifi access point
icraggs 9:5beb8609e9f7 47 float version = 0.47;
fwndz 19:1174b87e2111 48 char* topic = "abc123";
geky 17:92a64d43ee61 49
fwndz 19:1174b87e2111 50 pc.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
fwndz 19:1174b87e2111 54 char* hostname = "128.199.121.240"; // 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)
fwndz 19:1174b87e2111 58 pc.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;
fwndz 19:1174b87e2111 62 //data.clientID.cstring = "mbed-clientID";
fwndz 19:1174b87e2111 63 //data.username.cstring = "testuser";
fwndz 19:1174b87e2111 64 //data.password.cstring = "testpassword";
icraggs 16:28d062c5522b 65 if ((rc = client.connect(data)) != 0)
fwndz 19:1174b87e2111 66 pc.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)
fwndz 19:1174b87e2111 69 pc.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)
fwndz 19:1174b87e2111 83 {
icraggs 2:638c854c0695 84 client.yield(100);
fwndz 19:1174b87e2111 85
fwndz 19:1174b87e2111 86 }
geky 17:92a64d43ee61 87
icraggs 2:638c854c0695 88 // QoS 1
icraggs 2:638c854c0695 89 sprintf(buf, "Hello World! QoS 1 message from app version %f\n", version);
icraggs 2:638c854c0695 90 message.qos = MQTT::QOS1;
icraggs 2:638c854c0695 91 message.payloadlen = strlen(buf)+1;
icraggs 16:28d062c5522b 92 rc = client.publish(topic, message);
icraggs 12:086a9314e8a5 93 while (arrivedcount < 2)
icraggs 2:638c854c0695 94 client.yield(100);
geky 17:92a64d43ee61 95
icraggs 2:638c854c0695 96 // QoS 2
icraggs 2:638c854c0695 97 sprintf(buf, "Hello World! QoS 2 message from app version %f\n", version);
icraggs 2:638c854c0695 98 message.qos = MQTT::QOS2;
icraggs 2:638c854c0695 99 message.payloadlen = strlen(buf)+1;
icraggs 16:28d062c5522b 100 rc = client.publish(topic, message);
icraggs 12:086a9314e8a5 101 while (arrivedcount < 3)
icraggs 2:638c854c0695 102 client.yield(100);
geky 17:92a64d43ee61 103
icraggs 12:086a9314e8a5 104 // n * QoS 2
geky 17:92a64d43ee61 105 for (int i = 1; i <= 10; ++i) {
icraggs 12:086a9314e8a5 106 sprintf(buf, "Hello World! QoS 2 message number %d from app version %f\n", i, version);
icraggs 12:086a9314e8a5 107 message.qos = MQTT::QOS2;
icraggs 12:086a9314e8a5 108 message.payloadlen = strlen(buf)+1;
icraggs 16:28d062c5522b 109 rc = client.publish(topic, message);
icraggs 12:086a9314e8a5 110 while (arrivedcount < i + 3)
icraggs 12:086a9314e8a5 111 client.yield(100);
icraggs 12:086a9314e8a5 112 }
geky 17:92a64d43ee61 113
icraggs 8:a3e3113054a1 114 if ((rc = client.unsubscribe(topic)) != 0)
fwndz 19:1174b87e2111 115 pc.printf("rc from unsubscribe was %d\n", rc);
geky 17:92a64d43ee61 116
icraggs 8:a3e3113054a1 117 if ((rc = client.disconnect()) != 0)
fwndz 19:1174b87e2111 118 pc.printf("rc from disconnect was %d\n", rc);
geky 17:92a64d43ee61 119
icraggs 2:638c854c0695 120 ipstack.disconnect();
fwndz 19:1174b87e2111 121 pc.printf("Finishing with %d messages received\n", arrivedcount);
geky 17:92a64d43ee61 122
icraggs 0:0cae29831d01 123 return 0;
icraggs 0:0cae29831d01 124 }