#include <WiFi.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <NTPClient.h>
#include <WiFiUdp.h>
#include <TimeLib.h> // Para obtener la fecha
LiquidCrystal_I2C LCD = LiquidCrystal_I2C(0x27, 20, 4);
#define NTP_SERVER "pool.ntp.org"
#define UTC_OFFSET -6 // Desplazamiento UTC para CST (horario estándar)
#define UTC_OFFSET_DST -5 // Desplazamiento UTC para CDT (horario de verano)
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, NTP_SERVER, UTC_OFFSET * 3600);
void spinner() {
static int8_t counter = 0;
const char* glyphs = "\xa1\xa5\xdb";
LCD.setCursor(15, 1);
LCD.print(glyphs[counter++]);
if (counter == strlen(glyphs)) {
counter = 0;
}
}
void setup() {
Serial.begin(115200);
LCD.init();
LCD.backlight();
LCD.setCursor(0, 0);
LCD.print("Conectando a ");
LCD.setCursor(0, 1);
LCD.print("WiFi ");
WiFi.begin("Wokwi-GUEST", "", 6);
while (WiFi.status() != WL_CONNECTED) {
delay(250);
spinner();
}
Serial.println("");
Serial.println("WiFi conectado");
Serial.print("Dirección IP: ");
Serial.println(WiFi.localIP());
LCD.clear();
LCD.setCursor(0, 0);
LCD.println("En linea");
LCD.setCursor(0, 1);
LCD.println("Obteniendo hora...");
timeClient.begin();
configTime(UTC_OFFSET * 3600, UTC_OFFSET_DST * 3600, "pool.ntp.org");
}
void loop() {
timeClient.update();
// Obtener la hora actual en formato HH:MM:SS
String formattedTime = timeClient.getFormattedTime();
// Obtener la fecha actual en formato DD/MM/YYYY
struct tm timeinfo;
if(!getLocalTime(&timeinfo)){
Serial.println("Error al obtener la hora local");
return;
}
char formattedDate[20];
strftime(formattedDate, sizeof(formattedDate), "%d/%m/%Y", &timeinfo);
LCD.setCursor(0, 0);
LCD.print("Hora: " + formattedTime);
LCD.setCursor(0, 1);
LCD.print("Fecha: ");
LCD.print(formattedDate);
delay(1000); // Actualizar cada segundo
}