# HG changeset patch # User Michiel Broek # Date 1680611606 -7200 # Node ID 2a9f67ecbc720032b7685c18895fef0c9686c53b # Parent c3b29a1dcf1e742c3a879ebdcd76fb808495780e Fixed wrong voltage and current measurements due to differences between Arduino and ESP-IDF. diff -r c3b29a1dcf1e -r 2a9f67ecbc72 main/iotbalkon.c --- a/main/iotbalkon.c Mon Apr 03 20:11:29 2023 +0200 +++ b/main/iotbalkon.c Tue Apr 04 14:33:26 2023 +0200 @@ -7,14 +7,19 @@ static const char *TAG = "iotbalkon"; -#define State_Init 0 -#define State_Connect 1 -#define State_Working 2 -#define State_WorkDone 3 -#define State_Stop 4 -#define State_Wait 5 -#define State_Measure 6 -#define State_GoSleep 7 +/* + * Main State table. + */ +typedef enum { + State_Init = 0, + State_Connect, + State_Working, + State_WorkDone, + State_Stop, + State_Wait, + State_Measure, + State_GoSleep +} Main_State; static TaskHandle_t xTaskBMP280 = NULL; @@ -233,9 +238,9 @@ if (xSemaphoreTake(xSemaphoreINA219, 25) == pdTRUE) { - s_Volts[loopno] = ina219_state->Solar.volts + (ina219_state->Solar.shunt / 1000); + s_Volts[loopno] = ina219_state->Solar.volts + ina219_state->Solar.shunt; s_Current[loopno] = ina219_state->Solar.current; - b_Volts[loopno] = ina219_state->Battery.volts + (ina219_state->Battery.shunt / 1000); + b_Volts[loopno] = ina219_state->Battery.volts + ina219_state->Battery.shunt; b_Current[loopno] = ina219_state->Battery.current; m_Valid[loopno] = ina219_state->valid; xSemaphoreGive(xSemaphoreINA219); diff -r c3b29a1dcf1e -r 2a9f67ecbc72 main/task_ina219.c --- a/main/task_ina219.c Mon Apr 03 20:11:29 2023 +0200 +++ b/main/task_ina219.c Tue Apr 04 14:33:26 2023 +0200 @@ -43,7 +43,7 @@ */ void task_ina219(void *pvParameter) { - float bus_voltage, shunt_voltage, current, power; + float bus_voltage, shunt_voltage, current; ESP_LOGI(TAG, "Starting task INA219 sda=%d scl=%d", CONFIG_I2C_MASTER_SDA, CONFIG_I2C_MASTER_SCL); ina219_state = malloc(sizeof(INA219_State)); @@ -99,17 +99,18 @@ /* * Note, on Arduino power down and resume works, but not on esp-idf. * This could save only 0.5 mA per device. + * + * We run the ESP32-C3 in Power Save mode, Dynamic Frequency Scaling. + * This means a wrong current measurement (too high) unless we let the CPU + * rest for a while. The INA219 runs in continuous mode so we get the + * results during the vTaskDelay(). */ -// ESP_ERROR_CHECK(ina219_configure(&ina219_b_dev, INA219_BUS_RANGE_32V, INA219_GAIN_0_125, -// INA219_RES_12BIT_1S, INA219_RES_12BIT_1S, INA219_MODE_CONT_SHUNT_BUS)); +// vTaskDelay(20 / portTICK_PERIOD_MS); ESP_ERROR_CHECK(ina219_get_bus_voltage(&ina219_b_dev, &bus_voltage)); ESP_ERROR_CHECK(ina219_get_shunt_voltage(&ina219_b_dev, &shunt_voltage)); + vTaskDelay(20 / portTICK_PERIOD_MS); ESP_ERROR_CHECK(ina219_get_current(&ina219_b_dev, ¤t)); - ESP_ERROR_CHECK(ina219_get_power(&ina219_b_dev, &power)); -// ESP_ERROR_CHECK(ina219_configure(&ina219_b_dev, INA219_BUS_RANGE_32V, INA219_GAIN_0_125, -// INA219_RES_12BIT_1S, INA219_RES_12BIT_1S, INA219_MODE_POWER_DOWN)); - ESP_LOGI(TAG, "Battery VBUS: %.04f V, VSHUNT: %.04f mV, IBUS: %.04f mA, PBUS: %.04f mW", - bus_voltage, shunt_voltage * 1000, current * 1000, power * 1000); + ESP_LOGI(TAG, "Battery VBUS: %.04f V, VSHUNT: %.04f mV, IBUS: %.04f mA", bus_voltage, shunt_voltage * 1000, current * 1000); } if (xSemaphoreTake(xSemaphoreINA219, 25) == pdTRUE) { if (ina219_state->Battery.fake) { @@ -124,29 +125,25 @@ } else { ina219_state->Battery.volts = bus_voltage; ina219_state->Battery.shunt = shunt_voltage; - ina219_state->Battery.current = current; + ina219_state->Battery.current = current * 1000; } ina219_state->Battery.valid = true; xSemaphoreGive(xSemaphoreINA219); } if (! ina219_state->Solar.fake) { -// ESP_ERROR_CHECK(ina219_configure(&ina219_s_dev, INA219_BUS_RANGE_32V, INA219_GAIN_0_125, -// INA219_RES_12BIT_1S, INA219_RES_12BIT_1S, INA219_MODE_CONT_SHUNT_BUS)); + vTaskDelay(20 / portTICK_PERIOD_MS); ESP_ERROR_CHECK(ina219_get_bus_voltage(&ina219_s_dev, &bus_voltage)); ESP_ERROR_CHECK(ina219_get_shunt_voltage(&ina219_s_dev, &shunt_voltage)); + vTaskDelay(20 / portTICK_PERIOD_MS); ESP_ERROR_CHECK(ina219_get_current(&ina219_s_dev, ¤t)); - ESP_ERROR_CHECK(ina219_get_power(&ina219_s_dev, &power)); -// ESP_ERROR_CHECK(ina219_configure(&ina219_s_dev, INA219_BUS_RANGE_32V, INA219_GAIN_0_125, -// INA219_RES_12BIT_1S, INA219_RES_12BIT_1S, INA219_MODE_POWER_DOWN)); - ESP_LOGI(TAG, " Solar VBUS: %.04f V, VSHUNT: %.04f mV, IBUS: %.04f mA, PBUS: %.04f mW", - bus_voltage, shunt_voltage * 1000, current * 1000, power * 1000); + ESP_LOGI(TAG, " Solar VBUS: %.04f V, VSHUNT: %.04f mV, IBUS: %.04f mA", bus_voltage, shunt_voltage * 1000, current * 1000); } if (xSemaphoreTake(xSemaphoreINA219, 25) == pdTRUE) { if (! ina219_state->Solar.fake && ! ina219_state->Battery.fake) { ina219_state->Solar.volts = bus_voltage; ina219_state->Solar.shunt = shunt_voltage; - ina219_state->Solar.current = current; + ina219_state->Solar.current = current * 1000; } else if (ina219_state->Solar.fake && ! ina219_state->Battery.fake) { ina219_state->Solar.volts = ina219_state->Battery.volts + 0.78; ina219_state->Solar.shunt = 0.02341;