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 Dec 20 12:03:10 2014 +0000
Revision:
1:4be688be4e0d
Parent:
0:092b5e724233
Child:
2:f706efb3ea13
rev 01

Who changed what in which revision?

UserRevisionLine numberNew contents of line
hudakz 0:092b5e724233 1 // In this example an MQTT client is created.
hudakz 0:092b5e724233 2 // It is publishing parameters measured by a DHT11 sensor
hudakz 0:092b5e724233 3 // UIPEthernet library is used to drive the ENC28J60 board
hudakz 0:092b5e724233 4 #include <mbed.h>
hudakz 0:092b5e724233 5 #include <UIPEthernet.h>
hudakz 0:092b5e724233 6 #include <UIPClient.h>
hudakz 0:092b5e724233 7 #include <MQTTClient.h>
hudakz 0:092b5e724233 8 #include <DHT11.h>
hudakz 0:092b5e724233 9
hudakz 0:092b5e724233 10 // UIPEthernet is the name of a global instance of UIPEthernetClass.
hudakz 0:092b5e724233 11 // Do not change the name! It is used within the UIPEthernet library.
hudakz 0:092b5e724233 12 // Adapt the SPI pin names to your mbed platform/board if not present yet.
hudakz 0:092b5e724233 13 #if defined(TARGET_LPC1768)
hudakz 0:092b5e724233 14 UIPEthernetClass UIPEthernet(p11, p12, p13, p8); // mosi, miso, sck, cs
hudakz 0:092b5e724233 15 DHT11 dht11(p6);
hudakz 0:092b5e724233 16 #elif defined (TARGET_NUCLEO_F103RB)
hudakz 0:092b5e724233 17 UIPEthernetClass UIPEthernet(PB_5, PB_4, PB_3, PB_6); // mosi, miso, sck, cs
hudakz 0:092b5e724233 18 DHT11 dht11(PC_14);
hudakz 0:092b5e724233 19 #endif
hudakz 0:092b5e724233 20
hudakz 0:092b5e724233 21 // Make sure that the MAC number is unique within the connected network.
hudakz 0:092b5e724233 22 const uint8_t MY_MAC[6] = {0x00,0x01,0x02,0x03,0x04,0x05};
hudakz 0:092b5e724233 23 // IP address must be unique and compatible with your network too. Change as appropriate.
hudakz 0:092b5e724233 24 const IPAddress MY_IP(192,168,1,181);
hudakz 0:092b5e724233 25 char message_buff[100];
hudakz 0:092b5e724233 26 void onMessage(char* topic, uint8_t* payload, unsigned int length);
hudakz 0:092b5e724233 27 IPAddress serverIP(192,168,1,30); // MQTT server (e.g. 'mosquitto' running on a Raspberry Pi or Ubuntu)
hudakz 0:092b5e724233 28 EthernetClient ethernetClient;
hudakz 0:092b5e724233 29 void onMqttMessage(char* topic, uint8_t* payload, unsigned int length);
hudakz 0:092b5e724233 30 MQTTClient mqttClient(serverIP, 1883, onMqttMessage, ethernetClient);
hudakz 0:092b5e724233 31 const int PERIOD = 10; // seconds
hudakz 0:092b5e724233 32
hudakz 0:092b5e724233 33 Serial pc(USBTX, USBRX);
hudakz 0:092b5e724233 34
hudakz 0:092b5e724233 35 int main()
hudakz 0:092b5e724233 36 {
hudakz 0:092b5e724233 37 const int MAX_COUNT = 5;
hudakz 0:092b5e724233 38 int i = 0;
hudakz 0:092b5e724233 39 bool connected = false;
hudakz 0:092b5e724233 40 time_t t = 0;
hudakz 0:092b5e724233 41 time_t lastTime = 0;
hudakz 0:092b5e724233 42
hudakz 0:092b5e724233 43 // initialize the ethernet device
hudakz 1:4be688be4e0d 44 UIPEthernet.begin(MY_MAC, MY_IP);
hudakz 0:092b5e724233 45 pc.printf("Connecting to MQTT server ..\r\n");
hudakz 0:092b5e724233 46 do {
hudakz 0:092b5e724233 47 wait(1.0);
hudakz 0:092b5e724233 48 connected = mqttClient.connect("myMqttClient");
hudakz 0:092b5e724233 49 } while(!connected && (i < MAX_COUNT));
hudakz 0:092b5e724233 50
hudakz 0:092b5e724233 51 if(connected) {
hudakz 0:092b5e724233 52 pc.printf("MQTT Server connected.\r\n");
hudakz 0:092b5e724233 53 mqttClient.subscribe("rt_clock");
hudakz 0:092b5e724233 54 pc.printf("Subscribed to: ");
hudakz 0:092b5e724233 55 pc.printf("rt_clock\r\n");
hudakz 0:092b5e724233 56 } else {
hudakz 0:092b5e724233 57 pc.printf("Failed to connect to MQTT server.\r\n");
hudakz 0:092b5e724233 58 }
hudakz 0:092b5e724233 59
hudakz 0:092b5e724233 60 while(1) {
hudakz 0:092b5e724233 61 t = time(NULL);
hudakz 0:092b5e724233 62 if(t >= (lastTime + PERIOD)) {
hudakz 0:092b5e724233 63 lastTime = t;
hudakz 0:092b5e724233 64 if(connected) {
hudakz 0:092b5e724233 65 pc.printf("---------------------\r\n");
hudakz 1:4be688be4e0d 66 pc.printf("%ds:\r\n", t);
hudakz 0:092b5e724233 67 int state = dht11.readData();
hudakz 0:092b5e724233 68 if(state == DHT11::OK) {
hudakz 0:092b5e724233 69 float hum = dht11.readHumidity();
hudakz 0:092b5e724233 70 sprintf(message_buff, "%4.1f", hum);
hudakz 0:092b5e724233 71 pc.printf(" hum = %s%%\r\n", message_buff);
hudakz 0:092b5e724233 72 mqttClient.publish("outdoor/humidity", message_buff);
hudakz 0:092b5e724233 73 float temp = dht11.readTemperature();
hudakz 0:092b5e724233 74 sprintf(message_buff, "%5.1f", temp);
hudakz 0:092b5e724233 75 pc.printf(" temp = %s'C\r\n", message_buff);
hudakz 0:092b5e724233 76 mqttClient.publish("outdoor/temperature", message_buff);
hudakz 0:092b5e724233 77 float dewPoint = temp - (100 - hum)/5.0;
hudakz 0:092b5e724233 78 sprintf(message_buff, "%5.1f", dewPoint);
hudakz 0:092b5e724233 79 pc.printf(" dew point = %s'C\r\n", message_buff);
hudakz 0:092b5e724233 80 mqttClient.publish("outdoor/dewpoint", message_buff);
hudakz 0:092b5e724233 81 }
hudakz 0:092b5e724233 82 else
hudakz 0:092b5e724233 83 pc.printf(" DHT11 error: %d\r\n", state);
hudakz 0:092b5e724233 84 }
hudakz 0:092b5e724233 85 }
hudakz 0:092b5e724233 86 mqttClient.loop(); // MQTT client loop processing
hudakz 0:092b5e724233 87 }
hudakz 0:092b5e724233 88 }
hudakz 0:092b5e724233 89
hudakz 0:092b5e724233 90 void onMqttMessage(char* topic, uint8_t* payload, unsigned int length)
hudakz 0:092b5e724233 91 {
hudakz 0:092b5e724233 92 int i = 0;
hudakz 0:092b5e724233 93
hudakz 0:092b5e724233 94 pc.printf("Message arrived:\r\n");
hudakz 0:092b5e724233 95 pc.printf(" Topic: %s\r\n", topic);
hudakz 0:092b5e724233 96 pc.printf(" Length: %d\r\n", length);
hudakz 0:092b5e724233 97
hudakz 0:092b5e724233 98 // create character buffer with ending null terminator (string)
hudakz 0:092b5e724233 99 for(i = 0; i < length; i++) {
hudakz 0:092b5e724233 100 message_buff[i] = payload[i];
hudakz 0:092b5e724233 101 }
hudakz 0:092b5e724233 102
hudakz 0:092b5e724233 103 message_buff[i] = '\0';
hudakz 0:092b5e724233 104 pc.printf(" Payload: %s\r\n", message_buff);
hudakz 0:092b5e724233 105 }
hudakz 0:092b5e724233 106
hudakz 0:092b5e724233 107
hudakz 0:092b5e724233 108
hudakz 0:092b5e724233 109