MQ Telemetry Transport client publishing parameters measured by a DHT11 sensor. Ethernet connection is via an ENC28J60 module.
Dependencies: DHT11 MQTTClient UIPEthernet mbed
main.cpp
00001 // In this project an MQTT client is created. 00002 // It is publishing parameters measured by a DHT11 sensor. 00003 // Ethernet connection is assured via an ENC28J60 module. 00004 00005 #include "mbed.h" 00006 #include "UIPEthernet.h" 00007 #include "MQTTClient.h" 00008 #include "DHT11.h" 00009 00010 Serial pc(USBTX, USBRX); 00011 00012 // MAC address must be unique within the connected network. Modify as appropriate. 00013 const uint8_t MY_MAC[6] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05 }; 00014 00015 #define DHCP 1 // comment out this line if you'd like to use static IP address 00016 00017 #if !defined(DHCP) 00018 // In case you'd like to use static IP address: 00019 // IP address must be unique and compatible with your network. 00020 // Change as appropriate. 00021 const IPAddress MY_IP(192, 168, 1, 181); 00022 #endif 00023 00024 // uIPEthernet is the name of a global instance of UIPEthernet. 00025 // Do not change the name! It is used within the UIPEthernet library. 00026 // Adapt the SPI pin names to your mbed platform/board if not present yet. 00027 00028 #if defined(TARGET_LPC1768) 00029 UIPEthernet uIPEthernet(p11, p12, p13, p8); // mosi, miso, sck, cs 00030 DHT11 dht11(p6); 00031 #elif defined(TARGET_NUCLEO_F103RB) || defined(TARGET_NUCLEO_L152RE) || defined(TARGET_NUCLEO_F030R8) \ 00032 || defined(TARGET_NUCLEO_F401RE) || defined(TARGET_NUCLEO_F302R8) || defined(TARGET_NUCLEO_L053R8) \ 00033 || defined(TARGET_NUCLEO_F411RE) || defined(TARGET_NUCLEO_F334R8) || defined(TARGET_NUCLEO_F072RB) \ 00034 || defined(TARGET_NUCLEO_F091RC) || defined(TARGET_NUCLEO_F303RE) || defined(TARGET_NUCLEO_F070RB) \ 00035 || defined(TARGET_KL25Z ) || defined(TARGET_KL46Z) || defined(TARGET_K64F) || defined(TARGET_KL05Z) \ 00036 || defined(TARGET_K20D50M) || defined(TARGET_K22F) \ 00037 || defined(TARGET_NRF51822) \ 00038 || defined(TARGET_RZ_A1H) 00039 UIPEthernet uIPEthernet(D11, D12, D13, D10); // mosi, miso, sck, cs 00040 DHT11 dht11(PC_14); 00041 #endif 00042 00043 char message_buff[100]; 00044 void onMessage(char* topic, uint8_t* payload, unsigned int length); 00045 00046 // MQTT broker is like a post office. 00047 // Its task is to distribute messages published by clients to all subscribers (other clients). 00048 // So messages published by this client will be sent to the broker. 00049 // Then the broker will send them to all clients which subscribed to such topics. 00050 // Also this client will receive all messages with topics it subscribed to. 00051 // 'Mosquitto' is a free implementation of MQTT broker for Linux machines (e.g. Raspberry Pi, Ubuntu etc.) 00052 IPAddress serverIP(192, 168, 1, 30); // IP address of your MQTT broker (adapt) 00053 EthernetClient ethernetClient; 00054 void onMqttMessage(char* topic, uint8_t* payload, unsigned int length); 00055 MQTTClient mqttClient(serverIP, 1883, onMqttMessage, ethernetClient); 00056 const int INTERVAL = 10; // Interval for publishing messages (in seconds) 00057 00058 00059 /** 00060 * @brief Main 00061 * @note 00062 * @param 00063 * @retval 00064 */ 00065 int main(void) { 00066 const int MAX_COUNT = 5; 00067 int i = 0; 00068 bool connected = false; 00069 time_t t = 0; 00070 time_t lastTime = 0; 00071 00072 // initialize the ethernet device 00073 00074 #if defined(DHCP) 00075 pc.printf("Searching for DHCP server..\r\n"); 00076 00077 if (uIPEthernet.begin(MY_MAC) != 1) { 00078 pc.printf("No DHCP server found.\r\n"); 00079 pc.printf("Exiting application.\r\n"); 00080 return 0; 00081 } 00082 pc.printf("DHCP server found and configuration info received.\r\n"); 00083 #else 00084 uIPEthernet.begin(MY_MAC, MY_IP); 00085 #endif 00086 pc.printf("Local IP = %s\r\n", uIPEthernet.localIP().toString()); 00087 pc.printf("Connecting to MQTT broker ..\r\n"); 00088 do { 00089 wait(1.0); 00090 connected = mqttClient.connect("myMqttClient"); 00091 } while(!connected && (i < MAX_COUNT)); 00092 00093 if (connected) { 00094 pc.printf("MQTT broker connected.\r\n"); 00095 // The client can subscribe to as many MQTT messages as you like. 00096 mqttClient.subscribe("example/hello"); 00097 mqttClient.subscribe("boiler/outlet/temperature"); 00098 pc.printf("Subscribed to:\r\n"); 00099 pc.printf(" example/hello\r\n"); 00100 pc.printf(" boiler/outlet/temperature\r\n"); 00101 } 00102 else { 00103 pc.printf("Failed to connect to MQTT broker.\r\n"); 00104 } 00105 00106 while (1) { 00107 t = time(NULL); 00108 if(t >= (lastTime + INTERVAL)) { 00109 lastTime = t; 00110 if(connected) { 00111 pc.printf("---------------------\r\n"); 00112 pc.printf("%ds:\r\n", t); 00113 00114 int state = dht11.readData(); 00115 if(state == DHT11::OK) { 00116 float hum = dht11.readHumidity(); 00117 sprintf(message_buff, "%4.1f", hum); 00118 pc.printf(" hum = %s%%\r\n", message_buff); 00119 mqttClient.publish("outdoor/humidity", message_buff); 00120 00121 float temp = dht11.readTemperature(); 00122 sprintf(message_buff, "%5.1f", temp); 00123 pc.printf(" temp = %s'C\r\n", message_buff); 00124 mqttClient.publish("outdoor/temperature", message_buff); 00125 00126 float dewPoint = temp - (100 - hum) / 5.0; 00127 sprintf(message_buff, "%5.1f", dewPoint); 00128 pc.printf(" dew point = %s'C\r\n", message_buff); 00129 mqttClient.publish("outdoor/dewpoint", message_buff); 00130 } 00131 else 00132 pc.printf(" DHT11 error: %d\r\n", state); 00133 } 00134 } 00135 00136 mqttClient.loop(); // MQTT client loop processing 00137 } 00138 } 00139 00140 /** 00141 * @brief Called on new MQTT message arrival 00142 * @note 00143 * @param topic: The topic of the new message 00144 * payload: The payload of the new message 00145 * length: Payload's length 00146 * @retval 00147 */ 00148 void onMqttMessage(char* topic, uint8_t* payload, unsigned int length) { 00149 int i = 0; 00150 00151 pc.printf("Message arrived:\r\n"); 00152 pc.printf(" Topic: %s\r\n", topic); 00153 pc.printf(" Length: %d\r\n", length); 00154 00155 // create character buffer with ending null terminator (string) 00156 for (i = 0; i < length; i++) { 00157 message_buff[i] = payload[i]; 00158 } 00159 00160 message_buff[i] = '\0'; 00161 pc.printf(" Payload: %s\r\n", message_buff); 00162 } 00163
Generated on Tue Jul 19 2022 03:58:08 by 1.7.2