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
main.cpp@2:f706efb3ea13, 2015-03-21 (annotated)
- Committer:
- hudakz
- Date:
- Sat Mar 21 23:47:23 2015 +0000
- Revision:
- 2:f706efb3ea13
- Parent:
- 1:4be688be4e0d
- Child:
- 3:de6cc3ff5aaa
MQ Telemetry Transport client publishing parameters measured by a DHT11 sensor.
; Ethernet connection is via an ENC28J60 module.
Who changed what in which revision?
User | Revision | Line number | New 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 | 0:092b5e724233 | 5 | #include <mbed.h> |
hudakz | 0:092b5e724233 | 6 | #include <UIPEthernet.h> |
hudakz | 0:092b5e724233 | 7 | #include <UIPClient.h> |
hudakz | 0:092b5e724233 | 8 | #include <MQTTClient.h> |
hudakz | 0:092b5e724233 | 9 | #include <DHT11.h> |
hudakz | 0:092b5e724233 | 10 | |
hudakz | 0:092b5e724233 | 11 | // UIPEthernet is the name of a global instance of UIPEthernetClass. |
hudakz | 0:092b5e724233 | 12 | // Do not change the name! It is used within the UIPEthernet library. |
hudakz | 0:092b5e724233 | 13 | // Adapt the SPI pin names to your mbed platform/board if not present yet. |
hudakz | 0:092b5e724233 | 14 | #if defined(TARGET_LPC1768) |
hudakz | 2:f706efb3ea13 | 15 | UIPEthernetClass UIPEthernet(p11, p12, p13, p8); // mosi, miso, sck, cs |
hudakz | 2:f706efb3ea13 | 16 | DHT11 dht11(p6); |
hudakz | 2:f706efb3ea13 | 17 | #elif defined(TARGET_NUCLEO_F103RB) |
hudakz | 2:f706efb3ea13 | 18 | UIPEthernetClass UIPEthernet(PB_5, PB_4, PB_3, PB_6); // mosi, miso, sck, cs |
hudakz | 2:f706efb3ea13 | 19 | DHT11 dht11(PC_14); |
hudakz | 0:092b5e724233 | 20 | #endif |
hudakz | 0:092b5e724233 | 21 | |
hudakz | 0:092b5e724233 | 22 | // Make sure that the MAC number is unique within the connected network. |
hudakz | 2:f706efb3ea13 | 23 | const uint8_t MY_MAC[6] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05 }; |
hudakz | 2:f706efb3ea13 | 24 | // This client is using a static IP address. Make sure DHCP is disabled on your router/modem. |
hudakz | 0:092b5e724233 | 25 | // IP address must be unique and compatible with your network too. Change as appropriate. |
hudakz | 2:f706efb3ea13 | 26 | const IPAddress MY_IP(192, 168, 1, 181); |
hudakz | 0:092b5e724233 | 27 | char message_buff[100]; |
hudakz | 0:092b5e724233 | 28 | void onMessage(char* topic, uint8_t* payload, unsigned int length); |
hudakz | 2:f706efb3ea13 | 29 | // In case your MQTT broker has different IP adress modify the following definition accordingly. |
hudakz | 2:f706efb3ea13 | 30 | IPAddress serverIP(192, 168, 1, 30); // MQTT broker (e.g. 'Mosquitto' running on a Raspberry Pi or Linux device) |
hudakz | 2:f706efb3ea13 | 31 | // The MQTT broker is like a post office distributing messages received from publishing clients to all subscribers (clients). |
hudakz | 2:f706efb3ea13 | 32 | // So the messages published by this client will be sent via the broker to all clients which subscribed to such messages. |
hudakz | 2:f706efb3ea13 | 33 | // This way also this client will receive all subscribed messages from other clients, but via the broker. |
hudakz | 0:092b5e724233 | 34 | EthernetClient ethernetClient; |
hudakz | 0:092b5e724233 | 35 | void onMqttMessage(char* topic, uint8_t* payload, unsigned int length); |
hudakz | 0:092b5e724233 | 36 | MQTTClient mqttClient(serverIP, 1883, onMqttMessage, ethernetClient); |
hudakz | 2:f706efb3ea13 | 37 | const int PERIOD = 10; // period for publishing the messages (in seconds) |
hudakz | 0:092b5e724233 | 38 | Serial pc(USBTX, USBRX); |
hudakz | 0:092b5e724233 | 39 | |
hudakz | 2:f706efb3ea13 | 40 | /** |
hudakz | 2:f706efb3ea13 | 41 | * @brief Main |
hudakz | 2:f706efb3ea13 | 42 | * @note |
hudakz | 2:f706efb3ea13 | 43 | * @param |
hudakz | 2:f706efb3ea13 | 44 | * @retval |
hudakz | 2:f706efb3ea13 | 45 | */ |
hudakz | 2:f706efb3ea13 | 46 | int main(void) { |
hudakz | 0:092b5e724233 | 47 | const int MAX_COUNT = 5; |
hudakz | 0:092b5e724233 | 48 | int i = 0; |
hudakz | 0:092b5e724233 | 49 | bool connected = false; |
hudakz | 0:092b5e724233 | 50 | time_t t = 0; |
hudakz | 0:092b5e724233 | 51 | time_t lastTime = 0; |
hudakz | 2:f706efb3ea13 | 52 | |
hudakz | 0:092b5e724233 | 53 | // initialize the ethernet device |
hudakz | 1:4be688be4e0d | 54 | UIPEthernet.begin(MY_MAC, MY_IP); |
hudakz | 2:f706efb3ea13 | 55 | pc.printf("Connecting to MQTT broker ..\r\n"); |
hudakz | 2:f706efb3ea13 | 56 | do |
hudakz | 2:f706efb3ea13 | 57 | { |
hudakz | 0:092b5e724233 | 58 | wait(1.0); |
hudakz | 0:092b5e724233 | 59 | connected = mqttClient.connect("myMqttClient"); |
hudakz | 0:092b5e724233 | 60 | } while(!connected && (i < MAX_COUNT)); |
hudakz | 0:092b5e724233 | 61 | |
hudakz | 0:092b5e724233 | 62 | if(connected) { |
hudakz | 2:f706efb3ea13 | 63 | pc.printf("MQTT broker connected.\r\n"); |
hudakz | 2:f706efb3ea13 | 64 | // The client can subscribe to as many MQTT messages as you like. |
hudakz | 2:f706efb3ea13 | 65 | mqttClient.subscribe("outdoor/gasmeter"); |
hudakz | 2:f706efb3ea13 | 66 | mqttClient.subscribe("livingroom/temperature"); |
hudakz | 2:f706efb3ea13 | 67 | mqttClient.subscribe("boiler/outlet/temperature"); |
hudakz | 2:f706efb3ea13 | 68 | } |
hudakz | 2:f706efb3ea13 | 69 | else { |
hudakz | 2:f706efb3ea13 | 70 | pc.printf("Failed to connect to MQTT broker.\r\n"); |
hudakz | 0:092b5e724233 | 71 | } |
hudakz | 0:092b5e724233 | 72 | |
hudakz | 0:092b5e724233 | 73 | while(1) { |
hudakz | 0:092b5e724233 | 74 | t = time(NULL); |
hudakz | 0:092b5e724233 | 75 | if(t >= (lastTime + PERIOD)) { |
hudakz | 0:092b5e724233 | 76 | lastTime = t; |
hudakz | 0:092b5e724233 | 77 | if(connected) { |
hudakz | 0:092b5e724233 | 78 | pc.printf("---------------------\r\n"); |
hudakz | 1:4be688be4e0d | 79 | pc.printf("%ds:\r\n", t); |
hudakz | 2:f706efb3ea13 | 80 | |
hudakz | 0:092b5e724233 | 81 | int state = dht11.readData(); |
hudakz | 0:092b5e724233 | 82 | if(state == DHT11::OK) { |
hudakz | 2:f706efb3ea13 | 83 | float hum = dht11.readHumidity(); |
hudakz | 0:092b5e724233 | 84 | sprintf(message_buff, "%4.1f", hum); |
hudakz | 0:092b5e724233 | 85 | pc.printf(" hum = %s%%\r\n", message_buff); |
hudakz | 0:092b5e724233 | 86 | mqttClient.publish("outdoor/humidity", message_buff); |
hudakz | 2:f706efb3ea13 | 87 | |
hudakz | 2:f706efb3ea13 | 88 | float temp = dht11.readTemperature(); |
hudakz | 0:092b5e724233 | 89 | sprintf(message_buff, "%5.1f", temp); |
hudakz | 0:092b5e724233 | 90 | pc.printf(" temp = %s'C\r\n", message_buff); |
hudakz | 0:092b5e724233 | 91 | mqttClient.publish("outdoor/temperature", message_buff); |
hudakz | 2:f706efb3ea13 | 92 | |
hudakz | 2:f706efb3ea13 | 93 | float dewPoint = temp - (100 - hum) / 5.0; |
hudakz | 0:092b5e724233 | 94 | sprintf(message_buff, "%5.1f", dewPoint); |
hudakz | 0:092b5e724233 | 95 | pc.printf(" dew point = %s'C\r\n", message_buff); |
hudakz | 0:092b5e724233 | 96 | mqttClient.publish("outdoor/dewpoint", message_buff); |
hudakz | 0:092b5e724233 | 97 | } |
hudakz | 0:092b5e724233 | 98 | else |
hudakz | 0:092b5e724233 | 99 | pc.printf(" DHT11 error: %d\r\n", state); |
hudakz | 0:092b5e724233 | 100 | } |
hudakz | 0:092b5e724233 | 101 | } |
hudakz | 2:f706efb3ea13 | 102 | |
hudakz | 0:092b5e724233 | 103 | mqttClient.loop(); // MQTT client loop processing |
hudakz | 0:092b5e724233 | 104 | } |
hudakz | 0:092b5e724233 | 105 | } |
hudakz | 0:092b5e724233 | 106 | |
hudakz | 2:f706efb3ea13 | 107 | /** |
hudakz | 2:f706efb3ea13 | 108 | * @brief Called on new MQTT message arrival |
hudakz | 2:f706efb3ea13 | 109 | * @note |
hudakz | 2:f706efb3ea13 | 110 | * @param topic: The topic of the new message |
hudakz | 2:f706efb3ea13 | 111 | * payload: The payload of the new message |
hudakz | 2:f706efb3ea13 | 112 | * length: Payload's length |
hudakz | 2:f706efb3ea13 | 113 | * @retval |
hudakz | 2:f706efb3ea13 | 114 | */ |
hudakz | 2:f706efb3ea13 | 115 | void onMqttMessage(char* topic, uint8_t* payload, unsigned int length) { |
hudakz | 0:092b5e724233 | 116 | int i = 0; |
hudakz | 0:092b5e724233 | 117 | |
hudakz | 0:092b5e724233 | 118 | pc.printf("Message arrived:\r\n"); |
hudakz | 0:092b5e724233 | 119 | pc.printf(" Topic: %s\r\n", topic); |
hudakz | 0:092b5e724233 | 120 | pc.printf(" Length: %d\r\n", length); |
hudakz | 0:092b5e724233 | 121 | |
hudakz | 0:092b5e724233 | 122 | // create character buffer with ending null terminator (string) |
hudakz | 0:092b5e724233 | 123 | for(i = 0; i < length; i++) { |
hudakz | 0:092b5e724233 | 124 | message_buff[i] = payload[i]; |
hudakz | 0:092b5e724233 | 125 | } |
hudakz | 0:092b5e724233 | 126 | |
hudakz | 0:092b5e724233 | 127 | message_buff[i] = '\0'; |
hudakz | 0:092b5e724233 | 128 | pc.printf(" Payload: %s\r\n", message_buff); |
hudakz | 0:092b5e724233 | 129 | } |