From 45df5179b8df1968d4b4be35cdb181ace701c2b8 Mon Sep 17 00:00:00 2001 From: Blue Fox Date: Sun, 21 Aug 2022 19:21:56 +0200 Subject: [PATCH] Now using NTP server for getting time. --- http_grab.ino | 16 ++++++++++++++++ losungepaper.ino | 9 +++++++-- wifi.ino | 5 +++++ 3 files changed, 28 insertions(+), 2 deletions(-) diff --git a/http_grab.ino b/http_grab.ino index 1846ee5..c808200 100755 --- a/http_grab.ino +++ b/http_grab.ino @@ -28,6 +28,22 @@ String getHTML(String url) { } return result; } +String getHTML() { // gets the date and year from time.h library + struct tm timeinfo; + char dayInMonth[3]; + char month[3]; + char year[5]; + + if(!getLocalTime(&timeinfo)){ + Serial.println("Failed to obtain time"); + return ""; + } + strftime(dayInMonth,3, "%d", &timeinfo); + strftime(month,3, "%m", &timeinfo); + strftime(year,5, "%Y", &timeinfo); + + return getHTML(String("https://www.losungen.de/fileadmin/media-losungen/heute/") + String(year) + String("/") + String(month) + String(dayInMonth) + String(".html")); +} String getLosungFromHTML(String html) { int losungBeginIndex = html.indexOf(textBeginString)+textBeginString.length(); diff --git a/losungepaper.ino b/losungepaper.ino index 7d254e4..0f0f3c9 100755 --- a/losungepaper.ino +++ b/losungepaper.ino @@ -11,6 +11,11 @@ Epd epd; char wiFiSSID[] = "SSID"; // SSID of your network char wiFiPSK[] = "PRE-SHARED KEY"; //password of your WPA Network +// NTP (Network Time Protocol) +const String ntpServer = "pool.ntp.org"; // address of ntp server; may also be your router, other local service, etc. +const long gmtOffset_sec = 3600; // for localization (gmt = greenwich mean time); in germany: 3600 +const int daylightOffset_sec = 3600; // for daylight saving + // style of the frame shown sFONT TITLE_FONT = Font20; char TITLE_TEXT[] = "Losung heute"; @@ -33,8 +38,8 @@ void setup() { Serial.println("[INFO] Initialized e-Paper!"); connectWiFi(); - String html = getHTML("https://www.losungen.de/fileadmin/media-losungen/heute/2022/0821.html"); - + String html = getHTML(); + showDailyText(getLosungFromHTML(html), getLehrtextFromHTML(html), getLosungSourceFromHTML(html), getLehrtextSourceFromHTML(html)); } diff --git a/wifi.ino b/wifi.ino index d249d15..fe89f14 100755 --- a/wifi.ino +++ b/wifi.ino @@ -1,4 +1,5 @@ #include +#include "time.h" void connectWiFi() { // connect to the wifi with the above defined credentials if(WiFi.status() == WL_CONNECTED) { return; } // return if not connected @@ -11,5 +12,9 @@ void connectWiFi() { // connect to the wifi with the above defined credentials Serial.printf("[WiFi] Connected to WiFi \"%s\", got IP-Adress ", wiFiSSID); Serial.println(WiFi.localIP()); + Serial.println("[NTP] Getting time..."); + configTime(gmtOffset_sec, daylightOffset_sec, ntpServer.c_str()); + Serial.println("[NTP] Got time."); + return; } \ No newline at end of file