main/task_ina219.c

Thu, 30 Mar 2023 21:55:24 +0200

author
Michiel Broek <mbroek@mbse.eu>
date
Thu, 30 Mar 2023 21:55:24 +0200
changeset 6
bad3414f7bc4
parent 5
b1f38105ca7e
child 13
c3b29a1dcf1e
permissions
-rw-r--r--

Added the getTempBaro and getVoltsCurrent functions to process the measured data. Added some main task code. Added subscribe to MQTT events.

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 {
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
46 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
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");
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
57 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
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");
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
69 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
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) {
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
99 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
100 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
101 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
102 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
103 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
104 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
105 }
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
106 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
107 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
108 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
109 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
110 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
111 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
112 } 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
113 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
114 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
115 }
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 } 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
117 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
118 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
119 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
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 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
122 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
123 }
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
3
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
125 if (! ina219_state->Solar.fake) {
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
126 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
127 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
128 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
129 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
130 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
131 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
132 }
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
133 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
134 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
135 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
136 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
137 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
138 } 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
139 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
140 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
141 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
142 } 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
143 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
144 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
145 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
146 }
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
147 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
148
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
149 /*
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
150 * 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
151 */
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
152 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
153 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
154 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
155 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
156 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
157 xSemaphoreGive(xSemaphoreINA219);
3
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 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
161 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
162 }
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
163 }
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
164 }

mercurial