Reloj analógico con el Wemos D1 Mini
FunduinoPublicado el
Die Uhrzeit per NTP aus dem Internet holen
Eso necesitas
Pantalla OLED de 0,96 pulgadas - 128x64 píxeles, I2C, SSD1306
4,19 € *N.º de art. FUN-1010561Al producto — Pantalla OLED de 0,96 pulgadas - 128x64 píxeles, I2C, SSD1306
WeMos D1 Mini ESP8266 - placa de desarrollo con Wi-Fi, 3,3 V
5,95 € *N.º de art. F23106678Al producto — WeMos D1 Mini ESP8266 - placa de desarrollo con Wi-Fi, 3,3 V
Todas las 2 piezas en total 10,14 € *Listo para enviar de inmediato · Plazo de entrega: 1-3 días laborables
* Todos los precios incluyen el IVA legal, más gastos de envío.
Wemos D1 Mini y NTP
Qué se debe mostrar
Instalar la placa
1 / 4
Instalar las bibliotecas
1 / 2
El esquema eléctrico
El programa
Mostrar la fecha y la hora mediante NTP en la pantalla OLED
// Incluir bibliotecas
# include <ESP8266WiFi.h>
# include <NTP.h>
# include <WiFiUdp.h>
# include <NTP.h>
# include <U8g2lib.h>
/*
Tipo de pantalla y tamaño en píxeles
1 = modo de búfer de página
I2C por hardware
Nombre del OLED
Rotación R0 (ninguna)
*/
U8G2_SSD1306_128X64_NONAME_1_HW_I2C oled(U8G2_R0, U8X8_PIN_NONE);
// SSID y contraseña del router
char Router[] = "SSID_des_Routers";
char Passwort[] = "xxxxxxx";
// Iniciar NTP
WiFiUDP wifiUdp;
NTP ntp(wifiUdp);
// Tiempo hasta la siguiente medición
static unsigned long GesicherteStartZeit = 0;
unsigned long Startzeit;
// Intervalo de 30 segundos, se puede ajustar
int Intervall = 30000;
void setup()
{
Serial.begin(9600);
delay(500);
// Iniciar WiFi con DHCP
WiFi.begin(Router, Passwort);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
}
// Mostrar el SSID del router en el monitor serie
Serial.print("Conectado a ");
Serial.println(WiFi.SSID());
// Mostrar la IP
Serial.print("IP: ");
Serial.println(WiFi.localIP());
/*
Zona horaria
CEST: horario de verano de Europa Central
Inicio del horario de verano europeo: último domingo de marzo, a las 2 h GMT + 2 horas
*/
ntp.ruleDST("CEST", Last, Sun, Mar, 2, 120);
// CET: hora de Europa Central
// Inicio del horario estándar: último domingo de octubre, a las 3 h GMT + 1 hora
ntp.ruleSTD("CET", Last, Sun, Oct, 3, 60);
// Iniciar NTP
ntp.begin();
// Iniciar OLED
oled.begin();
// Tipo de letra
oled.setFont(u8g2_font_courB24_tf);
// Color de dibujo blanco
oled.setDrawColor(1);
// Orientación horizontal del texto
oled.setFontDirection(0);
// Mostrar la fecha y la hora al iniciar
ntp.update();
oled.clearDisplay();
oled.firstPage();
do
{
oled.setCursor(2, 15);
oled.setFont(u8g2_font_t0_22_te);
// Día: añadir un cero inicial
if (ntp.day() < 10) oled.print("0");
oled.print(ntp.day());
oled.print(".");
// Mes: añadir un cero inicial
if (ntp.month() < 10) oled.print("0");
oled.print(ntp.month());
oled.print(".");
// Año
oled.print(ntp.year());
// Línea horizontal
oled.drawHLine(1, 22, oled.getDisplayWidth());
oled.setCursor(2, 63);
// Hora
// Horas: añadir un cero inicial
oled.setFont(u8g2_font_logisoso32_tf);
if (ntp.hours() < 10) oled.print("0");
oled.print(String(ntp.hours()) + ":");
// Minutos: añadir un cero inicial
if (ntp.minutes() < 10) oled.print("0");
oled.print(String(ntp.minutes()));
}
while (oled.nextPage());
}
void loop()
{
// Establecer la hora de inicio
Startzeit = millis();
// Si se alcanza el intervalo establecido
if (Startzeit - GesicherteStartZeit > Intervall)
{
// Actualizar la hora
ntp.update();
oled.clearDisplay();
// Restablecer la hora de inicio
GesicherteStartZeit = Startzeit;
// Mostrar la fecha y la hora en el OLED
oled.setCursor(3, 20);
oled.firstPage();
do
{
oled.setCursor(2, 15);
oled.setFont(u8g2_font_t0_22_te);
// Día: añadir un cero inicial
if (ntp.day() < 10) oled.print("0");
oled.print(ntp.day());
oled.print(".");
// Mes: añadir un cero inicial
if (ntp.month() < 10) oled.print("0");
oled.print(ntp.month());
oled.print(".");
oled.print(ntp.year());
// Línea horizontal
oled.drawHLine(1, 22, oled.getDisplayWidth());
oled.setCursor(2, 63);
// Hora
// Horas: añadir un cero inicial
oled.setFont(u8g2_font_logisoso32_tf);
if (ntp.hours() < 10) oled.print("0");
oled.print(String(ntp.hours()) + ":");
// Minutos: añadir un cero inicial
if (ntp.minutes() < 10) oled.print("0");
oled.print(String(ntp.minutes()));
}
while (oled.nextPage());
}
}Calificar la guía