From da4719c0466f1b678551562f36a662f122ef2a9d Mon Sep 17 00:00:00 2001 From: Djuri Baars Date: Sun, 8 Oct 2023 16:07:05 +0200 Subject: [PATCH] Enable WiFi autoconnect explicitly, fixed some http connect quirks --- src/config.h | 2 ++ src/lib/functions.cpp | 1 + src/screens/halvingcountdown.cpp | 6 +++--- src/tasks/blocknotify.cpp | 15 ++++++++++----- src/tasks/get_price.cpp | 5 +++-- 5 files changed, 19 insertions(+), 10 deletions(-) diff --git a/src/config.h b/src/config.h index 2e2928f..e162a17 100644 --- a/src/config.h +++ b/src/config.h @@ -22,4 +22,6 @@ #define DEFAULT_FG_COLOR GxEPD_WHITE #define DEFAULT_BG_COLOR GxEPD_BLACK +#define USER_AGENT "BTClock/1.0" + #define I2C_DEV_ADDR 0x55 \ No newline at end of file diff --git a/src/lib/functions.cpp b/src/lib/functions.cpp index 4e7e39d..d066125 100644 --- a/src/lib/functions.cpp +++ b/src/lib/functions.cpp @@ -139,6 +139,7 @@ void setupWifi() setupSoftAP(); wm.setConfigPortalTimeout(preferences.getUInt("wpTimeout", 600)); + wm.setWiFiAutoReconnect(true); wm.setAPCallback([&](WiFiManager *wifiManager) { showSetupQr(softAP_SSID, softAP_password); diff --git a/src/screens/halvingcountdown.cpp b/src/screens/halvingcountdown.cpp index 67b43f3..d66fe00 100644 --- a/src/screens/halvingcountdown.cpp +++ b/src/screens/halvingcountdown.cpp @@ -23,13 +23,13 @@ void HalvingCountdownScreen::showScreen() int mins = floor(minutesToHalving - (years * 525600) - (days * (24*60)) - (hours * 60)); // int secs = floor((minutesToHalving - (years * 525600) - (days * (24*60)) - (hours * 60) - mins) * 60); - epdContent[0] = "BIT/COIN"; - epdContent[1] = "HALV/ING"; + epdContent[0] = F("BIT/COIN"); + epdContent[1] = F("HALV/ING"); epdContent[2] = String(years) + "/YRS"; epdContent[3] = String(days) + "/DAYS"; epdContent[4] = String(hours) + "/HRS"; epdContent[5] = String(mins) + "/MINS"; - epdContent[6] = "TO/GO"; + epdContent[6] = F("TO/GO"); } uint HalvingCountdownScreen::getNextHalvingBlockNr() diff --git a/src/tasks/blocknotify.cpp b/src/tasks/blocknotify.cpp index 7b11418..005521f 100644 --- a/src/tasks/blocknotify.cpp +++ b/src/tasks/blocknotify.cpp @@ -19,25 +19,28 @@ void checkBitcoinBlock(void *pvParameters) { int blockHeight = preferences.getUInt("blockHeight", currentBlockHeight); - HTTPClient http; + useBitcoind = preferences.getBool("useNode", false) && wifiClientInsecure.connect(preferences.getString("rpcHost", BITCOIND_HOST).c_str(), preferences.getUInt("rpcPort", BITCOIND_PORT)); if (useBitcoind) Serial.println("bitcoind node is reachable, using this for blocks."); else Serial.println("bitcoind node is not reachable, using mempool API instead."); + for (;;) { + HTTPClient http; + http.setUserAgent(USER_AGENT); + if (useBitcoind) { StaticJsonDocument<200> jsonDoc; http.begin(preferences.getString("rpcHost", BITCOIND_HOST).c_str(), preferences.getUInt("rpcPort", BITCOIND_PORT)); http.addHeader("Content-Type", "application/json"); - http.addHeader("User-Agent", "BTClock/1.0"); - String payload = "{\"jsonrpc\":\"1.0\",\"id\":\"current_block_height\",\"method\":\"getblockcount\",\"params\":[]}"; - String authEncoded = base64::encode(preferences.getString("rpcUser", BITCOIND_RPC_USER) + ":" + preferences.getString("rpcPass", BITCOIND_RPC_PASS)); + const String payload = "{\"jsonrpc\":\"1.0\",\"id\":\"current_block_height\",\"method\":\"getblockcount\",\"params\":[]}"; + const String authEncoded = base64::encode(preferences.getString("rpcUser", BITCOIND_RPC_USER) + ":" + preferences.getString("rpcPass", BITCOIND_RPC_PASS)); http.addHeader("Authorization", "Basic " + authEncoded); int httpCode = http.POST(payload); @@ -57,7 +60,6 @@ void checkBitcoinBlock(void *pvParameters) else { http.begin("https://" + preferences.getString("mempoolInstance", DEFAULT_MEMPOOL_INSTANCE) + "/api/blocks/tip/height"); - http.addHeader("User-Agent", "BTClock/1.0"); int httpCode = http.GET(); if (httpCode > 0 && httpCode == HTTP_CODE_OK) @@ -68,7 +70,9 @@ void checkBitcoinBlock(void *pvParameters) else { Serial.print(F("Error in HTTP request to mempool API: ")); + Serial.print(httpCode); Serial.println(http.errorToString(httpCode)); + } http.end(); @@ -82,6 +86,7 @@ void checkBitcoinBlock(void *pvParameters) currentBlockHeight = blockHeight; preferences.putUInt("blockHeight", currentBlockHeight); } + vTaskDelay(pdMS_TO_TICKS(BLOCKNOTIFY_WAIT_TIME)); // wait 1 minute before checking again } } diff --git a/src/tasks/get_price.cpp b/src/tasks/get_price.cpp index 63d9f15..1542310 100644 --- a/src/tasks/get_price.cpp +++ b/src/tasks/get_price.cpp @@ -13,12 +13,13 @@ TaskHandle_t getPriceTaskHandle; void taskGetPrice(void *pvParameters) { - HTTPClient http; for (;;) { + HTTPClient http; + http.setUserAgent(USER_AGENT); + // Send HTTP request to CoinDesk API http.begin(apiUrl); - http.addHeader("User-Agent", "BTClock/1.0"); int httpCode = http.GET();