//*******************************************************************
/*
* Filename : Temperature Humidity and Light Meter
* Description : LCD displays the value of temperature, humidity, and light level.
* Author : josepepe
*/
#include <Wire.h>
#include <stdlib.h>
#include "DHTesp.h"
#include <LiquidCrystal_I2C.h>
// Definiciones de pines
#define DHTPIN 15 // Pin donde se conecta el DHT22
#define LDRPIN A0 // Pin analógico donde se conecta el fotorresistor (LDR)
DHTesp dht; // Crear objeto para manejar el DHT22
LiquidCrystal_I2C LCD = LiquidCrystal_I2C(0x27, 16, 2);
// I2C_LCD12864 lcd(&Wire); // Crear LCD128*64 I2C, SDA -> 21, SCL -> 22
char buff[20];
const char* glyphs = "\xa1\xa5\xdb";
float temperature;
float humidity;
///////////////////////////////////////////////////////////////////////
void setup() {
Serial.begin(115200);
Wire.begin();
LCD.init();
LCD.backlight();
LCD.setCursor(0, 0);
dht.setup(DHTPIN, DHTesp::DHT22); // Inicializar DHT22 en el pin DHTPIN
}
///////////////////////////////////////////////////////////////////////
void loop() {
static int8_t counter = 0;
// Leer temperatura y humedad del DHT22
TempAndHumidity data = dht.getTempAndHumidity();
// Serial.println(data.temperature);
// Serial.println(data.humidity);
// LCD.clear();
LCD.setCursor(0, 0);
LCD.print("Temp: ");
LCD.setCursor(6, 0);
dtostrf(data.temperature, 5, 2, buff);
LCD.print(buff);
// LCD.print(data.temperature);
// LCD.setCursor(11, 0);
// LCD.print("\xb0");
// LCD.print(chr(176));
// LCD.print('{t} \xb0');
LCD.setCursor(12, 0);
// LCD.print("°C");
LCD.print("C");
LCD.setCursor(15, 0);
LCD.print(glyphs[counter++]);
if (counter == strlen(glyphs)) {
counter = 0;
}
LCD.setCursor(0, 1);
LCD.print("Humd: ");
if (data.humidity == 100.0)
LCD.setCursor(5, 1);
else
LCD.setCursor(6, 1);
dtostrf(data.humidity, 5, 2, buff);
// LCD.print(data.humidity);
LCD.print(buff);
LCD.setCursor(12, 1);
LCD.print("%");
delay(1000); // Esperar 2 segundos antes de la siguiente lectura
}
//*******************************************************************