synced time when on wifi

This commit is contained in:
Klemek
2021-09-20 22:25:34 +02:00
parent bed398b85e
commit b08a5db57f
5 changed files with 67 additions and 2 deletions
+2
View File
@@ -30,6 +30,8 @@ void WatchyTetris::drawWatchFace()
// Background
display.drawBitmap(0, 0, tetrisbg, DISPLAY_WIDTH, DISPLAY_HEIGHT, GxEPD_BLACK);
readWorldTime();
//Hour
display.drawBitmap(25, 20, tetris_nums_0[currentTime.Hour / 10], 40, 60, GxEPD_BLACK); //first digit
display.drawBitmap(75, 20, tetris_nums_1[currentTime.Hour % 10], 40, 60, GxEPD_BLACK); //second digit
+5 -1
View File
@@ -3,8 +3,12 @@
#include <Watchy.h>
#include "tetris.h"
#include "wta.h"
class WatchyTetris : public Watchy
#undef WTA_TIMEZONE
#define WTA_TIMEZONE "Europe/Paris"
class WatchyTetris : public WatchySynced
{
public:
WatchyTetris();
+42
View File
@@ -0,0 +1,42 @@
#include "wta.h"
RTC_DATA_ATTR int worldTimeIntervalCounter = WTA_UPDATE_INTERVAL;
void WatchySynced::readWorldTime()
{
if (worldTimeIntervalCounter >= WTA_UPDATE_INTERVAL)
{
if (connectWiFi())
{
HTTPClient http;
http.setConnectTimeout(10000);
String queryURL = String(WTA_URL) + String(WTA_TIMEZONE);
http.begin(queryURL.c_str());
int httpResponseCode = http.GET();
if (httpResponseCode == 200)
{
String payload = http.getString();
JSONVar responseObject = JSON.parse(payload);
tmElements_t tm;
String datetime = String((const char *)responseObject["datetime"]);
tm.Year = datetime.substring(0, 4).toInt() - YEAR_OFFSET;
tm.Month = datetime.substring(5, 7).toInt();
tm.Day = datetime.substring(8, 10).toInt();
tm.Hour = datetime.substring(11, 13).toInt();
tm.Minute = datetime.substring(14, 16).toInt();
tm.Second = 0;
time_t t = makeTime(tm);
RTC.set(t);
RTC.read(currentTime);
}
http.end();
WiFi.mode(WIFI_OFF);
btStop();
}
worldTimeIntervalCounter = 0;
}
else
{
worldTimeIntervalCounter++;
}
}
+16
View File
@@ -0,0 +1,16 @@
#ifndef WTA_H
#define WTA_H
#include <Watchy.h>
#define WTA_URL "http://worldtimeapi.org/api/timezone/"
#define WTA_TIMEZONE "Etc/UTC"
#define WTA_UPDATE_INTERVAL 60 //minutes
class WatchySynced : public Watchy
{
public:
void readWorldTime();
};
#endif