main/task_ina219.c

Thu, 30 Mar 2023 17:05:05 +0200

author
Michiel Broek <mbroek@mbse.eu>
date
Thu, 30 Mar 2023 17:05:05 +0200
changeset 5
b1f38105ca7e
parent 3
e5d91caa6ab4
child 6
bad3414f7bc4
permissions
-rw-r--r--

Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.

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;
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
18 extern float s_Volts[I_MAX_LOOPS + 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
19 extern float s_Current[I_MAX_LOOPS + 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
20 extern float b_Volts[I_MAX_LOOPS + 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
21 extern float b_Current[I_MAX_LOOPS + 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
22 extern uint8_t loopno;
3
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 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
25 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
26
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
27
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 void request_ina219(void)
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 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
32 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
33 }
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
34
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
35
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
36
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
37 bool ready_ina219(void)
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 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
40 return true;
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
41 return false;
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
42 }
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
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
45
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
46 /*
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
47 * 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
48 */
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
49 void task_ina219(void *pvParameter)
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 float bus_voltage, shunt_voltage, current, power;
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
52
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
53 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
54 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
55
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
56 ina219_state->Battery.valid = false;
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
57 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
58 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
59 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
60 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
61 ESP_LOGI(TAG, "Configuring INA219 Battery");
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
62 ESP_ERROR_CHECK(ina219_configure(&ina219_b_dev, INA219_BUS_RANGE_16V, INA219_GAIN_0_125,
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
63 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
64 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
65 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
66 }
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
67
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
68 ina219_state->Solar.valid = false;
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
69 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
70 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
71 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
72 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
73 ESP_LOGI(TAG, "Configuring INA219 Solar");
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
74 ESP_ERROR_CHECK(ina219_configure(&ina219_s_dev, INA219_BUS_RANGE_16V, INA219_GAIN_0_125,
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
75 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
76 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
77 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
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 /* 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
81 xEventGroupINA219 = xEventGroupCreate();
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
82 EventBits_t uxBits;
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
83
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
84 /*
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
85 * Task loop forever.
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
86 */
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
87 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
88 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
89 while (1) {
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 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
92
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
93 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
94
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
95 /*
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
96 * Four scenario's:
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
97 * 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
98 * 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
99 * 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
100 * 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
101 * 4. Fake everything.
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
102 */
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
103 if (! ina219_state->Battery.fake) {
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
104 ESP_ERROR_CHECK(ina219_get_bus_voltage(&ina219_b_dev, &bus_voltage));
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
105 ESP_ERROR_CHECK(ina219_get_shunt_voltage(&ina219_b_dev, &shunt_voltage));
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
106 ESP_ERROR_CHECK(ina219_get_current(&ina219_b_dev, &current));
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
107 ESP_ERROR_CHECK(ina219_get_power(&ina219_b_dev, &power));
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
108 ESP_LOGI(TAG, "Battery VBUS: %.04f V, VSHUNT: %.04f mV, IBUS: %.04f mA, PBUS: %.04f mW",
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
109 bus_voltage, shunt_voltage * 1000, current * 1000, power * 1000);
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
110 }
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
111 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
112 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
113 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
114 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
115 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
116 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
117 } 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
118 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
119 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
120 }
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 } 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
122 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
123 ina219_state->Battery.shunt = shunt_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
124 ina219_state->Battery.current = current;
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 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
127 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
128 }
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
3
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
130 if (! ina219_state->Solar.fake) {
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
131 ESP_ERROR_CHECK(ina219_get_bus_voltage(&ina219_s_dev, &bus_voltage));
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
132 ESP_ERROR_CHECK(ina219_get_shunt_voltage(&ina219_s_dev, &shunt_voltage));
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
133 ESP_ERROR_CHECK(ina219_get_current(&ina219_s_dev, &current));
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
134 ESP_ERROR_CHECK(ina219_get_power(&ina219_s_dev, &power));
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
135 ESP_LOGI(TAG, " Solar VBUS: %.04f V, VSHUNT: %.04f mV, IBUS: %.04f mA, PBUS: %.04f mW",
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
136 bus_voltage, shunt_voltage * 1000, current * 1000, power * 1000);
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
137 }
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
138 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
139 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
140 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
141 ina219_state->Solar.shunt = shunt_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
142 ina219_state->Solar.current = current;
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
143 } 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
144 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
145 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
146 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
147 } 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
148 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
149 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
150 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
151 }
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
152 ina219_state->Solar.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
153 xSemaphoreGive(xSemaphoreINA219);
3
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
154 }
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
155
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
156 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
157 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
158 }
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
159 }
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
160 }

mercurial