mirror of
https://github.com/btclock/btclock_v3.git
synced 2024-11-19 06:20:02 +01:00
Added price fetch code, fix webUI bug
This commit is contained in:
parent
d0eb007c4c
commit
78437c9722
@ -82,7 +82,7 @@
|
|||||||
-
|
-
|
||||||
Mempool.space connection:
|
Mempool.space connection:
|
||||||
<span>
|
<span>
|
||||||
{{#if connectionStatus.price}}
|
{{#if connectionStatus.blocks}}
|
||||||
✅
|
✅
|
||||||
{{else}}
|
{{else}}
|
||||||
❌
|
❌
|
||||||
|
@ -163,6 +163,7 @@ void setupPreferences()
|
|||||||
void setupWebsocketClients(void *pvParameters)
|
void setupWebsocketClients(void *pvParameters)
|
||||||
{
|
{
|
||||||
setupBlockNotify();
|
setupBlockNotify();
|
||||||
|
// setupPriceFetchTask();
|
||||||
setupPriceNotify();
|
setupPriceNotify();
|
||||||
|
|
||||||
vTaskDelete(NULL);
|
vTaskDelete(NULL);
|
||||||
|
60
src/lib/price_fetch.cpp
Normal file
60
src/lib/price_fetch.cpp
Normal file
@ -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);
|
||||||
|
|
||||||
|
}
|
9
src/lib/price_fetch.hpp
Normal file
9
src/lib/price_fetch.hpp
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
#include <Arduino.h>
|
||||||
|
#include <HTTPClient.h>
|
||||||
|
#include "config.hpp"
|
||||||
|
#include "shared.hpp"
|
||||||
|
|
||||||
|
extern TaskHandle_t priceFetchTaskHandle;
|
||||||
|
|
||||||
|
void setupPriceFetchTask();
|
||||||
|
void taskPriceFetch(void *pvParameters);
|
@ -227,6 +227,9 @@ void IRAM_ATTR minuteTimerISR(void *arg)
|
|||||||
// vTaskNotifyGiveFromISR(timeUpdateTaskHandle, &xHigherPriorityTaskWoken);
|
// vTaskNotifyGiveFromISR(timeUpdateTaskHandle, &xHigherPriorityTaskWoken);
|
||||||
WorkItem timeUpdate = {TASK_TIME_UPDATE, 0};
|
WorkItem timeUpdate = {TASK_TIME_UPDATE, 0};
|
||||||
xQueueSendFromISR(workQueue, &timeUpdate, &xHigherPriorityTaskWoken);
|
xQueueSendFromISR(workQueue, &timeUpdate, &xHigherPriorityTaskWoken);
|
||||||
|
if (priceFetchTaskHandle != NULL) {
|
||||||
|
vTaskNotifyGiveFromISR(priceFetchTaskHandle, &xHigherPriorityTaskWoken);
|
||||||
|
}
|
||||||
if (xHigherPriorityTaskWoken == pdTRUE)
|
if (xHigherPriorityTaskWoken == pdTRUE)
|
||||||
{
|
{
|
||||||
portYIELD_FROM_ISR();
|
portYIELD_FROM_ISR();
|
||||||
|
@ -5,6 +5,7 @@
|
|||||||
#include <freertos/FreeRTOS.h>
|
#include <freertos/FreeRTOS.h>
|
||||||
#include <freertos/task.h>
|
#include <freertos/task.h>
|
||||||
|
|
||||||
|
#include "price_fetch.hpp"
|
||||||
#include "shared.hpp"
|
#include "shared.hpp"
|
||||||
#include "lib/epd.hpp"
|
#include "lib/epd.hpp"
|
||||||
|
|
||||||
|
@ -5,7 +5,6 @@
|
|||||||
|
|
||||||
#include "lib/config.hpp"
|
#include "lib/config.hpp"
|
||||||
#define USE_QR
|
#define USE_QR
|
||||||
|
|
||||||
//char ptrTaskList[400];
|
//char ptrTaskList[400];
|
||||||
|
|
||||||
extern "C" void app_main()
|
extern "C" void app_main()
|
||||||
|
Loading…
Reference in New Issue
Block a user