#include <SimpleDHT.h>
#define SENSOR 7 // Pin de datos del DHT22
SimpleDHT22 dht22; // Se instancia el sensor (sin parámetro, el pin se pasa en la función read)
void setup() {
pinMode(SENSOR,INPUT);
Serial.begin(9600);
Serial.println("SENSOR ACTIVO");
}
void loop() {
Serial.println("Leyendo sensor...");
byte temperature = 0;
byte humidity = 0;
byte data[40] = {0}; // Buffer de datos
// Lectura del sensor con control de error
int err = dht22.read(SENSOR, &temperature, &humidity, data);
if (err != SimpleDHTErrSuccess) {
Serial.print("Ha habido un error de lectura, código de error: ");
Serial.println(err);
} else {
Serial.println(".... Sensor Leído correctamente");
Serial.print("Temperatura: ");
Serial.print((int)temperature);
Serial.println(" ºC");
Serial.print("Humedad: ");
Serial.print((int)humidity);
Serial.println(" %");
Serial.println("###################");
}
delay(600); // Espera de 0.6 s (data-sheet)
}