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

Dependencies:   DHT11 MQTTClient UIPEthernet mbed

Committer:
hudakz
Date:
Sat Jul 01 09:49:02 2017 +0000
Revision:
3:de6cc3ff5aaa
Parent:
2:f706efb3ea13
Updated.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
hudakz 2:f706efb3ea13 1 // In this project an MQTT client is created.
hudakz 2:f706efb3ea13 2 // It is publishing parameters measured by a DHT11 sensor.
hudakz 2:f706efb3ea13 3 // Ethernet connection is assured via an ENC28J60 module.
hudakz 2:f706efb3ea13 4
hudakz 3:de6cc3ff5aaa 5 #include "mbed.h"
hudakz 3:de6cc3ff5aaa 6 #include "UIPEthernet.h"
hudakz 3:de6cc3ff5aaa 7 #include "MQTTClient.h"
hudakz 3:de6cc3ff5aaa 8 #include "DHT11.h"
hudakz 3:de6cc3ff5aaa 9
hudakz 3:de6cc3ff5aaa 10 Serial pc(USBTX, USBRX);
hudakz 0:092b5e724233 11
hudakz 3:de6cc3ff5aaa 12 // MAC address must be unique within the connected network. Modify as appropriate.
hudakz 3:de6cc3ff5aaa 13 const uint8_t MY_MAC[6] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05 };
hudakz 3:de6cc3ff5aaa 14
hudakz 3:de6cc3ff5aaa 15 #define DHCP 1 // comment out this line if you'd like to use static IP address
hudakz 3:de6cc3ff5aaa 16
hudakz 3:de6cc3ff5aaa 17 #if !defined(DHCP)
hudakz 3:de6cc3ff5aaa 18 // In case you'd like to use static IP address:
hudakz 3:de6cc3ff5aaa 19 // IP address must be unique and compatible with your network.
hudakz 3:de6cc3ff5aaa 20 // Change as appropriate.
hudakz 3:de6cc3ff5aaa 21 const IPAddress MY_IP(192, 168, 1, 181);
hudakz 0:092b5e724233 22 #endif
hudakz 0:092b5e724233 23
hudakz 3:de6cc3ff5aaa 24 // uIPEthernet is the name of a global instance of UIPEthernet.
hudakz 3:de6cc3ff5aaa 25 // Do not change the name! It is used within the UIPEthernet library.
hudakz 3:de6cc3ff5aaa 26 // Adapt the SPI pin names to your mbed platform/board if not present yet.
hudakz 3:de6cc3ff5aaa 27
hudakz 3:de6cc3ff5aaa 28 #if defined(TARGET_LPC1768)
hudakz 3:de6cc3ff5aaa 29 UIPEthernet uIPEthernet(p11, p12, p13, p8); // mosi, miso, sck, cs
hudakz 3:de6cc3ff5aaa 30 DHT11 dht11(p6);
hudakz 3:de6cc3ff5aaa 31 #elif defined(TARGET_NUCLEO_F103RB) || defined(TARGET_NUCLEO_L152RE) || defined(TARGET_NUCLEO_F030R8) \
hudakz 3:de6cc3ff5aaa 32 || defined(TARGET_NUCLEO_F401RE) || defined(TARGET_NUCLEO_F302R8) || defined(TARGET_NUCLEO_L053R8) \
hudakz 3:de6cc3ff5aaa 33 || defined(TARGET_NUCLEO_F411RE) || defined(TARGET_NUCLEO_F334R8) || defined(TARGET_NUCLEO_F072RB) \
hudakz 3:de6cc3ff5aaa 34 || defined(TARGET_NUCLEO_F091RC) || defined(TARGET_NUCLEO_F303RE) || defined(TARGET_NUCLEO_F070RB) \
hudakz 3:de6cc3ff5aaa 35 || defined(TARGET_KL25Z ) || defined(TARGET_KL46Z) || defined(TARGET_K64F) || defined(TARGET_KL05Z) \
hudakz 3:de6cc3ff5aaa 36 || defined(TARGET_K20D50M) || defined(TARGET_K22F) \
hudakz 3:de6cc3ff5aaa 37 || defined(TARGET_NRF51822) \
hudakz 3:de6cc3ff5aaa 38 || defined(TARGET_RZ_A1H)
hudakz 3:de6cc3ff5aaa 39 UIPEthernet uIPEthernet(D11, D12, D13, D10); // mosi, miso, sck, cs
hudakz 3:de6cc3ff5aaa 40 DHT11 dht11(PC_14);
hudakz 3:de6cc3ff5aaa 41 #endif
hudakz 3:de6cc3ff5aaa 42
hudakz 3:de6cc3ff5aaa 43 char message_buff[100];
hudakz 3:de6cc3ff5aaa 44 void onMessage(char* topic, uint8_t* payload, unsigned int length);
hudakz 3:de6cc3ff5aaa 45
hudakz 3:de6cc3ff5aaa 46 // MQTT broker is like a post office.
hudakz 3:de6cc3ff5aaa 47 // Its task is to distribute messages published by clients to all subscribers (other clients).
hudakz 3:de6cc3ff5aaa 48 // So messages published by this client will be sent to the broker.
hudakz 3:de6cc3ff5aaa 49 // Then the broker will send them to all clients which subscribed to such topics.
hudakz 3:de6cc3ff5aaa 50 // Also this client will receive all messages with topics it subscribed to.
hudakz 3:de6cc3ff5aaa 51 // 'Mosquitto' is a free implementation of MQTT broker for Linux machines (e.g. Raspberry Pi, Ubuntu etc.)
hudakz 3:de6cc3ff5aaa 52 IPAddress serverIP(192, 168, 1, 30); // IP address of your MQTT broker (adapt)
hudakz 3:de6cc3ff5aaa 53 EthernetClient ethernetClient;
hudakz 3:de6cc3ff5aaa 54 void onMqttMessage(char* topic, uint8_t* payload, unsigned int length);
hudakz 3:de6cc3ff5aaa 55 MQTTClient mqttClient(serverIP, 1883, onMqttMessage, ethernetClient);
hudakz 3:de6cc3ff5aaa 56 const int INTERVAL = 10; // Interval for publishing messages (in seconds)
hudakz 3:de6cc3ff5aaa 57
hudakz 0:092b5e724233 58
hudakz 2:f706efb3ea13 59 /**
hudakz 2:f706efb3ea13 60 * @brief Main
hudakz 2:f706efb3ea13 61 * @note
hudakz 2:f706efb3ea13 62 * @param
hudakz 2:f706efb3ea13 63 * @retval
hudakz 2:f706efb3ea13 64 */
hudakz 2:f706efb3ea13 65 int main(void) {
hudakz 0:092b5e724233 66 const int MAX_COUNT = 5;
hudakz 0:092b5e724233 67 int i = 0;
hudakz 0:092b5e724233 68 bool connected = false;
hudakz 0:092b5e724233 69 time_t t = 0;
hudakz 0:092b5e724233 70 time_t lastTime = 0;
hudakz 2:f706efb3ea13 71
hudakz 0:092b5e724233 72 // initialize the ethernet device
hudakz 3:de6cc3ff5aaa 73
hudakz 3:de6cc3ff5aaa 74 #if defined(DHCP)
hudakz 3:de6cc3ff5aaa 75 pc.printf("Searching for DHCP server..\r\n");
hudakz 3:de6cc3ff5aaa 76
hudakz 3:de6cc3ff5aaa 77 if (uIPEthernet.begin(MY_MAC) != 1) {
hudakz 3:de6cc3ff5aaa 78 pc.printf("No DHCP server found.\r\n");
hudakz 3:de6cc3ff5aaa 79 pc.printf("Exiting application.\r\n");
hudakz 3:de6cc3ff5aaa 80 return 0;
hudakz 3:de6cc3ff5aaa 81 }
hudakz 3:de6cc3ff5aaa 82 pc.printf("DHCP server found and configuration info received.\r\n");
hudakz 3:de6cc3ff5aaa 83 #else
hudakz 3:de6cc3ff5aaa 84 uIPEthernet.begin(MY_MAC, MY_IP);
hudakz 3:de6cc3ff5aaa 85 #endif
hudakz 3:de6cc3ff5aaa 86 pc.printf("Local IP = %s\r\n", uIPEthernet.localIP().toString());
hudakz 2:f706efb3ea13 87 pc.printf("Connecting to MQTT broker ..\r\n");
hudakz 3:de6cc3ff5aaa 88 do {
hudakz 0:092b5e724233 89 wait(1.0);
hudakz 0:092b5e724233 90 connected = mqttClient.connect("myMqttClient");
hudakz 0:092b5e724233 91 } while(!connected && (i < MAX_COUNT));
hudakz 0:092b5e724233 92
hudakz 3:de6cc3ff5aaa 93 if (connected) {
hudakz 2:f706efb3ea13 94 pc.printf("MQTT broker connected.\r\n");
hudakz 2:f706efb3ea13 95 // The client can subscribe to as many MQTT messages as you like.
hudakz 3:de6cc3ff5aaa 96 mqttClient.subscribe("example/hello");
hudakz 2:f706efb3ea13 97 mqttClient.subscribe("boiler/outlet/temperature");
hudakz 3:de6cc3ff5aaa 98 pc.printf("Subscribed to:\r\n");
hudakz 3:de6cc3ff5aaa 99 pc.printf(" example/hello\r\n");
hudakz 3:de6cc3ff5aaa 100 pc.printf(" boiler/outlet/temperature\r\n");
hudakz 2:f706efb3ea13 101 }
hudakz 2:f706efb3ea13 102 else {
hudakz 2:f706efb3ea13 103 pc.printf("Failed to connect to MQTT broker.\r\n");
hudakz 0:092b5e724233 104 }
hudakz 0:092b5e724233 105
hudakz 3:de6cc3ff5aaa 106 while (1) {
hudakz 0:092b5e724233 107 t = time(NULL);
hudakz 3:de6cc3ff5aaa 108 if(t >= (lastTime + INTERVAL)) {
hudakz 0:092b5e724233 109 lastTime = t;
hudakz 0:092b5e724233 110 if(connected) {
hudakz 0:092b5e724233 111 pc.printf("---------------------\r\n");
hudakz 1:4be688be4e0d 112 pc.printf("%ds:\r\n", t);
hudakz 2:f706efb3ea13 113
hudakz 0:092b5e724233 114 int state = dht11.readData();
hudakz 0:092b5e724233 115 if(state == DHT11::OK) {
hudakz 2:f706efb3ea13 116 float hum = dht11.readHumidity();
hudakz 0:092b5e724233 117 sprintf(message_buff, "%4.1f", hum);
hudakz 0:092b5e724233 118 pc.printf(" hum = %s%%\r\n", message_buff);
hudakz 0:092b5e724233 119 mqttClient.publish("outdoor/humidity", message_buff);
hudakz 2:f706efb3ea13 120
hudakz 2:f706efb3ea13 121 float temp = dht11.readTemperature();
hudakz 0:092b5e724233 122 sprintf(message_buff, "%5.1f", temp);
hudakz 0:092b5e724233 123 pc.printf(" temp = %s'C\r\n", message_buff);
hudakz 0:092b5e724233 124 mqttClient.publish("outdoor/temperature", message_buff);
hudakz 2:f706efb3ea13 125
hudakz 2:f706efb3ea13 126 float dewPoint = temp - (100 - hum) / 5.0;
hudakz 0:092b5e724233 127 sprintf(message_buff, "%5.1f", dewPoint);
hudakz 0:092b5e724233 128 pc.printf(" dew point = %s'C\r\n", message_buff);
hudakz 0:092b5e724233 129 mqttClient.publish("outdoor/dewpoint", message_buff);
hudakz 0:092b5e724233 130 }
hudakz 0:092b5e724233 131 else
hudakz 0:092b5e724233 132 pc.printf(" DHT11 error: %d\r\n", state);
hudakz 0:092b5e724233 133 }
hudakz 0:092b5e724233 134 }
hudakz 2:f706efb3ea13 135
hudakz 0:092b5e724233 136 mqttClient.loop(); // MQTT client loop processing
hudakz 0:092b5e724233 137 }
hudakz 0:092b5e724233 138 }
hudakz 0:092b5e724233 139
hudakz 2:f706efb3ea13 140 /**
hudakz 2:f706efb3ea13 141 * @brief Called on new MQTT message arrival
hudakz 2:f706efb3ea13 142 * @note
hudakz 2:f706efb3ea13 143 * @param topic: The topic of the new message
hudakz 2:f706efb3ea13 144 * payload: The payload of the new message
hudakz 2:f706efb3ea13 145 * length: Payload's length
hudakz 2:f706efb3ea13 146 * @retval
hudakz 2:f706efb3ea13 147 */
hudakz 2:f706efb3ea13 148 void onMqttMessage(char* topic, uint8_t* payload, unsigned int length) {
hudakz 0:092b5e724233 149 int i = 0;
hudakz 0:092b5e724233 150
hudakz 0:092b5e724233 151 pc.printf("Message arrived:\r\n");
hudakz 3:de6cc3ff5aaa 152 pc.printf(" Topic: %s\r\n", topic);
hudakz 3:de6cc3ff5aaa 153 pc.printf(" Length: %d\r\n", length);
hudakz 0:092b5e724233 154
hudakz 0:092b5e724233 155 // create character buffer with ending null terminator (string)
hudakz 3:de6cc3ff5aaa 156 for (i = 0; i < length; i++) {
hudakz 0:092b5e724233 157 message_buff[i] = payload[i];
hudakz 0:092b5e724233 158 }
hudakz 0:092b5e724233 159
hudakz 0:092b5e724233 160 message_buff[i] = '\0';
hudakz 3:de6cc3ff5aaa 161 pc.printf(" Payload: %s\r\n", message_buff);
hudakz 0:092b5e724233 162 }
hudakz 3:de6cc3ff5aaa 163