#include <DHT.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// Configuracion DHT11
#define DHTPIN 23 //pin del sensor DHT11
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
// set the LCD address to 0x27 for a 16 chars and 2 line display
//LiquidCrystal_I2C lcd(0x3F,16,2);
LiquidCrystal_I2C lcd = LiquidCrystal_I2C(0x27,16,2);
// for motion-sensor
int inputPin = 4;
int inputPinb = 16;
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
pinMode(inputPin, INPUT);
pinMode(inputPinb, INPUT);
Serial.println("Hello, ESP32!");
// initialize the lcd
lcd.init();
// Print a message to the LCD.
lcd.backlight();
lcd.setCursor(3,0);
lcd.print("Iniciando...");
lcd.setCursor(2,1);
lcd.print("Temp y Hum");
delay(5000);
lcd.clear();
lcd.noBacklight();
}
void loop() {
// put your main code here, to run repeatedly:
delay(10); // this speeds up the simulation
// put your main code here, to run repeatedly:
float temperatura = dht.readTemperature();
float humedad = dht.readHumidity();
// validar lecturas
if (isnan(temperatura) || isnan(humedad)) {
Serial.println("Error leyendo el sensor DHT22");
return;
}
// Publicar temperatura
char tempStr[8];
dtostrf(temperatura, 6, 2, tempStr); //Convertir de float a streng
Serial.println("Temperatura publicada: ");
Serial.println(tempStr);
lcd.setCursor(0,0);
lcd.print("Temp: ");
lcd.print(tempStr);
// Publicar humedad
char humStr[8];
dtostrf(humedad, 6, 2, humStr); //Convertir de float a streng
Serial.println("Humedad publicada: ");
Serial.println(humStr);
lcd.setCursor(0,1);
lcd.print("Hum: ");
lcd.print(humStr);
int val = digitalRead(inputPin);
if (val == HIGH) {
Serial.println("Motion detected!");
lcd.backlight();
}
else {
Serial.println("No Motion detected!");
lcd.noBacklight();
}
int valb = digitalRead(inputPinb);
if (valb == HIGH) {
Serial.println("Yes Button Press");
lcd.backlight();
}
else {
Serial.println("No Button Press");
lcd.noBacklight();
}
// esperar antes de enviar nuevamente
delay(5000);
}