chore: Now the actual dates are automatically substituted

This commit is contained in:
Danil 2025-06-18 17:55:01 +03:00
parent 8c33a1fae6
commit 8be79fa368

View file

@ -2,9 +2,11 @@
#include <SPI.h> #include <SPI.h>
#include <Adafruit_GFX.h> #include <Adafruit_GFX.h>
#include <Adafruit_ST7735.h> #include <Adafruit_ST7735.h>
#include <WiFi.h>
#include <WiFiClientSecure.h> #include <WiFiClientSecure.h>
#include <HTTPClient.h> #include <HTTPClient.h>
#include <ArduinoJson.h> #include <ArduinoJson.h>
#include <time.h>
#define TFT_CS 12 #define TFT_CS 12
#define TFT_DC 2 #define TFT_DC 2
@ -13,7 +15,7 @@ Adafruit_ST7735 tft(TFT_CS, TFT_DC, TFT_RST);
const char* ssid = "NAME_WIFI"; const char* ssid = "NAME_WIFI";
const char* password = "WIFI_PSWD"; const char* password = "WIFI_PSWD";
const char* kekkai_instace = "https://kekkai-api.redume.su"; const char* kekkai = "https://kekkai-api.redume.su";
unsigned long previousMillis = 0; unsigned long previousMillis = 0;
const long interval = 500; const long interval = 500;
@ -22,8 +24,7 @@ int dotCount = 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,
uint16_t colorLine, uint16_t colorAxis) uint16_t colorLine, uint16_t colorAxis) {
{
float vMin = val[0], vMax = val[0]; float vMin = val[0], vMax = val[0];
for (int i = 1; i < len; ++i) { for (int i = 1; i < len; ++i) {
vMin = min(vMin, val[i]); vMin = min(vMin, val[i]);
@ -43,32 +44,35 @@ void drawGraph(const float *val, int len,
for (int i = 0; i < len - 1; ++i) { for (int i = 0; i < len - 1; ++i) {
int x1 = x0 + 1 + (i * (w - 2)) / (len - 1); int x1 = x0 + 1 + (i * (w - 2)) / (len - 1);
int y1 = y0 + h - 2 - (int)((val[i] - vMin) * (h - 3) / vRange); int y1 = y0 + h - 2 - (int)((val[i] - vMin) * (h - 3) / vRange);
int x2 = x0 + 1 + ((i+1) * (w - 2)) / (len - 1); int x2 = x0 + 1 + ((i + 1) * (w - 2)) / (len - 1);
int y2 = y0 + h - 2 - (int)((val[i + 1] - vMin) * (h - 3) / vRange); int y2 = y0 + h - 2 - (int)((val[i + 1] - vMin) * (h - 3) / vRange);
tft.drawLine(x1, y1, x2, y2, colorLine); tft.drawLine(x1, y1, x2, y2, colorLine);
} }
tft.setTextColor(colorAxis); tft.setTextColor(colorAxis);
tft.setTextSize(1); tft.setTextSize(1);
tft.setCursor(x0 + 120, y0 - 8); char buf[16];
tft.printf("%.1f", vMax); snprintf(buf, sizeof(buf), "%.1f", vMax);
tft.setCursor(x0 + 2, y0 + h + 2); int16_t x1, y1;
tft.printf("%.1f", vMin); uint16_t bw, bh;
tft.getTextBounds(buf, 0, 0, &x1, &y1, &bw, &bh);
tft.setCursor(x0 + w - bw - 1, y0 - bh - 1);
tft.print(buf);
snprintf(buf, sizeof(buf), "%.1f", vMin);
tft.setCursor(x0 + 1, y0 + h + 1);
tft.print(buf);
} }
void setup(){ void setup() {
Serial.begin(115200); Serial.begin(115200);
delay(1000);
tft.initR(INITR_BLACKTAB); tft.initR(INITR_BLACKTAB);
tft.setRotation(1); tft.setRotation(1);
tft.setSPISpeed(40000000); tft.setSPISpeed(40000000);
tft.fillScreen(ST77XX_BLACK); tft.fillScreen(ST77XX_BLACK);
tft.setTextColor(ST77XX_WHITE); tft.setTextColor(ST77XX_WHITE);
tft.setTextSize(1); tft.setTextSize(1);
tft.setCursor(0, 10);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password); WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) { while (WiFi.status() != WL_CONNECTED) {
unsigned long currentMillis = millis(); unsigned long currentMillis = millis();
@ -77,94 +81,64 @@ void setup(){
tft.fillScreen(ST77XX_BLACK); tft.fillScreen(ST77XX_BLACK);
tft.setCursor(0, 10); tft.setCursor(0, 10);
tft.print("Connecting"); tft.print("Connecting");
for (int i = 0; i < dotCount; i++) tft.print("."); for (int i = 0; i < dotCount; ++i) tft.print('.');
dotCount = (dotCount + 1) % 4; dotCount = (dotCount + 1) % 4;
} }
} }
tft.fillScreen(ST77XX_BLACK); tft.fillScreen(ST77XX_BLACK);
tft.setCursor(0, 10); tft.setCursor(0, 10);
tft.print("Connected: "); tft.print("Connected: ");
tft.print(WiFi.localIP()); tft.print(WiFi.localIP());
delay(1000);
HTTPClient http; configTime(0, 0, "pool.ntp.org", "time.nist.gov");
struct tm ti;
while (!getLocalTime(&ti));
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; WiFiClientSecure client;
client.setInsecure(); client.setInsecure();
HTTPClient http;
String dateRateUrl = String(kekkai_instace) String url = String(kekkai) + "/api/getRate/?from_currency=USD&conv_currency=RUB&date=" + endDate;
+ "/api/getRate/?from_currency=USD" http.begin(client, url.c_str());
+ "&conv_currency=RUB" int code = http.GET();
+ "&date=2025-05-26"; if (code == HTTP_CODE_OK) {
http.begin(client, dateRateUrl.c_str()); DynamicJsonDocument d(1024);
int httpResCode = http.GET(); String p = http.getString();
tft.fillScreen(ST77XX_BLACK); deserializeJson(d, p);
tft.setCursor(0, 10); float rate = d["rate"].as<float>();
if (httpResCode > 0) { tft.printf("1 USD = %.4f RUB", rate);
String payload = http.getString();
StaticJsonDocument<256> doc;
auto error = deserializeJson(doc, payload);
if (error) {
tft.print("JSON error1");
Serial.println(error.c_str());
} else {
String from = doc["from_currency"].as<const char*>();
String to = doc["conv_currency"].as<const char*>();
float rate = doc["rate"].as<float>();
tft.printf("1 %s = %.4f %s", from.c_str(), rate, to.c_str());
}
} }
http.end(); http.end();
delay(1000);
String periodRateUrl = String(kekkai_instace) url = String(kekkai)
+ "/api/getRate/?from_currency=USD" + "/api/getRate/?from_currency=USD&conv_currency=RUB&start_date=" + startDate
+ "&conv_currency=RUB" + "&end_date=" + endDate;
+ "&start_date=2025-05-01" http.begin(client, url.c_str());
+ "&end_date=2025-05-28"; code = http.GET();
http.begin(client, periodRateUrl.c_str()); if (code == HTTP_CODE_OK) {
httpResCode = http.GET(); DynamicJsonDocument d(32768);
if (httpResCode < 200) { String p = http.getString();
tft.fillScreen(ST77XX_BLACK); deserializeJson(d, p);
tft.setCursor(0, 10); JsonArray a = d.as<JsonArray>();
tft.printf("API Error: %d", httpResCode); int len = a.size();
http.end(); float *rates = new float[len];
return; for (int i = 0; i < len; ++i) {
rates[i] = a[i]["rate"].as<float>();
} }
drawGraph(rates, len, 8, 32, 144, 80, ST77XX_CYAN, ST77XX_WHITE);
String payload = http.getString();
DynamicJsonDocument doc(32 * 1024);
auto error = deserializeJson(doc, payload);
if (error) {
Serial.printf("JSON error2: %s\n", error.c_str());
tft.fillScreen(ST77XX_BLACK);
tft.setCursor(0, 10);
tft.print("JSON error2");
http.end();
return;
}
JsonArray arr = doc.as<JsonArray>();
size_t ratesLen = arr.size();
if (ratesLen < 2) {
tft.fillScreen(ST77XX_BLACK);
tft.setCursor(0, 10);
tft.print("No data");
http.end();
return;
}
float *rates = new float[ratesLen];
for (size_t i = 0; i < ratesLen; ++i) {
rates[i] = arr[i]["rate"].as<float>();
}
drawGraph(rates, ratesLen,
8, 32, 144, 80,
ST77XX_CYAN, ST77XX_WHITE);
http.end();
delete[] rates; delete[] rates;
}
http.end();
} }
void loop() {} void loop() {}