Example MQTT implemented on the ESP8266

Dependencies:   ESP8266Interface MQTT mbed-rtos mbed

Fork of HelloMQTT by MQTT

This is an example of how to run MQTT using the esp8266 as the network connection. This program will default to trying to talk to a public MQTT server (test.mosquitto.org). The example will subscribe to a topic and then publish to that topic. In this way it will effectively echo back to itself. You can alternatively use a private mqtt broker,[[TODO: |instructions here]]

Please note that the ESP8266 interface cannot translate hostnames to IP Addresses, it can only accept raw IP Addresses as input.

Files at this revision

API Documentation at this revision

Comitter:
mbedAustin
Date:
Tue Jun 09 17:57:43 2015 +0000
Parent:
17:92a64d43ee61
Commit message:
double checked everything working. Pub/sub to same topic for self echoing.

Changed in this revision

ESP8266Interface.lib Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/ESP8266Interface.lib	Fri Jun 05 15:15:24 2015 +0000
+++ b/ESP8266Interface.lib	Tue Jun 09 17:57:43 2015 +0000
@@ -1,1 +1,1 @@
-http://developer.mbed.org/teams/ESP8266/code/ESP8266Interface/#c180905b5b79
+http://developer.mbed.org/teams/ESP8266/code/ESP8266Interface/#1f4dd0e91837
--- a/main.cpp	Fri Jun 05 15:15:24 2015 +0000
+++ b/main.cpp	Tue Jun 09 17:57:43 2015 +0000
@@ -30,19 +30,20 @@
 
 int arrivedcount = 0;
 
-
-void messageArrived(MQTT::MessageData& md)
+// callback for subscribe topic
+void subscribeCallback(MQTT::MessageData& md)
 {
     MQTT::Message &message = md.message;
-    printf("Message arrived: qos %d, retained %d, dup %d, packetid %d\n", message.qos, message.retained, message.dup, message.id);
+    printf("Message received: qos %d, retained %d, dup %d, packetid %d\n", message.qos, message.retained, message.dup, message.id);
     printf("Payload %.*s\n", message.payloadlen, (char*)message.payload);
     ++arrivedcount;
 }
 
+// main function that sets up the subscription and makes a couple publishes
 int main(int argc, char* argv[])
 {
     printf("Starting\n");
-    MQTTESP8266 ipstack(D1, D0, D10, "ssid", "password");
+    MQTTESP8266 ipstack(D1, D0, D10, "demossid","passphrase"); // change to match your wifi access point
     float version = 0.47;
     char* topic = "mbed-sample";
 
@@ -50,7 +51,7 @@
 
     MQTT::Client<MQTTESP8266, Countdown> client = MQTT::Client<MQTTESP8266, Countdown>(ipstack);
 
-    char* hostname = "192.168.1.20";
+    char* hostname = "85.119.83.194"; // test.mosquitto.org
     int port = 1883;
     int rc = ipstack.connect(hostname, port);
     if (rc != 0)
@@ -58,14 +59,14 @@
 
     MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
     data.MQTTVersion = 3;
-    data.clientID.cstring = "mbed-sample";
+    data.clientID.cstring = "mbed-clientID";
     data.username.cstring = "testuser";
     data.password.cstring = "testpassword";
     if ((rc = client.connect(data)) != 0)
         printf("rc from MQTT connect is %d\n", rc);
 
-    if ((rc = client.subscribe("mbed-sample", MQTT::QOS1, messageArrived)) != 0)
-        printf("rc from MQTT subscribe is %d\n", rc);
+    if ((rc = client.subscribe(topic, MQTT::QOS1, subscribeCallback)) != 0)
+        printf("Recv'd from MQTT subscribe is %d\n", rc);
 
     MQTT::Message message;