main/task_ina219.c

Thu, 13 Apr 2023 15:28:11 +0200

author
Michiel Broek <mbroek@mbse.eu>
date
Thu, 13 Apr 2023 15:28:11 +0200
changeset 28
5872b972e553
parent 24
74609f70411e
child 30
7448b8dd4288
permissions
-rw-r--r--

Added INA219 power save mode again, it works now. Don't measure shunt voltage and current, just the shunt voltage and calculate the current.

3
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
1 /**
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
2 * @file task_ina219.c
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
3 * @brief The FreeRTOS task to query the INA219 sensors.
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
4 */
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
5
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
6
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
7 #include "config.h"
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
8
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
9
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
10 static const char *TAG = "task_ina219";
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
11
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
12 SemaphoreHandle_t xSemaphoreINA219 = NULL; ///< Semaphore INA219 task
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
13 EventGroupHandle_t xEventGroupINA219; ///< Events INA219 task
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
14 INA219_State *ina219_state; ///< Public state for other tasks
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
15
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
16 extern ina219_t ina219_b_dev;
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
17 extern ina219_t ina219_s_dev;
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
18
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
19 const int TASK_INA219_REQUEST_DONE = BIT0; ///< All requests are done.
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
20 const int TASK_INA219_REQUEST_POWER = BIT1; ///< Request power readings
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
21
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
22
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
23
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
24 void request_ina219(void)
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
25 {
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
26 xEventGroupClearBits(xEventGroupINA219, TASK_INA219_REQUEST_DONE);
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
27 xEventGroupSetBits(xEventGroupINA219, TASK_INA219_REQUEST_POWER);
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
28 }
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
29
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
30
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
31
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
32 bool ready_ina219(void)
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
33 {
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
34 if (xEventGroupGetBits(xEventGroupINA219) & TASK_INA219_REQUEST_DONE)
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
35 return true;
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
36 return false;
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
37 }
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
38
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
39
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
40
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
41 /*
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
42 * Task to read INA219 sensors on request.
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
43 */
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
44 void task_ina219(void *pvParameter)
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
45 {
28
5872b972e553 Added INA219 power save mode again, it works now. Don't measure shunt voltage and current, just the shunt voltage and calculate the current.
Michiel Broek <mbroek@mbse.eu>
parents: 24
diff changeset
46 float bus_voltage, shunt_voltage;
3
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
47
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
48 ESP_LOGI(TAG, "Starting task INA219 sda=%d scl=%d", CONFIG_I2C_MASTER_SDA, CONFIG_I2C_MASTER_SCL);
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
49 ina219_state = malloc(sizeof(INA219_State));
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
50
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
51 ina219_state->Battery.valid = false;
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
52 ina219_state->Battery.fake = (ina219_b_dev.i2c_dev.addr == 0) ? true:false;
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
53 ina219_state->Battery.address = ina219_b_dev.i2c_dev.addr;
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
54 ina219_state->Battery.error = INA219_ERR_NONE;
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
55 if (ina219_b_dev.i2c_dev.addr) {
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
56 ESP_LOGI(TAG, "Configuring INA219 Battery");
13
c3b29a1dcf1e Changed voltage range to 32 volt. Tried INA219 powersave mode, doesn't work.
Michiel Broek <mbroek@mbse.eu>
parents: 6
diff changeset
57 ESP_ERROR_CHECK(ina219_configure(&ina219_b_dev, INA219_BUS_RANGE_32V, INA219_GAIN_0_125,
3
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
58 INA219_RES_12BIT_1S, INA219_RES_12BIT_1S, INA219_MODE_CONT_SHUNT_BUS));
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
59 ESP_LOGI(TAG, "Calibrating INA219 Battery");
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
60 ESP_ERROR_CHECK(ina219_calibrate(&ina219_b_dev, (float)I_MAX_CURRENT, (float)I_SHUNT_RESISTOR_MILLI_OHM / 1000.0f));
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
61 }
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
62
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
63 ina219_state->Solar.valid = false;
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
64 ina219_state->Solar.fake = (ina219_s_dev.i2c_dev.addr == 0) ? true:false;
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
65 ina219_state->Solar.address = ina219_s_dev.i2c_dev.addr;
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
66 ina219_state->Solar.error = INA219_ERR_NONE;
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
67 if (ina219_s_dev.i2c_dev.addr) {
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
68 ESP_LOGI(TAG, "Configuring INA219 Solar");
13
c3b29a1dcf1e Changed voltage range to 32 volt. Tried INA219 powersave mode, doesn't work.
Michiel Broek <mbroek@mbse.eu>
parents: 6
diff changeset
69 ESP_ERROR_CHECK(ina219_configure(&ina219_s_dev, INA219_BUS_RANGE_32V, INA219_GAIN_0_125,
3
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
70 INA219_RES_12BIT_1S, INA219_RES_12BIT_1S, INA219_MODE_CONT_SHUNT_BUS));
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
71 ESP_LOGI(TAG, "Calibrating INA219 Solar");
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
72 ESP_ERROR_CHECK(ina219_calibrate(&ina219_s_dev, (float)I_MAX_CURRENT, (float)I_SHUNT_RESISTOR_MILLI_OHM / 1000.0f));
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
73 }
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
74
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
75 /* event handler and event group for this task */
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
76 xEventGroupINA219 = xEventGroupCreate();
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
77 EventBits_t uxBits;
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
78
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
79 /*
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
80 * Task loop forever.
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
81 */
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
82 ESP_LOGI(TAG, "Starting loop INA219 sensors 0x%02x %d, 0x%02x %d",
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
83 ina219_state->Battery.address, ina219_state->Battery.fake, ina219_state->Solar.address, ina219_state->Solar.fake);
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
84 while (1) {
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
85
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
86 uxBits = xEventGroupWaitBits(xEventGroupINA219, TASK_INA219_REQUEST_POWER, pdFALSE, pdFALSE, portMAX_DELAY );
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
87
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
88 if (uxBits & TASK_INA219_REQUEST_POWER) {
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
89
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
90 /*
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
91 * Four scenario's:
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
92 * 1. Both sensors present, just use them.
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
93 * 2. Only battery sensor (test environment). Use it and fake
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
94 * the solar chip as if it is charging.
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
95 * 3. Only solar sensor. Use scenario 4, but show measured values.
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
96 * 4. Fake everything.
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
97 */
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
98 if (! ina219_state->Battery.fake) {
13
c3b29a1dcf1e Changed voltage range to 32 volt. Tried INA219 powersave mode, doesn't work.
Michiel Broek <mbroek@mbse.eu>
parents: 6
diff changeset
99 /*
14
2a9f67ecbc72 Fixed wrong voltage and current measurements due to differences between Arduino and ESP-IDF.
Michiel Broek <mbroek@mbse.eu>
parents: 13
diff changeset
100 * We run the ESP32-C3 in Power Save mode, Dynamic Frequency Scaling.
2a9f67ecbc72 Fixed wrong voltage and current measurements due to differences between Arduino and ESP-IDF.
Michiel Broek <mbroek@mbse.eu>
parents: 13
diff changeset
101 * This means a wrong current measurement (too high) unless we let the CPU
2a9f67ecbc72 Fixed wrong voltage and current measurements due to differences between Arduino and ESP-IDF.
Michiel Broek <mbroek@mbse.eu>
parents: 13
diff changeset
102 * rest for a while. The INA219 runs in continuous mode so we get the
2a9f67ecbc72 Fixed wrong voltage and current measurements due to differences between Arduino and ESP-IDF.
Michiel Broek <mbroek@mbse.eu>
parents: 13
diff changeset
103 * results during the vTaskDelay().
13
c3b29a1dcf1e Changed voltage range to 32 volt. Tried INA219 powersave mode, doesn't work.
Michiel Broek <mbroek@mbse.eu>
parents: 6
diff changeset
104 */
28
5872b972e553 Added INA219 power save mode again, it works now. Don't measure shunt voltage and current, just the shunt voltage and calculate the current.
Michiel Broek <mbroek@mbse.eu>
parents: 24
diff changeset
105 ESP_ERROR_CHECK(ina219_configure(&ina219_b_dev, INA219_BUS_RANGE_32V, INA219_GAIN_0_125,
5872b972e553 Added INA219 power save mode again, it works now. Don't measure shunt voltage and current, just the shunt voltage and calculate the current.
Michiel Broek <mbroek@mbse.eu>
parents: 24
diff changeset
106 INA219_RES_12BIT_1S, INA219_RES_12BIT_1S, INA219_MODE_CONT_SHUNT_BUS));
5872b972e553 Added INA219 power save mode again, it works now. Don't measure shunt voltage and current, just the shunt voltage and calculate the current.
Michiel Broek <mbroek@mbse.eu>
parents: 24
diff changeset
107 vTaskDelay(10 / portTICK_PERIOD_MS);
3
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
108 ESP_ERROR_CHECK(ina219_get_bus_voltage(&ina219_b_dev, &bus_voltage));
24
74609f70411e Reduced I2C master speed from 400 to 100 Khz. Hope to get power-on init for BMP280 more reliable. Added warnings for I2C devices that are missing. Some logmessages reduced to debug log messages. Added extra 10 mSec delays before read shunt voltage in the INA219 task. Removed and reduced log levels in the MQTT task. Show received data events. Remove WiFi total time debug logging, it's ok now.
Michiel Broek <mbroek@mbse.eu>
parents: 14
diff changeset
109 vTaskDelay(10 / portTICK_PERIOD_MS);
3
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
110 ESP_ERROR_CHECK(ina219_get_shunt_voltage(&ina219_b_dev, &shunt_voltage));
28
5872b972e553 Added INA219 power save mode again, it works now. Don't measure shunt voltage and current, just the shunt voltage and calculate the current.
Michiel Broek <mbroek@mbse.eu>
parents: 24
diff changeset
111 /*
5872b972e553 Added INA219 power save mode again, it works now. Don't measure shunt voltage and current, just the shunt voltage and calculate the current.
Michiel Broek <mbroek@mbse.eu>
parents: 24
diff changeset
112 * We don't call ina219_get_current(&ina219_b_dev, &current) because it takes
5872b972e553 Added INA219 power save mode again, it works now. Don't measure shunt voltage and current, just the shunt voltage and calculate the current.
Michiel Broek <mbroek@mbse.eu>
parents: 24
diff changeset
113 * a new measurement which usual gives the same results as the shunt voltage.
5872b972e553 Added INA219 power save mode again, it works now. Don't measure shunt voltage and current, just the shunt voltage and calculate the current.
Michiel Broek <mbroek@mbse.eu>
parents: 24
diff changeset
114 * So just calculate the current.
5872b972e553 Added INA219 power save mode again, it works now. Don't measure shunt voltage and current, just the shunt voltage and calculate the current.
Michiel Broek <mbroek@mbse.eu>
parents: 24
diff changeset
115 */
5872b972e553 Added INA219 power save mode again, it works now. Don't measure shunt voltage and current, just the shunt voltage and calculate the current.
Michiel Broek <mbroek@mbse.eu>
parents: 24
diff changeset
116 ESP_ERROR_CHECK(ina219_configure(&ina219_b_dev, INA219_BUS_RANGE_32V, INA219_GAIN_0_125,
5872b972e553 Added INA219 power save mode again, it works now. Don't measure shunt voltage and current, just the shunt voltage and calculate the current.
Michiel Broek <mbroek@mbse.eu>
parents: 24
diff changeset
117 INA219_RES_12BIT_1S, INA219_RES_12BIT_1S, INA219_MODE_POWER_DOWN));
5872b972e553 Added INA219 power save mode again, it works now. Don't measure shunt voltage and current, just the shunt voltage and calculate the current.
Michiel Broek <mbroek@mbse.eu>
parents: 24
diff changeset
118 ESP_LOGI(TAG, "Battery VBUS: %.03f V, VSHUNT: %.02f mV, IBUS: %.01f mA", bus_voltage, shunt_voltage * 1000, shunt_voltage * 10000);
3
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
119 }
5
b1f38105ca7e Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents: 3
diff changeset
120 if (xSemaphoreTake(xSemaphoreINA219, 25) == pdTRUE) {
b1f38105ca7e Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents: 3
diff changeset
121 if (ina219_state->Battery.fake) {
b1f38105ca7e Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents: 3
diff changeset
122 ina219_state->Battery.volts = 13.21;
b1f38105ca7e Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents: 3
diff changeset
123 if (ready_WiFi()) {
b1f38105ca7e Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents: 3
diff changeset
124 ina219_state->Battery.shunt = 0.00785;
b1f38105ca7e Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents: 3
diff changeset
125 ina219_state->Battery.current = 78.5;
b1f38105ca7e Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents: 3
diff changeset
126 } else {
b1f38105ca7e Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents: 3
diff changeset
127 ina219_state->Battery.shunt = 0.00182;
b1f38105ca7e Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents: 3
diff changeset
128 ina219_state->Battery.current = 18.2;
b1f38105ca7e Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents: 3
diff changeset
129 }
b1f38105ca7e Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents: 3
diff changeset
130 } else {
b1f38105ca7e Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents: 3
diff changeset
131 ina219_state->Battery.volts = bus_voltage;
b1f38105ca7e Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents: 3
diff changeset
132 ina219_state->Battery.shunt = shunt_voltage;
28
5872b972e553 Added INA219 power save mode again, it works now. Don't measure shunt voltage and current, just the shunt voltage and calculate the current.
Michiel Broek <mbroek@mbse.eu>
parents: 24
diff changeset
133 ina219_state->Battery.current = shunt_voltage * 10000;
5
b1f38105ca7e Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents: 3
diff changeset
134 }
b1f38105ca7e Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents: 3
diff changeset
135 ina219_state->Battery.valid = true;
b1f38105ca7e Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents: 3
diff changeset
136 xSemaphoreGive(xSemaphoreINA219);
b1f38105ca7e Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents: 3
diff changeset
137 }
b1f38105ca7e Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents: 3
diff changeset
138
3
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
139 if (! ina219_state->Solar.fake) {
28
5872b972e553 Added INA219 power save mode again, it works now. Don't measure shunt voltage and current, just the shunt voltage and calculate the current.
Michiel Broek <mbroek@mbse.eu>
parents: 24
diff changeset
140 ESP_ERROR_CHECK(ina219_configure(&ina219_s_dev, INA219_BUS_RANGE_32V, INA219_GAIN_0_125,
5872b972e553 Added INA219 power save mode again, it works now. Don't measure shunt voltage and current, just the shunt voltage and calculate the current.
Michiel Broek <mbroek@mbse.eu>
parents: 24
diff changeset
141 INA219_RES_12BIT_1S, INA219_RES_12BIT_1S, INA219_MODE_CONT_SHUNT_BUS));
5872b972e553 Added INA219 power save mode again, it works now. Don't measure shunt voltage and current, just the shunt voltage and calculate the current.
Michiel Broek <mbroek@mbse.eu>
parents: 24
diff changeset
142 vTaskDelay(10 / portTICK_PERIOD_MS);
3
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
143 ESP_ERROR_CHECK(ina219_get_bus_voltage(&ina219_s_dev, &bus_voltage));
24
74609f70411e Reduced I2C master speed from 400 to 100 Khz. Hope to get power-on init for BMP280 more reliable. Added warnings for I2C devices that are missing. Some logmessages reduced to debug log messages. Added extra 10 mSec delays before read shunt voltage in the INA219 task. Removed and reduced log levels in the MQTT task. Show received data events. Remove WiFi total time debug logging, it's ok now.
Michiel Broek <mbroek@mbse.eu>
parents: 14
diff changeset
144 vTaskDelay(10 / portTICK_PERIOD_MS);
3
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
145 ESP_ERROR_CHECK(ina219_get_shunt_voltage(&ina219_s_dev, &shunt_voltage));
28
5872b972e553 Added INA219 power save mode again, it works now. Don't measure shunt voltage and current, just the shunt voltage and calculate the current.
Michiel Broek <mbroek@mbse.eu>
parents: 24
diff changeset
146 ESP_ERROR_CHECK(ina219_configure(&ina219_s_dev, INA219_BUS_RANGE_32V, INA219_GAIN_0_125,
5872b972e553 Added INA219 power save mode again, it works now. Don't measure shunt voltage and current, just the shunt voltage and calculate the current.
Michiel Broek <mbroek@mbse.eu>
parents: 24
diff changeset
147 INA219_RES_12BIT_1S, INA219_RES_12BIT_1S, INA219_MODE_POWER_DOWN));
5872b972e553 Added INA219 power save mode again, it works now. Don't measure shunt voltage and current, just the shunt voltage and calculate the current.
Michiel Broek <mbroek@mbse.eu>
parents: 24
diff changeset
148 ESP_LOGI(TAG, " Solar VBUS: %.03f V, VSHUNT: %.02f mV, IBUS: %.01f mA", bus_voltage, shunt_voltage * 1000, shunt_voltage * 10000);
3
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
149 }
5
b1f38105ca7e Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents: 3
diff changeset
150 if (xSemaphoreTake(xSemaphoreINA219, 25) == pdTRUE) {
b1f38105ca7e Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents: 3
diff changeset
151 if (! ina219_state->Solar.fake && ! ina219_state->Battery.fake) {
b1f38105ca7e Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents: 3
diff changeset
152 ina219_state->Solar.volts = bus_voltage;
b1f38105ca7e Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents: 3
diff changeset
153 ina219_state->Solar.shunt = shunt_voltage;
28
5872b972e553 Added INA219 power save mode again, it works now. Don't measure shunt voltage and current, just the shunt voltage and calculate the current.
Michiel Broek <mbroek@mbse.eu>
parents: 24
diff changeset
154 ina219_state->Solar.current = shunt_voltage * 10000;
5
b1f38105ca7e Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents: 3
diff changeset
155 } else if (ina219_state->Solar.fake && ! ina219_state->Battery.fake) {
b1f38105ca7e Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents: 3
diff changeset
156 ina219_state->Solar.volts = ina219_state->Battery.volts + 0.78;
b1f38105ca7e Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents: 3
diff changeset
157 ina219_state->Solar.shunt = 0.02341;
b1f38105ca7e Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents: 3
diff changeset
158 ina219_state->Solar.current = 234.1;
b1f38105ca7e Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents: 3
diff changeset
159 } else {
b1f38105ca7e Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents: 3
diff changeset
160 ina219_state->Solar.volts = 13.98;
b1f38105ca7e Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents: 3
diff changeset
161 ina219_state->Solar.shunt = 0.02341;
b1f38105ca7e Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents: 3
diff changeset
162 ina219_state->Solar.current = 234.1;
3
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
163 }
5
b1f38105ca7e Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents: 3
diff changeset
164 ina219_state->Solar.valid = true;
6
bad3414f7bc4 Added the getTempBaro and getVoltsCurrent functions to process the measured data. Added some main task code. Added subscribe to MQTT events.
Michiel Broek <mbroek@mbse.eu>
parents: 5
diff changeset
165
bad3414f7bc4 Added the getTempBaro and getVoltsCurrent functions to process the measured data. Added some main task code. Added subscribe to MQTT events.
Michiel Broek <mbroek@mbse.eu>
parents: 5
diff changeset
166 /*
bad3414f7bc4 Added the getTempBaro and getVoltsCurrent functions to process the measured data. Added some main task code. Added subscribe to MQTT events.
Michiel Broek <mbroek@mbse.eu>
parents: 5
diff changeset
167 * Now update the outer state
bad3414f7bc4 Added the getTempBaro and getVoltsCurrent functions to process the measured data. Added some main task code. Added subscribe to MQTT events.
Michiel Broek <mbroek@mbse.eu>
parents: 5
diff changeset
168 */
bad3414f7bc4 Added the getTempBaro and getVoltsCurrent functions to process the measured data. Added some main task code. Added subscribe to MQTT events.
Michiel Broek <mbroek@mbse.eu>
parents: 5
diff changeset
169 ina219_state->valid = (ina219_state->Battery.valid && ina219_state->Solar.valid);
bad3414f7bc4 Added the getTempBaro and getVoltsCurrent functions to process the measured data. Added some main task code. Added subscribe to MQTT events.
Michiel Broek <mbroek@mbse.eu>
parents: 5
diff changeset
170 ina219_state->fake = (ina219_state->Battery.fake || ina219_state->Solar.fake);
bad3414f7bc4 Added the getTempBaro and getVoltsCurrent functions to process the measured data. Added some main task code. Added subscribe to MQTT events.
Michiel Broek <mbroek@mbse.eu>
parents: 5
diff changeset
171 ina219_state->error = ina219_state->Battery.error;
bad3414f7bc4 Added the getTempBaro and getVoltsCurrent functions to process the measured data. Added some main task code. Added subscribe to MQTT events.
Michiel Broek <mbroek@mbse.eu>
parents: 5
diff changeset
172 if (ina219_state->error == 0)
bad3414f7bc4 Added the getTempBaro and getVoltsCurrent functions to process the measured data. Added some main task code. Added subscribe to MQTT events.
Michiel Broek <mbroek@mbse.eu>
parents: 5
diff changeset
173 ina219_state->error = ina219_state->Solar.error;
5
b1f38105ca7e Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents: 3
diff changeset
174 xSemaphoreGive(xSemaphoreINA219);
3
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
175 }
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
176
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
177 xEventGroupClearBits(xEventGroupINA219, TASK_INA219_REQUEST_POWER);
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
178 xEventGroupSetBits(xEventGroupINA219, TASK_INA219_REQUEST_DONE);
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
179 }
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
180 }
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
181 }

mercurial