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

Dependencies:   UIPEthernet MQTTClient

Revision:
3:3e5f4d503a66
Parent:
2:6cd2390302ac
Child:
4:abcd50d71e61
Child:
6:fb8b9925e844
--- a/main.cpp	Sat Mar 21 23:45:41 2015 +0000
+++ b/main.cpp	Sat Nov 28 10:33:33 2015 +0000
@@ -1,7 +1,7 @@
 // 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.
+// and subscribes to some messages assumably published by other client(s).
+// Ethernet connection is assured by an ENC28J60 Ehernet module.
 #include "mbed.h"
 #include <UIPEthernet.h>
 #include <UIPClient.h>
@@ -12,25 +12,44 @@
 
 Serial              pc(USBTX, USBRX);
 
+#define DHCP    1   // comment out this line if you'd like to use static IP address
+
 // 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
-#elif defined(TARGET_LPC1114)
-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
+#elif defined(TARGET_NUCLEO_F103RB) || defined(TARGET_NUCLEO_L152RE) || defined(TARGET_NUCLEO_F030R8) || defined(TARGET_NUCLEO_F401RE) \
+   || defined(TARGET_NUCLEO_F302R8) || defined(TARGET_NUCLEO_L053R8) || defined(TARGET_NUCLEO_F411RE) || defined(TARGET_NUCLEO_F334R8) \
+   || defined(TARGET_NUCLEO_F072RB) || defined(TARGET_NUCLEO_F091RC) || defined(TARGET_NUCLEO_F303RE) || defined(TARGET_NUCLEO_F070RB) \
+   || defined(TARGET_KL25Z ) || defined(TARGET_KL46Z) || defined(TARGET_K64F) || defined(TARGET_KL05Z) \
+   || defined(TARGET_K20D50M) || defined(TARGET_K22F) \
+   || defined(TARGET_NRF51822) \
+   || defined(TARGET_RZ_A1H)
+UIPEthernetClass    UIPEthernet(D11, D12, D13, D10);        // mosi, miso, sck, cs
 #endif
 
-// Make sure that the MAC number is unique within the connected network.
+// MAC number must be unique within the connected network. Modify as appropriate.
 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);
+
+#if !defined(DHCP)
+// In case you'd like to use static IP address:
+// IP address must be unique and compatible with your network.
+// Change as appropriate.
+const IPAddress MY_IP(192, 168, 1, 181);
+#endif
+
+const int           INTERVAL = 10;    // Interval for publishing the messages (in seconds)
+
 char                message_buff[100];
-IPAddress           serverIP(192, 168, 1, 30);  // MQTT broker (e.g. 'Mosquitto' running on a Raspberry Pi or Linux machine)
+
+// MQTT broker is like a post office.
+// Its task is to distribute messages published by clients to all subscribers (other clients).
+// So the 'example/hello' messages published by this client will be sent to the broker.
+// Then the broker will send them to all clients which subscribed to such topic (example/hello).
+// Also this client will receive all messages with topics it subscribed to.
+// 'Mosquitto' is a free implementation of MQTT broker for Linux machines (e.g. Raspberry Pi, Ubuntu etc.)
+IPAddress           serverIP(192, 168, 1, 30);  // IP address of your MQTT broker (adapt)
 EthernetClient      ethernetClient;
 void                onMqttMessage(char* topic, uint8_t* payload, unsigned int length);
 MQTTClient          mqttClient(serverIP, 1883, onMqttMessage, ethernetClient);
@@ -52,12 +71,24 @@
 
     // initialize the ethernet device
 
+#if defined(DHCP)
+    pc.printf("Searching for DHCP server..\r\n");
+ 
+    if(UIPEthernet.begin(MY_MAC) != 1) {
+        pc.printf("No DHCP server found.\r\n");
+        pc.printf("Exiting application.\r\n");
+        return 0;
+    }
+    pc.printf("DHCP server found and configuration info received.\r\n");
+    IPAddress   localIP = UIPEthernet.localIP();
+    pc.printf("Local IP = ");
+    for(uint8_t i = 0; i < 3; i++)
+        pc.printf("%d.", localIP[i]);
+    pc.printf("%d\r\n", localIP[3]);
+#else
     UIPEthernet.begin(MY_MAC, MY_IP);
-
-    // 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).
+#endif
+    
     pc.printf("Connecting to MQTT broker ..\r\n");
     do
     {
@@ -67,8 +98,12 @@
 
     if(connected) {
         pc.printf("MQTT broker connected.\r\n");
-        mqttClient.subscribe("outdoor/temperature");
-        pc.printf("Subscribed to: outdoor/temperature\r\n");
+        // The client can subscribe to as many topics as you like.
+        mqttClient.subscribe("outdoor/temperature"); 
+        mqttClient.subscribe("boiler/outlet/temperature");
+        pc.printf("Subscribed to the following topics:\r\n");
+        pc.printf("    outdoor/temperature\r\n");
+        pc.printf("    boiler/outlet/temperature\r\n");
     }
     else {
         pc.printf("Failed to connect to MQTT broker.\r\n");
@@ -83,8 +118,7 @@
                 pc.printf("Published: example/hello\r\n");
             }
         }
-
-        mqttClient.loop();  // MQTT client loop processing
+        mqttClient.loop();  // MQTT client loop processing (receiving messages)
     }
 }
 
@@ -100,8 +134,8 @@
     int i = 0;
 
     pc.printf("Message arrived:\r\n");
-    pc.printf("  Topic: %s\r\n", topic);
-    pc.printf("  Length: %d\r\n", length);
+    pc.printf("    Topic: %s\r\n", topic);
+    pc.printf("    Length: %d\r\n", length);
 
     // create character buffer with ending null terminator (string)
     for(i = 0; i < length; i++) {