Compare commits

..

4 commits

Author SHA1 Message Date
Djuri Baars
957a947bc5 More clean-up and bugfixes
All checks were successful
BTClock CI / build (push) Successful in 14m23s
BTClock CI / merge (map[name:btclock_rev_b version:esp32s3], 213epd) (push) Successful in 20s
BTClock CI / merge (map[name:btclock_v8 version:esp32s3], 213epd) (push) Successful in 27s
BTClock CI / merge (map[name:lolin_s3_mini version:esp32s3], 29epd) (push) Successful in 25s
BTClock CI / merge (map[name:lolin_s3_mini version:esp32s3], 213epd) (push) Successful in 19s
BTClock CI / release (push) Successful in 36s
2024-12-21 17:53:35 +01:00
Djuri Baars
c44626cb42 Unclutter main.cpp 2024-12-21 17:46:10 +01:00
Djuri Baars
4fdd6b6b4f Fix button handling 2024-12-21 17:36:07 +01:00
Djuri Baars
8a818c66a0 Update readme
All checks were successful
BTClock CI / build (push) Successful in 10m20s
BTClock CI / merge (map[name:btclock_rev_b version:esp32s3], 213epd) (push) Successful in 20s
BTClock CI / merge (map[name:btclock_v8 version:esp32s3], 213epd) (push) Successful in 27s
BTClock CI / merge (map[name:lolin_s3_mini version:esp32s3], 29epd) (push) Successful in 25s
BTClock CI / merge (map[name:lolin_s3_mini version:esp32s3], 213epd) (push) Successful in 19s
BTClock CI / release (push) Successful in 35s
2024-12-21 01:15:59 +01:00
9 changed files with 190 additions and 208 deletions

View file

@ -16,21 +16,21 @@ Biggest differences with v2 are:
New features:
- BitAxe integration
- Zap notifier
- Braiins Pool and Ocean mining stats integration
- Nostr Zap notifier
- Multiple mining pool stats integrations
"Steal focus on new block" means that when a new block is mined, the display will switch to the block height screen if it's not on it already.
Most [information](https://github.com/btclock/btclock_v2/wiki) about BTClock v2 is still valid for this version.
See the [docs](https://git.btclock.dev/btclock/docs) repo for more information and building instructions.
**NOTE**: The software assumes that the hardware is run in a controlled private network. ~~The Web UI and the OTA update mechanism are not password protected and accessible to anyone in the network. Also, since the device only fetches numbers through WebSockets it will skip server certificate verification to save resources.~~ Since 3.2.0 the WebUI is password protectable and all certificates are verified. OTA update mechanism is not password-protected.
## Building
Use PlatformIO to build it yourself. Make sure you fetch the [WebUI](https://github.com/btclock/webui) submodule.
Use PlatformIO to build it yourself. Make sure you fetch the [WebUI](https://git.btclock.dev/btclock/webui) submodule.
## Braiins Pool and Ocean integration
## Mining pool stats
Enable mining pool stats by accessing your btclock's web UI (point a web browser at the device's IP address).
Under Settings -> Extra Features: toggle Enable Mining Pool Stats.
@ -41,6 +41,8 @@ The Mining Pool Earnings screen displays:
* Braiins: Today's mining reward thus far
* Ocean: Your estimated earnings if the pool were to find a block right now
For solo mining pools, there are no earning estimations. Your username is the onchain withdrawal address, without the worker name.
### Braiins Pool integration
Create an API key based on the steps [here](https://academy.braiins.com/en/braiins-pool/monitoring/#api-configuration).

View file

@ -54,7 +54,7 @@ void taskBitaxeFetch(void *pvParameters)
void setupBitaxeFetchTask()
{
xTaskCreate(taskBitaxeFetch, "bitaxeFetch", (6 * 1024), NULL, tskIDLE_PRIORITY,
xTaskCreate(taskBitaxeFetch, "bitaxeFetch", (3 * 1024), NULL, tskIDLE_PRIORITY,
&bitaxeFetchTaskHandle);
xTaskNotifyGive(bitaxeFetchTaskHandle);

View file

@ -19,41 +19,52 @@ TickType_t lastDebounceTime = 0;
void buttonTask(void *parameter) {
while (1) {
ulTaskNotifyTake(pdTRUE, portMAX_DELAY);
std::lock_guard<std::mutex> lock(mcpMutex);
TickType_t currentTime = xTaskGetTickCount();
if ((currentTime - lastDebounceTime) >= debounceDelay) {
lastDebounceTime = currentTime;
if (!digitalRead(MCP_INT_PIN)) {
uint pin = mcp1.getInterruptFlagRegister();
std::lock_guard<std::mutex> lock(mcpMutex);
switch (pin) {
case BTN_1:
toggleTimerActive();
break;
case BTN_2:
nextScreen();
break;
case BTN_3:
previousScreen();
break;
case BTN_4:
showSystemStatusScreen();
break;
if (!digitalRead(MCP_INT_PIN)) {
uint16_t intFlags = mcp1.getInterruptFlagRegister();
uint16_t intCap = mcp1.getInterruptCaptureRegister();
// Check each button individually
if (intFlags & BTN_1) handleButton1();
if (intFlags & BTN_2) handleButton2();
if (intFlags & BTN_3) handleButton3();
if (intFlags & BTN_4) handleButton4();
}
}
mcp1.getInterruptCaptureRegister();
} else {
}
// Very ugly, but for some reason this is necessary
// Clear interrupt state
while (!digitalRead(MCP_INT_PIN)) {
std::lock_guard<std::mutex> lock(mcpMutex);
mcp1.getInterruptCaptureRegister();
delay(1); // Small delay to prevent tight loop
}
}
}
// Helper functions to handle each button
void handleButton1() {
toggleTimerActive();
}
void handleButton2() {
nextScreen();
}
void handleButton3() {
previousScreen();
}
void handleButton4() {
showSystemStatusScreen();
}
void IRAM_ATTR handleButtonInterrupt() {
BaseType_t xHigherPriorityTaskWoken = pdFALSE;
xTaskNotifyFromISR(buttonTaskHandle, 0, eNoAction, &xHigherPriorityTaskWoken);

View file

@ -8,6 +8,13 @@
extern TaskHandle_t buttonTaskHandle;
// Task and setup functions
void buttonTask(void *pvParameters);
void IRAM_ATTR handleButtonInterrupt();
void setupButtonTask();
// Individual button handlers
void handleButton1();
void handleButton2();
void handleButton3();
void handleButton4();

View file

@ -466,32 +466,34 @@ void setupHardware()
Wire.begin(I2C_SDA_PIN, I2C_SCK_PIN, 400000);
if (!mcp1.begin())
{
if (!mcp1.begin()) {
Serial.println(F("Error MCP23017 1"));
// while (1)
// ;
}
else
{
} else {
pinMode(MCP_INT_PIN, INPUT_PULLUP);
// mcp1.setupInterrupts(false, false, LOW);
mcp1.enableControlRegister(MCP23x17_IOCR_ODR);
mcp1.mirrorInterrupts(true);
// Enable mirrored interrupts (both INTA and INTB pins signal any interrupt)
if (!mcp1.mirrorInterrupts(true)) {
Serial.println(F("Error setting up mirrored interrupts"));
}
for (int i = 0; i < 4; i++)
{
mcp1.pinMode1(i, INPUT_PULLUP);
mcp1.enableInterrupt(i, LOW);
// Configure all 4 button pins as inputs with pullups and interrupts
for (int i = 0; i < 4; i++) {
if (!mcp1.pinMode1(i, INPUT_PULLUP)) {
Serial.printf("Error setting pin %d to input pull up\n", i);
}
#ifndef IS_BTCLOCK_V8
for (int i = 8; i <= 14; i++)
{
mcp1.pinMode1(i, OUTPUT);
// Enable interrupt on CHANGE for each pin
if (!mcp1.enableInterrupt(i, CHANGE)) {
Serial.printf("Error enabling interrupt for pin %d\n", i);
}
#endif
}
// Set interrupt pins as open drain with active-low polarity
if (!mcp1.setInterruptPolarity(2)) { // 2 = Open drain
Serial.println(F("Error setting interrupt polarity"));
}
// Clear any pending interrupts
mcp1.getInterruptCaptureRegister();
}
#ifdef IS_HW_REV_B

View file

@ -149,25 +149,6 @@ void forceFullRefresh()
}
}
void refreshFromMemory()
{
for (uint i = 0; i < NUM_SCREENS; i++)
{
int *taskParam = new int;
*taskParam = i;
xTaskCreate(
[](void *pvParameters)
{
const int epdIndex = *(int *)pvParameters;
delete (int *)pvParameters;
displays[epdIndex].refresh(false);
vTaskDelete(NULL);
},
"PrepareUpd", 4096, taskParam, tskIDLE_PRIORITY, NULL);
}
}
void setupDisplays()
{
std::lock_guard<std::mutex> lockMcp(mcpMutex);

View file

@ -26,7 +26,6 @@ typedef struct {
} UpdateDisplayTaskItem;
void forceFullRefresh();
void refreshFromMemory();
void setupDisplays();
void splitText(const uint dispNum, const String &top, const String &bottom,

View file

@ -93,7 +93,7 @@ void setupMiningPoolStatsFetchTask()
"logoDownload",
(6 * 1024),
NULL,
12,
tskIDLE_PRIORITY,
NULL);
xTaskCreate(taskMiningPoolStatsFetch,

View file

@ -22,152 +22,132 @@
uint wifiLostConnection;
uint priceNotifyLostConnection = 0;
uint blockNotifyLostConnection = 0;
// char ptrTaskList[1500];
extern "C" void app_main()
{
initArduino();
int64_t getUptime() {
return esp_timer_get_time() / 1000000;
}
Serial.begin(115200);
setup();
void handlePriceNotifyDisconnection() {
if (priceNotifyLostConnection == 0) {
priceNotifyLostConnection = getUptime();
Serial.println(F("Lost price notification connection, trying to reconnect..."));
}
while (true)
{
// vTaskList(ptrTaskList);
// Serial.println(F("**********************************"));
// Serial.println(F("Task State Prio Stack Num"));
// Serial.println(F("**********************************"));
// Serial.print(ptrTaskList);
// Serial.println(F("**********************************"));
if (eventSourceTaskHandle != NULL)
xTaskNotifyGive(eventSourceTaskHandle);
if ((getUptime() - priceNotifyLostConnection) > 300) { // 5 minutes timeout
Serial.println(F("Price notification connection lost for 5 minutes, restarting handler..."));
restartPriceNotify();
priceNotifyLostConnection = 0;
}
}
int64_t currentUptime = esp_timer_get_time() / 1000000;
;
void handleBlockNotifyDisconnection() {
if (blockNotifyLostConnection == 0) {
blockNotifyLostConnection = getUptime();
Serial.println(F("Lost block notification connection, trying to reconnect..."));
}
if (!getIsOTAUpdating())
{
if ((getUptime() - blockNotifyLostConnection) > 300) { // 5 minutes timeout
Serial.println(F("Block notification connection lost for 5 minutes, restarting handler..."));
restartBlockNotify();
blockNotifyLostConnection = 0;
}
}
void handleFrontlight() {
#ifdef HAS_FRONTLIGHT
if (hasLightLevel()) {
if (preferences.getUInt("luxLightToggle", DEFAULT_LUX_LIGHT_TOGGLE) != 0)
{
if (hasLightLevel() && getLightLevel() <= 1 && preferences.getBool("flOffWhenDark", DEFAULT_FL_OFF_WHEN_DARK))
{
if (frontlightIsOn()) {
frontlightFadeOutAll();
}
}
else if (hasLightLevel() && getLightLevel() < preferences.getUInt("luxLightToggle", DEFAULT_LUX_LIGHT_TOGGLE) && !frontlightIsOn())
{
if (hasLightLevel() && preferences.getUInt("luxLightToggle", DEFAULT_LUX_LIGHT_TOGGLE) != 0) {
uint lightLevel = getLightLevel();
uint luxThreshold = preferences.getUInt("luxLightToggle", DEFAULT_LUX_LIGHT_TOGGLE);
if (lightLevel <= 1 && preferences.getBool("flOffWhenDark", DEFAULT_FL_OFF_WHEN_DARK)) {
if (frontlightIsOn()) frontlightFadeOutAll();
} else if (lightLevel < luxThreshold && !frontlightIsOn()) {
frontlightFadeInAll();
}
else if (frontlightIsOn() && getLightLevel() > preferences.getUInt("luxLightToggle", DEFAULT_LUX_LIGHT_TOGGLE))
{
} else if (frontlightIsOn() && lightLevel > luxThreshold) {
frontlightFadeOutAll();
}
}
}
#endif
if (!WiFi.isConnected())
{
if (!wifiLostConnection)
{
wifiLostConnection = currentUptime;
Serial.println(F("Lost WiFi connection, trying to reconnect..."));
}
if ((currentUptime - wifiLostConnection) > 600)
{
void checkWiFiConnection() {
if (!WiFi.isConnected()) {
if (!wifiLostConnection) {
wifiLostConnection = getUptime();
Serial.println(F("Lost WiFi connection, trying to reconnect..."));
}
if ((getUptime() - wifiLostConnection) > 600) {
Serial.println(F("Still no connection after 10 minutes, restarting..."));
delay(2000);
ESP.restart();
}
WiFi.begin();
}
else if (wifiLostConnection)
{
} else if (wifiLostConnection) {
wifiLostConnection = 0;
Serial.println(F("Connection restored, reset timer."));
}
if (getPriceNotifyInit() && !preferences.getBool("fetchEurPrice", DEFAULT_FETCH_EUR_PRICE) && !isPriceNotifyConnected())
{
priceNotifyLostConnection++;
Serial.println(F("Lost price data connection..."));
queueLedEffect(LED_DATA_PRICE_ERROR);
// if price WS connection does not come back after 6*5 seconds, destroy and recreate
if (priceNotifyLostConnection > 6)
{
Serial.println(F("Restarting price handler..."));
restartPriceNotify();
// setupPriceNotify();
priceNotifyLostConnection = 0;
}
}
else if (priceNotifyLostConnection > 0 && isPriceNotifyConnected())
{
priceNotifyLostConnection = 0;
}
if (getBlockNotifyInit() && !isBlockNotifyConnected())
{
blockNotifyLostConnection++;
Serial.println(F("Lost block data connection..."));
queueLedEffect(LED_DATA_BLOCK_ERROR);
// if mempool WS connection does not come back after 6*5 seconds, destroy and recreate
if (blockNotifyLostConnection > 6)
{
Serial.println(F("Restarting block handler..."));
restartBlockNotify();
// setupBlockNotify();
blockNotifyLostConnection = 0;
}
}
else if (blockNotifyLostConnection > 0 && isBlockNotifyConnected())
{
blockNotifyLostConnection = 0;
}
// if more than 5 price updates are missed, there is probably something wrong, reconnect
if ((getLastPriceUpdate(CURRENCY_USD) - currentUptime) > (preferences.getUInt("minSecPriceUpd", DEFAULT_SECONDS_BETWEEN_PRICE_UPDATE) * 5))
{
Serial.println(F("Detected 5 missed price updates... restarting price handler."));
restartPriceNotify();
// setupPriceNotify();
priceNotifyLostConnection = 0;
}
// If after 45 minutes no mempool blocks, check the rest API
if ((getLastBlockUpdate() - currentUptime) > 45 * 60)
{
void checkMissedBlocks() {
Serial.println(F("Long time (45 min) since last block, checking if I missed anything..."));
int currentBlock = getBlockFetch();
if (currentBlock != -1)
{
if (currentBlock != getBlockHeight())
{
if (currentBlock != -1) {
if (currentBlock != getBlockHeight()) {
Serial.println(F("Detected stuck block height... restarting block handler."));
// Mempool source stuck, restart
restartBlockNotify();
// setupBlockNotify();
}
// set last block update so it doesn't fetch for 45 minutes
setLastBlockUpdate(currentUptime);
setLastBlockUpdate(getUptime());
}
}
if (currentUptime - getLastTimeSync() > 24 * 60 * 60)
{
void monitorDataConnections() {
// Price notification monitoring
if (getPriceNotifyInit() && !preferences.getBool("fetchEurPrice", DEFAULT_FETCH_EUR_PRICE) && !isPriceNotifyConnected()) {
handlePriceNotifyDisconnection();
} else if (priceNotifyLostConnection > 0 && isPriceNotifyConnected()) {
priceNotifyLostConnection = 0;
}
// Block notification monitoring
if (getBlockNotifyInit() && !isBlockNotifyConnected()) {
handleBlockNotifyDisconnection();
} else if (blockNotifyLostConnection > 0 && isBlockNotifyConnected()) {
blockNotifyLostConnection = 0;
}
// Check for missed price updates
if ((getLastPriceUpdate(CURRENCY_USD) - getUptime()) > (preferences.getUInt("minSecPriceUpd", DEFAULT_SECONDS_BETWEEN_PRICE_UPDATE) * 5)) {
Serial.println(F("Detected 5 missed price updates... restarting price handler."));
restartPriceNotify();
priceNotifyLostConnection = 0;
}
// Check for missed blocks
if ((getLastBlockUpdate() - getUptime()) > 45 * 60) {
checkMissedBlocks();
}
}
extern "C" void app_main() {
initArduino();
Serial.begin(115200);
setup();
while (true) {
if (eventSourceTaskHandle != NULL) {
xTaskNotifyGive(eventSourceTaskHandle);
}
if (!getIsOTAUpdating()) {
handleFrontlight();
checkWiFiConnection();
monitorDataConnections();
if (getUptime() - getLastTimeSync() > 24 * 60 * 60) {
Serial.println(F("Last time update is longer than 24 hours ago, sync again"));
syncTime();
};
}
}
vTaskDelay(pdMS_TO_TICKS(5000));