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; |
46 float bus_voltage, shunt_voltage; |
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; |
95 * 3. Only solar sensor. Use scenario 4, but show measured values. |
95 * 3. Only solar sensor. Use scenario 4, but show measured values. |
96 * 4. Fake everything. |
96 * 4. Fake everything. |
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. |
|
101 * This could save only 0.5 mA per device. |
|
102 * |
|
103 * We run the ESP32-C3 in Power Save mode, Dynamic Frequency Scaling. |
100 * 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 |
101 * 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 |
102 * rest for a while. The INA219 runs in continuous mode so we get the |
106 * results during the vTaskDelay(). |
103 * results during the vTaskDelay(). |
107 */ |
104 */ |
108 vTaskDelay(20 / portTICK_PERIOD_MS); |
105 ESP_ERROR_CHECK(ina219_configure(&ina219_b_dev, INA219_BUS_RANGE_32V, INA219_GAIN_0_125, |
|
106 INA219_RES_12BIT_1S, INA219_RES_12BIT_1S, INA219_MODE_CONT_SHUNT_BUS)); |
|
107 vTaskDelay(10 / portTICK_PERIOD_MS); |
109 ESP_ERROR_CHECK(ina219_get_bus_voltage(&ina219_b_dev, &bus_voltage)); |
108 ESP_ERROR_CHECK(ina219_get_bus_voltage(&ina219_b_dev, &bus_voltage)); |
110 vTaskDelay(10 / portTICK_PERIOD_MS); |
109 vTaskDelay(10 / portTICK_PERIOD_MS); |
111 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)); |
112 vTaskDelay(10 / portTICK_PERIOD_MS); |
111 /* |
113 ESP_ERROR_CHECK(ina219_get_current(&ina219_b_dev, ¤t)); |
112 * We don't call ina219_get_current(&ina219_b_dev, ¤t) because it takes |
114 ESP_LOGI(TAG, "Battery VBUS: %.04f V, VSHUNT: %.04f mV, IBUS: %.04f mA", bus_voltage, shunt_voltage * 1000, current * 1000); |
113 * a new measurement which usual gives the same results as the shunt voltage. |
|
114 * So just calculate the current. |
|
115 */ |
|
116 ESP_ERROR_CHECK(ina219_configure(&ina219_b_dev, INA219_BUS_RANGE_32V, INA219_GAIN_0_125, |
|
117 INA219_RES_12BIT_1S, INA219_RES_12BIT_1S, INA219_MODE_POWER_DOWN)); |
|
118 ESP_LOGI(TAG, "Battery VBUS: %.03f V, VSHUNT: %.02f mV, IBUS: %.01f mA", bus_voltage, shunt_voltage * 1000, shunt_voltage * 10000); |
115 } |
119 } |
116 if (xSemaphoreTake(xSemaphoreINA219, 25) == pdTRUE) { |
120 if (xSemaphoreTake(xSemaphoreINA219, 25) == pdTRUE) { |
117 if (ina219_state->Battery.fake) { |
121 if (ina219_state->Battery.fake) { |
118 ina219_state->Battery.volts = 13.21; |
122 ina219_state->Battery.volts = 13.21; |
119 if (ready_WiFi()) { |
123 if (ready_WiFi()) { |
124 ina219_state->Battery.current = 18.2; |
128 ina219_state->Battery.current = 18.2; |
125 } |
129 } |
126 } else { |
130 } else { |
127 ina219_state->Battery.volts = bus_voltage; |
131 ina219_state->Battery.volts = bus_voltage; |
128 ina219_state->Battery.shunt = shunt_voltage; |
132 ina219_state->Battery.shunt = shunt_voltage; |
129 ina219_state->Battery.current = current * 1000; |
133 ina219_state->Battery.current = shunt_voltage * 10000; |
130 } |
134 } |
131 ina219_state->Battery.valid = true; |
135 ina219_state->Battery.valid = true; |
132 xSemaphoreGive(xSemaphoreINA219); |
136 xSemaphoreGive(xSemaphoreINA219); |
133 } |
137 } |
134 |
138 |
135 if (! ina219_state->Solar.fake) { |
139 if (! ina219_state->Solar.fake) { |
136 vTaskDelay(20 / portTICK_PERIOD_MS); |
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_CONT_SHUNT_BUS)); |
|
142 vTaskDelay(10 / portTICK_PERIOD_MS); |
137 ESP_ERROR_CHECK(ina219_get_bus_voltage(&ina219_s_dev, &bus_voltage)); |
143 ESP_ERROR_CHECK(ina219_get_bus_voltage(&ina219_s_dev, &bus_voltage)); |
138 vTaskDelay(10 / portTICK_PERIOD_MS); |
144 vTaskDelay(10 / portTICK_PERIOD_MS); |
139 ESP_ERROR_CHECK(ina219_get_shunt_voltage(&ina219_s_dev, &shunt_voltage)); |
145 ESP_ERROR_CHECK(ina219_get_shunt_voltage(&ina219_s_dev, &shunt_voltage)); |
140 vTaskDelay(10 / portTICK_PERIOD_MS); |
146 ESP_ERROR_CHECK(ina219_configure(&ina219_s_dev, INA219_BUS_RANGE_32V, INA219_GAIN_0_125, |
141 ESP_ERROR_CHECK(ina219_get_current(&ina219_s_dev, ¤t)); |
147 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", bus_voltage, shunt_voltage * 1000, current * 1000); |
148 ESP_LOGI(TAG, " Solar VBUS: %.03f V, VSHUNT: %.02f mV, IBUS: %.01f mA", bus_voltage, shunt_voltage * 1000, shunt_voltage * 10000); |
143 } |
149 } |
144 if (xSemaphoreTake(xSemaphoreINA219, 25) == pdTRUE) { |
150 if (xSemaphoreTake(xSemaphoreINA219, 25) == pdTRUE) { |
145 if (! ina219_state->Solar.fake && ! ina219_state->Battery.fake) { |
151 if (! ina219_state->Solar.fake && ! ina219_state->Battery.fake) { |
146 ina219_state->Solar.volts = bus_voltage; |
152 ina219_state->Solar.volts = bus_voltage; |
147 ina219_state->Solar.shunt = shunt_voltage; |
153 ina219_state->Solar.shunt = shunt_voltage; |
148 ina219_state->Solar.current = current * 1000; |
154 ina219_state->Solar.current = shunt_voltage * 10000; |
149 } else if (ina219_state->Solar.fake && ! ina219_state->Battery.fake) { |
155 } else if (ina219_state->Solar.fake && ! ina219_state->Battery.fake) { |
150 ina219_state->Solar.volts = ina219_state->Battery.volts + 0.78; |
156 ina219_state->Solar.volts = ina219_state->Battery.volts + 0.78; |
151 ina219_state->Solar.shunt = 0.02341; |
157 ina219_state->Solar.shunt = 0.02341; |
152 ina219_state->Solar.current = 234.1; |
158 ina219_state->Solar.current = 234.1; |
153 } else { |
159 } else { |