Add BH1750 lux sensor support

This commit is contained in:
Djuri 2024-06-29 02:19:25 +02:00
parent a9489c30f6
commit fb67893f85
10 changed files with 196 additions and 98 deletions

View file

@ -22,8 +22,7 @@
uint wifiLostConnection;
uint priceNotifyLostConnection = 0;
uint blockNotifyLostConnection = 0;
//char ptrTaskList[1500];
// char ptrTaskList[1500];
extern "C" void app_main()
{
@ -46,103 +45,129 @@ extern "C" void app_main()
int64_t currentUptime = esp_timer_get_time() / 1000000;
;
if (!WiFi.isConnected())
if (!getIsOTAUpdating())
{
if (!wifiLostConnection)
#ifdef HAS_FRONTLIGHT
if (preferences.getUInt("luxLightToggle", 128) != 0)
{
wifiLostConnection = currentUptime;
Serial.println(F("Lost WiFi connection, trying to reconnect..."));
if (hasLightLevel() && getLightLevel() == 0)
{
if (frontlightIsOn()) {
frontlightFadeOutAll();
}
}
else if (hasLightLevel() && getLightLevel() < preferences.getUInt("luxLightToggle", 128) && !frontlightIsOn())
{
frontlightFadeInAll();
}
else if (frontlightIsOn() && getLightLevel() > preferences.getUInt("luxLightToggle", 128))
{
frontlightFadeOutAll();
}
}
if ((currentUptime - wifiLostConnection) > 600)
#endif
if (!WiFi.isConnected())
{
Serial.println(F("Still no connection after 10 minutes, restarting..."));
delay(2000);
ESP.restart();
if (!wifiLostConnection)
{
wifiLostConnection = currentUptime;
Serial.println(F("Lost WiFi connection, trying to reconnect..."));
}
if ((currentUptime - wifiLostConnection) > 600)
{
Serial.println(F("Still no connection after 10 minutes, restarting..."));
delay(2000);
ESP.restart();
}
WiFi.begin();
}
else if (wifiLostConnection)
{
wifiLostConnection = 0;
Serial.println(F("Connection restored, reset timer."));
}
WiFi.begin();
}
else if (wifiLostConnection)
{
wifiLostConnection = 0;
Serial.println(F("Connection restored, reset timer."));
}
if (getPriceNotifyInit() && !preferences.getBool("fetchEurPrice", false) && !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)
if (getPriceNotifyInit() && !preferences.getBool("fetchEurPrice", false) && !isPriceNotifyConnected())
{
Serial.println(F("Restarting price handler..."));
priceNotifyLostConnection++;
Serial.println(F("Lost price data connection..."));
queueLedEffect(LED_DATA_PRICE_ERROR);
restartPriceNotify();
// setupPriceNotify();
// 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;
}
} 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)
if (getBlockNotifyInit() && !isBlockNotifyConnected())
{
Serial.println(F("Restarting block handler..."));
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();
restartBlockNotify();
// setupBlockNotify();
blockNotifyLostConnection = 0;
}
}
else if (blockNotifyLostConnection > 0 && isBlockNotifyConnected())
{
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() - 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)
{
Serial.println(F("Long time (45 min) since last block, checking if I missed anything..."));
int currentBlock = getBlockFetch();
if (currentBlock != -1)
// if more than 5 price updates are missed, there is probably something wrong, reconnect
if ((getLastPriceUpdate() - currentUptime) > (preferences.getUInt("minSecPriceUpd", DEFAULT_SECONDS_BETWEEN_PRICE_UPDATE) * 5))
{
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);
}
}
Serial.println(F("Detected 5 missed price updates... restarting price handler."));
if (currentUptime - getLastTimeSync() > 24 * 60 * 60) {
Serial.println(F("Last time update is longer than 24 hours ago, sync again"));
syncTime();
};
restartPriceNotify();
// setupPriceNotify();
priceNotifyLostConnection = 0;
}
// If after 45 minutes no mempool blocks, check the rest API
if ((getLastBlockUpdate() - currentUptime) > 45 * 60)
{
Serial.println(F("Long time (45 min) since last block, checking if I missed anything..."));
int currentBlock = getBlockFetch();
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);
}
}
if (currentUptime - getLastTimeSync() > 24 * 60 * 60)
{
Serial.println(F("Last time update is longer than 24 hours ago, sync again"));
syncTime();
};
}
vTaskDelay(pdMS_TO_TICKS(5000));
}