#include <Adafruit_GFX.h>
#include <Adafruit_ILI9341.h>
#include <DHT.h>
#define TFT_CS 10
#define TFT_DC 9
#define DHTPIN 2
#define DHTTYPE DHT22
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
DHT dht(DHTPIN, DHTTYPE);
const unsigned long DELAY_BETWEEN_READINGS_MS = 5000; // 10 segundos
void setup() {
tft.begin();
tft.setRotation(3); // Ajusta según necesites
tft.fillScreen(ILI9341_BLACK);
tft.setTextColor(ILI9341_WHITE);
tft.setTextSize(2);
dht.begin();
delay(2000); // Esperar a que el sensor se estabilice
}
void loop() {
float humidity = dht.readHumidity();
float temperatureC = dht.readTemperature();
float heatIndexC = dht.computeHeatIndex(temperatureC, humidity, false);
if (isnan(humidity) || isnan(temperatureC)) {
displayError();
} else {
displaySensorData(humidity, temperatureC, heatIndexC);
}
delay(DELAY_BETWEEN_READINGS_MS);
}
void displaySensorData(float humidity, float tempC, float heatIndexC) {
tft.fillScreen(ILI9341_BLACK);
tft.setCursor(0, 0);
tft.println("Sensor DHT22:");
tft.println();
tft.print("Humedad: ");
tft.print(humidity);
tft.println(" %");
tft.print("Temperatura: ");
tft.print(tempC);
tft.println(" C");
tft.print("Indice de calor: ");
tft.print(heatIndexC);
tft.println(" C");
}
void displayError() {
tft.fillScreen(ILI9341_BLACK);
tft.setCursor(0, 0);
tft.println("Error leyendo");
tft.println("el sensor DHT22");
}