diff --git a/CO2_Gadget.ino b/CO2_Gadget.ino index 15cc7eae..18951070 100644 --- a/CO2_Gadget.ino +++ b/CO2_Gadget.ino @@ -85,6 +85,7 @@ bool inMenu = false; bool shouldWakeUpDisplay = false; bool shouldRedrawDisplay = false; uint16_t measurementInterval = 10; +uint16_t sampleInterval = 60; bool bleInitialized = false; int8_t selectedCO2Sensor = -1; bool outputsModeRelay = false; @@ -311,6 +312,7 @@ void wakeUpDisplay() { } void processPendingCommands() { + if (isDownloadingBLE) return; if (pendingCalibration == true) { if (calibrationValue != 0) { printf("-->[MAIN] Calibrating CO2 sensor at %d PPM\n", calibrationValue); @@ -398,6 +400,7 @@ void outputsRGBLeds() { } void outputsLoop() { + if (isDownloadingBLE) return; outputsRelays(); outputsRGBLeds(); neopixelLoop(); @@ -405,6 +408,7 @@ void outputsLoop() { } void readingsLoop() { + if (isDownloadingBLE) return; if (esp_timer_get_time() - lastReadingsCommunicationTime >= startCheckingAfterUs) { if (newReadingsAvailable) { lastReadingsCommunicationTime = esp_timer_get_time(); @@ -430,6 +434,7 @@ void readingsLoop() { void adjustBrightnessLoop() { #if defined(SUPPORT_OLED) || defined(SUPPORT_TFT) + if (isDownloadingBLE) return; if (shouldWakeUpDisplay) { wakeUpDisplay(); @@ -508,6 +513,7 @@ void setCpuFrequencyAndReinitSerial(int16_t newCpuFrequency) { } void utilityLoop() { + if (isDownloadingBLE) return; int16_t actualCPUFrequency = getCpuFrequencyMhz(); const int16_t highCpuFrequency = 240; const int16_t lowCpuFrequency = 80; diff --git a/CO2_Gadget_BLE.h b/CO2_Gadget_BLE.h index b5077633..c87259b7 100644 --- a/CO2_Gadget_BLE.h +++ b/CO2_Gadget_BLE.h @@ -14,7 +14,15 @@ DataProvider provider(lib, DataType::T_RH_CO2_ALT, true, true, true, &wifi); void setBLEHistoryInterval(uint64_t interval) { #ifdef SUPPORT_BLE - provider.setHistoryInterval(interval * 1000); + if (provider.getHistoryInterval() != interval * 1000) { +#ifdef DEBUG_BLE + Serial.flush(); + Serial.println("-->[BLE ] Actual history interval: " + String(provider.getHistoryInterval() / 1000) + " seconds"); + Serial.println("-->[BLE ] Setting history interval to: " + String(interval) + " seconds"); + delay(20); +#endif + provider.setHistoryInterval(interval * 1000); + } #endif } @@ -23,15 +31,15 @@ void initBLE() { if (activeBLE) { if (bleInitialized) { Serial.print( - "-->[SBLE] Sensirion Gadget BLE Lib already initialized with deviceId = "); + "-->[BLE ] Sensirion Gadget BLE Lib already initialized with deviceId = "); Serial.println(provider.getDeviceIdString()); return; } else { - setBLEHistoryInterval(60); + setBLEHistoryInterval(sampleInterval); provider.begin(); - Serial.print("-->[SBLE] Sensirion Gadget BLE Lib initialized with deviceId = "); + Serial.print("-->[BLE ] Sensirion Gadget BLE Lib initialized with deviceId = "); Serial.println(provider.getDeviceIdString()); - Serial.print("-->[SBLE] History interval set to: "); + Serial.print("-->[BLE ] History interval set to: "); Serial.print(provider.getHistoryInterval() / 1000); Serial.println(" seconds"); // Set initial battery level @@ -128,6 +136,7 @@ void BLELoop() { } provider.handleDownload(); isDownloadingBLE = provider.isDownloading(); + if (isDownloadingBLE) return; // delay(3); if (provider.wifiChanged()) handleBLEwifiChanged(); if (provider.historyIntervalChanged()) { diff --git a/CO2_Gadget_Battery.h b/CO2_Gadget_Battery.h index a4067b81..3581b0ad 100644 --- a/CO2_Gadget_Battery.h +++ b/CO2_Gadget_Battery.h @@ -61,6 +61,7 @@ void readBatteryVoltage() { } void batteryLoop() { + if (isDownloadingBLE) return; readBatteryVoltage(); if (!inMenu) { if (abs(lastBatteryVoltage - batteryVoltage) >= 0.1) { // If battery voltage changed by at least 0.1V, update battery level diff --git a/CO2_Gadget_Improv.h b/CO2_Gadget_Improv.h index a6c6618f..3989ea86 100644 --- a/CO2_Gadget_Improv.h +++ b/CO2_Gadget_Improv.h @@ -46,6 +46,7 @@ void initImprov() { } void improvLoop() { + if (isDownloadingBLE) return; if (inMenu) { // If in menu ignore Improv functionality #ifdef DEBUG_IMPROV_WIFI static unsigned long lastPrintTime = 0; diff --git a/CO2_Gadget_MQTT.h b/CO2_Gadget_MQTT.h index 63c9ce2d..aea83fb7 100644 --- a/CO2_Gadget_MQTT.h +++ b/CO2_Gadget_MQTT.h @@ -321,6 +321,7 @@ void publishMQTT() { void mqttClientLoop() { #ifdef SUPPORT_MQTT + if (isDownloadingBLE) return; if (troubledMQTT && (millis() - timeTroubledMQTT >= timeToRetryTroubledMQTT * 1000)) { troubledMQTT = false; timeTroubledMQTT = 0; diff --git a/CO2_Gadget_Preferences.h b/CO2_Gadget_Preferences.h index 6254da3d..ed2ec3df 100644 --- a/CO2_Gadget_Preferences.h +++ b/CO2_Gadget_Preferences.h @@ -200,6 +200,7 @@ void printPreferences() { Serial.println("-->[PREF] displayReverse is:\t#" + String(displayReverse ? "Reversed" : "Normal") + "# (" + String(displayReverse) + ")"); Serial.println("-->[PREF] showFahrenheit is:\t#" + String(showFahrenheit ? "Fahrenheit" : "Celsius") + "#"); Serial.println("-->[PREF] measInterval:\t #" + String(measurementInterval) + "#"); + Serial.println("-->[PREF] sampInterval:\t #" + String(sampleInterval) + "#"); Serial.println("-->[PREF] outModeRelay is:\t#" + String(outputsModeRelay ? "Relay" : "RGB LED") + "#"); Serial.println("-->[PREF] channelESPNow:\t #" + String(channelESPNow) + "#"); Serial.println("-->[PREF] boardIdESPNow:\t #" + String(boardIdESPNow) + "#"); @@ -291,6 +292,7 @@ void initPreferences() { displayReverse = preferences.getBool("displayReverse", false); showFahrenheit = preferences.getBool("showFahrenheit", false); measurementInterval = preferences.getUInt("measInterval", 10); + sampleInterval = preferences.getUInt("sampInterval", 60); outputsModeRelay = preferences.getBool("outModeRelay", false); channelESPNow = preferences.getUInt("channelESPNow", ESPNOW_WIFI_CH); boardIdESPNow = preferences.getUInt("boardIdESPNow", 0); @@ -394,6 +396,7 @@ void putPreferences() { preferences.putBool("displayReverse", displayReverse); preferences.putBool("showFahrenheit", showFahrenheit); preferences.putUInt("measInterval", measurementInterval); + preferences.putUInt("sampInterval", sampleInterval); preferences.putBool("outModeRelay", outputsModeRelay); preferences.putUInt("channelESPNow", channelESPNow); preferences.putUInt("boardIdESPNow", boardIdESPNow); @@ -490,6 +493,7 @@ String getPreferencesAsJson() { doc["showCO2"] = preferences.getBool("showCO2", true); doc["showPM25"] = preferences.getBool("showPM25", true); doc["measInterval"] = preferences.getInt("measInterval", 10); + doc["sampInterval"] = preferences.getInt("sampInterval", 60); // Buzzer preferences doc["toneBzrBeep"] = preferences.getUInt("toneBzrBeep", 1000); // Buzzer frequency @@ -570,6 +574,7 @@ String getActualSettingsAsJson() { doc["showCO2"] = displayShowCO2; doc["showPM25"] = displayShowPM25; doc["measInterval"] = measurementInterval; + doc["sampInterval"] = sampleInterval; // Buzzer preferences doc["toneBzrBeep"] = toneBuzzerBeep; // Buzzer frequency @@ -646,6 +651,7 @@ bool handleSavePreferencesfromJSON(String jsonPreferences) { displayReverse = JsonDocument["displayReverse"]; showFahrenheit = JsonDocument["showFahrenheit"]; measurementInterval = JsonDocument["measurementInterval"]; + sampleInterval = JsonDocument["sampleInterval"]; outputsModeRelay = JsonDocument["outModeRelay"]; channelESPNow = JsonDocument["channelESPNow"]; boardIdESPNow = JsonDocument["boardIdESPNow"]; diff --git a/CO2_Gadget_Sensors.h b/CO2_Gadget_Sensors.h index df1e8c4d..7570ecb0 100644 --- a/CO2_Gadget_Sensors.h +++ b/CO2_Gadget_Sensors.h @@ -117,7 +117,7 @@ void initSensors() { sensors.setCO2AltitudeOffset(altitudeMeters); // sensors.setAutoSelfCalibration(false); // TO-DO: Implement in CanAirIO Sensors Lib - setBLEHistoryInterval(60); + setBLEHistoryInterval(sampleInterval); Serial.printf("-->[SENS] Selected CO2 Sensor: %d\n", selectedCO2Sensor); @@ -152,6 +152,7 @@ void initSensors() { } void sensorsLoop() { + if (isDownloadingBLE) return; if (!buzzerBeeping) { sensors.loop(); } diff --git a/CO2_Gadget_WIFI.h b/CO2_Gadget_WIFI.h index e557bc9f..e952fd6a 100644 --- a/CO2_Gadget_WIFI.h +++ b/CO2_Gadget_WIFI.h @@ -1065,6 +1065,7 @@ void initWifi() { } void wifiClientLoop() { + if (isDownloadingBLE) return; if (activeWIFI && troubledWIFI && (millis() - timeTroubledWIFI >= timeToRetryTroubledWIFI * 1000)) { initWifi(); } @@ -1091,6 +1092,7 @@ void wifiClientLoop() { void OTALoop() { #ifdef SUPPORT_OTA + if (isDownloadingBLE) return; if ((activeWIFI) && (activeOTA) && (!troubledWIFI) && (WiFi.status() == WL_CONNECTED)) { AsyncElegantOTA.loop(); }