diff -r bad3414f7bc4 -r 2b337dd92f25 main/iotbalkon.c --- a/main/iotbalkon.c Thu Mar 30 21:55:24 2023 +0200 +++ b/main/iotbalkon.c Fri Mar 31 20:31:12 2023 +0200 @@ -23,23 +23,27 @@ static TaskHandle_t xTaskWifi = NULL; #define MAX_LOOPS 32 +#define SUB_TIME 1000 float temperature; float pressure; -RTC_DATA_ATTR float solarVolts, solarCurrent, solarPower; -RTC_DATA_ATTR float batteryVolts, batteryCurrent, batteryPower; -RTC_DATA_ATTR int batteryState; -RTC_DATA_ATTR float s_Volts[MAX_LOOPS + 1]; -RTC_DATA_ATTR float s_Current[MAX_LOOPS + 1]; -RTC_DATA_ATTR float b_Volts[MAX_LOOPS + 1]; -RTC_DATA_ATTR float b_Current[MAX_LOOPS + 1]; -RTC_DATA_ATTR bool m_Valid[MAX_LOOPS + 1]; -RTC_DATA_ATTR unsigned long m_Time[MAX_LOOPS + 1]; -RTC_DATA_ATTR unsigned long gLastTime; // millis() +float solarVolts, solarCurrent, solarPower; +float batteryVolts, batteryCurrent, batteryPower; +int batteryState; +float s_Volts[MAX_LOOPS + 1]; +float s_Current[MAX_LOOPS + 1]; +float b_Volts[MAX_LOOPS + 1]; +float b_Current[MAX_LOOPS + 1]; +bool m_Valid[MAX_LOOPS + 1]; +uint64_t m_Time[MAX_LOOPS + 1]; +uint64_t gLastTime; +uint64_t gTimeNext; uint8_t loopno = 0; uint8_t loops = 0; +uint8_t ST_LOOPS = 6; +bool WorkAgain = false; int DisCounter = 0; extern BMP280_State *bmp280_state; ///< I2C state @@ -56,8 +60,8 @@ uint32_t Alarm = 0; /* - Alarm bits -*/ + * Alarm bits + */ #define AL_ACCULOW 0x01 #define AL_NOWIFI 0x02 @@ -171,6 +175,12 @@ +uint64_t millis(void) { + return esp_timer_get_time() / 1000; +} + + + /* * Read the temperature and pressure from the BMP280. */ @@ -240,10 +250,11 @@ } } - // m_Time[loopno] = millis() - gLastTime; - //gLastTime = millis(); + uint64_t ms = millis(); + m_Time[loopno] = ms - gLastTime; + gLastTime = ms; - ESP_LOGI(TAG, "Measure: %d Valid: %s Solar: %.4fV %.4fmA Battery: %.4fV %.4fmA time %ld", + ESP_LOGI(TAG, "Measure: %d Valid: %s Solar: %.4fV %.4fmA Battery: %.4fV %.4fmA time %llu", loopno, (m_Valid[loopno]) ? "true":"false", s_Volts[loopno], s_Current[loopno], b_Volts[loopno], b_Current[loopno], m_Time[loopno]); if (loopno < (MAX_LOOPS - 1)) @@ -254,6 +265,8 @@ void app_main(void) { + uint64_t totalTime, gTimeInMillis; + #ifdef CONFIG_CODE_PRODUCTION ESP_LOGI(TAG, "Starting production"); #endif @@ -261,6 +274,7 @@ ESP_LOGI(TAG, "Starting testing"); #endif + gLastTime = millis(); ESP_ERROR_CHECK(i2cdev_init()); bmp280_init_default_params(&bmp280_params); @@ -330,6 +344,9 @@ ESP_LOGI(TAG, "Switch to state %d", State); OldState = State; } + + gTimeInMillis = millis(); + switch (State) { case State_Init: getTempBaro(); // getLightValues(); @@ -342,6 +359,7 @@ case State_Connect: if (ready_WiFi() && ready_mqtt()) { State = State_Working; Alarm &= ~AL_NOWIFI; + ESP_LOGI(TAG, "Connected counter %d", DisCounter); DisCounter = 0; } else { DisCounter++; @@ -354,14 +372,14 @@ } break; - case State_Working: // WorkAgain = false; + case State_Working: WorkAgain = false; // Measure getVoltsCurrent(); solarVolts = solarCurrent = solarPower = batteryVolts = batteryCurrent = batteryPower = 0; - // loops = 0; - // totalTime = 0; - // for (int i = 0; i < loopno; i++) { + loops = 0; + totalTime = 0; + for (int i = 0; i < loopno; i++) { /* * If there are only 2 loops, and the flag that we came from deep-sleep is set * then assume the following: @@ -375,19 +393,19 @@ * 2. Take the last one, and weight for 5 seconds. * Calculate the average battery current from this. */ - // if (m_Valid[i]) { - // solarVolts += s_Volts[i]; - // solarCurrent += s_Current[i]; - // batteryVolts += b_Volts[i]; - // if (i == (loopno - 1)) { - // // Add the extra time - // m_Time[i] += SUB_TIME; - // } - // batteryCurrent += b_Current[i] * m_Time[i]; - // totalTime += m_Time[i]; - // loops++; - // } - // } + if (m_Valid[i]) { + solarVolts += s_Volts[i]; + solarCurrent += s_Current[i]; + batteryVolts += b_Volts[i]; + if (i == (loopno - 1)) { + // Add the extra time + m_Time[i] += SUB_TIME; + } + batteryCurrent += b_Current[i] * m_Time[i]; + totalTime += m_Time[i]; + loops++; + } + } // if (EEPROM.read(EM_DS_Active)) { // totalTime += EEPROM.read(EM_DS_Time) * 1000; @@ -396,49 +414,48 @@ // } // If valid measurements - // if (loops) { - // solarVolts = solarVolts / loops; - // solarCurrent = solarCurrent / loops; - // solarPower = solarVolts * solarCurrent; - // batteryVolts = batteryVolts / loops; - // batteryCurrent = batteryCurrent / totalTime; - // batteryPower = batteryVolts * batteryCurrent; - BatteryState(batteryVolts, (0 - batteryCurrent) + solarCurrent); + if (loops) { + solarVolts = solarVolts / loops; + solarCurrent = solarCurrent / loops; + solarPower = solarVolts * solarCurrent; + batteryVolts = batteryVolts / loops; + batteryCurrent = batteryCurrent / totalTime; + batteryPower = batteryVolts * batteryCurrent; + BatteryState(batteryVolts, (0 - batteryCurrent) + solarCurrent); - ESP_LOGI(TAG, " Solar Volts: %.4fV Current %.4fmA Power %.4fmW", solarVolts, solarCurrent, solarPower); - ESP_LOGI(TAG, "Battery Volts: %.4fV Current %.4fmA Power %.4fmW", batteryVolts, batteryCurrent, batteryPower); + ESP_LOGI(TAG, " Solar Volts: %.4fV Current %.4fmA Power %.4fmW", solarVolts, solarCurrent, solarPower); + ESP_LOGI(TAG, "Battery Volts: %.4fV Current %.4fmA Power %.4fmW", batteryVolts, batteryCurrent, batteryPower); - /* Check alarm conditions */ - if (batteryState <= 10) { - Alarm |= AL_ACCULOW; - } else { - Alarm &= ~AL_ACCULOW; - } - // } + /* Check alarm conditions */ + if (batteryState <= 10) { + Alarm |= AL_ACCULOW; + } else { + Alarm &= ~AL_ACCULOW; + } + } getTempBaro(); - // Publish(); - // State = State_WorkDone; - // gTimeNext = millis() + SUB_TIME; + publish(); + State = State_WorkDone; + gTimeNext = millis() + SUB_TIME; break; - case State_WorkDone: // Hang around for a while to process the subscriptions. - // client.loop(); - // delay(1); - // if (WorkAgain) { - // // Some command was executed. - // State = State_Working; - // } - // if (gTimeInMillis > gTimeNext) { - // State = State_Stop; - // } + case State_WorkDone: vTaskDelay(2 / portTICK_PERIOD_MS); + // Hang around for a while to process the subscriptions. + if (WorkAgain) { + // Some command was executed. + State = State_Working; + } + if (gTimeInMillis > gTimeNext) { + State = State_Stop; + } break; case State_Stop: request_WiFi(false); // // Reset values for average current measurement. // HaveIP = false; loopno = 0; - // gLastTime = millis(); - // delay(10); + gLastTime = millis(); + vTaskDelay(10 / portTICK_PERIOD_MS); // #if defined(ARDUINO_ESP8266_WEMOS_D1MINI) // WiFi.forceSleepBegin(0); // 0 == forever // #endif @@ -454,8 +471,10 @@ // } // // Active mode, 60 seconds loop - // ST_LOOPS = 6; - // gTimeNext = millis() + ST_INTERVAL; + ST_LOOPS = 6; + gTimeNext = millis() + ST_INTERVAL; + ESP_LOGI(TAG, "Start sleeploops"); + State = State_Wait; // #if Debug == true // Serial.println(F("Start sleeploops")); // #endif @@ -490,25 +509,30 @@ // #endif break; - case State_Wait: // if (gTimeInMillis > gTimeNext) { - // State = State_Measure; - // } + case State_Wait: if (gTimeInMillis > gTimeNext) { + State = State_Measure; + } + vTaskDelay(10 / portTICK_PERIOD_MS); break; case State_Measure: getVoltsCurrent(); // if (isnan(Temperature)) { // getTempHumi(); // } - // gTimeNext = millis() + ST_INTERVAL; - // if (loopno >= ST_LOOPS) { + gTimeNext = millis() + ST_INTERVAL; + if (loopno >= ST_LOOPS) { + ESP_LOGI(TAG, "Enough loops, do connect"); // getLightValues(); - // State = State_Connect; - // } else { - // State = State_Wait; - // } + State = State_Connect; + DisCounter = 0; + request_WiFi(true); + } else { + State = State_Wait; + } break; - case State_GoSleep: // ds_time = EEPROM.read(EM_DS_Time); + case State_GoSleep: ESP_LOGE(TAG, "Entered GoSleep -- not supported"); + // ds_time = EEPROM.read(EM_DS_Time); // #if Debug == true // Serial.printf("Going to deep-sleep for %d seconds\n", ds_time); // #endif