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

Dependencies:   UIPEthernet MQTTClient

Revision:
11:a049ad4b63c0
Parent:
10:27440d67d4f2
Child:
12:3af0ae09bd01
--- a/main.cpp	Sun Nov 29 19:45:46 2015 +0000
+++ b/main.cpp	Sat Jul 01 09:06:43 2017 +0000
@@ -3,18 +3,15 @@
 // The MQTT client also subscribes to some topics which are in its interest to receive.
 // Ethernet connection is assured by an affordable ENC28J60 Ethernet module.
 #include "mbed.h"
-#include <UIPEthernet.h>
-#include <UIPClient.h>
-#include <MQTTClient.h>
-#include <string>
+#include "UIPEthernet.h"
+#include "MQTTClient.h"
 
 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.
+#define DHCP    1   // if you'd like to use static IP address comment out this line 
+
 #if defined(TARGET_LPC1768)
-UIPEthernetClass    UIPEthernet(p11, p12, p13, p8);         // mosi, miso, sck, cs
+UIPEthernet  uIPEthernet(p11, p12, p13, p8);         // 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)  \
@@ -23,10 +20,10 @@
    || defined(TARGET_K20D50M) || defined(TARGET_K22F) \
    || defined(TARGET_NRF51822) \
    || defined(TARGET_RZ_A1H)
-UIPEthernetClass    UIPEthernet(D11, D12, D13, D10);        // mosi, miso, sck, cs
+UIPEthernet  uIPEthernet(D11, D12, D13, D10);        // mosi, miso, sck, cs
 #endif
 
-// MAC number must be unique within the connected network. Modify as appropriate.
+// MAC address must be unique within the connected network. Modify as appropriate.
 const uint8_t       MY_MAC[6] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05 };
 
 // MQTT broker is like a post office.
@@ -41,16 +38,6 @@
 MQTTClient          mqttClient(serverIP, 1883, onMqttMessage, ethernetClient);
 char                message_buff[100];  // buffer to store received messages
 
-#define DHCP    1   // comment out this line if you'd like to use static IP address
-
-#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
-
-
 /**
  * @brief
  * @note
@@ -66,29 +53,22 @@
     unsigned long       t = 0;
     unsigned long       lastTime = t;
 
-    // initialize the ethernet device
-
 #if defined(DHCP)
     pc.printf("Searching for DHCP server..\r\n");
-
-    if(UIPEthernet.begin(MY_MAC) != 1) {
+    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]);
+    pc.printf("DHCP server found.\r\n");
 #else
-    UIPEthernet.begin(MY_MAC, MY_IP);
+    // IP address must be unique and compatible with your network.
+    const IPAddress MY_IP(192, 168, 1, 181);    //  Change as appropriate.
+    uIPEthernet.begin(MY_MAC, MY_IP);
 #endif
-
+    pc.printf("Local IP = %s\r\n", uIPEthernet.localIP().toString());
     pc.printf("Connecting to MQTT broker ..\r\n");
-    do
-    {
+    do {
         wait(1.0);
         connected = mqttClient.connect("myMQTTHelloClient");
     } while(!connected && (i++ < MAX_TRIES));