Thu, 13 Apr 2023 12:08:24 +0200
Version 0.4.0. Disable normal logging mqtt.
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 | { |
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
|
46 | float bus_voltage, shunt_voltage, current; |
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 | /* |
c3b29a1dcf1e
Changed voltage range to 32 volt. Tried INA219 powersave mode, doesn't work.
Michiel Broek <mbroek@mbse.eu>
parents:
6
diff
changeset
|
100 | * Note, on Arduino power down and resume works, but not on esp-idf. |
c3b29a1dcf1e
Changed voltage range to 32 volt. Tried INA219 powersave mode, doesn't work.
Michiel Broek <mbroek@mbse.eu>
parents:
6
diff
changeset
|
101 | * This could save only 0.5 mA per device. |
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
|
102 | * |
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 | * 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
|
104 | * 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
|
105 | * 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
|
106 | * 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
|
107 | */ |
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
|
108 | vTaskDelay(20 / portTICK_PERIOD_MS); |
3
e5d91caa6ab4
Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
109 | 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
|
110 | 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
|
111 | ESP_ERROR_CHECK(ina219_get_shunt_voltage(&ina219_b_dev, &shunt_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
|
112 | 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
|
113 | ESP_ERROR_CHECK(ina219_get_current(&ina219_b_dev, ¤t)); |
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
|
114 | ESP_LOGI(TAG, "Battery VBUS: %.04f V, VSHUNT: %.04f mV, IBUS: %.04f mA", bus_voltage, shunt_voltage * 1000, current * 1000); |
3
e5d91caa6ab4
Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
115 | } |
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
|
116 | 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
|
117 | 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
|
118 | 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
|
119 | 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
|
120 | 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
|
121 | 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
|
122 | } 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
|
123 | 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
|
124 | 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
|
125 | } |
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.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
|
128 | ina219_state->Battery.shunt = shunt_voltage; |
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
|
129 | ina219_state->Battery.current = current * 1000; |
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
|
130 | } |
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.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
|
132 | 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
|
133 | } |
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 | |
3
e5d91caa6ab4
Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
135 | if (! ina219_state->Solar.fake) { |
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
|
136 | vTaskDelay(20 / portTICK_PERIOD_MS); |
3
e5d91caa6ab4
Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
137 | 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
|
138 | 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
|
139 | ESP_ERROR_CHECK(ina219_get_shunt_voltage(&ina219_s_dev, &shunt_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
|
140 | 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
|
141 | ESP_ERROR_CHECK(ina219_get_current(&ina219_s_dev, ¤t)); |
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
|
142 | ESP_LOGI(TAG, " Solar VBUS: %.04f V, VSHUNT: %.04f mV, IBUS: %.04f mA", bus_voltage, shunt_voltage * 1000, current * 1000); |
3
e5d91caa6ab4
Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
143 | } |
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
|
144 | 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
|
145 | 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
|
146 | 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
|
147 | ina219_state->Solar.shunt = shunt_voltage; |
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
|
148 | ina219_state->Solar.current = current * 1000; |
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
|
149 | } 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
|
150 | 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
|
151 | 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
|
152 | 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
|
153 | } 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
|
154 | 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
|
155 | 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
|
156 | 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
|
157 | } |
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
|
158 | 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
|
159 | |
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
|
160 | /* |
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
|
161 | * 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
|
162 | */ |
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
|
163 | 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
|
164 | 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
|
165 | 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
|
166 | 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
|
167 | 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
|
168 | xSemaphoreGive(xSemaphoreINA219); |
3
e5d91caa6ab4
Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
169 | } |
e5d91caa6ab4
Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
170 | |
e5d91caa6ab4
Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
171 | 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
|
172 | 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
|
173 | } |
e5d91caa6ab4
Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
174 | } |
e5d91caa6ab4
Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
175 | } |