#include "RTClib.h"
RTC_DS1307 rtc;
char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
#include <LiquidCrystal_I2C.h>
#define I2C_ADDR 0x27
#define LCD_COLUMNS 16
#define LCD_LINES 2
LiquidCrystal_I2C lcd(I2C_ADDR, LCD_COLUMNS, LCD_LINES);
#include "DHTesp.h"
const int DHT_PIN = 12;
DHTesp dhtSensor;
#define S0 14
#define S1 27
void setup () {
Serial.begin(115200);
// Init
lcd.init();
lcd.backlight();
// Print something
lcd.setCursor(3, 0);
lcd.print("Hello, world!");
lcd.setCursor(2, 1);
lcd.print("Wokwi Online IoT");
lcd.setCursor(5, 2);
lcd.print("Simulator");
lcd.setCursor(7, 3);
lcd.print("Enjoy!");
lcd.clear();
if (! rtc.begin()) {
Serial.println("Couldn't find RTC");
Serial.flush();
abort();
}
dhtSensor.setup(DHT_PIN, DHTesp::DHT22);
pinMode(S0,INPUT);
pinMode(S1,INPUT);
}
void loop () {
int SW0 = digitalRead(S0);
int SW1 = digitalRead(S1);
if ((SW0 and SW1)==0)
tiempo();
if ((SW0 ==0 and SW1==1))
temperatura();
if ((SW0==1 and SW1==0))
humedad();
}
void tiempo() {
DateTime now = rtc.now();
Serial.print("Current time: ");
Serial.print(now.year(), DEC);
Serial.print('/');
int year=now.year();
lcd.setCursor(3,0);
lcd.print(year);
lcd.print("/");
Serial.print(now.month(), DEC);
Serial.print('/');
int month=now.month();
lcd.print(month);
lcd.print('/');
Serial.print(now.day(), DEC);
int day=now.day();
lcd.print(day);
Serial.print(" (");
Serial.print(daysOfTheWeek[now.dayOfTheWeek()]);
Serial.print(") ");
Serial.print(now.hour(), DEC);
Serial.print(':');
lcd.setCursor(4,1);
int hour=now.hour();
lcd.print(hour);
lcd.print(':');
Serial.print(now.minute(), DEC);
int minute=now.minute();
lcd.print(minute);
lcd.print(':');
Serial.print(':');
Serial.print(now.second(), DEC);
Serial.println();
Serial.println();
int second=now.second();
lcd.print(second);
delay(1000);
}
void humedad () {
TempAndHumidity data = dhtSensor.getTempAndHumidity();
float humedad = data.humidity;
Serial.println("Humidity: " + String(humedad) + "%");
Serial.println("---");
lcd.clear();
lcd.setCursor(5,0);
lcd.print("Humedad");
lcd.setCursor(6,1);
lcd.print(humedad);
delay(2000); // Wait for a new reading from the sensor (DHT22 has ~0.5Hz sample rate)
}
void temperatura () {
TempAndHumidity data = dhtSensor.getTempAndHumidity();
float temperatura = data.temperature;
Serial.println("Temp: " + String(temperatura) + "°C");
Serial.println("---");
lcd.clear();
lcd.setCursor(3,0);
lcd.print("Temperatura");
lcd.setCursor(6,1);
lcd.print(temperatura);
delay(2000); // Wait for a new reading from the sensor (DHT22 has ~0.5Hz sample rate)
}