test
This commit is contained in:
@@ -6,12 +6,18 @@
|
||||
|
||||
#define BLINK_GPIO 2
|
||||
|
||||
//uint8_t powerMode = LOW_POWER_MODE;
|
||||
uint8_t powerMode = HIGH_POWER_MODE;
|
||||
|
||||
static uint8_t s_led_state = 0;
|
||||
|
||||
uint8_t telemetryIndex = 1;
|
||||
|
||||
uint8_t foundDevices[128];
|
||||
uint8_t prevDevices[128];
|
||||
const uint8_t expectedAdressesCount = 5;
|
||||
const uint8_t expectedAdresses[] = {MCP23018_ADDRESS, BME680_ADDRESS, CCS811_ADDRESS, MPU9250_ADDRESS, INA260_ADDRESS};
|
||||
|
||||
uint8_t foundDevices[5];
|
||||
uint8_t prevDevices[5];
|
||||
|
||||
static void configure_led(void)
|
||||
{
|
||||
@@ -19,93 +25,131 @@ static void configure_led(void)
|
||||
gpio_set_direction(BLINK_GPIO, GPIO_MODE_OUTPUT);
|
||||
}
|
||||
|
||||
// void update_devices() {
|
||||
// memcpy(prevDevices, foundDevices, sizeof(prevDevices));
|
||||
// memset(foundDevices, 0, sizeof(foundDevices));
|
||||
// for (uint8_t i = 0; i < 128; i++)
|
||||
// {
|
||||
// fflush(stdout);
|
||||
|
||||
// esp_err_t ret = i2c_master_probe(i2c0_bus_hdl, i, 20);
|
||||
|
||||
// if (ret == ESP_OK)
|
||||
// {
|
||||
// foundDevices[i] = 1;
|
||||
// printf("Found device at 0x%02X\n", i);
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
void update_devices()
|
||||
{
|
||||
memcpy(prevDevices, foundDevices, sizeof(prevDevices));
|
||||
for (uint8_t i = 0; i < expectedAdressesCount; i++)
|
||||
{
|
||||
fflush(stdout);
|
||||
|
||||
// void init_connected() {
|
||||
// for (uint8_t i = 0; i < 128; i++) {
|
||||
// if (foundDevices[i] != prevDevices[i]) {
|
||||
// if (foundDevices[i]) {
|
||||
// switch (i)
|
||||
// {
|
||||
// case MCP23018_ADDRESS:
|
||||
// /* code */
|
||||
// if (mcp23018_init() == ESP_OK) {
|
||||
// foundDevices[i] = 2;
|
||||
// }
|
||||
// break;
|
||||
esp_err_t ret = i2c_master_probe(i2c0_bus_hdl, expectedAdresses[i], 20);
|
||||
|
||||
// case INA260_ADDRESS:
|
||||
// if (ina260_init() == ESP_OK) {
|
||||
// foundDevices[i] = 2;
|
||||
// }
|
||||
// /* code */
|
||||
// break;
|
||||
if (ret == ESP_OK)
|
||||
{
|
||||
if (foundDevices[i] == 0)
|
||||
{
|
||||
foundDevices[i] = 1;
|
||||
}
|
||||
// printf("Found device at 0x%02X\n", i);
|
||||
}
|
||||
else
|
||||
{
|
||||
foundDevices[i] = 0;
|
||||
if (i == 1 && powerMode != HIGH_POWER_MODE)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
printf("Not found device at 0x%02X\n", expectedAdresses[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// case CCS811_ADDRESS:
|
||||
// if (ccs811_init() == ESP_OK) {
|
||||
// foundDevices[i] = 2;
|
||||
// }
|
||||
// /* code */
|
||||
// break;
|
||||
void setPowerMode(uint8_t powerModeIn)
|
||||
{
|
||||
powerMode = powerModeIn;
|
||||
if (foundDevices[0] == 2)
|
||||
{
|
||||
if (powerMode == HIGH_POWER_MODE) {
|
||||
mcp23018_set_pin(MCP23018_DEV_HANDLE, MCP_CCS811_WAKE, 0);
|
||||
mcp23018_set_pin(MCP23018_DEV_HANDLE, MCP_CCS811_POWER, 1);
|
||||
mcp23018_set_pin(MCP23018_DEV_HANDLE, MCP_MICS_POWER, 1);
|
||||
} else {
|
||||
mcp23018_set_pin(MCP23018_DEV_HANDLE, MCP_CCS811_WAKE, 1);
|
||||
mcp23018_set_pin(MCP23018_DEV_HANDLE, MCP_CCS811_POWER, 0);
|
||||
mcp23018_set_pin(MCP23018_DEV_HANDLE, MCP_MICS_POWER, 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// case MPU9250_ADDRESS:
|
||||
// if (mpu9250_init() == ESP_OK) {
|
||||
// foundDevices[i] = 2;
|
||||
// }
|
||||
// /* code */
|
||||
// break;
|
||||
void init_connected()
|
||||
{
|
||||
|
||||
// case BME680_ADDRESS:
|
||||
// if (bme680b_init() == ESP_OK) {
|
||||
// foundDevices[i] = 2;
|
||||
// }
|
||||
// /* code */
|
||||
// break;
|
||||
|
||||
// default:
|
||||
// break;
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
for (uint8_t i = 0; i < expectedAdressesCount; i++)
|
||||
{
|
||||
if (foundDevices[i] != prevDevices[i])
|
||||
{
|
||||
if (foundDevices[i])
|
||||
{
|
||||
esp_err_t ret = ESP_FAIL;
|
||||
bool foundUsed = true;
|
||||
switch (i)
|
||||
{
|
||||
case MCP23018_ADDRESS:
|
||||
/* code */
|
||||
ret = mcp23018_init();
|
||||
setPowerMode(powerMode);
|
||||
mcp3550_spi_init();
|
||||
break;
|
||||
|
||||
case INA260_ADDRESS:
|
||||
ret = ina260_init();
|
||||
/* code */
|
||||
break;
|
||||
|
||||
case CCS811_ADDRESS:
|
||||
ret = ccs811_init();
|
||||
/* code */
|
||||
break;
|
||||
|
||||
case MPU9250_ADDRESS:
|
||||
ret = mpu9250_init();
|
||||
/* code */
|
||||
break;
|
||||
|
||||
case BME680_ADDRESS:
|
||||
ret = bme680b_init();
|
||||
/* code */
|
||||
break;
|
||||
|
||||
default:
|
||||
foundUsed = false;
|
||||
break;
|
||||
}
|
||||
if (foundUsed)
|
||||
{
|
||||
if (ret == ESP_OK)
|
||||
{
|
||||
foundDevices[i] = 2;
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("Device init error at 0x%02X - %s\n", expectedAdresses[i], esp_err_to_name(ret));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void i2c_sensors_task(void *pvParameters)
|
||||
{
|
||||
memset(foundDevices, 0, sizeof(foundDevices));
|
||||
memset(prevDevices, 0, sizeof(prevDevices));
|
||||
|
||||
bme680b_init();
|
||||
mpu9250_init();
|
||||
ccs811_init();
|
||||
ina260_init();
|
||||
|
||||
// bme680b_init();
|
||||
// mpu9250_init();
|
||||
// ccs811_init();
|
||||
// ina260_init();
|
||||
|
||||
// update_devices();
|
||||
// init_connected();
|
||||
update_devices();
|
||||
init_connected();
|
||||
// initialize the xLastWakeTime variable with the current time.
|
||||
const int64_t interval_us = 100000; // 100 ms
|
||||
const int64_t interval_us = 50000; // 50 ms
|
||||
int64_t start_time, end_time, elapsed;
|
||||
|
||||
//
|
||||
// initialize i2c device configuration
|
||||
|
||||
mcp3550_spi_init();
|
||||
configure_led();
|
||||
|
||||
int16_t accel[3], gyro[3], temp, magnet[3];
|
||||
@@ -121,82 +165,96 @@ void i2c_sensors_task(void *pvParameters)
|
||||
uint16_t power;
|
||||
|
||||
bme680_data_t bmeData;
|
||||
|
||||
mics_adc_data_t ADCData;
|
||||
// task loop entry point
|
||||
for (;;)
|
||||
{
|
||||
start_time = esp_timer_get_time(); // µs since boot
|
||||
|
||||
uint8_t presentDevices = 0;
|
||||
update_devices();
|
||||
init_connected();
|
||||
|
||||
//
|
||||
// handle sensor
|
||||
|
||||
if (BME680_DEV_HANDLE)
|
||||
if (foundDevices[BME680_ADDRESS] == 2)
|
||||
{
|
||||
esp_err_t result = bme680_get_data(BME680_DEV_HANDLE, &bmeData);
|
||||
if (result != ESP_OK)
|
||||
|
||||
presentDevices |= BME680_PRESENT_BIT;
|
||||
|
||||
if (BME680_DEV_HANDLE)
|
||||
{
|
||||
ESP_LOGE(TAG_BME, "bme680 device read failed (%s)", esp_err_to_name(result));
|
||||
esp_err_t result = bme680_get_data(BME680_DEV_HANDLE, &bmeData);
|
||||
if (result != ESP_OK)
|
||||
{
|
||||
ESP_LOGE(TAG_BME, "bme680 device read failed (%s)", esp_err_to_name(result));
|
||||
}
|
||||
else
|
||||
{
|
||||
bmeData.barometric_pressure = bmeData.barometric_pressure / 100;
|
||||
ESP_LOGI(TAG_BME, "dewpoint temperature:%.2f °C", bmeData.dewpoint_temperature);
|
||||
ESP_LOGI(TAG_BME, "air temperature: %.2f °C", bmeData.air_temperature);
|
||||
ESP_LOGI(TAG_BME, "relative humidity: %.2f %%", bmeData.relative_humidity);
|
||||
ESP_LOGI(TAG_BME, "barometric pressure: %.2f hPa", bmeData.barometric_pressure);
|
||||
ccs811_set_env_data(bmeData.air_temperature, bmeData.relative_humidity);
|
||||
ESP_LOGI(TAG_BME, "gas resistance: %.2f kOhms", bmeData.gas_resistance / 1000);
|
||||
ESP_LOGI(TAG_BME, "iaq score: %u (%s)", bmeData.iaq_score, bme680_air_quality_to_string(bmeData.iaq_score));
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
bmeData.barometric_pressure = bmeData.barometric_pressure / 100;
|
||||
ESP_LOGI(TAG_BME, "dewpoint temperature:%.2f °C", bmeData.dewpoint_temperature);
|
||||
ESP_LOGI(TAG_BME, "air temperature: %.2f °C", bmeData.air_temperature);
|
||||
ESP_LOGI(TAG_BME, "relative humidity: %.2f %%", bmeData.relative_humidity);
|
||||
ESP_LOGI(TAG_BME, "barometric pressure: %.2f hPa", bmeData.barometric_pressure);
|
||||
ccs811_set_env_data(bmeData.air_temperature, bmeData.relative_humidity);
|
||||
ESP_LOGI(TAG_BME, "gas resistance: %.2f kOhms", bmeData.gas_resistance / 1000);
|
||||
ESP_LOGI(TAG_BME, "iaq score: %u (%s)", bmeData.iaq_score, bme680_air_quality_to_string(bmeData.iaq_score));
|
||||
bme680b_init();
|
||||
}
|
||||
}
|
||||
else
|
||||
|
||||
if (foundDevices[CCS811_ADDRESS] == 2)
|
||||
{
|
||||
bme680b_init();
|
||||
presentDevices |= CCS811_PRESENT_BIT;
|
||||
ccs811_get_data(&eCO2, &tvoc, ¤tCCS, &rawData);
|
||||
ESP_LOGI(TAG_CCS, "eCO₂: %d ppm, TVOC: %d ppb", eCO2, tvoc);
|
||||
ESP_LOGI(TAG_CCS, "Current: %d μA, Raw voltage: %d V", currentCCS, rawData);
|
||||
}
|
||||
|
||||
ccs811_get_data(&eCO2, &tvoc, ¤tCCS, &rawData);
|
||||
ESP_LOGI(TAG_CCS, "eCO₂: %d ppm, TVOC: %d ppb", eCO2, tvoc);
|
||||
ESP_LOGI(TAG_CCS, "Current: %d μA, Raw voltage: %d V", currentCCS, rawData);
|
||||
|
||||
if (mpu9250_read_sensor_data(MPU9250_DEV_HANDLE, accel, gyro, &temp, magnet) == ESP_OK)
|
||||
if (foundDevices[MPU9250_ADDRESS] == 2)
|
||||
{
|
||||
mpu9250_convert_data(accel, gyro, temp, magnet, accel_f, gyro_f, &temp_f, magnet_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, "Magnet: X=%.2fμT, Y=%.2fμT, Z=%.2fμT", magnet_f[0], magnet_f[1], magnet_f[2]);
|
||||
ESP_LOGI(TAG_MPU, "Temperature: %.2f °C", temp_f);
|
||||
}
|
||||
else
|
||||
{
|
||||
ESP_LOGE(TAG_MPU, "Failed to read sensor data");
|
||||
presentDevices |= MPU9250_PRESENT_BIT;
|
||||
if (mpu9250_read_sensor_data(MPU9250_DEV_HANDLE, accel, gyro, &temp, magnet) == ESP_OK)
|
||||
{
|
||||
mpu9250_convert_data(accel, gyro, temp, magnet, accel_f, gyro_f, &temp_f, magnet_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, "Magnet: X=%.2fμT, Y=%.2fμT, Z=%.2fμT", magnet_f[0], magnet_f[1], magnet_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);
|
||||
|
||||
float VREFVoltage = 2.5;
|
||||
|
||||
// mics_adc_data_t ADCData;
|
||||
// memset(&ADCData, 0, sizeof(ADCData));
|
||||
mics_adc_data_t ADCData = mcp3550_read_all(VREFVoltage);
|
||||
|
||||
log_mics_adc_values(&ADCData);
|
||||
|
||||
//int32_t nh3val = mcp3550_read(MCP_CS_ADC_NH3);
|
||||
|
||||
//ESP_LOGI(TAG_BME, "MICS NH3: %ld -> %fV", nh3val, mcp3550_to_voltage(nh3val, VREFVoltage));
|
||||
|
||||
//gpio_set_level(BLINK_GPIO, s_led_state);
|
||||
mcp23018_set_pin(MCP23018_DEV_HANDLE, MCP_CS_ADC_UVC, s_led_state);
|
||||
/* Toggle the LED state */
|
||||
s_led_state = !s_led_state;
|
||||
end_time = esp_timer_get_time();
|
||||
elapsed = end_time - start_time;
|
||||
|
||||
if (elapsed < interval_us)
|
||||
if (foundDevices[INA260_ADDRESS] == 2)
|
||||
{
|
||||
vTaskDelay(pdMS_TO_TICKS((interval_us - elapsed) / 1000));
|
||||
|
||||
presentDevices |= INA260_PRESENT_BIT;
|
||||
ina260_readParams(&volts, ¤t, &power);
|
||||
ina260_printParams(volts, current, power);
|
||||
}
|
||||
if (packetReadiness == 0) {
|
||||
|
||||
if (foundDevices[MCP23018_ADDRESS] == 2)
|
||||
{
|
||||
presentDevices |= MCP23018_PRESENT_BIT;
|
||||
float VREFVoltage = 2.5;
|
||||
ADCData = mcp3550_read_all(VREFVoltage);
|
||||
|
||||
log_mics_adc_values(&ADCData);
|
||||
}
|
||||
|
||||
if (packetReadiness == 0)
|
||||
{
|
||||
memset(&telemetryPacket, 0, sizeof(telemetryPacket));
|
||||
telemetryPacket.accelerationX = accel[0];
|
||||
telemetryPacket.accelerationY = accel[1];
|
||||
@@ -221,7 +279,6 @@ void i2c_sensors_task(void *pvParameters)
|
||||
telemetryPacket.current = current;
|
||||
telemetryPacket.power = power;
|
||||
|
||||
|
||||
telemetryPacket.temperature = bmeData.raw_data.temperature;
|
||||
telemetryPacket.humidity = bmeData.raw_data.humidity;
|
||||
telemetryPacket.pressure = bmeData.raw_data.pressure;
|
||||
@@ -244,7 +301,7 @@ void i2c_sensors_task(void *pvParameters)
|
||||
telemetryPacket.NO2 = ADCData.raw_no2;
|
||||
telemetryPacket.UVC = ADCData.raw_uvc;
|
||||
|
||||
//TODO MOVE THIS TO A BETTER PLACE FOR SYNC
|
||||
// TODO MOVE THIS TO A BETTER PLACE FOR SYNC
|
||||
telemetryPacket.time_seconds = gpsDataOut.time_seconds;
|
||||
telemetryPacket.latitude_centi_degrees = gpsDataOut.latitude_centi_degrees;
|
||||
telemetryPacket.longitude_centi_degrees = gpsDataOut.longitude_centi_degrees;
|
||||
@@ -263,11 +320,20 @@ void i2c_sensors_task(void *pvParameters)
|
||||
telemetryPacket.currentServoA = servoState.currentServoA;
|
||||
telemetryPacket.currentServoB = servoState.currentServoB;
|
||||
|
||||
telemetryPacket.presentDevices = presentDevices;
|
||||
|
||||
telemetryPacket.telemetryIndex = telemetryIndex++;
|
||||
|
||||
|
||||
}
|
||||
packetReadiness = 1;
|
||||
|
||||
s_led_state = !s_led_state;
|
||||
end_time = esp_timer_get_time();
|
||||
elapsed = end_time - start_time;
|
||||
|
||||
if (elapsed < interval_us)
|
||||
{
|
||||
vTaskDelay(pdMS_TO_TICKS((interval_us - elapsed) / 1000));
|
||||
}
|
||||
}
|
||||
//
|
||||
// free resources
|
||||
|
Reference in New Issue
Block a user