#include "DHTesp.h"
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define BUTTON_PIN 18 // Pin para el botón
#define LED1_PIN 19 // Pin para el primer LED
#define LED2_PIN 16 // Pin para el segundo LED
#define DHT_PIN 23 // Pin para el sensor DHT22
#define TEXT_SIZE_OLED 2
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
DHTesp dhtSensor;
void setup() {
Serial.begin(115200);
// Configuración de pines
pinMode(LED1_PIN, OUTPUT);
pinMode(LED2_PIN, OUTPUT);
pinMode(BUTTON_PIN, INPUT_PULLUP);
// Inicialización de LEDs apagados
apagarLEDs();
// Init oled
initOled();
// Configuración del sensor DHT
dhtSensor.setup(DHT_PIN, DHTesp::DHT22);
Serial.println("Setup complete.");
writeOnOled("Setup complete :D", WHITE, TEXT_SIZE_OLED);
}
void loop() {
// Leer el estado del botón
int buttonState = digitalRead(BUTTON_PIN);
// Serial.print("Estado del botón: ");
// Serial.println(buttonState);
if (buttonState == LOW) { // Cuando el botón esté presionado
encenderLEDsSecuencia();
} else {
apagarLEDs();
}
delay(100); // Pequeño retraso para evitar rebotes en el botón
}
void encenderLEDsSecuencia() {
digitalWrite(LED1_PIN, HIGH); // Encender LED1
Serial.println("LED1_PIN ON!");
delay(500);
digitalWrite(LED2_PIN, HIGH); // Encender LED2
Serial.println("LED2_PIN ON!");
delay(500);
if (leerTemperatura()) { // Leer y mostrar la temperatura solo si los datos son válidos
delay(1000); // Retraso adicional para permitir lectura
}
digitalWrite(LED2_PIN, LOW); // Apagar LED2
delay(500);
digitalWrite(LED1_PIN, LOW); // Apagar LED1
delay(500);
}
void apagarLEDs() {
digitalWrite(LED1_PIN, LOW);
digitalWrite(LED2_PIN, LOW);
}
// Función para leer la temperatura y humedad
bool leerTemperatura() {
TempAndHumidity data = dhtSensor.getTempAndHumidity();
// Verificar si la lectura fue exitosa
if (isnan(data.temperature) || isnan(data.humidity)) {
Serial.println("Error al leer el sensor DHT!!");
return false; // Indica que hubo un error en la lectura
}
// Mostrar los valores de temperatura y humedad
Serial.println("Lectura del DHT:");
Serial.println("Temp: " + String(data.temperature, 2) + "°C");
Serial.println("Humedad: " + String(data.humidity, 1) + "%");
Serial.println("------------------------");
writeOnOled("Temp: " + String(data.temperature, 2) + " Hum: " + String(data.humidity, 1) + "%", WHITE, 1);
return true; // Indica que la lectura fue exitosa
}
void initOled(){
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3D for 128x64
Serial.println(F("SSD1306 allocation failed"));
for(;;);
}
delay(2000);
display.clearDisplay();
}
void writeOnOled(String text, int color, int textSize) {
// Validate text size
if (textSize < 1 || textSize > 8) {
Serial.println(F("Invalid text size. Must be between 1 and 8."));
return;
}
// Set text size and color
display.setTextSize(textSize);
display.setTextColor(color);
// Set cursor position to the top-left corner
display.setCursor(0, 0);
// Print text on the display
display.clearDisplay();
display.print(text);
display.display();
}