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
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]);
}
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;
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
//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");
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;
// 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");
}