#define LOG_LOCAL_LEVEL ESP_LOG_DEBUG #include "co2_sensor.h" #include #include #include #include #include #include #include #include "voc_sensor.h" #include "i2c_utils.h" #include "periph_init.h" #include "data_report.h" #include static const char *TAG = "co2"; #define CO2_ADDR 104 #define CO2_UART_NUM 1 #define TIMEOUT_MS 500 void co2_read_task(void *param) { esp_err_t rv; vTaskDelay(pdMS_TO_TICKS(500)); // TODO uint8_t buffer[128]; while (1) { vTaskDelay(pdMS_TO_TICKS(2000)); uint16_t ppm = 0; int i = 0; buffer[i++] = 0x68; buffer[i++] = 0x04; buffer[i++] = 0x00; buffer[i++] = 0x00; buffer[i++] = 0x00; buffer[i++] = 0x04; buffer[i++] = 0xf8; buffer[i++] = 0xf0; uart_write_bytes(CO2_UART_NUM, buffer, i); i = uart_read_bytes(CO2_UART_NUM, buffer, 13, pdMS_TO_TICKS(1000)); if (i != 13) { ESP_LOGE(TAG, "Rx failed"); continue; } ESP_LOG_BUFFER_HEXDUMP(TAG, buffer, i, ESP_LOG_DEBUG); continue; ESP_LOGI(TAG, "CO2 ppm %d", ppm); if (pdPASS == xSemaphoreTake(g_mux_data_report, pdMS_TO_TICKS(750))) { if (ppm > 400 && ppm < 5000) { g_data_report.co2_ppm = (float) ppm; g_data_report.co2_ready = true; g_data_report.co2_timestamp = xTaskGetTickCount(); } else { g_data_report.co2_ready = false; } } xSemaphoreGive(g_mux_data_report); } }