main/task_ina219.c

changeset 14
2a9f67ecbc72
parent 13
c3b29a1dcf1e
child 24
74609f70411e
equal deleted inserted replaced
13:c3b29a1dcf1e 14:2a9f67ecbc72
41 /* 41 /*
42 * Task to read INA219 sensors on request. 42 * Task to read INA219 sensors on request.
43 */ 43 */
44 void task_ina219(void *pvParameter) 44 void task_ina219(void *pvParameter)
45 { 45 {
46 float bus_voltage, shunt_voltage, current, power; 46 float bus_voltage, shunt_voltage, current;
47 47
48 ESP_LOGI(TAG, "Starting task INA219 sda=%d scl=%d", CONFIG_I2C_MASTER_SDA, CONFIG_I2C_MASTER_SCL); 48 ESP_LOGI(TAG, "Starting task INA219 sda=%d scl=%d", CONFIG_I2C_MASTER_SDA, CONFIG_I2C_MASTER_SCL);
49 ina219_state = malloc(sizeof(INA219_State)); 49 ina219_state = malloc(sizeof(INA219_State));
50 50
51 ina219_state->Battery.valid = false; 51 ina219_state->Battery.valid = false;
97 */ 97 */
98 if (! ina219_state->Battery.fake) { 98 if (! ina219_state->Battery.fake) {
99 /* 99 /*
100 * Note, on Arduino power down and resume works, but not on esp-idf. 100 * Note, on Arduino power down and resume works, but not on esp-idf.
101 * This could save only 0.5 mA per device. 101 * This could save only 0.5 mA per device.
102 *
103 * We run the ESP32-C3 in Power Save mode, Dynamic Frequency Scaling.
104 * This means a wrong current measurement (too high) unless we let the CPU
105 * rest for a while. The INA219 runs in continuous mode so we get the
106 * results during the vTaskDelay().
102 */ 107 */
103 // ESP_ERROR_CHECK(ina219_configure(&ina219_b_dev, INA219_BUS_RANGE_32V, INA219_GAIN_0_125, 108 // vTaskDelay(20 / portTICK_PERIOD_MS);
104 // INA219_RES_12BIT_1S, INA219_RES_12BIT_1S, INA219_MODE_CONT_SHUNT_BUS));
105 ESP_ERROR_CHECK(ina219_get_bus_voltage(&ina219_b_dev, &bus_voltage)); 109 ESP_ERROR_CHECK(ina219_get_bus_voltage(&ina219_b_dev, &bus_voltage));
106 ESP_ERROR_CHECK(ina219_get_shunt_voltage(&ina219_b_dev, &shunt_voltage)); 110 ESP_ERROR_CHECK(ina219_get_shunt_voltage(&ina219_b_dev, &shunt_voltage));
111 vTaskDelay(20 / portTICK_PERIOD_MS);
107 ESP_ERROR_CHECK(ina219_get_current(&ina219_b_dev, &current)); 112 ESP_ERROR_CHECK(ina219_get_current(&ina219_b_dev, &current));
108 ESP_ERROR_CHECK(ina219_get_power(&ina219_b_dev, &power)); 113 ESP_LOGI(TAG, "Battery VBUS: %.04f V, VSHUNT: %.04f mV, IBUS: %.04f mA", bus_voltage, shunt_voltage * 1000, current * 1000);
109 // ESP_ERROR_CHECK(ina219_configure(&ina219_b_dev, INA219_BUS_RANGE_32V, INA219_GAIN_0_125,
110 // INA219_RES_12BIT_1S, INA219_RES_12BIT_1S, INA219_MODE_POWER_DOWN));
111 ESP_LOGI(TAG, "Battery VBUS: %.04f V, VSHUNT: %.04f mV, IBUS: %.04f mA, PBUS: %.04f mW",
112 bus_voltage, shunt_voltage * 1000, current * 1000, power * 1000);
113 } 114 }
114 if (xSemaphoreTake(xSemaphoreINA219, 25) == pdTRUE) { 115 if (xSemaphoreTake(xSemaphoreINA219, 25) == pdTRUE) {
115 if (ina219_state->Battery.fake) { 116 if (ina219_state->Battery.fake) {
116 ina219_state->Battery.volts = 13.21; 117 ina219_state->Battery.volts = 13.21;
117 if (ready_WiFi()) { 118 if (ready_WiFi()) {
122 ina219_state->Battery.current = 18.2; 123 ina219_state->Battery.current = 18.2;
123 } 124 }
124 } else { 125 } else {
125 ina219_state->Battery.volts = bus_voltage; 126 ina219_state->Battery.volts = bus_voltage;
126 ina219_state->Battery.shunt = shunt_voltage; 127 ina219_state->Battery.shunt = shunt_voltage;
127 ina219_state->Battery.current = current; 128 ina219_state->Battery.current = current * 1000;
128 } 129 }
129 ina219_state->Battery.valid = true; 130 ina219_state->Battery.valid = true;
130 xSemaphoreGive(xSemaphoreINA219); 131 xSemaphoreGive(xSemaphoreINA219);
131 } 132 }
132 133
133 if (! ina219_state->Solar.fake) { 134 if (! ina219_state->Solar.fake) {
134 // ESP_ERROR_CHECK(ina219_configure(&ina219_s_dev, INA219_BUS_RANGE_32V, INA219_GAIN_0_125, 135 vTaskDelay(20 / portTICK_PERIOD_MS);
135 // INA219_RES_12BIT_1S, INA219_RES_12BIT_1S, INA219_MODE_CONT_SHUNT_BUS));
136 ESP_ERROR_CHECK(ina219_get_bus_voltage(&ina219_s_dev, &bus_voltage)); 136 ESP_ERROR_CHECK(ina219_get_bus_voltage(&ina219_s_dev, &bus_voltage));
137 ESP_ERROR_CHECK(ina219_get_shunt_voltage(&ina219_s_dev, &shunt_voltage)); 137 ESP_ERROR_CHECK(ina219_get_shunt_voltage(&ina219_s_dev, &shunt_voltage));
138 vTaskDelay(20 / portTICK_PERIOD_MS);
138 ESP_ERROR_CHECK(ina219_get_current(&ina219_s_dev, &current)); 139 ESP_ERROR_CHECK(ina219_get_current(&ina219_s_dev, &current));
139 ESP_ERROR_CHECK(ina219_get_power(&ina219_s_dev, &power)); 140 ESP_LOGI(TAG, " Solar VBUS: %.04f V, VSHUNT: %.04f mV, IBUS: %.04f mA", bus_voltage, shunt_voltage * 1000, current * 1000);
140 // ESP_ERROR_CHECK(ina219_configure(&ina219_s_dev, INA219_BUS_RANGE_32V, INA219_GAIN_0_125,
141 // INA219_RES_12BIT_1S, INA219_RES_12BIT_1S, INA219_MODE_POWER_DOWN));
142 ESP_LOGI(TAG, " Solar VBUS: %.04f V, VSHUNT: %.04f mV, IBUS: %.04f mA, PBUS: %.04f mW",
143 bus_voltage, shunt_voltage * 1000, current * 1000, power * 1000);
144 } 141 }
145 if (xSemaphoreTake(xSemaphoreINA219, 25) == pdTRUE) { 142 if (xSemaphoreTake(xSemaphoreINA219, 25) == pdTRUE) {
146 if (! ina219_state->Solar.fake && ! ina219_state->Battery.fake) { 143 if (! ina219_state->Solar.fake && ! ina219_state->Battery.fake) {
147 ina219_state->Solar.volts = bus_voltage; 144 ina219_state->Solar.volts = bus_voltage;
148 ina219_state->Solar.shunt = shunt_voltage; 145 ina219_state->Solar.shunt = shunt_voltage;
149 ina219_state->Solar.current = current; 146 ina219_state->Solar.current = current * 1000;
150 } else if (ina219_state->Solar.fake && ! ina219_state->Battery.fake) { 147 } else if (ina219_state->Solar.fake && ! ina219_state->Battery.fake) {
151 ina219_state->Solar.volts = ina219_state->Battery.volts + 0.78; 148 ina219_state->Solar.volts = ina219_state->Battery.volts + 0.78;
152 ina219_state->Solar.shunt = 0.02341; 149 ina219_state->Solar.shunt = 0.02341;
153 ina219_state->Solar.current = 234.1; 150 ina219_state->Solar.current = 234.1;
154 } else { 151 } else {

mercurial