Example MQTT implemented on the ESP8266

Dependencies:   ESP8266Interface MQTT mbed-rtos mbed

Fork of HelloMQTT by MQTT

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /*******************************************************************************
00002  * Copyright (c) 2014 IBM Corp.
00003  *
00004  * All rights reserved. This program and the accompanying materials
00005  * are made available under the terms of the Eclipse Public License v1.0
00006  * and Eclipse Distribution License v1.0 which accompany this distribution.
00007  *
00008  * The Eclipse Public License is available at
00009  *    http://www.eclipse.org/legal/epl-v10.html
00010  * and the Eclipse Distribution License is available at
00011  *   http://www.eclipse.org/org/documents/edl-v10.php.
00012  *
00013  * Contributors:
00014  *    Ian Craggs - initial API and implementation and/or initial documentation
00015  *******************************************************************************/
00016 
00017 /**
00018  This is a sample program to illustrate the use of the MQTT Client library
00019  on the mbed platform.  The Client class requires two classes which mediate
00020  access to system interfaces for networking and timing.  As long as these two
00021  classes provide the required public programming interfaces, it does not matter
00022  what facilities they use underneath. In this program, they use the mbed
00023  system libraries.
00024 
00025 */
00026 
00027 
00028 #include "MQTTESP8266.h"
00029 #include "MQTTClient.h"
00030 
00031 int arrivedcount = 0;
00032 
00033 // callback for subscribe topic
00034 void subscribeCallback(MQTT::MessageData& md)
00035 {
00036     MQTT::Message &message = md.message;
00037     printf("Message received: qos %d, retained %d, dup %d, packetid %d\n", message.qos, message.retained, message.dup, message.id);
00038     printf("Payload %.*s\n", message.payloadlen, (char*)message.payload);
00039     ++arrivedcount;
00040 }
00041 
00042 // main function that sets up the subscription and makes a couple publishes
00043 int main(int argc, char* argv[])
00044 {
00045     printf("Starting\n");
00046     MQTTESP8266 ipstack(D1, D0, D10, "demossid","passphrase"); // change to match your wifi access point
00047     float version = 0.47;
00048     char* topic = "mbed-sample";
00049 
00050     printf("Version is %f\n", version);
00051 
00052     MQTT::Client<MQTTESP8266, Countdown> client = MQTT::Client<MQTTESP8266, Countdown>(ipstack);
00053 
00054     char* hostname = "85.119.83.194"; // test.mosquitto.org
00055     int port = 1883;
00056     int rc = ipstack.connect(hostname, port);
00057     if (rc != 0)
00058         printf("rc from TCP connect is %d\n", rc);
00059 
00060     MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
00061     data.MQTTVersion = 3;
00062     data.clientID.cstring = "mbed-clientID";
00063     data.username.cstring = "testuser";
00064     data.password.cstring = "testpassword";
00065     if ((rc = client.connect(data)) != 0)
00066         printf("rc from MQTT connect is %d\n", rc);
00067 
00068     if ((rc = client.subscribe(topic, MQTT::QOS1, subscribeCallback)) != 0)
00069         printf("Recv'd from MQTT subscribe is %d\n", rc);
00070 
00071     MQTT::Message message;
00072 
00073     // QoS 0
00074     char buf[100];
00075     sprintf(buf, "Hello World!  QoS 0 message from app version %f\n", version);
00076     message.qos = MQTT::QOS0;
00077     message.retained = false;
00078     message.dup = false;
00079     message.payload = (void*)buf;
00080     message.payloadlen = strlen(buf)+1;
00081     rc = client.publish(topic, message);
00082     while (arrivedcount < 1)
00083         client.yield(100);
00084 
00085     // QoS 1
00086     sprintf(buf, "Hello World!  QoS 1 message from app version %f\n", version);
00087     message.qos = MQTT::QOS1;
00088     message.payloadlen = strlen(buf)+1;
00089     rc = client.publish(topic, message);
00090     while (arrivedcount < 2)
00091         client.yield(100);
00092 
00093     // QoS 2
00094     sprintf(buf, "Hello World!  QoS 2 message from app version %f\n", version);
00095     message.qos = MQTT::QOS2;
00096     message.payloadlen = strlen(buf)+1;
00097     rc = client.publish(topic, message);
00098     while (arrivedcount < 3)
00099         client.yield(100);
00100 
00101     // n * QoS 2
00102     for (int i = 1; i <= 10; ++i) {
00103         sprintf(buf, "Hello World!  QoS 2 message number %d from app version %f\n", i, version);
00104         message.qos = MQTT::QOS2;
00105         message.payloadlen = strlen(buf)+1;
00106         rc = client.publish(topic, message);
00107         while (arrivedcount < i + 3)
00108             client.yield(100);
00109     }
00110 
00111     if ((rc = client.unsubscribe(topic)) != 0)
00112         printf("rc from unsubscribe was %d\n", rc);
00113 
00114     if ((rc = client.disconnect()) != 0)
00115         printf("rc from disconnect was %d\n", rc);
00116 
00117     ipstack.disconnect();
00118     printf("Finishing with %d messages received\n", arrivedcount);
00119 
00120     return 0;
00121 }