/*
Wokwi | General
JOSENOPEPE — Today at 7:22 PM
plz help Project Link: https://wokwi.com/projects/406512814133403649?gh=1 what do i do
Filename : Temperature Humidity and Light Meter
Description : LCD displays the value of temperature, humidity, and light level.
Author : AnonEngineering / josepepe
*/
#include <DHTesp.h>
#include <LiquidCrystal_I2C.h>
// Definiciones de pines
#define DHTPIN 33 // Pin donde se conecta el DHT22
#define LDRPIN 32 // Pin analógico donde se conecta el fotorresistor (LDR)
// These constants should match the photoresistor's "gamma" and "rl10" attributes
const float GAMMA = 0.7;
const float RL10 = 33;
DHTesp dht; // Crear objeto para manejar el DHT22
LiquidCrystal_I2C LCD = LiquidCrystal_I2C(0x27, 16, 2);
void setup() {
LCD.init();
LCD.backlight();
LCD.setCursor(0, 0);
dht.setup(DHTPIN, DHTesp::DHT22); // Inicializar DHT22 en el pin DHTPIN
}
void loop() {
char tempfVal[8];
char humifVal[8];
char luxfVal[16];
char dispBuff[16];
// Leer temperatura y humedad del DHT22
float temperature = dht.getTemperature(); // Leer temperatura
float humidity = dht.getHumidity(); // Leer humedad
// dtostrf(float_value, min_width, num_digits_after_decimal, where_to_store_string)
dtostrf(temperature, 3, 1, tempfVal);
dtostrf(humidity, 3, 1, humifVal);
// Mostrar temperatura en el LCD
LCD.setCursor(2, 0); // Establecer posición de visualización
LCD.print("Temperature:"); // Configurar la visualización
snprintf(dispBuff, 16, "%s%cC", tempfVal, char(223));
LCD.setCursor(5, 1);
LCD.print(dispBuff); // Mostrar temperatura con 1 decimal
delay(3000);
LCD.clear();
// Mostrar humedad en el LCD
LCD.setCursor(3, 0);
LCD.print("Humidity:");
snprintf(dispBuff, 16, "%s%%", humifVal);
LCD.setCursor(5, 1);
LCD.print(dispBuff); // Mostrar humedad con 1 decimal
delay(3000);
LCD.clear();
// Leer valor del fotorresistor
int ldrValue = analogRead(LDRPIN);
// Convert the analog value into lux value:
float voltage = ldrValue / 4096. * 3.3;
float resistance = 2000 * voltage / (1 - voltage / 3.3);
float lux = pow(RL10 * 1e3 * pow(10, GAMMA) / resistance, (1 / GAMMA));
dtostrf(lux, 8, 1, luxfVal);
// Mostrar valor de la luz (fotorresistor)
LCD.setCursor(5, 0);
LCD.print("Light:");
snprintf(dispBuff, 16, "%s lux", luxfVal);
LCD.setCursor(2, 1);
LCD.print(dispBuff);
//LCD.print(ldrValue);
delay(3000);
LCD.clear();
}