chore: The data is now updated every two hours
This commit is contained in:
parent
8be79fa368
commit
bec48059ac
1 changed files with 55 additions and 38 deletions
93
src/main.cpp
93
src/main.cpp
|
@ -21,6 +21,9 @@ unsigned long previousMillis = 0;
|
||||||
const long interval = 500;
|
const long interval = 500;
|
||||||
int dotCount = 0;
|
int dotCount = 0;
|
||||||
|
|
||||||
|
const unsigned long updateInterval = 7200000; // 2h
|
||||||
|
unsigned long lastUpdate = 0;
|
||||||
|
|
||||||
void drawGraph(const float *val, int len,
|
void drawGraph(const float *val, int len,
|
||||||
uint16_t x0, uint16_t y0,
|
uint16_t x0, uint16_t y0,
|
||||||
uint16_t w, uint16_t h,
|
uint16_t w, uint16_t h,
|
||||||
|
@ -53,9 +56,9 @@ void drawGraph(const float *val, int len,
|
||||||
tft.setTextSize(1);
|
tft.setTextSize(1);
|
||||||
char buf[16];
|
char buf[16];
|
||||||
snprintf(buf, sizeof(buf), "%.1f", vMax);
|
snprintf(buf, sizeof(buf), "%.1f", vMax);
|
||||||
int16_t x1, y1;
|
int16_t bx, by;
|
||||||
uint16_t bw, bh;
|
uint16_t bw, bh;
|
||||||
tft.getTextBounds(buf, 0, 0, &x1, &y1, &bw, &bh);
|
tft.getTextBounds(buf, 0, 0, &bx, &by, &bw, &bh);
|
||||||
tft.setCursor(x0 + w - bw - 1, y0 - bh - 1);
|
tft.setCursor(x0 + w - bw - 1, y0 - bh - 1);
|
||||||
tft.print(buf);
|
tft.print(buf);
|
||||||
|
|
||||||
|
@ -64,58 +67,32 @@ void drawGraph(const float *val, int len,
|
||||||
tft.print(buf);
|
tft.print(buf);
|
||||||
}
|
}
|
||||||
|
|
||||||
void setup() {
|
void updateData() {
|
||||||
Serial.begin(115200);
|
|
||||||
tft.initR(INITR_BLACKTAB);
|
|
||||||
tft.setRotation(1);
|
|
||||||
tft.setSPISpeed(40000000);
|
|
||||||
tft.fillScreen(ST77XX_BLACK);
|
|
||||||
tft.setTextColor(ST77XX_WHITE);
|
|
||||||
tft.setTextSize(1);
|
|
||||||
|
|
||||||
WiFi.begin(ssid, password);
|
|
||||||
while (WiFi.status() != WL_CONNECTED) {
|
|
||||||
unsigned long currentMillis = millis();
|
|
||||||
if (currentMillis - previousMillis >= interval) {
|
|
||||||
previousMillis = currentMillis;
|
|
||||||
tft.fillScreen(ST77XX_BLACK);
|
|
||||||
tft.setCursor(0, 10);
|
|
||||||
tft.print("Connecting");
|
|
||||||
for (int i = 0; i < dotCount; ++i) tft.print('.');
|
|
||||||
dotCount = (dotCount + 1) % 4;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
tft.fillScreen(ST77XX_BLACK);
|
|
||||||
tft.setCursor(0, 10);
|
|
||||||
tft.print("Connected: ");
|
|
||||||
tft.print(WiFi.localIP());
|
|
||||||
|
|
||||||
configTime(0, 0, "pool.ntp.org", "time.nist.gov");
|
configTime(0, 0, "pool.ntp.org", "time.nist.gov");
|
||||||
struct tm ti;
|
struct tm ti;
|
||||||
while (!getLocalTime(&ti));
|
if (!getLocalTime(&ti)) return;
|
||||||
|
|
||||||
char endDate[11], startDate[11];
|
char endDate[11], startDate[11];
|
||||||
strftime(endDate, sizeof(endDate), "%Y-%m-%d", &ti);
|
strftime(endDate, sizeof(endDate), "%Y-%m-%d", &ti);
|
||||||
ti.tm_mon--;
|
ti.tm_mon--;
|
||||||
mktime(&ti);
|
mktime(&ti);
|
||||||
strftime(startDate, sizeof(startDate), "%Y-%m-%d", &ti);
|
strftime(startDate, sizeof(startDate), "%Y-%m-%d", &ti);
|
||||||
|
|
||||||
tft.fillScreen(ST77XX_BLACK);
|
|
||||||
tft.setCursor(0, 10);
|
|
||||||
|
|
||||||
WiFiClientSecure client;
|
WiFiClientSecure client;
|
||||||
client.setInsecure();
|
client.setInsecure();
|
||||||
HTTPClient http;
|
HTTPClient http;
|
||||||
|
|
||||||
String url = String(kekkai) + "/api/getRate/?from_currency=USD&conv_currency=RUB&date=" + endDate;
|
String url = String(kekkai) + "/api/getRate/?from_currency=USD&conv_currency=RUB&date=" + endDate;
|
||||||
http.begin(client, url.c_str());
|
http.begin(client, url.c_str());
|
||||||
int code = http.GET();
|
if (http.GET() == HTTP_CODE_OK) {
|
||||||
if (code == HTTP_CODE_OK) {
|
|
||||||
DynamicJsonDocument d(1024);
|
DynamicJsonDocument d(1024);
|
||||||
String p = http.getString();
|
String p = http.getString();
|
||||||
deserializeJson(d, p);
|
deserializeJson(d, p);
|
||||||
float rate = d["rate"].as<float>();
|
float rate = d["rate"].as<float>();
|
||||||
|
|
||||||
|
tft.fillScreen(ST77XX_BLACK);
|
||||||
|
tft.setCursor(0, 10);
|
||||||
|
|
||||||
tft.printf("1 USD = %.4f RUB", rate);
|
tft.printf("1 USD = %.4f RUB", rate);
|
||||||
}
|
}
|
||||||
http.end();
|
http.end();
|
||||||
|
@ -124,8 +101,7 @@ void setup() {
|
||||||
+ "/api/getRate/?from_currency=USD&conv_currency=RUB&start_date=" + startDate
|
+ "/api/getRate/?from_currency=USD&conv_currency=RUB&start_date=" + startDate
|
||||||
+ "&end_date=" + endDate;
|
+ "&end_date=" + endDate;
|
||||||
http.begin(client, url.c_str());
|
http.begin(client, url.c_str());
|
||||||
code = http.GET();
|
if (http.GET() == HTTP_CODE_OK) {
|
||||||
if (code == HTTP_CODE_OK) {
|
|
||||||
DynamicJsonDocument d(32768);
|
DynamicJsonDocument d(32768);
|
||||||
String p = http.getString();
|
String p = http.getString();
|
||||||
deserializeJson(d, p);
|
deserializeJson(d, p);
|
||||||
|
@ -141,4 +117,45 @@ void setup() {
|
||||||
http.end();
|
http.end();
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop() {}
|
|
||||||
|
void setup() {
|
||||||
|
Serial.begin(115200);
|
||||||
|
tft.initR(INITR_BLACKTAB);
|
||||||
|
tft.setRotation(1);
|
||||||
|
tft.setSPISpeed(40000000);
|
||||||
|
tft.fillScreen(ST77XX_BLACK);
|
||||||
|
tft.setTextColor(ST77XX_WHITE);
|
||||||
|
tft.setTextSize(1);
|
||||||
|
|
||||||
|
WiFi.begin(ssid, password);
|
||||||
|
previousMillis = millis();
|
||||||
|
dotCount = 0;
|
||||||
|
while (WiFi.status() != WL_CONNECTED) {
|
||||||
|
unsigned long currentMillis = millis();
|
||||||
|
if (currentMillis - previousMillis >= interval) {
|
||||||
|
previousMillis = currentMillis;
|
||||||
|
tft.fillScreen(ST77XX_BLACK);
|
||||||
|
tft.setCursor(10, 10);
|
||||||
|
tft.print("Connecting");
|
||||||
|
for (int i = 0; i < dotCount; i++) {
|
||||||
|
tft.print(".");
|
||||||
|
}
|
||||||
|
dotCount++;
|
||||||
|
if (dotCount > 3) {
|
||||||
|
dotCount = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
tft.fillScreen(ST77XX_BLACK);
|
||||||
|
updateData();
|
||||||
|
lastUpdate = millis();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void loop() {
|
||||||
|
if (millis() - lastUpdate >= updateInterval) {
|
||||||
|
updateData();
|
||||||
|
lastUpdate = millis();
|
||||||
|
}
|
||||||
|
}
|
Loading…
Add table
Reference in a new issue