Change to CoinGecko API for price data and keep the webserver priority default

This commit is contained in:
Djuri Baars 2023-10-27 20:32:33 +02:00
parent aae94b56c9
commit 0bf60d160f
2 changed files with 63 additions and 27 deletions

View file

@ -62,7 +62,7 @@ build_flags =
-D WITH_BUTTONS -D WITH_BUTTONS
-D ARDUINO_USB_CDC_ON_BOOT -D ARDUINO_USB_CDC_ON_BOOT
-D HOSTNAME="\"btclock3\"" -D HOSTNAME="\"btclock3\""
-D CONFIG_ASYNC_TCP_PRIORITY=500 -mfix-esp32-psram-cache-issue
[env:esp32wemos-s3-mini_3C] [env:esp32wemos-s3-mini_3C]
platform = espressif32 platform = espressif32

View file

@ -1,6 +1,7 @@
#include "get_price.hpp" #include "get_price.hpp"
const PROGMEM char *apiUrl = "https://api.coindesk.com/v1/bpi/currentprice/USD.json"; 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 std::vector<EventCallbackWithNumber> priceEventCallbacks; // Define a vector to hold multiple event callbacks
TaskHandle_t getPriceTaskHandle; TaskHandle_t getPriceTaskHandle;
@ -18,32 +19,66 @@ void taskGetPrice(void *pvParameters)
HTTPClient http; HTTPClient http;
http.setUserAgent(USER_AGENT); http.setUserAgent(USER_AGENT);
// Send HTTP request to CoinDesk API if (true)
http.begin(apiUrl);
int httpCode = http.GET();
// Parse JSON response and extract average price
float usdPrice, eurPrice;
if (httpCode == 200)
{ {
String payload = http.getString(); // Send HTTP request to CoinGecko API
StaticJsonDocument<768> doc; http.begin(cgApiUrl);
deserializeJson(doc, payload);
JsonObject bpi = doc["bpi"]; int httpCode = http.GET();
usdPrice = bpi["USD"]["rate_float"];
eurPrice = bpi["EUR"]["rate_float"]; // Parse JSON response and extract average price
for(auto &callback : priceEventCallbacks) { // Loop through all the event callbacks and call them float usdPrice, eurPrice;
callback(usdPrice); 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); // Send HTTP request to CoinDesk API
preferences.putFloat("btcPriceEur", eurPrice); http.begin(apiUrl);
}
else int httpCode = http.GET();
{
Serial.print(F("Error retrieving BTC/USD price. HTTP status code: ")); // Parse JSON response and extract average price
Serial.println(httpCode); 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(); http.end();
@ -54,7 +89,8 @@ void taskGetPrice(void *pvParameters)
void setupGetPriceTask() void setupGetPriceTask()
{ {
if (getPriceTaskHandle == nullptr) { if (getPriceTaskHandle == nullptr)
{
xTaskCreate(taskGetPrice, "getPrice", 8192, NULL, 1, &getPriceTaskHandle); xTaskCreate(taskGetPrice, "getPrice", 8192, NULL, 1, &getPriceTaskHandle);
vTaskSuspend(getPriceTaskHandle); vTaskSuspend(getPriceTaskHandle);
} }
@ -62,5 +98,5 @@ void setupGetPriceTask()
void registerNewPriceCallback(EventCallbackWithNumber cb) void registerNewPriceCallback(EventCallbackWithNumber cb)
{ {
priceEventCallbacks.push_back(cb); priceEventCallbacks.push_back(cb);
} }