Init
This commit is contained in:
88
main/components/sensors.c
Normal file
88
main/components/sensors.c
Normal file
@@ -0,0 +1,88 @@
|
||||
#include "sensors.h"
|
||||
|
||||
#define BLINK_GPIO 2
|
||||
|
||||
static uint8_t s_led_state = 0;
|
||||
|
||||
static void configure_led(void)
|
||||
{
|
||||
gpio_reset_pin(BLINK_GPIO);
|
||||
gpio_set_direction(BLINK_GPIO, GPIO_MODE_OUTPUT);
|
||||
}
|
||||
|
||||
void i2c_sensors_task(void *pvParameters)
|
||||
{
|
||||
// initialize the xLastWakeTime variable with the current time.
|
||||
TickType_t last_wake_time = xTaskGetTickCount();
|
||||
const TickType_t I2C0_TASK_SAMPLING_RATE = 5;
|
||||
//
|
||||
// initialize i2c device configuration
|
||||
|
||||
bme680b_init();
|
||||
mpu9250_init();
|
||||
mcp23018_init();
|
||||
ina260_init();
|
||||
ccs811_init();
|
||||
|
||||
configure_led();
|
||||
|
||||
int16_t accel[3], gyro[3], temp;
|
||||
float accel_f[3], gyro_f[3], temp_f;
|
||||
uint16_t eCO2;
|
||||
uint16_t tvoc;
|
||||
uint32_t volts;
|
||||
uint32_t current;
|
||||
uint32_t power;
|
||||
|
||||
// task loop entry point
|
||||
for (;;)
|
||||
{
|
||||
//
|
||||
// handle sensor
|
||||
|
||||
bme680_data_t data;
|
||||
esp_err_t result = bme680_get_data(BME680_DEV_HANDLE, &data);
|
||||
if (result != ESP_OK)
|
||||
{
|
||||
ESP_LOGE(TAG_BME, "bme680 device read failed (%s)", esp_err_to_name(result));
|
||||
}
|
||||
else
|
||||
{
|
||||
data.barometric_pressure = data.barometric_pressure / 100;
|
||||
// ESP_LOGI(TAG, "dewpoint temperature:%.2f °C", data.dewpoint_temperature);
|
||||
ESP_LOGI(TAG_BME, "air temperature: %.2f °C", data.air_temperature);
|
||||
ESP_LOGI(TAG_BME, "relative humidity: %.2f %%", data.relative_humidity);
|
||||
ESP_LOGI(TAG_BME, "barometric pressure: %.2f hPa", data.barometric_pressure);
|
||||
// ESP_LOGI(TAG, "gas resistance: %.2f kOhms", data.gas_resistance / 1000);
|
||||
// ESP_LOGI(TAG, "iaq score: %u (%s)", data.iaq_score, bme680_air_quality_to_string(data.iaq_score));
|
||||
}
|
||||
|
||||
ccs811_get_data(&eCO2, &tvoc);
|
||||
ESP_LOGI(TAG_CCS, "eCO₂: %d ppm, TVOC: %d ppb", eCO2, tvoc);
|
||||
|
||||
if (mpu9250_read_sensor_data(MPU9250_DEV_HANDLE, accel, gyro, &temp) == ESP_OK)
|
||||
{
|
||||
mpu9250_convert_data(accel, gyro, temp, accel_f, gyro_f, &temp_f);
|
||||
|
||||
ESP_LOGI(TAG_MPU, "Accel: X=%.2f g, Y=%.2f g, Z=%.2f g", accel_f[0], accel_f[1], accel_f[2]);
|
||||
ESP_LOGI(TAG_MPU, "Gyro: X=%.2f°/s, Y=%.2f°/s, Z=%.2f°/s", gyro_f[0], gyro_f[1], gyro_f[2]);
|
||||
ESP_LOGI(TAG_MPU, "Temperature: %.2f °C", temp_f);
|
||||
}
|
||||
else
|
||||
{
|
||||
ESP_LOGE(TAG_MPU, "Failed to read sensor data");
|
||||
}
|
||||
|
||||
ina260_readParams(&volts, ¤t, &power);
|
||||
ina260_printParams(volts, current, power);
|
||||
|
||||
gpio_set_level(BLINK_GPIO, s_led_state);
|
||||
/* Toggle the LED state */
|
||||
s_led_state = !s_led_state;
|
||||
vTaskDelaySecUntil(&last_wake_time, I2C0_TASK_SAMPLING_RATE);
|
||||
}
|
||||
//
|
||||
// free resources
|
||||
bme680_delete(BME680_DEV_HANDLE);
|
||||
vTaskDelete(NULL);
|
||||
}
|
Reference in New Issue
Block a user