6
6
*/
7
7
8
8
#include < Arduino.h>
9
- #include < dht11 .h>
9
+ #include < DHT .h>
10
10
#include < LiquidCrystal.h>
11
11
#include < ArduinoJson.h> // https://github.com/bblanchon/ArduinoJson (use v6.xx)
12
12
#include < SoftwareSerial.h>
13
13
14
14
#define DEBUG true
15
- #define DHT11PIN 4
16
15
17
- dht11 sensor;
16
+ #define DHT_PIN 4 // pin connected to data pin of DHT11
17
+ #define DHT_TYPE DHT11 // Type of the DHT Sensor, DHT11/DHT22
18
+
19
+ DHT sensor (DHT_PIN, DHT_TYPE);
18
20
19
21
// initialize the library with the numbers of the interface pins
20
22
LiquidCrystal lcd (7 , 8 , 9 , 10 , 11 , 12 );
@@ -24,18 +26,20 @@ LiquidCrystal lcd(7, 8, 9, 10, 11, 12);
24
26
SoftwareSerial wifi (2 , 3 );
25
27
26
28
// declaring custom function to follow C++ validation rules
27
- String prepareDataForWiFi ( float humidity, float temperature);
29
+ // **************
28
30
String sendDataToWiFi (String command, const int timeout, boolean debug);
29
31
String sendDataToWiFi (String command, const int timeout, boolean debug);
32
+ String prepareDataForWiFi (float humidity, float temperature, float headIndex);
33
+ // **************
30
34
31
-
32
- String prepareDataForWiFi (float humidity, float temperature)
35
+ String prepareDataForWiFi (float humidity, float temperature, float headIndex)
33
36
{
34
37
35
38
StaticJsonDocument<200 > doc;
36
39
37
- doc[" humidity" ] = (String)humidity;
38
- doc[" temperature" ] = (String)temperature;
40
+ doc[" humidity" ] = String (humidity);
41
+ doc[" temperature" ] = String (temperature);
42
+ doc[" head_index" ] = String (headIndex);
39
43
40
44
char jsonBuffer[512 ];
41
45
serializeJson (doc, jsonBuffer);
@@ -108,25 +112,34 @@ void loop() {
108
112
}
109
113
110
114
// read from the digital pin
111
- int check = sensor.read (DHT11PIN);
112
- float temperature = (float )sensor.temperature ;
113
- float humidity = (float )sensor.humidity ;
114
-
115
- // display Humidity on the LCD screen
116
- lcd.setCursor (0 , 0 );
117
- lcd.print (" Humidity (%): " );
118
- lcd.print (humidity, 2 );
119
-
120
- // display Temperature on the LCD screen
121
- lcd.setCursor (0 , 1 );
122
- lcd.print (" Temp (C): " );
123
- lcd.print (temperature, 2 );
124
-
125
- String preparedData = prepareDataForWiFi (humidity, temperature);
126
- if (DEBUG == true ) {
127
- Serial.println (preparedData);
115
+ sensor.begin ();
116
+
117
+ float temperature = sensor.readTemperature (); // return temperature in °C
118
+ float humidity = sensor.readHumidity (); // return humidity in %
119
+
120
+ // Compute heat index in Celsius (isFahrenheit = false)
121
+ float heatIndex = sensor.computeHeatIndex (temperature, humidity, false );
122
+
123
+ // check whether reading was successful or not
124
+ if (temperature == NAN || humidity == NAN) { // NAN means no available data
125
+ lcd.print (" Reading failed." );
126
+ } else {
127
+ // display Humidity on the LCD screen
128
+ lcd.setCursor (0 , 0 );
129
+ lcd.print (" Humidity (%): " );
130
+ lcd.print (String (humidity));
131
+
132
+ // display Temperature on the LCD screen
133
+ lcd.setCursor (0 , 1 );
134
+ lcd.print (" Temp (C): " );
135
+ lcd.print (String (temperature));
136
+
137
+ String preparedData = prepareDataForWiFi (humidity, temperature, heatIndex);
138
+ if (DEBUG == true ) {
139
+ Serial.println (preparedData);
140
+ }
141
+ sendDataToWiFi (preparedData, 1000 , DEBUG);
128
142
}
129
- sendDataToWiFi (preparedData, 1000 , DEBUG);
130
143
131
144
delay (2000 ); // take measurements every 2 sec
132
145
}
0 commit comments