Skip to content

Commit 53decc6

Browse files
committed
Use Adafruit Unified Sensor Library
1 parent 6de7c91 commit 53decc6

File tree

2 files changed

+40
-27
lines changed

2 files changed

+40
-27
lines changed

platformio.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,4 @@ board = uno
1414
framework = arduino
1515
lib_extra_dirs = ~/Documents/Arduino/libraries
1616
monitor_speed = 9600
17-
serial_port = /dev/cu.usbserial-A50285BI
17+
lib_deps = adafruit/Adafruit Unified Sensor@^1.1.4

src/main.cpp

Lines changed: 39 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,17 @@
66
*/
77

88
#include <Arduino.h>
9-
#include <dht11.h>
9+
#include <DHT.h>
1010
#include <LiquidCrystal.h>
1111
#include <ArduinoJson.h> //https://github.com/bblanchon/ArduinoJson (use v6.xx)
1212
#include <SoftwareSerial.h>
1313

1414
#define DEBUG true
15-
#define DHT11PIN 4
1615

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);
1820

1921
// initialize the library with the numbers of the interface pins
2022
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);
@@ -24,18 +26,20 @@ LiquidCrystal lcd(7, 8, 9, 10, 11, 12);
2426
SoftwareSerial wifi(2, 3);
2527

2628
// declaring custom function to follow C++ validation rules
27-
String prepareDataForWiFi(float humidity, float temperature);
29+
// **************
2830
String sendDataToWiFi(String command, const int timeout, boolean debug);
2931
String sendDataToWiFi(String command, const int timeout, boolean debug);
32+
String prepareDataForWiFi(float humidity, float temperature, float headIndex);
33+
// **************
3034

31-
32-
String prepareDataForWiFi(float humidity, float temperature)
35+
String prepareDataForWiFi(float humidity, float temperature, float headIndex)
3336
{
3437

3538
StaticJsonDocument<200> doc;
3639

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);
3943

4044
char jsonBuffer[512];
4145
serializeJson(doc, jsonBuffer);
@@ -108,25 +112,34 @@ void loop() {
108112
}
109113

110114
// 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);
128142
}
129-
sendDataToWiFi(preparedData, 1000, DEBUG);
130143

131144
delay(2000); // take measurements every 2 sec
132145
}

0 commit comments

Comments
 (0)