MQTT

Dependencies:   ESP8266Interface3 MQTT mbed-rtos mbed

Fork of ESP8266_MQTT_HelloWorld by ESP8266

Committer:
blownelco
Date:
Mon Feb 06 08:34:14 2017 +0000
Revision:
19:cff20f14096e
Parent:
18:76d0899bc3ce
mqtt

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
blownelco 19:cff20f14096e 27 #include <test.h>
blownelco 19:cff20f14096e 28 #include "mbed.h"
blownelco 19:cff20f14096e 29
blownelco 19:cff20f14096e 30 //DigitalOut PWD(PTB2); waar wordt deze pin dan gezet
blownelco 19:cff20f14096e 31 DigitalOut G15(PTB11);
icraggs 2:638c854c0695 32
geky 17:92a64d43ee61 33 #include "MQTTESP8266.h"
icraggs 2:638c854c0695 34 #include "MQTTClient.h"
icraggs 2:638c854c0695 35
icraggs 2:638c854c0695 36 int arrivedcount = 0;
icraggs 2:638c854c0695 37
blownelco 19:cff20f14096e 38 char data[100];
blownelco 19:cff20f14096e 39 int cnt = 0;
blownelco 19:cff20f14096e 40
mbedAustin 18:76d0899bc3ce 41 // callback for subscribe topic
mbedAustin 18:76d0899bc3ce 42 void subscribeCallback(MQTT::MessageData& md)
icraggs 2:638c854c0695 43 {
icraggs 9:5beb8609e9f7 44 MQTT::Message &message = md.message;
mbedAustin 18:76d0899bc3ce 45 printf("Message received: qos %d, retained %d, dup %d, packetid %d\n", message.qos, message.retained, message.dup, message.id);
icraggs 9:5beb8609e9f7 46 printf("Payload %.*s\n", message.payloadlen, (char*)message.payload);
icraggs 2:638c854c0695 47 ++arrivedcount;
icraggs 2:638c854c0695 48 }
icraggs 0:0cae29831d01 49
mbedAustin 18:76d0899bc3ce 50 // main function that sets up the subscription and makes a couple publishes
icraggs 2:638c854c0695 51 int main(int argc, char* argv[])
geky 17:92a64d43ee61 52 {
blownelco 19:cff20f14096e 53 //PWD = 1; dit is de reset pin
blownelco 19:cff20f14096e 54 G15 = 0;
blownelco 19:cff20f14096e 55
blownelco 19:cff20f14096e 56 printf("Starting MQTT\n");
blownelco 19:cff20f14096e 57 MQTTESP8266 ipstack(PTD3,PTD2,PTB2, "QBMT","zoranetwerk"); // change to match your wifi access point PTD9 is fake pin
blownelco 19:cff20f14096e 58 float version = 0.51;
blownelco 19:cff20f14096e 59 char* topic = "mbe d-sample";
geky 17:92a64d43ee61 60
sam_grove 5:4a257f6ac09a 61 printf("Version is %f\n", version);
blownelco 19:cff20f14096e 62
geky 17:92a64d43ee61 63 MQTT::Client<MQTTESP8266, Countdown> client = MQTT::Client<MQTTESP8266, Countdown>(ipstack);
geky 17:92a64d43ee61 64
blownelco 19:cff20f14096e 65 char* hostname = "192.168.1.103"; // test.mosquitto.org
icraggs 6:e4c690c45021 66 int port = 1883;
icraggs 6:e4c690c45021 67 int rc = ipstack.connect(hostname, port);
icraggs 6:e4c690c45021 68 if (rc != 0)
geky 17:92a64d43ee61 69 printf("rc from TCP connect is %d\n", rc);
geky 17:92a64d43ee61 70
geky 17:92a64d43ee61 71 MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
icraggs 6:e4c690c45021 72 data.MQTTVersion = 3;
blownelco 19:cff20f14096e 73 data.clientID.cstring = "blownelco";
blownelco 19:cff20f14096e 74 data.username.cstring = "";
blownelco 19:cff20f14096e 75 data.password.cstring = "";
icraggs 16:28d062c5522b 76 if ((rc = client.connect(data)) != 0)
geky 17:92a64d43ee61 77 printf("rc from MQTT connect is %d\n", rc);
geky 17:92a64d43ee61 78
mbedAustin 18:76d0899bc3ce 79 if ((rc = client.subscribe(topic, MQTT::QOS1, subscribeCallback)) != 0)
mbedAustin 18:76d0899bc3ce 80 printf("Recv'd from MQTT subscribe is %d\n", rc);
icraggs 2:638c854c0695 81
icraggs 2:638c854c0695 82 MQTT::Message message;
icraggs 0:0cae29831d01 83
icraggs 2:638c854c0695 84 // QoS 0
icraggs 2:638c854c0695 85 char buf[100];
icraggs 2:638c854c0695 86 sprintf(buf, "Hello World! QoS 0 message from app version %f\n", version);
icraggs 2:638c854c0695 87 message.qos = MQTT::QOS0;
icraggs 2:638c854c0695 88 message.retained = false;
icraggs 2:638c854c0695 89 message.dup = false;
icraggs 2:638c854c0695 90 message.payload = (void*)buf;
icraggs 2:638c854c0695 91 message.payloadlen = strlen(buf)+1;
icraggs 16:28d062c5522b 92 rc = client.publish(topic, message);
icraggs 12:086a9314e8a5 93 while (arrivedcount < 1)
icraggs 2:638c854c0695 94 client.yield(100);
geky 17:92a64d43ee61 95
icraggs 2:638c854c0695 96 // QoS 1
icraggs 2:638c854c0695 97 sprintf(buf, "Hello World! QoS 1 message from app version %f\n", version);
icraggs 2:638c854c0695 98 message.qos = MQTT::QOS1;
icraggs 2:638c854c0695 99 message.payloadlen = strlen(buf)+1;
icraggs 16:28d062c5522b 100 rc = client.publish(topic, message);
icraggs 12:086a9314e8a5 101 while (arrivedcount < 2)
icraggs 2:638c854c0695 102 client.yield(100);
geky 17:92a64d43ee61 103
icraggs 2:638c854c0695 104 // QoS 2
icraggs 2:638c854c0695 105 sprintf(buf, "Hello World! QoS 2 message from app version %f\n", version);
icraggs 2:638c854c0695 106 message.qos = MQTT::QOS2;
icraggs 2:638c854c0695 107 message.payloadlen = strlen(buf)+1;
icraggs 16:28d062c5522b 108 rc = client.publish(topic, message);
icraggs 12:086a9314e8a5 109 while (arrivedcount < 3)
icraggs 2:638c854c0695 110 client.yield(100);
geky 17:92a64d43ee61 111
icraggs 12:086a9314e8a5 112 // n * QoS 2
geky 17:92a64d43ee61 113 for (int i = 1; i <= 10; ++i) {
icraggs 12:086a9314e8a5 114 sprintf(buf, "Hello World! QoS 2 message number %d from app version %f\n", i, version);
icraggs 12:086a9314e8a5 115 message.qos = MQTT::QOS2;
icraggs 12:086a9314e8a5 116 message.payloadlen = strlen(buf)+1;
icraggs 16:28d062c5522b 117 rc = client.publish(topic, message);
icraggs 12:086a9314e8a5 118 while (arrivedcount < i + 3)
icraggs 12:086a9314e8a5 119 client.yield(100);
icraggs 12:086a9314e8a5 120 }
geky 17:92a64d43ee61 121
icraggs 8:a3e3113054a1 122 if ((rc = client.unsubscribe(topic)) != 0)
sam_grove 5:4a257f6ac09a 123 printf("rc from unsubscribe was %d\n", rc);
geky 17:92a64d43ee61 124
icraggs 8:a3e3113054a1 125 if ((rc = client.disconnect()) != 0)
sam_grove 5:4a257f6ac09a 126 printf("rc from disconnect was %d\n", rc);
geky 17:92a64d43ee61 127
icraggs 2:638c854c0695 128 ipstack.disconnect();
sam_grove 5:4a257f6ac09a 129 printf("Finishing with %d messages received\n", arrivedcount);
geky 17:92a64d43ee61 130
icraggs 0:0cae29831d01 131 return 0;
icraggs 0:0cae29831d01 132 }