MQ Telemetry Transport client publishing parameters measured by a DHT11 sensor. Ethernet connection is via an ENC28J60 module.
Dependencies: DHT11 MQTTClient UIPEthernet mbed
Fork of MQTT_DHT11_ENC28J60 by
Diff: main.cpp
- Revision:
- 2:f706efb3ea13
- Parent:
- 1:4be688be4e0d
- Child:
- 3:de6cc3ff5aaa
--- a/main.cpp Sat Dec 20 12:03:10 2014 +0000 +++ b/main.cpp Sat Mar 21 23:47:23 2015 +0000 @@ -1,6 +1,7 @@ -// In this example an MQTT client is created. -// It is publishing parameters measured by a DHT11 sensor -// UIPEthernet library is used to drive the ENC28J60 board +// In this project an MQTT client is created. +// 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> @@ -11,50 +12,62 @@ // 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); +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); #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}; +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); +const IPAddress MY_IP(192, 168, 1, 181); char message_buff[100]; void onMessage(char* topic, uint8_t* payload, unsigned int length); -IPAddress serverIP(192,168,1,30); // MQTT server (e.g. 'mosquitto' running on a Raspberry Pi or Ubuntu) +// 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; // seconds - +const int PERIOD = 10; // period for publishing the messages (in seconds) Serial pc(USBTX, USBRX); -int main() -{ +/** + * @brief Main + * @note + * @param + * @retval + */ +int main(void) { const int MAX_COUNT = 5; int i = 0; bool connected = false; time_t t = 0; time_t lastTime = 0; - + // initialize the ethernet device UIPEthernet.begin(MY_MAC, MY_IP); - pc.printf("Connecting to MQTT server ..\r\n"); - do { + pc.printf("Connecting to MQTT broker ..\r\n"); + do + { wait(1.0); connected = mqttClient.connect("myMqttClient"); } while(!connected && (i < MAX_COUNT)); if(connected) { - pc.printf("MQTT Server connected.\r\n"); - mqttClient.subscribe("rt_clock"); - pc.printf("Subscribed to: "); - pc.printf("rt_clock\r\n"); - } else { - pc.printf("Failed to connect to MQTT server.\r\n"); + 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("boiler/outlet/temperature"); + } + else { + pc.printf("Failed to connect to MQTT broker.\r\n"); } while(1) { @@ -64,17 +77,20 @@ if(connected) { pc.printf("---------------------\r\n"); pc.printf("%ds:\r\n", t); + int state = dht11.readData(); if(state == DHT11::OK) { - float hum = dht11.readHumidity(); + float hum = dht11.readHumidity(); sprintf(message_buff, "%4.1f", hum); pc.printf(" hum = %s%%\r\n", message_buff); mqttClient.publish("outdoor/humidity", message_buff); - float temp = dht11.readTemperature(); + + float temp = dht11.readTemperature(); sprintf(message_buff, "%5.1f", temp); pc.printf(" temp = %s'C\r\n", message_buff); mqttClient.publish("outdoor/temperature", message_buff); - float dewPoint = temp - (100 - hum)/5.0; + + float dewPoint = temp - (100 - hum) / 5.0; sprintf(message_buff, "%5.1f", dewPoint); pc.printf(" dew point = %s'C\r\n", message_buff); mqttClient.publish("outdoor/dewpoint", message_buff); @@ -83,12 +99,20 @@ pc.printf(" DHT11 error: %d\r\n", state); } } + 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"); @@ -103,7 +127,3 @@ message_buff[i] = '\0'; pc.printf(" Payload: %s\r\n", message_buff); } - - - -