first pass at making the main loop a consistent time interval, and slight cleanup...
authorjweigele <jweigele@local>
Tue, 26 Mar 2024 19:19:43 +0000 (12:19 -0700)
committerjweigele <jweigele@local>
Tue, 26 Mar 2024 19:19:43 +0000 (12:19 -0700)
aqi/main/aqi.c

index 0dfae010a98fde1395679c27284114287f98956f..0acaad10cf015042265f9f8028ac422906e29f49 100644 (file)
@@ -758,10 +758,15 @@ static void co2_pwm_task(void* discard){
             xSemaphoreTake(report_data.mutex, portMAX_DELAY);
             report_data.co2_ppm = total_ppm;
             xSemaphoreGive(report_data.mutex);            
+
+            // just in case the block took awhile, reset the current time so the next sleep is appropriate
+            xLastWakeTime = xTaskGetTickCount();
+
             
             total_high = 0;
             total_low = 0;
             counter = 0;
+
         }
 
         // get the 
@@ -983,7 +988,7 @@ aqi_data_t get_average_pm25(uint8_t* data, int data_size){
     int pm10, pm25;
     while (cur_entry + 32 <= data + data_size){
         //printf("doing one iteration\n");
-        ESP_LOG_BUFFER_HEXDUMP(TAG, cur_entry, 32, ESP_LOG_INFO);
+        //ESP_LOG_BUFFER_HEXDUMP(TAG, cur_entry, 32, ESP_LOG_INFO);
         if (verify_checksum(cur_entry)){
             pm25 = (data[12] << 8) + (data[13]);
             pm10 = (data[10] << 8) + (data[11]);
@@ -999,7 +1004,6 @@ aqi_data_t get_average_pm25(uint8_t* data, int data_size){
         }
         cur_entry += 32;
     }
-    ESP_LOGI(TAG, "pm10_total %d pm25_total %d count %d", pm10_total, pm25_total, count);
     if (count != 0){
         retval.pm10 = ((float)pm10_total)/count;
         retval.pm25 = ((float)pm25_total)/count;
@@ -1429,6 +1433,11 @@ static void monitoring_task(void* discard)
     int64_t cur_uptime = 0;
     int64_t next_sleep_uptime = 0;
     int64_t remaining_time = 0;
+    TickType_t xLastWakeTime;
+    const TickType_t xFrequency = pdMS_TO_TICKS(10000); //1/portTICK_PERIOD_MS;
+            
+    // Initialise the xLastWakeTime variable with the current time.
+    xLastWakeTime = xTaskGetTickCount();    
 #if defined(CONFIG_WIFI_ENABLED) || defined(CONFIG_OT_ENABLED)
     esp_mqtt_client_handle_t queue_item;
 #endif
@@ -1466,11 +1475,9 @@ static void monitoring_task(void* discard)
             //ESP_LOGI(TAG, "Read %d bytes: '%s'", rxBytes, aqi_data);
             //ESP_LOG_BUFFER_HEXDUMP(TAG, aqi_data, rxBytes, ESP_LOG_INFO);
             aqi_data_t cur_avg = get_average_pm25(aqi_data, rxBytes);
-            ESP_LOGI(TAG, "average from last 10 seconds pm10: %f", cur_avg.pm10);                
-            ESP_LOGI(TAG, "average from last 10 seconds pm25: %f", cur_avg.pm25);
-            
+            ESP_LOGI(TAG, "average from last 10 seconds pm10: %f pm25: %f", cur_avg.pm10, cur_avg.pm25);                
+
             if (cur_avg.pm10 < MAX_PM10 && cur_avg.pm25 < MAX_PM25 ){
-                ESP_LOGI(TAG, "setting attrib value and sending, I guess");
                 cur_pm = new_pm_average(cur_avg);
             } else {
                 ESP_LOGW(TAG, "pm average outside of threshold, ignoring");
@@ -1534,7 +1541,7 @@ static void monitoring_task(void* discard)
             
         cur_uptime = esp_timer_get_time();
         remaining_time = next_sleep_uptime - cur_uptime;
-        ESP_LOGI(TAG, "next sleep uptime %lld cur_uptime %lld remaining time %lld", next_sleep_uptime, cur_uptime, remaining_time);
+        //ESP_LOGI(TAG, "next sleep uptime %lld cur_uptime %lld remaining time %lld", next_sleep_uptime, cur_uptime, remaining_time);
 #ifdef CONFIG_LIGHT_SLEEP_ENABLED
         // needs to be in microseconds, so convert
         int sleep_us = 100000;
@@ -1571,7 +1578,9 @@ static void monitoring_task(void* discard)
         // sleep for 10 seconds before measuring again
         ESP_LOGI(TAG, "sleeping for %lld ms", remaining_time/1000);
         if ( remaining_time > 0 ){
-            vTaskDelay( pdMS_TO_TICKS(remaining_time/1000) );
+            vTaskDelayUntil( &xLastWakeTime, xFrequency );
+            
+//            vTaskDelay( pdMS_TO_TICKS(remaining_time/1000) );
         } else {
             ESP_LOGW(TAG, "sleep time was negative so we're already behind, just looping around");
         }