MQ Telemetry Transport client publishing parameters measured by a DHT11 sensor. Ethernet connection is via an ENC28J60 module.

Dependencies:   DHT11 MQTTClient UIPEthernet mbed

Files at this revision

API Documentation at this revision

Comitter:
hudakz
Date:
Sat Jul 01 09:49:02 2017 +0000
Parent:
2:f706efb3ea13
Commit message:
Updated.

Changed in this revision

MQTTClient.lib Show annotated file Show diff for this revision Revisions of this file
UIPEthernet.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
mbed.bld Show annotated file Show diff for this revision Revisions of this file
--- a/MQTTClient.lib	Sat Mar 21 23:47:23 2015 +0000
+++ b/MQTTClient.lib	Sat Jul 01 09:49:02 2017 +0000
@@ -1,1 +1,1 @@
-http://mbed.org/users/hudakz/code/MQTTClient/#87da395325fc
+http://mbed.org/users/hudakz/code/MQTTClient/#ca17c5d846e7
--- a/UIPEthernet.lib	Sat Mar 21 23:47:23 2015 +0000
+++ b/UIPEthernet.lib	Sat Jul 01 09:49:02 2017 +0000
@@ -1,1 +1,1 @@
-http://mbed.org/users/hudakz/code/UIPEthernet/#d774541a34da
+http://mbed.org/users/hudakz/code/UIPEthernet/#4acb22344932
--- a/main.cpp	Sat Mar 21 23:47:23 2015 +0000
+++ b/main.cpp	Sat Jul 01 09:49:02 2017 +0000
@@ -2,40 +2,59 @@
 // It is publishing parameters measured by a DHT11 sensor.
 // Ethernet connection is assured via an ENC28J60 module.
 
-#include <mbed.h>
-#include <UIPEthernet.h>
-#include <UIPClient.h>
-#include <MQTTClient.h>
-#include <DHT11.h>
+#include "mbed.h"
+#include "UIPEthernet.h"
+#include "MQTTClient.h"
+#include "DHT11.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.
-#if defined(TARGET_LPC1768)
-UIPEthernetClass    UIPEthernet(p11, p12, p13, p8);         // mosi, miso, sck, cs
-DHT11               dht11(p6);
-#elif defined(TARGET_NUCLEO_F103RB)
-UIPEthernetClass    UIPEthernet(PB_5, PB_4, PB_3, PB_6);    // mosi, miso, sck, cs
-DHT11               dht11(PC_14);
+// MAC address must be unique within the connected network. Modify as appropriate.
+const uint8_t   MY_MAC[6] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05 };
+
+#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
 
-// Make sure that the MAC number is unique within the connected network.
-const uint8_t       MY_MAC[6] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05 };
-// This client is using a static IP address. Make sure DHCP is disabled on your router/modem.
-// 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];
-void                onMessage(char* topic, uint8_t* payload, unsigned int length);
-// In case your MQTT broker has different IP adress modify the following definition accordingly.
-IPAddress           serverIP(192, 168, 1, 30);  // MQTT broker (e.g. 'Mosquitto' running on a Raspberry Pi or Linux device)
-// The MQTT broker is like a post office distributing messages received from publishing clients to all subscribers (clients).  
-// So the 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 from other clients, but via the broker.
-EthernetClient      ethernetClient;
-void                onMqttMessage(char* topic, uint8_t* payload, unsigned int length);
-MQTTClient          mqttClient(serverIP, 1883, onMqttMessage, ethernetClient);
-const int           PERIOD = 10;    // period for publishing the messages (in seconds)
-Serial              pc(USBTX, USBRX);
+// uIPEthernet is the name of a global instance of UIPEthernet.
+// 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)
+UIPEthernet  uIPEthernet(p11, p12, p13, p8);         // mosi, miso, sck, cs
+DHT11        dht11(p6);
+#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)
+UIPEthernet  uIPEthernet(D11, D12, D13, D10);        // mosi, miso, sck, cs
+DHT11        dht11(PC_14);
+#endif
+
+char  message_buff[100];
+void  onMessage(char* topic, uint8_t* payload, unsigned int length);
+
+// MQTT broker is like a post office.
+// Its task is to distribute messages published by clients to all subscribers (other clients).
+// So messages published by this client will be sent to the broker.
+// Then the broker will send them to all clients which subscribed to such topics.
+// 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);
+const int       INTERVAL = 10;  // Interval for publishing messages (in seconds)
+
 
 /**
  * @brief   Main
@@ -51,28 +70,42 @@
     time_t      lastTime = 0;
 
     // initialize the ethernet device
-    UIPEthernet.begin(MY_MAC, MY_IP);
+    
+#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");
+#else
+    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("myMqttClient");
     } while(!connected && (i < MAX_COUNT));
 
-    if(connected) {
+    if (connected) {
         pc.printf("MQTT broker connected.\r\n");
         // The client can subscribe to as many MQTT messages as you like.
-        mqttClient.subscribe("outdoor/gasmeter"); 
-        mqttClient.subscribe("livingroom/temperature");
+        mqttClient.subscribe("example/hello"); 
         mqttClient.subscribe("boiler/outlet/temperature");
+        pc.printf("Subscribed to:\r\n");
+        pc.printf("    example/hello\r\n");
+        pc.printf("    boiler/outlet/temperature\r\n");
     }
     else {
         pc.printf("Failed to connect to MQTT broker.\r\n");
     }
 
-    while(1) {
+    while (1) {
         t = time(NULL);
-        if(t >= (lastTime + PERIOD)) {
+        if(t >= (lastTime + INTERVAL)) {
             lastTime = t;
             if(connected) {
                 pc.printf("---------------------\r\n");
@@ -116,14 +149,15 @@
     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++) {
+    for (i = 0; i < length; i++) {
         message_buff[i] = payload[i];
     }
 
     message_buff[i] = '\0';
-    pc.printf("  Payload: %s\r\n", message_buff);
+    pc.printf("    Payload: %s\r\n", message_buff);
 }
+
--- a/mbed.bld	Sat Mar 21 23:47:23 2015 +0000
+++ b/mbed.bld	Sat Jul 01 09:49:02 2017 +0000
@@ -1,1 +1,1 @@
-http://mbed.org/users/mbed_official/code/mbed/builds/487b796308b0
\ No newline at end of file
+https://mbed.org/users/mbed_official/code/mbed/builds/64910690c574
\ No newline at end of file