#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <DHT.h>
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
#define DHTPIN 15
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
// Pines para los LEDs
const int tempLed1 = 26; // Temperatura < 25°C
const int tempLed2 = 25; // 25°C ≤ Temperatura ≤ 40°C
const int tempLed3 = 33; // Temperatura > 40°C
const int humLed1 = 14; // Humedad < 40%
const int humLed2 = 12; // 40% ≤ Humedad ≤ 70%
const int humLed3 = 13; // Humedad > 70%
void setup() {
Serial.begin(115200);
dht.begin();
// Inicializar la pantalla OLED
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
for(;;);
}
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 0);
display.println("DHT22 + OLED + ESP32");
display.display();
// Configurar pines de los LEDs como salidas
pinMode(tempLed1, OUTPUT);
pinMode(tempLed2, OUTPUT);
pinMode(tempLed3, OUTPUT);
pinMode(humLed1, OUTPUT);
pinMode(humLed2, OUTPUT);
pinMode(humLed3, OUTPUT);
}
void loop() {
// Leer temperatura y humedad
float temperatura = random(10,999)/10.00; // dht.readtemperature();
float humedad = random(10,999)/10.00; // dht.readHumidity();
// Control de LEDs según la temperatura
if(temperatura < 25)
{
digitalWrite(tempLed1, HIGH);
}
else
{
digitalWrite(tempLed1, LOW);
}
if(temperatura >= 25 && temperatura <= 40)
{
digitalWrite(tempLed2, HIGH);
}
else
{
digitalWrite(tempLed2, LOW);
}
if(temperatura > 40)
{
digitalWrite(tempLed3, HIGH);
}
else
{
digitalWrite(tempLed3, LOW);
}
// Mostrar información en la pantalla OLED
display.clearDisplay();
display.setCursor(0, 0);
display.print("Temp: ");
display.print(temperatura);
display.println(" C");
display.print("Humedad: ");
display.print(humedad);
display.println(" %");
display.display();
delay(2000); // Esperar 2 segundos antes de la próxima lectura
}