diff --git a/src/main.cpp b/src/main.cpp index 663bd7e..c5660c5 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -21,6 +21,9 @@ unsigned long previousMillis = 0; const long interval = 500; int dotCount = 0; +const unsigned long updateInterval = 7200000; // 2h +unsigned long lastUpdate = 0; + void drawGraph(const float *val, int len, uint16_t x0, uint16_t y0, uint16_t w, uint16_t h, @@ -53,9 +56,9 @@ void drawGraph(const float *val, int len, tft.setTextSize(1); char buf[16]; snprintf(buf, sizeof(buf), "%.1f", vMax); - int16_t x1, y1; + int16_t bx, by; 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.print(buf); @@ -64,58 +67,32 @@ void drawGraph(const float *val, int len, tft.print(buf); } -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); - 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()); - +void updateData() { configTime(0, 0, "pool.ntp.org", "time.nist.gov"); struct tm ti; - while (!getLocalTime(&ti)); + if (!getLocalTime(&ti)) return; + char endDate[11], startDate[11]; strftime(endDate, sizeof(endDate), "%Y-%m-%d", &ti); ti.tm_mon--; mktime(&ti); strftime(startDate, sizeof(startDate), "%Y-%m-%d", &ti); - tft.fillScreen(ST77XX_BLACK); - tft.setCursor(0, 10); - WiFiClientSecure client; client.setInsecure(); HTTPClient http; String url = String(kekkai) + "/api/getRate/?from_currency=USD&conv_currency=RUB&date=" + endDate; http.begin(client, url.c_str()); - int code = http.GET(); - if (code == HTTP_CODE_OK) { + if (http.GET() == HTTP_CODE_OK) { DynamicJsonDocument d(1024); String p = http.getString(); deserializeJson(d, p); float rate = d["rate"].as(); + tft.fillScreen(ST77XX_BLACK); + tft.setCursor(0, 10); + tft.printf("1 USD = %.4f RUB", rate); } http.end(); @@ -124,8 +101,7 @@ void setup() { + "/api/getRate/?from_currency=USD&conv_currency=RUB&start_date=" + startDate + "&end_date=" + endDate; http.begin(client, url.c_str()); - code = http.GET(); - if (code == HTTP_CODE_OK) { + if (http.GET() == HTTP_CODE_OK) { DynamicJsonDocument d(32768); String p = http.getString(); deserializeJson(d, p); @@ -141,4 +117,45 @@ void setup() { 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(); + } +} \ No newline at end of file