Enable WiFi autoconnect explicitly, fixed some http connect quirks

This commit is contained in:
Djuri Baars 2023-10-08 16:07:05 +02:00
parent 37cb13f5f4
commit da4719c046
5 changed files with 19 additions and 10 deletions

View file

@ -22,4 +22,6 @@
#define DEFAULT_FG_COLOR GxEPD_WHITE #define DEFAULT_FG_COLOR GxEPD_WHITE
#define DEFAULT_BG_COLOR GxEPD_BLACK #define DEFAULT_BG_COLOR GxEPD_BLACK
#define USER_AGENT "BTClock/1.0"
#define I2C_DEV_ADDR 0x55 #define I2C_DEV_ADDR 0x55

View file

@ -139,6 +139,7 @@ void setupWifi()
setupSoftAP(); setupSoftAP();
wm.setConfigPortalTimeout(preferences.getUInt("wpTimeout", 600)); wm.setConfigPortalTimeout(preferences.getUInt("wpTimeout", 600));
wm.setWiFiAutoReconnect(true);
wm.setAPCallback([&](WiFiManager *wifiManager) wm.setAPCallback([&](WiFiManager *wifiManager)
{ {
showSetupQr(softAP_SSID, softAP_password); showSetupQr(softAP_SSID, softAP_password);

View file

@ -23,13 +23,13 @@ void HalvingCountdownScreen::showScreen()
int mins = floor(minutesToHalving - (years * 525600) - (days * (24*60)) - (hours * 60)); int mins = floor(minutesToHalving - (years * 525600) - (days * (24*60)) - (hours * 60));
// int secs = floor((minutesToHalving - (years * 525600) - (days * (24*60)) - (hours * 60) - mins) * 60); // int secs = floor((minutesToHalving - (years * 525600) - (days * (24*60)) - (hours * 60) - mins) * 60);
epdContent[0] = "BIT/COIN"; epdContent[0] = F("BIT/COIN");
epdContent[1] = "HALV/ING"; epdContent[1] = F("HALV/ING");
epdContent[2] = String(years) + "/YRS"; epdContent[2] = String(years) + "/YRS";
epdContent[3] = String(days) + "/DAYS"; epdContent[3] = String(days) + "/DAYS";
epdContent[4] = String(hours) + "/HRS"; epdContent[4] = String(hours) + "/HRS";
epdContent[5] = String(mins) + "/MINS"; epdContent[5] = String(mins) + "/MINS";
epdContent[6] = "TO/GO"; epdContent[6] = F("TO/GO");
} }
uint HalvingCountdownScreen::getNextHalvingBlockNr() uint HalvingCountdownScreen::getNextHalvingBlockNr()

View file

@ -19,25 +19,28 @@ void checkBitcoinBlock(void *pvParameters)
{ {
int blockHeight = preferences.getUInt("blockHeight", currentBlockHeight); 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)); useBitcoind = preferences.getBool("useNode", false) && wifiClientInsecure.connect(preferences.getString("rpcHost", BITCOIND_HOST).c_str(), preferences.getUInt("rpcPort", BITCOIND_PORT));
if (useBitcoind) if (useBitcoind)
Serial.println("bitcoind node is reachable, using this for blocks."); Serial.println("bitcoind node is reachable, using this for blocks.");
else else
Serial.println("bitcoind node is not reachable, using mempool API instead."); Serial.println("bitcoind node is not reachable, using mempool API instead.");
for (;;) for (;;)
{ {
HTTPClient http;
http.setUserAgent(USER_AGENT);
if (useBitcoind) if (useBitcoind)
{ {
StaticJsonDocument<200> jsonDoc; StaticJsonDocument<200> jsonDoc;
http.begin(preferences.getString("rpcHost", BITCOIND_HOST).c_str(), preferences.getUInt("rpcPort", BITCOIND_PORT)); http.begin(preferences.getString("rpcHost", BITCOIND_HOST).c_str(), preferences.getUInt("rpcPort", BITCOIND_PORT));
http.addHeader("Content-Type", "application/json"); 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\":[]}"; const 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 authEncoded = base64::encode(preferences.getString("rpcUser", BITCOIND_RPC_USER) + ":" + preferences.getString("rpcPass", BITCOIND_RPC_PASS));
http.addHeader("Authorization", "Basic " + authEncoded); http.addHeader("Authorization", "Basic " + authEncoded);
int httpCode = http.POST(payload); int httpCode = http.POST(payload);
@ -57,7 +60,6 @@ void checkBitcoinBlock(void *pvParameters)
else else
{ {
http.begin("https://" + preferences.getString("mempoolInstance", DEFAULT_MEMPOOL_INSTANCE) + "/api/blocks/tip/height"); http.begin("https://" + preferences.getString("mempoolInstance", DEFAULT_MEMPOOL_INSTANCE) + "/api/blocks/tip/height");
http.addHeader("User-Agent", "BTClock/1.0");
int httpCode = http.GET(); int httpCode = http.GET();
if (httpCode > 0 && httpCode == HTTP_CODE_OK) if (httpCode > 0 && httpCode == HTTP_CODE_OK)
@ -68,7 +70,9 @@ void checkBitcoinBlock(void *pvParameters)
else else
{ {
Serial.print(F("Error in HTTP request to mempool API: ")); Serial.print(F("Error in HTTP request to mempool API: "));
Serial.print(httpCode);
Serial.println(http.errorToString(httpCode)); Serial.println(http.errorToString(httpCode));
} }
http.end(); http.end();
@ -82,6 +86,7 @@ void checkBitcoinBlock(void *pvParameters)
currentBlockHeight = blockHeight; currentBlockHeight = blockHeight;
preferences.putUInt("blockHeight", currentBlockHeight); preferences.putUInt("blockHeight", currentBlockHeight);
} }
vTaskDelay(pdMS_TO_TICKS(BLOCKNOTIFY_WAIT_TIME)); // wait 1 minute before checking again vTaskDelay(pdMS_TO_TICKS(BLOCKNOTIFY_WAIT_TIME)); // wait 1 minute before checking again
} }
} }

View file

@ -13,12 +13,13 @@ TaskHandle_t getPriceTaskHandle;
void taskGetPrice(void *pvParameters) void taskGetPrice(void *pvParameters)
{ {
HTTPClient http;
for (;;) for (;;)
{ {
HTTPClient http;
http.setUserAgent(USER_AGENT);
// Send HTTP request to CoinDesk API // Send HTTP request to CoinDesk API
http.begin(apiUrl); http.begin(apiUrl);
http.addHeader("User-Agent", "BTClock/1.0");
int httpCode = http.GET(); int httpCode = http.GET();