diff options
| author | steckbrief <steckbrief@chefmail.de> | 2017-06-23 00:18:31 +0200 |
|---|---|---|
| committer | steckbrief <steckbrief@chefmail.de> | 2017-06-23 00:18:31 +0200 |
| commit | 3df152f8ec9ced7a81bb3039d713176b9923a659 (patch) | |
| tree | a0a4963f6b9f6c44b452c6dbb12d8d6ea56954c4 /devices/DHT22_DHT11_Parallel_auslesen | |
Initialization of repository
Diffstat (limited to 'devices/DHT22_DHT11_Parallel_auslesen')
| -rw-r--r-- | devices/DHT22_DHT11_Parallel_auslesen/DHT22_DHT11_Parallel_auslesen.ino | 67 |
1 files changed, 67 insertions, 0 deletions
diff --git a/devices/DHT22_DHT11_Parallel_auslesen/DHT22_DHT11_Parallel_auslesen.ino b/devices/DHT22_DHT11_Parallel_auslesen/DHT22_DHT11_Parallel_auslesen.ino new file mode 100644 index 0000000..1225a77 --- /dev/null +++ b/devices/DHT22_DHT11_Parallel_auslesen/DHT22_DHT11_Parallel_auslesen.ino @@ -0,0 +1,67 @@ +// Including the DHT library +#include "DHT.h" + +const int DATA_PIN_DHT22 = D4; +const int DATA_PIN_DHT11 = D2; + +// Initialize DHT sensor. +DHT dht22(DATA_PIN_DHT22, DHT22); +DHT dht11(DATA_PIN_DHT11, DHT11); + +float readTemperature(DHT dht) { + float t = dht.readTemperature(); + if (isnan(t)) { + Serial.println("Failed to read temperature from DHT sensor"); + } + return t; +} + +float readHumidity(DHT dht) { + float h = dht.readHumidity(); + if (isnan(h)) { + Serial.println("Failed to read humidity from DHT sensor"); + } + return h; +} + +void printValues(String sensorType, float humidity, float temperature) { + Serial.print("Sensor: "); + Serial.println(sensorType); + Serial.print("Humidity: "); + Serial.print(humidity); + Serial.println("%"); + + Serial.print("Temperature: "); + Serial.print(temperature); + Serial.println("°C"); +} + +void readAndPrintValues(char* sensorType) { + DHT dht(0,0); + if ("DHT11" == sensorType) { + dht = dht11; + } else if ("DHT22" == sensorType) { + dht = dht22; + } + float temperature = readTemperature(dht); + float humidity = readHumidity(dht); + printValues(sensorType, humidity, temperature); +} + +void setup() { + Serial.begin(115200); + Serial.println("Read humidity and temperature from DHT11 and DHT22"); + dht22.begin(); + delay(1000); + dht11.begin(); + + delay(10000); +} + +void loop() { + readAndPrintValues("DHT11"); + delay(1000); + readAndPrintValues("DHT22"); + delay(5000); +} + |
