From 78437c97221ec3fe74e5670ae171cc5fb23b679b Mon Sep 17 00:00:00 2001 From: Djuri Baars Date: Sun, 12 Nov 2023 13:29:52 +0100 Subject: [PATCH] Added price fetch code, fix webUI bug --- data/src/index.html | 2 +- src/lib/config.cpp | 1 + src/lib/price_fetch.cpp | 60 ++++++++++++++++++++++++++++++++++++++ src/lib/price_fetch.hpp | 9 ++++++ src/lib/screen_handler.cpp | 3 ++ src/lib/screen_handler.hpp | 1 + src/main.cpp | 1 - 7 files changed, 75 insertions(+), 2 deletions(-) create mode 100644 src/lib/price_fetch.cpp create mode 100644 src/lib/price_fetch.hpp diff --git a/data/src/index.html b/data/src/index.html index 19ce6f7..2865177 100644 --- a/data/src/index.html +++ b/data/src/index.html @@ -82,7 +82,7 @@ - Mempool.space connection: - {{#if connectionStatus.price}} + {{#if connectionStatus.blocks}} ✅ {{else}} ❌ diff --git a/src/lib/config.cpp b/src/lib/config.cpp index ed96c1b..d633cac 100644 --- a/src/lib/config.cpp +++ b/src/lib/config.cpp @@ -163,6 +163,7 @@ void setupPreferences() void setupWebsocketClients(void *pvParameters) { setupBlockNotify(); + // setupPriceFetchTask(); setupPriceNotify(); vTaskDelete(NULL); diff --git a/src/lib/price_fetch.cpp b/src/lib/price_fetch.cpp new file mode 100644 index 0000000..08a588c --- /dev/null +++ b/src/lib/price_fetch.cpp @@ -0,0 +1,60 @@ +#include "price_fetch.hpp" + +const PROGMEM char *cgApiUrl = "https://api.coingecko.com/api/v3/simple/price?ids=bitcoin&vs_currencies=usd%2Ceur"; + +TaskHandle_t priceFetchTaskHandle; + +void taskPriceFetch(void *pvParameters) +{ + WiFiClientSecure *client = new WiFiClientSecure; + client->setInsecure(); + for (;;) + { + ulTaskNotifyTake(pdTRUE, portMAX_DELAY); + + HTTPClient *http = new HTTPClient(); + http->setUserAgent(USER_AGENT); + + // Send HTTP request to CoinGecko API + http->begin(*client, cgApiUrl); + + int httpCode = http->GET(); + + // Parse JSON response and extract average price + uint usdPrice, eurPrice; + if (httpCode == 200) + { + String payload = http->getString(); + StaticJsonDocument<96> doc; + deserializeJson(doc, payload); + usdPrice = doc["bitcoin"]["usd"]; + eurPrice = doc["bitcoin"]["eur"]; + + setPrice(usdPrice); + if (workQueue != nullptr && (getCurrentScreen() == SCREEN_BTC_TICKER || getCurrentScreen() == SCREEN_MSCW_TIME || getCurrentScreen() == SCREEN_MARKET_CAP)) + { + WorkItem priceUpdate = {TASK_PRICE_UPDATE, 0}; + xQueueSend(workQueue, &priceUpdate, portMAX_DELAY); + } + + preferences.putUInt("lastPrice", usdPrice); + } + else + { + Serial.print(F("Error retrieving BTC/USD price (CoinGecko). HTTP status code: ")); + Serial.println(httpCode); + if (httpCode == -1) + { + WiFi.reconnect(); + } + } + } +} + +void setupPriceFetchTask() +{ + xTaskCreate(taskPriceFetch, "priceFetch", (6*1024), NULL, tskIDLE_PRIORITY, &priceFetchTaskHandle); + + xTaskNotifyGive(priceFetchTaskHandle); + +} \ No newline at end of file diff --git a/src/lib/price_fetch.hpp b/src/lib/price_fetch.hpp new file mode 100644 index 0000000..08caa86 --- /dev/null +++ b/src/lib/price_fetch.hpp @@ -0,0 +1,9 @@ +#include +#include +#include "config.hpp" +#include "shared.hpp" + +extern TaskHandle_t priceFetchTaskHandle; + +void setupPriceFetchTask(); +void taskPriceFetch(void *pvParameters); \ No newline at end of file diff --git a/src/lib/screen_handler.cpp b/src/lib/screen_handler.cpp index 2005442..bef59cf 100644 --- a/src/lib/screen_handler.cpp +++ b/src/lib/screen_handler.cpp @@ -227,6 +227,9 @@ void IRAM_ATTR minuteTimerISR(void *arg) // vTaskNotifyGiveFromISR(timeUpdateTaskHandle, &xHigherPriorityTaskWoken); WorkItem timeUpdate = {TASK_TIME_UPDATE, 0}; xQueueSendFromISR(workQueue, &timeUpdate, &xHigherPriorityTaskWoken); + if (priceFetchTaskHandle != NULL) { + vTaskNotifyGiveFromISR(priceFetchTaskHandle, &xHigherPriorityTaskWoken); + } if (xHigherPriorityTaskWoken == pdTRUE) { portYIELD_FROM_ISR(); diff --git a/src/lib/screen_handler.hpp b/src/lib/screen_handler.hpp index 45a6d61..20f8f3b 100644 --- a/src/lib/screen_handler.hpp +++ b/src/lib/screen_handler.hpp @@ -5,6 +5,7 @@ #include #include +#include "price_fetch.hpp" #include "shared.hpp" #include "lib/epd.hpp" diff --git a/src/main.cpp b/src/main.cpp index 9d4b5e2..9c009a7 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -5,7 +5,6 @@ #include "lib/config.hpp" #define USE_QR - //char ptrTaskList[400]; extern "C" void app_main()