Compare commits

..

3 commits

2 changed files with 122 additions and 68 deletions

View file

@ -1,13 +1,3 @@
; PlatformIO Project Configuration File
;
; Build options: build flags, source filter
; Upload options: custom upload port, speed and extra flags
; Library options: dependencies, extra library storages
; Advanced options: extra scripting
;
; Please visit documentation for the other options and examples
; https://docs.platformio.org/page/projectconf.html
[env:esp32dev]
platform = espressif32
board = esp32dev

View file

@ -1,55 +1,145 @@
#include <Arduino.h>
#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_ST7735.h>
#include <WiFi.h>
#include <WiFiClientSecure.h>
#include <HTTPClient.h>
#include <Arduino.h>
#include <ArduinoJson.h>
#include <time.h>
#define TFT_CS 12
#define TFT_DC 2
#define TFT_RST 25
Adafruit_ST7735 tft(TFT_CS, TFT_DC, TFT_RST);
#define TFT_CS 12 // D12, GPIO12
#define TFT_RST 25 // D25, GPIO25
#define TFT_DC 2 // D2, GPIO2
Adafruit_ST7735 tft = Adafruit_ST7735(TFT_CS, TFT_DC, TFT_RST);
const char* ssid = "Keenetic-4604";
const char* password = "23137099";
const char* ssid = "NAME_WIFI";
const char* password = "WIFI_PSWD";
const char* kekkai = "https://kekkai-api.redume.su";
unsigned long previousMillis = 0;
const long interval = 500;
int dotCount = 0;
int cursorY = 10;
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,
uint16_t colorLine, uint16_t colorAxis) {
float vMin = val[0], vMax = val[0];
for (int i = 1; i < len; ++i) {
vMin = min(vMin, val[i]);
vMax = max(vMax, val[i]);
}
float vRange = (vMax - vMin == 0) ? 1.0f : (vMax - vMin);
tft.fillRect(x0, y0, w, h, ST77XX_BLACK);
tft.drawRect(x0, y0, w, h, colorAxis);
const uint8_t GRID_Y = 4;
for (uint8_t g = 1; g < GRID_Y; ++g) {
uint16_t y = y0 + g * h / GRID_Y;
tft.drawFastHLine(x0 + 1, y, w - 2, 0x7BEF);
}
for (int i = 0; i < len - 1; ++i) {
int x1 = x0 + 1 + (i * (w - 2)) / (len - 1);
int y1 = y0 + h - 2 - (int)((val[i] - vMin) * (h - 3) / vRange);
int x2 = x0 + 1 + ((i + 1) * (w - 2)) / (len - 1);
int y2 = y0 + h - 2 - (int)((val[i + 1] - vMin) * (h - 3) / vRange);
tft.drawLine(x1, y1, x2, y2, colorLine);
}
tft.setTextColor(colorAxis);
tft.setTextSize(1);
char buf[16];
snprintf(buf, sizeof(buf), "%.1f", vMax);
int16_t bx, by;
uint16_t bw, bh;
tft.getTextBounds(buf, 0, 0, &bx, &by, &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 updateData() {
configTime(0, 0, "pool.ntp.org", "time.nist.gov");
struct tm 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);
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());
if (http.GET() == HTTP_CODE_OK) {
DynamicJsonDocument d(1024);
String p = http.getString();
deserializeJson(d, p);
float rate = d["rate"].as<float>();
tft.fillScreen(ST77XX_BLACK);
tft.setCursor(0, 10);
tft.printf("1 USD = %.4f RUB", rate);
}
http.end();
url = String(kekkai)
+ "/api/getRate/?from_currency=USD&conv_currency=RUB&start_date=" + startDate
+ "&end_date=" + endDate;
http.begin(client, url.c_str());
if (http.GET() == HTTP_CODE_OK) {
DynamicJsonDocument d(32768);
String p = http.getString();
deserializeJson(d, p);
JsonArray a = d.as<JsonArray>();
int len = a.size();
float *rates = new float[len];
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);
delete[] rates;
}
http.end();
}
void setup() {
Serial.begin(115200);
delay(1000);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
tft.initR(INITR_BLACKTAB);
tft.setRotation(1);
tft.setSPISpeed(40000000);
tft.fillScreen(ST77XX_BLACK);
tft.setTextColor(ST77XX_WHITE);
tft.setTextSize(2);
tft.setCursor(10, cursorY);
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;
@ -58,40 +148,14 @@ void setup() {
}
tft.fillScreen(ST77XX_BLACK);
tft.setCursor(10, 10);
tft.println("Connected:");
tft.println(WiFi.localIP());
HTTPClient http;
String serverName = "https://kekkai-api.redume.su/api/getRate/";
String serverPath = serverName + "?from_currency=USD&conv_currency=RUB&date=2025-05-26";
http.begin(serverPath.c_str());
int httpResCode = http.GET();
if (httpResCode > 0) {
tft.fillScreen(ST77XX_BLACK);
tft.setCursor(10, 10);
tft.setTextSize(1.3);
String payload = http.getString();
StaticJsonDocument<256> doc;
DeserializationError error = deserializeJson(doc, payload);
if (error) {
tft.println("JSON error");
return;
updateData();
lastUpdate = millis();
}
String from = doc["from_currency"].as<const char*>();
String to = doc["conv_currency"].as<const char*>();
float rate = doc["rate"].as<float>();
tft.print("1 " + String(from) + " = " + String(rate) + " " + String(to));
}
}
void loop() {}
void loop() {
if (millis() - lastUpdate >= updateInterval) {
updateData();
lastUpdate = millis();
}
}