MQTT Client example program. Ethernet connection is via an ENC28J60 module.

Dependencies:   UIPEthernet MQTTClient

Revision:
2:6cd2390302ac
Parent:
1:49fb5754df16
Child:
3:3e5f4d503a66
--- a/main.cpp	Sat Dec 20 12:04:36 2014 +0000
+++ b/main.cpp	Sat Mar 21 23:45:41 2015 +0000
@@ -1,44 +1,48 @@
-// In this example an MQTT client is created using an ENC28J60
-// It is publishing a simple 'example/hello' message 'Hello World.'
-// and subscribes to 'outdoor/temperature' messages
-// UIPEthernet library is used to drive the ENC28J60
+// In this example an MQTT client is created.
+// It is publishing a simple 'example/hello' message with payload 'Hello World.'
+// and subscribes to 'outdoor/temperature' messages assumably published by other client(s).
+// Ethernet connection is assured via an ENC28J60 module.
 #include "mbed.h"
 #include <UIPEthernet.h>
 #include <UIPClient.h>
 #include <MQTTClient.h>
 #include <string>
 
-using namespace std;
+using namespace     std;
+
+Serial              pc(USBTX, USBRX);
 
 // UIPEthernet is the name of a global instance of UIPEthernetClass.
 // Do not change the name! It is used within the UIPEthernet library.
 // Adapt the SPI pin names to your mbed platform/board if not present yet.
 #if defined(TARGET_LPC1768)
-UIPEthernetClass UIPEthernet(p11, p12, p13, p8); // mosi, miso, sck, cs
+UIPEthernetClass    UIPEthernet(p11, p12, p13, p8);         // mosi, miso, sck, cs
 #elif defined(TARGET_LPC1114)
-UIPEthernetClass UIPEthernet(dp2, dp1, dp6, dp25); // mosi, miso, sck, cs
+UIPEthernetClass    UIPEthernet(dp2, dp1, dp6, dp25);       // mosi, miso, sck, cs
 #elif defined(TARGET_LPC11U68)
-UIPEthernetClass UIPEthernet(P0_9, P0_8, P1_29, P0_2); // mosi, miso, sck, cs
-#elif defined (TARGET_NUCLEO_F103RB)
-UIPEthernetClass UIPEthernet(PB_5, PB_4, PB_3, PB_6); // mosi, miso, sck, cs
+UIPEthernetClass    UIPEthernet(P0_9, P0_8, P1_29, P0_2);   // mosi, miso, sck, cs
+#elif defined(TARGET_NUCLEO_F103RB)
+UIPEthernetClass    UIPEthernet(PB_5, PB_4, PB_3, PB_6);    // mosi, miso, sck, cs
 #endif
 
-//Serial pc(PA_9, PA_10);
-Serial pc(USBTX, USBRX);
-
-
 // Make sure that the MAC number is unique within the connected network.
-const uint8_t    MY_MAC[6] = {0x00,0x01,0x02,0x03,0x04,0x05};
+const uint8_t       MY_MAC[6] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05 };
 // IP address must be unique and compatible with your network too. Change as appropriate.
-const IPAddress  MY_IP(192,168,1,181);
-char             message_buff[100];
-IPAddress        serverIP(192,168,1,30);     // MQTT server (e.g. 'mosquitto' running on a Ubuntu PC or Raspberry Pi)
-EthernetClient   ethernetClient;
-void             onMqttMessage(char* topic, uint8_t* payload, unsigned int length);
-MQTTClient       mqttClient(serverIP, 1883, onMqttMessage, ethernetClient);
+const IPAddress     MY_IP(192, 168, 1, 181);
+char                message_buff[100];
+IPAddress           serverIP(192, 168, 1, 30);  // MQTT broker (e.g. 'Mosquitto' running on a Raspberry Pi or Linux machine)
+EthernetClient      ethernetClient;
+void                onMqttMessage(char* topic, uint8_t* payload, unsigned int length);
+MQTTClient          mqttClient(serverIP, 1883, onMqttMessage, ethernetClient);
 
-int main()
-{
+/**
+ * @brief
+ * @note
+ * @param
+ * @retval
+ */
+
+int main(void) {
     const int   MAX_COUNT = 5;
     int         i = 0;
     bool        connected = false;
@@ -47,24 +51,27 @@
     time_t      lastTime = t;
 
     // initialize the ethernet device
+
     UIPEthernet.begin(MY_MAC, MY_IP);
-//    IPAddress localIP = UIPEthernet.localIP();
-//    pc.printf("IP = ");
-//    for(uint8_t i = 0; i < 3; i++)
-//        pc.printf("%d.", localIP[i]);
-//    pc.printf("%d\r\n", localIP[3]);
-    pc.printf("Connecting to MQTT server ..\r\n");
-    do {
+
+    // The MQTT broker is like a post office distributing messages published by clients to all subscribers (clients).
+    // So the 'example/hello' messages published by this client will be sent via the broker to all clients which subscribed to such messages.
+    // This way also this client will receive all subscribed messages (in this case 'outdoor/temperature' messages)
+    // published by other clients (however not directly but via the broker).
+    pc.printf("Connecting to MQTT broker ..\r\n");
+    do
+    {
         wait(1.0);
         connected = mqttClient.connect("myMQTTHelloClient");
     } while(!connected && (i < MAX_COUNT));
 
     if(connected) {
-        pc.printf("MQTT Server connected.\r\n");
+        pc.printf("MQTT broker connected.\r\n");
         mqttClient.subscribe("outdoor/temperature");
         pc.printf("Subscribed to: outdoor/temperature\r\n");
-    } else {
-        pc.printf("Failed to connect to MQTT server.\r\n");
+    }
+    else {
+        pc.printf("Failed to connect to MQTT broker.\r\n");
     }
 
     while(1) {
@@ -75,14 +82,21 @@
                 mqttClient.publish("example/hello", payload);
                 pc.printf("Published: example/hello\r\n");
             }
+        }
 
-        }
         mqttClient.loop();  // MQTT client loop processing
     }
 }
 
-void onMqttMessage(char* topic, uint8_t* payload, unsigned int length)
-{
+/**
+ * @brief   Called on new MQTT message arrival
+ * @note    
+ * @param   topic:      The topic of the new message
+ *          payload:    The payload of the new message
+ *          length:     Payload's length
+ * @retval
+ */
+void onMqttMessage(char* topic, uint8_t* payload, unsigned int length) {
     int i = 0;
 
     pc.printf("Message arrived:\r\n");