31 lines
618 B
Arduino
31 lines
618 B
Arduino
|
// Incluimos librería
|
||
|
#include <DHT.h>
|
||
|
#define DHTPIN 4 //DHT21
|
||
|
#define DHTTYPE DHT21
|
||
|
DHT dht21(DHTPIN,DHTTYPE);
|
||
|
float temp, hum;
|
||
|
|
||
|
void setup() {
|
||
|
Serial.begin(9600);
|
||
|
dht21.begin();
|
||
|
}
|
||
|
|
||
|
void loop() {
|
||
|
|
||
|
temp = dht21.readTemperature();
|
||
|
hum = dht21.readHumidity();
|
||
|
// Comprobamos si ha habido algún error en la lectura
|
||
|
if (isnan(hum) || isnan(temp)) {
|
||
|
Serial.println("Error obteniendo los datos del sensor DHT11");
|
||
|
return;
|
||
|
}
|
||
|
Serial.print("Humedad: ");
|
||
|
Serial.print(hum);
|
||
|
Serial.println(" %");
|
||
|
Serial.print("Temperatura: ");
|
||
|
Serial.print(temp);
|
||
|
Serial.print(" *C ");
|
||
|
|
||
|
delay(2000);
|
||
|
}
|