Change to CoinGecko API for price data and keep the webserver priority default
This commit is contained in:
parent
aae94b56c9
commit
0bf60d160f
2 changed files with 63 additions and 27 deletions
|
@ -62,7 +62,7 @@ build_flags =
|
|||
-D WITH_BUTTONS
|
||||
-D ARDUINO_USB_CDC_ON_BOOT
|
||||
-D HOSTNAME="\"btclock3\""
|
||||
-D CONFIG_ASYNC_TCP_PRIORITY=500
|
||||
-mfix-esp32-psram-cache-issue
|
||||
|
||||
[env:esp32wemos-s3-mini_3C]
|
||||
platform = espressif32
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
#include "get_price.hpp"
|
||||
|
||||
const PROGMEM char *apiUrl = "https://api.coindesk.com/v1/bpi/currentprice/USD.json";
|
||||
const PROGMEM char *cgApiUrl = "https://api.coingecko.com/api/v3/simple/price?ids=bitcoin&vs_currencies=usd%2Ceur";
|
||||
|
||||
std::vector<EventCallbackWithNumber> priceEventCallbacks; // Define a vector to hold multiple event callbacks
|
||||
TaskHandle_t getPriceTaskHandle;
|
||||
|
@ -18,32 +19,66 @@ void taskGetPrice(void *pvParameters)
|
|||
HTTPClient http;
|
||||
http.setUserAgent(USER_AGENT);
|
||||
|
||||
// Send HTTP request to CoinDesk API
|
||||
http.begin(apiUrl);
|
||||
|
||||
int httpCode = http.GET();
|
||||
|
||||
// Parse JSON response and extract average price
|
||||
float usdPrice, eurPrice;
|
||||
if (httpCode == 200)
|
||||
if (true)
|
||||
{
|
||||
String payload = http.getString();
|
||||
StaticJsonDocument<768> doc;
|
||||
deserializeJson(doc, payload);
|
||||
JsonObject bpi = doc["bpi"];
|
||||
usdPrice = bpi["USD"]["rate_float"];
|
||||
eurPrice = bpi["EUR"]["rate_float"];
|
||||
for(auto &callback : priceEventCallbacks) { // Loop through all the event callbacks and call them
|
||||
callback(usdPrice);
|
||||
// Send HTTP request to CoinGecko API
|
||||
http.begin(cgApiUrl);
|
||||
|
||||
int httpCode = http.GET();
|
||||
|
||||
// Parse JSON response and extract average price
|
||||
float usdPrice, eurPrice;
|
||||
if (httpCode == 200)
|
||||
{
|
||||
String payload = http.getString();
|
||||
StaticJsonDocument<768> doc;
|
||||
deserializeJson(doc, payload);
|
||||
JsonObject bpi = doc["bitcoin"];
|
||||
usdPrice = bpi["usd"];
|
||||
eurPrice = bpi["eur"];
|
||||
for (auto &callback : priceEventCallbacks)
|
||||
{ // Loop through all the event callbacks and call them
|
||||
callback(usdPrice);
|
||||
}
|
||||
|
||||
preferences.putFloat("btcPrice", usdPrice);
|
||||
preferences.putFloat("btcPriceEur", eurPrice);
|
||||
}
|
||||
else
|
||||
{
|
||||
Serial.print(F("Error retrieving BTC/USD price (CoinGecko). HTTP status code: "));
|
||||
Serial.println(httpCode);
|
||||
}
|
||||
} else {
|
||||
|
||||
preferences.putFloat("btcPrice", usdPrice);
|
||||
preferences.putFloat("btcPriceEur", eurPrice);
|
||||
}
|
||||
else
|
||||
{
|
||||
Serial.print(F("Error retrieving BTC/USD price. HTTP status code: "));
|
||||
Serial.println(httpCode);
|
||||
// Send HTTP request to CoinDesk API
|
||||
http.begin(apiUrl);
|
||||
|
||||
int httpCode = http.GET();
|
||||
|
||||
// Parse JSON response and extract average price
|
||||
float usdPrice, eurPrice;
|
||||
if (httpCode == 200)
|
||||
{
|
||||
String payload = http.getString();
|
||||
StaticJsonDocument<768> doc;
|
||||
deserializeJson(doc, payload);
|
||||
JsonObject bpi = doc["bpi"];
|
||||
usdPrice = bpi["USD"]["rate_float"];
|
||||
eurPrice = bpi["EUR"]["rate_float"];
|
||||
for (auto &callback : priceEventCallbacks)
|
||||
{ // Loop through all the event callbacks and call them
|
||||
callback(usdPrice);
|
||||
}
|
||||
|
||||
preferences.putFloat("btcPrice", usdPrice);
|
||||
preferences.putFloat("btcPriceEur", eurPrice);
|
||||
}
|
||||
else
|
||||
{
|
||||
Serial.print(F("Error retrieving BTC/USD price (CoinDesk). HTTP status code: "));
|
||||
Serial.println(httpCode);
|
||||
}
|
||||
}
|
||||
|
||||
http.end();
|
||||
|
@ -54,7 +89,8 @@ void taskGetPrice(void *pvParameters)
|
|||
|
||||
void setupGetPriceTask()
|
||||
{
|
||||
if (getPriceTaskHandle == nullptr) {
|
||||
if (getPriceTaskHandle == nullptr)
|
||||
{
|
||||
xTaskCreate(taskGetPrice, "getPrice", 8192, NULL, 1, &getPriceTaskHandle);
|
||||
vTaskSuspend(getPriceTaskHandle);
|
||||
}
|
||||
|
@ -62,5 +98,5 @@ void setupGetPriceTask()
|
|||
|
||||
void registerNewPriceCallback(EventCallbackWithNumber cb)
|
||||
{
|
||||
priceEventCallbacks.push_back(cb);
|
||||
priceEventCallbacks.push_back(cb);
|
||||
}
|
Loading…
Reference in a new issue