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_BG_COLOR GxEPD_BLACK
#define USER_AGENT "BTClock/1.0"
#define I2C_DEV_ADDR 0x55

View File

@ -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);

View File

@ -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()

View File

@ -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
}
}

View File

@ -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();