Air quality sensor
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
esp-airsensor/main/co2_sensor.c

75 lines
1.8 KiB

#define LOG_LOCAL_LEVEL ESP_LOG_DEBUG
#include "co2_sensor.h"
#include <esp_log.h>
#include <esp_err.h>
#include <freertos/FreeRTOS.h>
#include <freertos/task.h>
#include <driver/i2c.h>
#include <sys/time.h>
#include <hal/i2c_hal.h>
#include "voc_sensor.h"
#include "i2c_utils.h"
#include "periph_init.h"
#include "data_report.h"
#include <driver/uart.h>
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);
}
}