Fri, 31 Mar 2023 20:31:12 +0200
Added volts/current loop calculations. Added millis() timer running on the hardware clock. Completed most of the main state loop. Added MQTT wait for disconnect. MQTT disconnect in two passes, disconnect and stop.
1
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
1 | /** |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
2 | * @file iotbalkon.c |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
3 | * @brief iotbalkon project. |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
4 | */ |
0 | 5 | |
1
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
6 | #include "config.h" |
0 | 7 | |
8 | static const char *TAG = "iotbalkon"; | |
9 | ||
3
e5d91caa6ab4
Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
1
diff
changeset
|
10 | #define State_Init 0 |
e5d91caa6ab4
Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
1
diff
changeset
|
11 | #define State_Connect 1 |
e5d91caa6ab4
Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
1
diff
changeset
|
12 | #define State_Working 2 |
e5d91caa6ab4
Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
1
diff
changeset
|
13 | #define State_WorkDone 3 |
e5d91caa6ab4
Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
1
diff
changeset
|
14 | #define State_Stop 4 |
e5d91caa6ab4
Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
1
diff
changeset
|
15 | #define State_Wait 5 |
e5d91caa6ab4
Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
1
diff
changeset
|
16 | #define State_Measure 6 |
e5d91caa6ab4
Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
1
diff
changeset
|
17 | #define State_GoSleep 7 |
e5d91caa6ab4
Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
1
diff
changeset
|
18 | |
1
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
19 | |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
20 | static TaskHandle_t xTaskBMP280 = NULL; |
3
e5d91caa6ab4
Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
1
diff
changeset
|
21 | static TaskHandle_t xTaskINA219 = NULL; |
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:
4
diff
changeset
|
22 | static TaskHandle_t xTaskMQTT = NULL; |
4 | 23 | static TaskHandle_t xTaskWifi = NULL; |
1
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
24 | |
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:
4
diff
changeset
|
25 | #define MAX_LOOPS 32 |
7
2b337dd92f25
Added volts/current loop calculations. Added millis() timer running on the hardware clock. Completed most of the main state loop. Added MQTT wait for disconnect. MQTT disconnect in two passes, disconnect and stop.
Michiel Broek <mbroek@mbse.eu>
parents:
6
diff
changeset
|
26 | #define SUB_TIME 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:
4
diff
changeset
|
27 | |
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
|
28 | float temperature; |
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
|
29 | float pressure; |
7
2b337dd92f25
Added volts/current loop calculations. Added millis() timer running on the hardware clock. Completed most of the main state loop. Added MQTT wait for disconnect. MQTT disconnect in two passes, disconnect and stop.
Michiel Broek <mbroek@mbse.eu>
parents:
6
diff
changeset
|
30 | float solarVolts, solarCurrent, solarPower; |
2b337dd92f25
Added volts/current loop calculations. Added millis() timer running on the hardware clock. Completed most of the main state loop. Added MQTT wait for disconnect. MQTT disconnect in two passes, disconnect and stop.
Michiel Broek <mbroek@mbse.eu>
parents:
6
diff
changeset
|
31 | float batteryVolts, batteryCurrent, batteryPower; |
2b337dd92f25
Added volts/current loop calculations. Added millis() timer running on the hardware clock. Completed most of the main state loop. Added MQTT wait for disconnect. MQTT disconnect in two passes, disconnect and stop.
Michiel Broek <mbroek@mbse.eu>
parents:
6
diff
changeset
|
32 | int batteryState; |
2b337dd92f25
Added volts/current loop calculations. Added millis() timer running on the hardware clock. Completed most of the main state loop. Added MQTT wait for disconnect. MQTT disconnect in two passes, disconnect and stop.
Michiel Broek <mbroek@mbse.eu>
parents:
6
diff
changeset
|
33 | float s_Volts[MAX_LOOPS + 1]; |
2b337dd92f25
Added volts/current loop calculations. Added millis() timer running on the hardware clock. Completed most of the main state loop. Added MQTT wait for disconnect. MQTT disconnect in two passes, disconnect and stop.
Michiel Broek <mbroek@mbse.eu>
parents:
6
diff
changeset
|
34 | float s_Current[MAX_LOOPS + 1]; |
2b337dd92f25
Added volts/current loop calculations. Added millis() timer running on the hardware clock. Completed most of the main state loop. Added MQTT wait for disconnect. MQTT disconnect in two passes, disconnect and stop.
Michiel Broek <mbroek@mbse.eu>
parents:
6
diff
changeset
|
35 | float b_Volts[MAX_LOOPS + 1]; |
2b337dd92f25
Added volts/current loop calculations. Added millis() timer running on the hardware clock. Completed most of the main state loop. Added MQTT wait for disconnect. MQTT disconnect in two passes, disconnect and stop.
Michiel Broek <mbroek@mbse.eu>
parents:
6
diff
changeset
|
36 | float b_Current[MAX_LOOPS + 1]; |
2b337dd92f25
Added volts/current loop calculations. Added millis() timer running on the hardware clock. Completed most of the main state loop. Added MQTT wait for disconnect. MQTT disconnect in two passes, disconnect and stop.
Michiel Broek <mbroek@mbse.eu>
parents:
6
diff
changeset
|
37 | bool m_Valid[MAX_LOOPS + 1]; |
2b337dd92f25
Added volts/current loop calculations. Added millis() timer running on the hardware clock. Completed most of the main state loop. Added MQTT wait for disconnect. MQTT disconnect in two passes, disconnect and stop.
Michiel Broek <mbroek@mbse.eu>
parents:
6
diff
changeset
|
38 | uint64_t m_Time[MAX_LOOPS + 1]; |
2b337dd92f25
Added volts/current loop calculations. Added millis() timer running on the hardware clock. Completed most of the main state loop. Added MQTT wait for disconnect. MQTT disconnect in two passes, disconnect and stop.
Michiel Broek <mbroek@mbse.eu>
parents:
6
diff
changeset
|
39 | uint64_t gLastTime; |
2b337dd92f25
Added volts/current loop calculations. Added millis() timer running on the hardware clock. Completed most of the main state loop. Added MQTT wait for disconnect. MQTT disconnect in two passes, disconnect and stop.
Michiel Broek <mbroek@mbse.eu>
parents:
6
diff
changeset
|
40 | uint64_t gTimeNext; |
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
|
41 | |
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
|
42 | uint8_t loopno = 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
|
43 | uint8_t loops = 0; |
7
2b337dd92f25
Added volts/current loop calculations. Added millis() timer running on the hardware clock. Completed most of the main state loop. Added MQTT wait for disconnect. MQTT disconnect in two passes, disconnect and stop.
Michiel Broek <mbroek@mbse.eu>
parents:
6
diff
changeset
|
44 | uint8_t ST_LOOPS = 6; |
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
|
45 | |
7
2b337dd92f25
Added volts/current loop calculations. Added millis() timer running on the hardware clock. Completed most of the main state loop. Added MQTT wait for disconnect. MQTT disconnect in two passes, disconnect and stop.
Michiel Broek <mbroek@mbse.eu>
parents:
6
diff
changeset
|
46 | bool WorkAgain = false; |
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
|
47 | int DisCounter = 0; |
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:
4
diff
changeset
|
48 | |
1
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
49 | extern BMP280_State *bmp280_state; ///< I2C state |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
50 | extern SemaphoreHandle_t xSemaphoreBMP280; ///< I2C lock semaphore |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
51 | extern bmp280_params_t bmp280_params; |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
52 | extern bmp280_t bmp280_dev; |
3
e5d91caa6ab4
Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
1
diff
changeset
|
53 | extern SemaphoreHandle_t xSemaphoreINA219; |
e5d91caa6ab4
Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
1
diff
changeset
|
54 | extern ina219_t ina219_b_dev; |
e5d91caa6ab4
Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
1
diff
changeset
|
55 | extern ina219_t ina219_s_dev; |
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
|
56 | extern INA219_State *ina219_state; |
4 | 57 | extern SemaphoreHandle_t xSemaphoreWiFi; |
58 | extern WIFI_State *wifi_state; ///< WiFi state | |
59 | ||
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:
4
diff
changeset
|
60 | uint32_t Alarm = 0; |
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:
4
diff
changeset
|
61 | |
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:
4
diff
changeset
|
62 | /* |
7
2b337dd92f25
Added volts/current loop calculations. Added millis() timer running on the hardware clock. Completed most of the main state loop. Added MQTT wait for disconnect. MQTT disconnect in two passes, disconnect and stop.
Michiel Broek <mbroek@mbse.eu>
parents:
6
diff
changeset
|
63 | * Alarm bits |
2b337dd92f25
Added volts/current loop calculations. Added millis() timer running on the hardware clock. Completed most of the main state loop. Added MQTT wait for disconnect. MQTT disconnect in two passes, disconnect and stop.
Michiel Broek <mbroek@mbse.eu>
parents:
6
diff
changeset
|
64 | */ |
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:
4
diff
changeset
|
65 | #define AL_ACCULOW 0x01 |
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:
4
diff
changeset
|
66 | #define AL_NOWIFI 0x02 |
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:
4
diff
changeset
|
67 | |
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:
4
diff
changeset
|
68 | |
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:
4
diff
changeset
|
69 | #define ST_INTERVAL 10000 |
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:
4
diff
changeset
|
70 | #define MAX_LOOPS 32 |
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:
4
diff
changeset
|
71 | |
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:
4
diff
changeset
|
72 | |
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:
4
diff
changeset
|
73 | |
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:
4
diff
changeset
|
74 | // 0% 10% 20% 30% 40% 50% 60% 70% 80% 90% 100% |
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:
4
diff
changeset
|
75 | float Charge_C_5[11] = { 12.40, 12.60, 12.75, 12.95, 13.20, 13.35, 13.55, 13.67, 14.00, 15.25, 15.94 }; |
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:
4
diff
changeset
|
76 | float Charge_C_10[11] = { 12.20, 12.38, 12.60, 12.80, 13.05, 13.20, 13.28, 13.39, 13.60, 14.20, 15.25 }; |
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:
4
diff
changeset
|
77 | float Charge_C_20[11] = { 12.00, 12.07, 12.42, 12.70, 12.85, 13.02, 13.11, 13.15, 13.25, 13.60, 14.15 }; |
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:
4
diff
changeset
|
78 | float Charge_C_40[11] = { 11.55, 11.80, 12.25, 12.57, 12.70, 12.80, 12.90, 12.95, 13.00, 13.20, 13.50 }; |
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:
4
diff
changeset
|
79 | float Rest[11] = { 11.50, 11.70, 11.88, 12.10, 12.22, 12.30, 12.40, 12.50, 12.57, 12.64, 12.72 }; |
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:
4
diff
changeset
|
80 | float Load_C_20[11] = { 11.45, 11.70, 11.80, 12.05, 12.20, 12.28, 12.37, 12.48, 12.55, 12.57, 12.60 }; |
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:
4
diff
changeset
|
81 | float Load_C_10[11] = { 10.98, 11.27, 11.50, 11.65, 11.85, 12.00, 12.10, 12.20, 12.30, 12.40, 12.50 }; |
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:
4
diff
changeset
|
82 | float Load_C_5[11] = { 10.20, 10.65, 10.90, 11.15, 11.35, 11.55, 11.63, 11.75, 11.90, 12.00, 12.08 }; |
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:
4
diff
changeset
|
83 | |
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:
4
diff
changeset
|
84 | |
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:
4
diff
changeset
|
85 | /* |
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:
4
diff
changeset
|
86 | * Calculate the load state of the battery. |
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:
4
diff
changeset
|
87 | */ |
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:
4
diff
changeset
|
88 | void BatteryState(float Voltage, float 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:
4
diff
changeset
|
89 | int i; |
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:
4
diff
changeset
|
90 | |
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:
4
diff
changeset
|
91 | batteryState = 0; |
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:
4
diff
changeset
|
92 | ESP_LOGI(TAG, "Batt %.4fV %.4fmA", Voltage, 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:
4
diff
changeset
|
93 | |
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:
4
diff
changeset
|
94 | if (Current < -750) { |
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:
4
diff
changeset
|
95 | // Load current > C/5, 1A |
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:
4
diff
changeset
|
96 | for (i = 0; i < 10; i++) { |
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:
4
diff
changeset
|
97 | if (Load_C_5[i + 1] >= 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:
4
diff
changeset
|
98 | break; |
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:
4
diff
changeset
|
99 | } |
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:
4
diff
changeset
|
100 | } |
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:
4
diff
changeset
|
101 | batteryState = i * 10; |
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:
4
diff
changeset
|
102 | ESP_LOGI(TAG, "Load C/5 %d%%", batteryState); |
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:
4
diff
changeset
|
103 | |
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:
4
diff
changeset
|
104 | } else if (Current < -375) { |
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:
4
diff
changeset
|
105 | // Load current > C/10, 500mA |
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:
4
diff
changeset
|
106 | for (i = 0; i < 10; i++) { |
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:
4
diff
changeset
|
107 | if (Load_C_10[i + 1] >= 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:
4
diff
changeset
|
108 | break; |
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:
4
diff
changeset
|
109 | } |
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:
4
diff
changeset
|
110 | } |
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:
4
diff
changeset
|
111 | batteryState = i * 10; |
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:
4
diff
changeset
|
112 | ESP_LOGI(TAG, "Load C/10 %d%%", batteryState); |
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:
4
diff
changeset
|
113 | |
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:
4
diff
changeset
|
114 | } else if (Current < -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:
4
diff
changeset
|
115 | // Load current > C/20, 250mA |
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:
4
diff
changeset
|
116 | for (i = 0; i < 10; i++) { |
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:
4
diff
changeset
|
117 | if (Load_C_20[i + 1] >= 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:
4
diff
changeset
|
118 | break; |
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:
4
diff
changeset
|
119 | } |
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:
4
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:
4
diff
changeset
|
121 | batteryState = i * 10; |
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:
4
diff
changeset
|
122 | ESP_LOGI(TAG, "Load C/20 %d%%", batteryState); |
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:
4
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:
4
diff
changeset
|
124 | } else if (Current > 750) { |
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:
4
diff
changeset
|
125 | // Charge > C/5, 1A |
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:
4
diff
changeset
|
126 | for (i = 0; i < 10; i++) { |
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:
4
diff
changeset
|
127 | if (Charge_C_5[i + 1] >= 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:
4
diff
changeset
|
128 | break; |
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:
4
diff
changeset
|
129 | } |
b1f38105ca7e
Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents:
4
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:
4
diff
changeset
|
131 | batteryState = i * 10; |
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:
4
diff
changeset
|
132 | ESP_LOGI(TAG, "Charge C/5 %d%%", batteryState); |
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:
4
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:
4
diff
changeset
|
134 | } else if (Current > 375) { |
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:
4
diff
changeset
|
135 | // Charge > C/10, 500mA |
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:
4
diff
changeset
|
136 | for (i = 0; i < 10; i++) { |
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:
4
diff
changeset
|
137 | if (Charge_C_10[i + 1] >= 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:
4
diff
changeset
|
138 | break; |
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:
4
diff
changeset
|
139 | } |
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:
4
diff
changeset
|
140 | } |
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:
4
diff
changeset
|
141 | batteryState = i * 10; |
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:
4
diff
changeset
|
142 | ESP_LOGI(TAG, "Charge C/10 %d%%", batteryState); |
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:
4
diff
changeset
|
143 | |
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:
4
diff
changeset
|
144 | } else if (Current > 187.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:
4
diff
changeset
|
145 | // Charge > C/20, 250 mA |
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:
4
diff
changeset
|
146 | for (i = 0; i < 10; i++) { |
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:
4
diff
changeset
|
147 | if (Charge_C_20[i + 1] >= 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:
4
diff
changeset
|
148 | break; |
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:
4
diff
changeset
|
149 | } |
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:
4
diff
changeset
|
150 | } |
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:
4
diff
changeset
|
151 | batteryState = i * 10; |
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:
4
diff
changeset
|
152 | ESP_LOGI(TAG, "Charge C/20 %d%%", batteryState); |
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:
4
diff
changeset
|
153 | |
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:
4
diff
changeset
|
154 | } else if (Current > 62.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:
4
diff
changeset
|
155 | // Charge > C/40, 125 mA |
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:
4
diff
changeset
|
156 | for (i = 0; i < 10; i++) { |
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:
4
diff
changeset
|
157 | if (Charge_C_40[i + 1] >= 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:
4
diff
changeset
|
158 | break; |
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:
4
diff
changeset
|
159 | } |
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:
4
diff
changeset
|
160 | } |
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:
4
diff
changeset
|
161 | batteryState = i * 10; |
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:
4
diff
changeset
|
162 | ESP_LOGI(TAG, "Charge C/40 %d%%", batteryState); |
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:
4
diff
changeset
|
163 | |
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:
4
diff
changeset
|
164 | } 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:
4
diff
changeset
|
165 | // Rest |
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:
4
diff
changeset
|
166 | for (i = 0; i < 10; i++) { |
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:
4
diff
changeset
|
167 | if (Rest[i + 1] >= 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:
4
diff
changeset
|
168 | break; |
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:
4
diff
changeset
|
169 | } |
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:
4
diff
changeset
|
170 | } |
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:
4
diff
changeset
|
171 | batteryState = i * 10; |
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:
4
diff
changeset
|
172 | ESP_LOGI(TAG, "Rest %d%%", batteryState); |
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:
4
diff
changeset
|
173 | } |
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:
4
diff
changeset
|
174 | } |
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:
4
diff
changeset
|
175 | |
1
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
176 | |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
177 | |
7
2b337dd92f25
Added volts/current loop calculations. Added millis() timer running on the hardware clock. Completed most of the main state loop. Added MQTT wait for disconnect. MQTT disconnect in two passes, disconnect and stop.
Michiel Broek <mbroek@mbse.eu>
parents:
6
diff
changeset
|
178 | uint64_t millis(void) { |
2b337dd92f25
Added volts/current loop calculations. Added millis() timer running on the hardware clock. Completed most of the main state loop. Added MQTT wait for disconnect. MQTT disconnect in two passes, disconnect and stop.
Michiel Broek <mbroek@mbse.eu>
parents:
6
diff
changeset
|
179 | return esp_timer_get_time() / 1000; |
2b337dd92f25
Added volts/current loop calculations. Added millis() timer running on the hardware clock. Completed most of the main state loop. Added MQTT wait for disconnect. MQTT disconnect in two passes, disconnect and stop.
Michiel Broek <mbroek@mbse.eu>
parents:
6
diff
changeset
|
180 | } |
2b337dd92f25
Added volts/current loop calculations. Added millis() timer running on the hardware clock. Completed most of the main state loop. Added MQTT wait for disconnect. MQTT disconnect in two passes, disconnect and stop.
Michiel Broek <mbroek@mbse.eu>
parents:
6
diff
changeset
|
181 | |
2b337dd92f25
Added volts/current loop calculations. Added millis() timer running on the hardware clock. Completed most of the main state loop. Added MQTT wait for disconnect. MQTT disconnect in two passes, disconnect and stop.
Michiel Broek <mbroek@mbse.eu>
parents:
6
diff
changeset
|
182 | |
2b337dd92f25
Added volts/current loop calculations. Added millis() timer running on the hardware clock. Completed most of the main state loop. Added MQTT wait for disconnect. MQTT disconnect in two passes, disconnect and stop.
Michiel Broek <mbroek@mbse.eu>
parents:
6
diff
changeset
|
183 | |
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
|
184 | /* |
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
|
185 | * Read the temperature and pressure from the BMP280. |
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
|
186 | */ |
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
|
187 | void getTempBaro() { |
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
|
188 | temperature = 20; |
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
|
189 | pressure = 1000; |
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
|
190 | |
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
|
191 | request_bmp280(); |
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
|
192 | |
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
|
193 | while (! ready_bmp280()) { |
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
|
194 | vTaskDelay(10 / portTICK_PERIOD_MS); |
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
|
195 | } |
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
|
196 | |
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
|
197 | if (xSemaphoreTake(xSemaphoreBMP280, 25) == pdTRUE) { |
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
|
198 | temperature = bmp280_state->temperature; |
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
|
199 | pressure = bmp280_state->pressure / 100.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
|
200 | ESP_LOGI(TAG, "Temperature: %.3f Pressure: %.1f hPa\n", temperature, pressure); |
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
|
201 | xSemaphoreGive(xSemaphoreBMP280); |
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
|
202 | } else { |
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
|
203 | ESP_LOGE(TAG, "Can't lock xSemaphoreBMP280"); |
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
|
204 | } |
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
|
205 | } |
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
|
206 | |
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
|
207 | |
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
|
208 | |
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
|
209 | /* |
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
|
210 | * Read voltage and current from both INA219 boards. The results |
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
|
211 | * are stored in an array so that we later can average the results. |
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
|
212 | */ |
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
|
213 | void getVoltsCurrent() { |
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
|
214 | |
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
|
215 | request_ina219(); |
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
|
216 | |
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
|
217 | while (! ready_ina219()) { |
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
|
218 | vTaskDelay(10 / portTICK_PERIOD_MS); |
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
|
219 | } |
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
|
220 | |
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
|
221 | if (xSemaphoreTake(xSemaphoreINA219, 25) == pdTRUE) { |
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
|
222 | |
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
|
223 | s_Volts[loopno] = ina219_state->Solar.volts + (ina219_state->Solar.shunt / 1000); |
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
|
224 | s_Current[loopno] = ina219_state->Solar.current; |
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
|
225 | b_Volts[loopno] = ina219_state->Battery.volts + (ina219_state->Battery.shunt / 1000); |
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
|
226 | b_Current[loopno] = ina219_state->Battery.current; |
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
|
227 | m_Valid[loopno] = ina219_state->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
|
228 | xSemaphoreGive(xSemaphoreINA219); |
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
|
229 | |
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
|
230 | /* |
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
|
231 | * Check for crazy values |
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
|
232 | */ |
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
|
233 | if ((b_Volts[loopno] < 10) || (b_Volts[loopno] > 15.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
|
234 | m_Valid[loopno] = false; |
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
|
235 | } |
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
|
236 | if ((b_Current[loopno] < 0) || (b_Current[loopno] > 2000)) { |
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
|
237 | m_Valid[loopno] = false; |
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
|
238 | } |
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
|
239 | if (s_Volts[loopno] < 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
|
240 | s_Volts[loopno] = 0.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
|
241 | } |
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
|
242 | if (s_Volts[loopno] > 24) { |
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
|
243 | m_Valid[loopno] = false; |
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
|
244 | } |
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
|
245 | if (s_Current[loopno] < 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
|
246 | s_Current[loopno] = 0.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
|
247 | } |
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
|
248 | if (s_Current[loopno] > 2000) { |
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
|
249 | m_Valid[loopno] = false; |
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
|
250 | } |
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
|
251 | } |
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
|
252 | |
7
2b337dd92f25
Added volts/current loop calculations. Added millis() timer running on the hardware clock. Completed most of the main state loop. Added MQTT wait for disconnect. MQTT disconnect in two passes, disconnect and stop.
Michiel Broek <mbroek@mbse.eu>
parents:
6
diff
changeset
|
253 | uint64_t ms = millis(); |
2b337dd92f25
Added volts/current loop calculations. Added millis() timer running on the hardware clock. Completed most of the main state loop. Added MQTT wait for disconnect. MQTT disconnect in two passes, disconnect and stop.
Michiel Broek <mbroek@mbse.eu>
parents:
6
diff
changeset
|
254 | m_Time[loopno] = ms - gLastTime; |
2b337dd92f25
Added volts/current loop calculations. Added millis() timer running on the hardware clock. Completed most of the main state loop. Added MQTT wait for disconnect. MQTT disconnect in two passes, disconnect and stop.
Michiel Broek <mbroek@mbse.eu>
parents:
6
diff
changeset
|
255 | gLastTime = ms; |
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
|
256 | |
7
2b337dd92f25
Added volts/current loop calculations. Added millis() timer running on the hardware clock. Completed most of the main state loop. Added MQTT wait for disconnect. MQTT disconnect in two passes, disconnect and stop.
Michiel Broek <mbroek@mbse.eu>
parents:
6
diff
changeset
|
257 | ESP_LOGI(TAG, "Measure: %d Valid: %s Solar: %.4fV %.4fmA Battery: %.4fV %.4fmA time %llu", |
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
|
258 | loopno, (m_Valid[loopno]) ? "true":"false", s_Volts[loopno], s_Current[loopno], b_Volts[loopno], b_Current[loopno], m_Time[loopno]); |
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
|
259 | |
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
|
260 | if (loopno < (MAX_LOOPS - 1)) |
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
|
261 | loopno++; |
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
|
262 | } |
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
|
263 | |
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
|
264 | |
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
|
265 | |
0 | 266 | void app_main(void) |
267 | { | |
7
2b337dd92f25
Added volts/current loop calculations. Added millis() timer running on the hardware clock. Completed most of the main state loop. Added MQTT wait for disconnect. MQTT disconnect in two passes, disconnect and stop.
Michiel Broek <mbroek@mbse.eu>
parents:
6
diff
changeset
|
268 | uint64_t totalTime, gTimeInMillis; |
2b337dd92f25
Added volts/current loop calculations. Added millis() timer running on the hardware clock. Completed most of the main state loop. Added MQTT wait for disconnect. MQTT disconnect in two passes, disconnect and stop.
Michiel Broek <mbroek@mbse.eu>
parents:
6
diff
changeset
|
269 | |
1
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
270 | #ifdef CONFIG_CODE_PRODUCTION |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
271 | ESP_LOGI(TAG, "Starting production"); |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
272 | #endif |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
273 | #ifdef CONFIG_CODE_TESTING |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
274 | ESP_LOGI(TAG, "Starting testing"); |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
275 | #endif |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
276 | |
7
2b337dd92f25
Added volts/current loop calculations. Added millis() timer running on the hardware clock. Completed most of the main state loop. Added MQTT wait for disconnect. MQTT disconnect in two passes, disconnect and stop.
Michiel Broek <mbroek@mbse.eu>
parents:
6
diff
changeset
|
277 | gLastTime = millis(); |
1
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
278 | ESP_ERROR_CHECK(i2cdev_init()); |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
279 | |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
280 | bmp280_init_default_params(&bmp280_params); |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
281 | memset(&bmp280_dev, 0, sizeof(bmp280_t)); |
3
e5d91caa6ab4
Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
1
diff
changeset
|
282 | memset(&ina219_b_dev, 0, sizeof(ina219_t)); |
e5d91caa6ab4
Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
1
diff
changeset
|
283 | memset(&ina219_s_dev, 0, sizeof(ina219_t)); |
1
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
284 | |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
285 | i2c_dev_t dev = { 0 }; |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
286 | dev.cfg.sda_io_num = CONFIG_I2C_MASTER_SDA; |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
287 | dev.cfg.scl_io_num = CONFIG_I2C_MASTER_SCL; |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
288 | dev.cfg.master.clk_speed = 1000000; |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
289 | |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
290 | dev.addr = 0x39; |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
291 | if (i2c_dev_probe(&dev, I2C_DEV_WRITE) == 0) { |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
292 | ESP_LOGI(TAG, "Found ADPS-9930"); |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
293 | } |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
294 | dev.addr = 0x40; |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
295 | if (i2c_dev_probe(&dev, I2C_DEV_WRITE) == 0) { |
3
e5d91caa6ab4
Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
1
diff
changeset
|
296 | ESP_ERROR_CHECK(ina219_init_desc(&ina219_b_dev, 0x40, 0, 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:
1
diff
changeset
|
297 | ESP_ERROR_CHECK(ina219_init(&ina219_b_dev)); |
1
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
298 | ESP_LOGI(TAG, "Found INA219 Battery"); |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
299 | } |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
300 | dev.addr = 0x41; |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
301 | if (i2c_dev_probe(&dev, I2C_DEV_WRITE) == 0) { |
3
e5d91caa6ab4
Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
1
diff
changeset
|
302 | ESP_ERROR_CHECK(ina219_init_desc(&ina219_s_dev, 0x41, 0, 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:
1
diff
changeset
|
303 | ESP_ERROR_CHECK(ina219_init(&ina219_s_dev)); |
1
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
304 | ESP_LOGI(TAG, "Found INA219 Solar"); |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
305 | } |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
306 | dev.addr = 0x76; |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
307 | if (i2c_dev_probe(&dev, I2C_DEV_WRITE) == 0) { |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
308 | ESP_ERROR_CHECK(bmp280_init_desc(&bmp280_dev, BMP280_I2C_ADDRESS_0, 0, CONFIG_I2C_MASTER_SDA, CONFIG_I2C_MASTER_SCL)); |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
309 | ESP_ERROR_CHECK(bmp280_init(&bmp280_dev, &bmp280_params)); |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
310 | ESP_LOGI(TAG, "Found BMP280 @ 0x76 id: 0x%02x", bmp280_dev.id); |
3
e5d91caa6ab4
Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
1
diff
changeset
|
311 | } else { |
e5d91caa6ab4
Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
1
diff
changeset
|
312 | dev.addr = 0x77; |
e5d91caa6ab4
Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
1
diff
changeset
|
313 | if (i2c_dev_probe(&dev, I2C_DEV_WRITE) == 0) { |
e5d91caa6ab4
Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
1
diff
changeset
|
314 | ESP_ERROR_CHECK(bmp280_init_desc(&bmp280_dev, BMP280_I2C_ADDRESS_1, 0, 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:
1
diff
changeset
|
315 | ESP_ERROR_CHECK(bmp280_init(&bmp280_dev, &bmp280_params)); |
e5d91caa6ab4
Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
1
diff
changeset
|
316 | ESP_LOGI(TAG, "Found BMP280 @ 0x77 id: 0x%02x", bmp280_dev.id); |
e5d91caa6ab4
Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
1
diff
changeset
|
317 | } |
1
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
318 | } |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
319 | |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
320 | /* |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
321 | * Create FreeRTOS tasks |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
322 | */ |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
323 | xSemaphoreBMP280 = xSemaphoreCreateMutex(); |
4 | 324 | xSemaphoreINA219 = xSemaphoreCreateMutex(); |
1
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
325 | |
4 | 326 | |
327 | xTaskCreate(&task_bmp280, "task_bmp280", 2560, NULL, 8, &xTaskBMP280); | |
328 | xTaskCreate(&task_ina219, "task_ina219", 2560, NULL, 8, &xTaskINA219); | |
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:
4
diff
changeset
|
329 | // esp_log_level_set("MQTT_CLIENT", ESP_LOG_ERROR); |
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:
4
diff
changeset
|
330 | xTaskCreate(&task_mqtt, "task_mqtt", 4096, NULL, 5, &xTaskMQTT); |
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:
4
diff
changeset
|
331 | // esp_log_level_set("wifi", ESP_LOG_ERROR); |
4 | 332 | xTaskCreate(&task_wifi, "task_wifi", 4096, NULL, 3, &xTaskWifi); |
333 | ||
334 | vTaskDelay(10 / portTICK_PERIOD_MS); | |
1
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
335 | |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
336 | /* |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
337 | * Main application loop. |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
338 | */ |
3
e5d91caa6ab4
Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
1
diff
changeset
|
339 | int State = State_Init; |
e5d91caa6ab4
Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
1
diff
changeset
|
340 | int OldState = State_Init + 1; |
e5d91caa6ab4
Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
1
diff
changeset
|
341 | |
1
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
342 | while (1) { |
3
e5d91caa6ab4
Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
1
diff
changeset
|
343 | if (OldState != State) { |
e5d91caa6ab4
Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
1
diff
changeset
|
344 | ESP_LOGI(TAG, "Switch to state %d", State); |
e5d91caa6ab4
Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
1
diff
changeset
|
345 | OldState = State; |
e5d91caa6ab4
Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
1
diff
changeset
|
346 | } |
7
2b337dd92f25
Added volts/current loop calculations. Added millis() timer running on the hardware clock. Completed most of the main state loop. Added MQTT wait for disconnect. MQTT disconnect in two passes, disconnect and stop.
Michiel Broek <mbroek@mbse.eu>
parents:
6
diff
changeset
|
347 | |
2b337dd92f25
Added volts/current loop calculations. Added millis() timer running on the hardware clock. Completed most of the main state loop. Added MQTT wait for disconnect. MQTT disconnect in two passes, disconnect and stop.
Michiel Broek <mbroek@mbse.eu>
parents:
6
diff
changeset
|
348 | gTimeInMillis = millis(); |
2b337dd92f25
Added volts/current loop calculations. Added millis() timer running on the hardware clock. Completed most of the main state loop. Added MQTT wait for disconnect. MQTT disconnect in two passes, disconnect and stop.
Michiel Broek <mbroek@mbse.eu>
parents:
6
diff
changeset
|
349 | |
3
e5d91caa6ab4
Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
1
diff
changeset
|
350 | switch (State) { |
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
|
351 | case State_Init: getTempBaro(); |
3
e5d91caa6ab4
Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
1
diff
changeset
|
352 | // getLightValues(); |
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
|
353 | getVoltsCurrent(); |
3
e5d91caa6ab4
Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
1
diff
changeset
|
354 | State = State_Connect; |
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
|
355 | DisCounter = 0; |
4 | 356 | request_WiFi(true); |
3
e5d91caa6ab4
Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
1
diff
changeset
|
357 | break; |
e5d91caa6ab4
Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
1
diff
changeset
|
358 | |
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
|
359 | case State_Connect: if (ready_WiFi() && ready_mqtt()) { |
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
|
360 | State = State_Working; |
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
|
361 | Alarm &= ~AL_NOWIFI; |
7
2b337dd92f25
Added volts/current loop calculations. Added millis() timer running on the hardware clock. Completed most of the main state loop. Added MQTT wait for disconnect. MQTT disconnect in two passes, disconnect and stop.
Michiel Broek <mbroek@mbse.eu>
parents:
6
diff
changeset
|
362 | ESP_LOGI(TAG, "Connected counter %d", DisCounter); |
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
|
363 | DisCounter = 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
|
364 | } else { |
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
|
365 | DisCounter++; |
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
|
366 | if (DisCounter > 30) { |
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
|
367 | Alarm |= AL_NOWIFI; |
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
|
368 | request_WiFi(false); |
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
|
369 | State = State_Init; |
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
|
370 | } |
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
|
371 | vTaskDelay(2000 / portTICK_PERIOD_MS); |
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
|
372 | } |
3
e5d91caa6ab4
Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
1
diff
changeset
|
373 | break; |
e5d91caa6ab4
Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
1
diff
changeset
|
374 | |
7
2b337dd92f25
Added volts/current loop calculations. Added millis() timer running on the hardware clock. Completed most of the main state loop. Added MQTT wait for disconnect. MQTT disconnect in two passes, disconnect and stop.
Michiel Broek <mbroek@mbse.eu>
parents:
6
diff
changeset
|
375 | case State_Working: WorkAgain = false; |
3
e5d91caa6ab4
Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
1
diff
changeset
|
376 | |
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
|
377 | // Measure |
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
|
378 | getVoltsCurrent(); |
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:
4
diff
changeset
|
379 | solarVolts = solarCurrent = solarPower = batteryVolts = batteryCurrent = batteryPower = 0; |
7
2b337dd92f25
Added volts/current loop calculations. Added millis() timer running on the hardware clock. Completed most of the main state loop. Added MQTT wait for disconnect. MQTT disconnect in two passes, disconnect and stop.
Michiel Broek <mbroek@mbse.eu>
parents:
6
diff
changeset
|
380 | loops = 0; |
2b337dd92f25
Added volts/current loop calculations. Added millis() timer running on the hardware clock. Completed most of the main state loop. Added MQTT wait for disconnect. MQTT disconnect in two passes, disconnect and stop.
Michiel Broek <mbroek@mbse.eu>
parents:
6
diff
changeset
|
381 | totalTime = 0; |
2b337dd92f25
Added volts/current loop calculations. Added millis() timer running on the hardware clock. Completed most of the main state loop. Added MQTT wait for disconnect. MQTT disconnect in two passes, disconnect and stop.
Michiel Broek <mbroek@mbse.eu>
parents:
6
diff
changeset
|
382 | for (int i = 0; i < loopno; i++) { |
3
e5d91caa6ab4
Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
1
diff
changeset
|
383 | /* |
e5d91caa6ab4
Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
1
diff
changeset
|
384 | * If there are only 2 loops, and the flag that we came from deep-sleep is set |
e5d91caa6ab4
Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
1
diff
changeset
|
385 | * then assume the following: |
e5d91caa6ab4
Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
1
diff
changeset
|
386 | * 1. No current (or very low for the dc converter) during DS_TIME seconds. |
e5d91caa6ab4
Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
1
diff
changeset
|
387 | * 2. Low power during 0.5 second (no WiFi yet). |
e5d91caa6ab4
Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
1
diff
changeset
|
388 | * 3. 5 seconds power usage for the last measurement. |
e5d91caa6ab4
Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
1
diff
changeset
|
389 | * Calculate the average battery current from this. |
e5d91caa6ab4
Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
1
diff
changeset
|
390 | * |
e5d91caa6ab4
Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
1
diff
changeset
|
391 | * If there are more loops and we came from continues running do the following: |
e5d91caa6ab4
Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
1
diff
changeset
|
392 | * 1. Take the current use from all exept the last one, weight loops * 10 seconds. |
e5d91caa6ab4
Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
1
diff
changeset
|
393 | * 2. Take the last one, and weight for 5 seconds. |
e5d91caa6ab4
Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
1
diff
changeset
|
394 | * Calculate the average battery current from this. |
e5d91caa6ab4
Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
1
diff
changeset
|
395 | */ |
7
2b337dd92f25
Added volts/current loop calculations. Added millis() timer running on the hardware clock. Completed most of the main state loop. Added MQTT wait for disconnect. MQTT disconnect in two passes, disconnect and stop.
Michiel Broek <mbroek@mbse.eu>
parents:
6
diff
changeset
|
396 | if (m_Valid[i]) { |
2b337dd92f25
Added volts/current loop calculations. Added millis() timer running on the hardware clock. Completed most of the main state loop. Added MQTT wait for disconnect. MQTT disconnect in two passes, disconnect and stop.
Michiel Broek <mbroek@mbse.eu>
parents:
6
diff
changeset
|
397 | solarVolts += s_Volts[i]; |
2b337dd92f25
Added volts/current loop calculations. Added millis() timer running on the hardware clock. Completed most of the main state loop. Added MQTT wait for disconnect. MQTT disconnect in two passes, disconnect and stop.
Michiel Broek <mbroek@mbse.eu>
parents:
6
diff
changeset
|
398 | solarCurrent += s_Current[i]; |
2b337dd92f25
Added volts/current loop calculations. Added millis() timer running on the hardware clock. Completed most of the main state loop. Added MQTT wait for disconnect. MQTT disconnect in two passes, disconnect and stop.
Michiel Broek <mbroek@mbse.eu>
parents:
6
diff
changeset
|
399 | batteryVolts += b_Volts[i]; |
2b337dd92f25
Added volts/current loop calculations. Added millis() timer running on the hardware clock. Completed most of the main state loop. Added MQTT wait for disconnect. MQTT disconnect in two passes, disconnect and stop.
Michiel Broek <mbroek@mbse.eu>
parents:
6
diff
changeset
|
400 | if (i == (loopno - 1)) { |
2b337dd92f25
Added volts/current loop calculations. Added millis() timer running on the hardware clock. Completed most of the main state loop. Added MQTT wait for disconnect. MQTT disconnect in two passes, disconnect and stop.
Michiel Broek <mbroek@mbse.eu>
parents:
6
diff
changeset
|
401 | // Add the extra time |
2b337dd92f25
Added volts/current loop calculations. Added millis() timer running on the hardware clock. Completed most of the main state loop. Added MQTT wait for disconnect. MQTT disconnect in two passes, disconnect and stop.
Michiel Broek <mbroek@mbse.eu>
parents:
6
diff
changeset
|
402 | m_Time[i] += SUB_TIME; |
2b337dd92f25
Added volts/current loop calculations. Added millis() timer running on the hardware clock. Completed most of the main state loop. Added MQTT wait for disconnect. MQTT disconnect in two passes, disconnect and stop.
Michiel Broek <mbroek@mbse.eu>
parents:
6
diff
changeset
|
403 | } |
2b337dd92f25
Added volts/current loop calculations. Added millis() timer running on the hardware clock. Completed most of the main state loop. Added MQTT wait for disconnect. MQTT disconnect in two passes, disconnect and stop.
Michiel Broek <mbroek@mbse.eu>
parents:
6
diff
changeset
|
404 | batteryCurrent += b_Current[i] * m_Time[i]; |
2b337dd92f25
Added volts/current loop calculations. Added millis() timer running on the hardware clock. Completed most of the main state loop. Added MQTT wait for disconnect. MQTT disconnect in two passes, disconnect and stop.
Michiel Broek <mbroek@mbse.eu>
parents:
6
diff
changeset
|
405 | totalTime += m_Time[i]; |
2b337dd92f25
Added volts/current loop calculations. Added millis() timer running on the hardware clock. Completed most of the main state loop. Added MQTT wait for disconnect. MQTT disconnect in two passes, disconnect and stop.
Michiel Broek <mbroek@mbse.eu>
parents:
6
diff
changeset
|
406 | loops++; |
2b337dd92f25
Added volts/current loop calculations. Added millis() timer running on the hardware clock. Completed most of the main state loop. Added MQTT wait for disconnect. MQTT disconnect in two passes, disconnect and stop.
Michiel Broek <mbroek@mbse.eu>
parents:
6
diff
changeset
|
407 | } |
2b337dd92f25
Added volts/current loop calculations. Added millis() timer running on the hardware clock. Completed most of the main state loop. Added MQTT wait for disconnect. MQTT disconnect in two passes, disconnect and stop.
Michiel Broek <mbroek@mbse.eu>
parents:
6
diff
changeset
|
408 | } |
3
e5d91caa6ab4
Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
1
diff
changeset
|
409 | |
e5d91caa6ab4
Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
1
diff
changeset
|
410 | // if (EEPROM.read(EM_DS_Active)) { |
e5d91caa6ab4
Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
1
diff
changeset
|
411 | // totalTime += EEPROM.read(EM_DS_Time) * 1000; |
e5d91caa6ab4
Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
1
diff
changeset
|
412 | // batteryCurrent += DS_CURRENT * EEPROM.read(EM_DS_Time) * 1000; |
e5d91caa6ab4
Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
1
diff
changeset
|
413 | // // Serial.printf("Added %d totalTime %d\n", EEPROM.read(EM_DS_Time) * 1000, totalTime); |
e5d91caa6ab4
Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
1
diff
changeset
|
414 | // } |
e5d91caa6ab4
Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
1
diff
changeset
|
415 | |
e5d91caa6ab4
Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
1
diff
changeset
|
416 | // If valid measurements |
7
2b337dd92f25
Added volts/current loop calculations. Added millis() timer running on the hardware clock. Completed most of the main state loop. Added MQTT wait for disconnect. MQTT disconnect in two passes, disconnect and stop.
Michiel Broek <mbroek@mbse.eu>
parents:
6
diff
changeset
|
417 | if (loops) { |
2b337dd92f25
Added volts/current loop calculations. Added millis() timer running on the hardware clock. Completed most of the main state loop. Added MQTT wait for disconnect. MQTT disconnect in two passes, disconnect and stop.
Michiel Broek <mbroek@mbse.eu>
parents:
6
diff
changeset
|
418 | solarVolts = solarVolts / loops; |
2b337dd92f25
Added volts/current loop calculations. Added millis() timer running on the hardware clock. Completed most of the main state loop. Added MQTT wait for disconnect. MQTT disconnect in two passes, disconnect and stop.
Michiel Broek <mbroek@mbse.eu>
parents:
6
diff
changeset
|
419 | solarCurrent = solarCurrent / loops; |
2b337dd92f25
Added volts/current loop calculations. Added millis() timer running on the hardware clock. Completed most of the main state loop. Added MQTT wait for disconnect. MQTT disconnect in two passes, disconnect and stop.
Michiel Broek <mbroek@mbse.eu>
parents:
6
diff
changeset
|
420 | solarPower = solarVolts * solarCurrent; |
2b337dd92f25
Added volts/current loop calculations. Added millis() timer running on the hardware clock. Completed most of the main state loop. Added MQTT wait for disconnect. MQTT disconnect in two passes, disconnect and stop.
Michiel Broek <mbroek@mbse.eu>
parents:
6
diff
changeset
|
421 | batteryVolts = batteryVolts / loops; |
2b337dd92f25
Added volts/current loop calculations. Added millis() timer running on the hardware clock. Completed most of the main state loop. Added MQTT wait for disconnect. MQTT disconnect in two passes, disconnect and stop.
Michiel Broek <mbroek@mbse.eu>
parents:
6
diff
changeset
|
422 | batteryCurrent = batteryCurrent / totalTime; |
2b337dd92f25
Added volts/current loop calculations. Added millis() timer running on the hardware clock. Completed most of the main state loop. Added MQTT wait for disconnect. MQTT disconnect in two passes, disconnect and stop.
Michiel Broek <mbroek@mbse.eu>
parents:
6
diff
changeset
|
423 | batteryPower = batteryVolts * batteryCurrent; |
2b337dd92f25
Added volts/current loop calculations. Added millis() timer running on the hardware clock. Completed most of the main state loop. Added MQTT wait for disconnect. MQTT disconnect in two passes, disconnect and stop.
Michiel Broek <mbroek@mbse.eu>
parents:
6
diff
changeset
|
424 | BatteryState(batteryVolts, (0 - batteryCurrent) + solarCurrent); |
3
e5d91caa6ab4
Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
1
diff
changeset
|
425 | |
7
2b337dd92f25
Added volts/current loop calculations. Added millis() timer running on the hardware clock. Completed most of the main state loop. Added MQTT wait for disconnect. MQTT disconnect in two passes, disconnect and stop.
Michiel Broek <mbroek@mbse.eu>
parents:
6
diff
changeset
|
426 | ESP_LOGI(TAG, " Solar Volts: %.4fV Current %.4fmA Power %.4fmW", solarVolts, solarCurrent, solarPower); |
2b337dd92f25
Added volts/current loop calculations. Added millis() timer running on the hardware clock. Completed most of the main state loop. Added MQTT wait for disconnect. MQTT disconnect in two passes, disconnect and stop.
Michiel Broek <mbroek@mbse.eu>
parents:
6
diff
changeset
|
427 | ESP_LOGI(TAG, "Battery Volts: %.4fV Current %.4fmA Power %.4fmW", batteryVolts, batteryCurrent, batteryPower); |
3
e5d91caa6ab4
Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
1
diff
changeset
|
428 | |
7
2b337dd92f25
Added volts/current loop calculations. Added millis() timer running on the hardware clock. Completed most of the main state loop. Added MQTT wait for disconnect. MQTT disconnect in two passes, disconnect and stop.
Michiel Broek <mbroek@mbse.eu>
parents:
6
diff
changeset
|
429 | /* Check alarm conditions */ |
2b337dd92f25
Added volts/current loop calculations. Added millis() timer running on the hardware clock. Completed most of the main state loop. Added MQTT wait for disconnect. MQTT disconnect in two passes, disconnect and stop.
Michiel Broek <mbroek@mbse.eu>
parents:
6
diff
changeset
|
430 | if (batteryState <= 10) { |
2b337dd92f25
Added volts/current loop calculations. Added millis() timer running on the hardware clock. Completed most of the main state loop. Added MQTT wait for disconnect. MQTT disconnect in two passes, disconnect and stop.
Michiel Broek <mbroek@mbse.eu>
parents:
6
diff
changeset
|
431 | Alarm |= AL_ACCULOW; |
2b337dd92f25
Added volts/current loop calculations. Added millis() timer running on the hardware clock. Completed most of the main state loop. Added MQTT wait for disconnect. MQTT disconnect in two passes, disconnect and stop.
Michiel Broek <mbroek@mbse.eu>
parents:
6
diff
changeset
|
432 | } else { |
2b337dd92f25
Added volts/current loop calculations. Added millis() timer running on the hardware clock. Completed most of the main state loop. Added MQTT wait for disconnect. MQTT disconnect in two passes, disconnect and stop.
Michiel Broek <mbroek@mbse.eu>
parents:
6
diff
changeset
|
433 | Alarm &= ~AL_ACCULOW; |
2b337dd92f25
Added volts/current loop calculations. Added millis() timer running on the hardware clock. Completed most of the main state loop. Added MQTT wait for disconnect. MQTT disconnect in two passes, disconnect and stop.
Michiel Broek <mbroek@mbse.eu>
parents:
6
diff
changeset
|
434 | } |
2b337dd92f25
Added volts/current loop calculations. Added millis() timer running on the hardware clock. Completed most of the main state loop. Added MQTT wait for disconnect. MQTT disconnect in two passes, disconnect and stop.
Michiel Broek <mbroek@mbse.eu>
parents:
6
diff
changeset
|
435 | } |
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
|
436 | getTempBaro(); |
7
2b337dd92f25
Added volts/current loop calculations. Added millis() timer running on the hardware clock. Completed most of the main state loop. Added MQTT wait for disconnect. MQTT disconnect in two passes, disconnect and stop.
Michiel Broek <mbroek@mbse.eu>
parents:
6
diff
changeset
|
437 | publish(); |
2b337dd92f25
Added volts/current loop calculations. Added millis() timer running on the hardware clock. Completed most of the main state loop. Added MQTT wait for disconnect. MQTT disconnect in two passes, disconnect and stop.
Michiel Broek <mbroek@mbse.eu>
parents:
6
diff
changeset
|
438 | State = State_WorkDone; |
2b337dd92f25
Added volts/current loop calculations. Added millis() timer running on the hardware clock. Completed most of the main state loop. Added MQTT wait for disconnect. MQTT disconnect in two passes, disconnect and stop.
Michiel Broek <mbroek@mbse.eu>
parents:
6
diff
changeset
|
439 | gTimeNext = millis() + SUB_TIME; |
3
e5d91caa6ab4
Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
1
diff
changeset
|
440 | break; |
e5d91caa6ab4
Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
1
diff
changeset
|
441 | |
7
2b337dd92f25
Added volts/current loop calculations. Added millis() timer running on the hardware clock. Completed most of the main state loop. Added MQTT wait for disconnect. MQTT disconnect in two passes, disconnect and stop.
Michiel Broek <mbroek@mbse.eu>
parents:
6
diff
changeset
|
442 | case State_WorkDone: vTaskDelay(2 / portTICK_PERIOD_MS); |
2b337dd92f25
Added volts/current loop calculations. Added millis() timer running on the hardware clock. Completed most of the main state loop. Added MQTT wait for disconnect. MQTT disconnect in two passes, disconnect and stop.
Michiel Broek <mbroek@mbse.eu>
parents:
6
diff
changeset
|
443 | // Hang around for a while to process the subscriptions. |
2b337dd92f25
Added volts/current loop calculations. Added millis() timer running on the hardware clock. Completed most of the main state loop. Added MQTT wait for disconnect. MQTT disconnect in two passes, disconnect and stop.
Michiel Broek <mbroek@mbse.eu>
parents:
6
diff
changeset
|
444 | if (WorkAgain) { |
2b337dd92f25
Added volts/current loop calculations. Added millis() timer running on the hardware clock. Completed most of the main state loop. Added MQTT wait for disconnect. MQTT disconnect in two passes, disconnect and stop.
Michiel Broek <mbroek@mbse.eu>
parents:
6
diff
changeset
|
445 | // Some command was executed. |
2b337dd92f25
Added volts/current loop calculations. Added millis() timer running on the hardware clock. Completed most of the main state loop. Added MQTT wait for disconnect. MQTT disconnect in two passes, disconnect and stop.
Michiel Broek <mbroek@mbse.eu>
parents:
6
diff
changeset
|
446 | State = State_Working; |
2b337dd92f25
Added volts/current loop calculations. Added millis() timer running on the hardware clock. Completed most of the main state loop. Added MQTT wait for disconnect. MQTT disconnect in two passes, disconnect and stop.
Michiel Broek <mbroek@mbse.eu>
parents:
6
diff
changeset
|
447 | } |
2b337dd92f25
Added volts/current loop calculations. Added millis() timer running on the hardware clock. Completed most of the main state loop. Added MQTT wait for disconnect. MQTT disconnect in two passes, disconnect and stop.
Michiel Broek <mbroek@mbse.eu>
parents:
6
diff
changeset
|
448 | if (gTimeInMillis > gTimeNext) { |
2b337dd92f25
Added volts/current loop calculations. Added millis() timer running on the hardware clock. Completed most of the main state loop. Added MQTT wait for disconnect. MQTT disconnect in two passes, disconnect and stop.
Michiel Broek <mbroek@mbse.eu>
parents:
6
diff
changeset
|
449 | State = State_Stop; |
2b337dd92f25
Added volts/current loop calculations. Added millis() timer running on the hardware clock. Completed most of the main state loop. Added MQTT wait for disconnect. MQTT disconnect in two passes, disconnect and stop.
Michiel Broek <mbroek@mbse.eu>
parents:
6
diff
changeset
|
450 | } |
3
e5d91caa6ab4
Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
1
diff
changeset
|
451 | break; |
e5d91caa6ab4
Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
1
diff
changeset
|
452 | |
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
|
453 | case State_Stop: request_WiFi(false); |
3
e5d91caa6ab4
Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
1
diff
changeset
|
454 | // // Reset values for average current measurement. |
e5d91caa6ab4
Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
1
diff
changeset
|
455 | // HaveIP = false; |
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
|
456 | loopno = 0; |
7
2b337dd92f25
Added volts/current loop calculations. Added millis() timer running on the hardware clock. Completed most of the main state loop. Added MQTT wait for disconnect. MQTT disconnect in two passes, disconnect and stop.
Michiel Broek <mbroek@mbse.eu>
parents:
6
diff
changeset
|
457 | gLastTime = millis(); |
2b337dd92f25
Added volts/current loop calculations. Added millis() timer running on the hardware clock. Completed most of the main state loop. Added MQTT wait for disconnect. MQTT disconnect in two passes, disconnect and stop.
Michiel Broek <mbroek@mbse.eu>
parents:
6
diff
changeset
|
458 | vTaskDelay(10 / portTICK_PERIOD_MS); |
3
e5d91caa6ab4
Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
1
diff
changeset
|
459 | // #if defined(ARDUINO_ESP8266_WEMOS_D1MINI) |
e5d91caa6ab4
Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
1
diff
changeset
|
460 | // WiFi.forceSleepBegin(0); // 0 == forever |
e5d91caa6ab4
Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
1
diff
changeset
|
461 | // #endif |
e5d91caa6ab4
Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
1
diff
changeset
|
462 | |
e5d91caa6ab4
Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
1
diff
changeset
|
463 | /* |
e5d91caa6ab4
Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
1
diff
changeset
|
464 | * If any output is on, use 6 10 seconds loops. |
e5d91caa6ab4
Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
1
diff
changeset
|
465 | * If nothing on, do a deep sleep. |
e5d91caa6ab4
Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
1
diff
changeset
|
466 | */ |
e5d91caa6ab4
Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
1
diff
changeset
|
467 | // if (EEPROM.read(EM_Relay1) || EEPROM.read(EM_Relay2) || EEPROM.read(EM_Dimmer3) || EEPROM.read(EM_Dimmer4)) { |
e5d91caa6ab4
Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
1
diff
changeset
|
468 | // if (EEPROM.read(EM_DS_Active)) { |
e5d91caa6ab4
Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
1
diff
changeset
|
469 | // EEPROM.write(EM_DS_Active, 0); |
e5d91caa6ab4
Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
1
diff
changeset
|
470 | // EEPROM.commit(); |
e5d91caa6ab4
Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
1
diff
changeset
|
471 | // } |
e5d91caa6ab4
Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
1
diff
changeset
|
472 | |
e5d91caa6ab4
Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
1
diff
changeset
|
473 | // // Active mode, 60 seconds loop |
7
2b337dd92f25
Added volts/current loop calculations. Added millis() timer running on the hardware clock. Completed most of the main state loop. Added MQTT wait for disconnect. MQTT disconnect in two passes, disconnect and stop.
Michiel Broek <mbroek@mbse.eu>
parents:
6
diff
changeset
|
474 | ST_LOOPS = 6; |
2b337dd92f25
Added volts/current loop calculations. Added millis() timer running on the hardware clock. Completed most of the main state loop. Added MQTT wait for disconnect. MQTT disconnect in two passes, disconnect and stop.
Michiel Broek <mbroek@mbse.eu>
parents:
6
diff
changeset
|
475 | gTimeNext = millis() + ST_INTERVAL; |
2b337dd92f25
Added volts/current loop calculations. Added millis() timer running on the hardware clock. Completed most of the main state loop. Added MQTT wait for disconnect. MQTT disconnect in two passes, disconnect and stop.
Michiel Broek <mbroek@mbse.eu>
parents:
6
diff
changeset
|
476 | ESP_LOGI(TAG, "Start sleeploops"); |
2b337dd92f25
Added volts/current loop calculations. Added millis() timer running on the hardware clock. Completed most of the main state loop. Added MQTT wait for disconnect. MQTT disconnect in two passes, disconnect and stop.
Michiel Broek <mbroek@mbse.eu>
parents:
6
diff
changeset
|
477 | State = State_Wait; |
3
e5d91caa6ab4
Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
1
diff
changeset
|
478 | // #if Debug == true |
e5d91caa6ab4
Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
1
diff
changeset
|
479 | // Serial.println(F("Start sleeploops")); |
e5d91caa6ab4
Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
1
diff
changeset
|
480 | // #endif |
e5d91caa6ab4
Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
1
diff
changeset
|
481 | // State = State_Wait; |
e5d91caa6ab4
Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
1
diff
changeset
|
482 | // } else { |
e5d91caa6ab4
Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
1
diff
changeset
|
483 | // ds_time = DS_TIME; |
e5d91caa6ab4
Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
1
diff
changeset
|
484 | // if (solarVolts < 6) { |
e5d91caa6ab4
Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
1
diff
changeset
|
485 | // // At night, increase the deep-sleep time. |
e5d91caa6ab4
Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
1
diff
changeset
|
486 | // ds_time *= 4; |
e5d91caa6ab4
Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
1
diff
changeset
|
487 | // } |
e5d91caa6ab4
Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
1
diff
changeset
|
488 | // if ((! EEPROM.read(EM_DS_Active)) || (EEPROM.read(EM_DS_Time) != ds_time)) { |
e5d91caa6ab4
Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
1
diff
changeset
|
489 | // EEPROM.write(EM_DS_Active, 1); |
e5d91caa6ab4
Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
1
diff
changeset
|
490 | // EEPROM.write(EM_DS_Time, ds_time); |
e5d91caa6ab4
Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
1
diff
changeset
|
491 | // EEPROM.commit(); |
e5d91caa6ab4
Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
1
diff
changeset
|
492 | // Serial.println("wrote new deep-sleep settings"); |
e5d91caa6ab4
Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
1
diff
changeset
|
493 | // } |
e5d91caa6ab4
Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
1
diff
changeset
|
494 | // State = State_GoSleep; |
e5d91caa6ab4
Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
1
diff
changeset
|
495 | // } |
e5d91caa6ab4
Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
1
diff
changeset
|
496 | /* |
e5d91caa6ab4
Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
1
diff
changeset
|
497 | * Update CRC and write rtcData. |
e5d91caa6ab4
Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
1
diff
changeset
|
498 | */ |
e5d91caa6ab4
Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
1
diff
changeset
|
499 | // rtcData.crc32 = calculateCRC32((uint8_t*) &rtcData.data, sizeof(rtcData)-4); |
e5d91caa6ab4
Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
1
diff
changeset
|
500 | // #if defined(ARDUINO_ESP8266_WEMOS_D1MINI) |
e5d91caa6ab4
Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
1
diff
changeset
|
501 | // if (ESP.rtcUserMemoryWrite(0, (uint32_t*) &rtcData, sizeof(rtcData))) { |
e5d91caa6ab4
Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
1
diff
changeset
|
502 | // #if Debug == true |
e5d91caa6ab4
Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
1
diff
changeset
|
503 | // Serial.print("Write: "); |
e5d91caa6ab4
Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
1
diff
changeset
|
504 | // printMemory(); |
e5d91caa6ab4
Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
1
diff
changeset
|
505 | // #endif |
e5d91caa6ab4
Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
1
diff
changeset
|
506 | // } else { |
e5d91caa6ab4
Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
1
diff
changeset
|
507 | // Serial.println("Write error rtcData"); |
e5d91caa6ab4
Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
1
diff
changeset
|
508 | // } |
e5d91caa6ab4
Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
1
diff
changeset
|
509 | // #endif |
e5d91caa6ab4
Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
1
diff
changeset
|
510 | break; |
e5d91caa6ab4
Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
1
diff
changeset
|
511 | |
7
2b337dd92f25
Added volts/current loop calculations. Added millis() timer running on the hardware clock. Completed most of the main state loop. Added MQTT wait for disconnect. MQTT disconnect in two passes, disconnect and stop.
Michiel Broek <mbroek@mbse.eu>
parents:
6
diff
changeset
|
512 | case State_Wait: if (gTimeInMillis > gTimeNext) { |
2b337dd92f25
Added volts/current loop calculations. Added millis() timer running on the hardware clock. Completed most of the main state loop. Added MQTT wait for disconnect. MQTT disconnect in two passes, disconnect and stop.
Michiel Broek <mbroek@mbse.eu>
parents:
6
diff
changeset
|
513 | State = State_Measure; |
2b337dd92f25
Added volts/current loop calculations. Added millis() timer running on the hardware clock. Completed most of the main state loop. Added MQTT wait for disconnect. MQTT disconnect in two passes, disconnect and stop.
Michiel Broek <mbroek@mbse.eu>
parents:
6
diff
changeset
|
514 | } |
2b337dd92f25
Added volts/current loop calculations. Added millis() timer running on the hardware clock. Completed most of the main state loop. Added MQTT wait for disconnect. MQTT disconnect in two passes, disconnect and stop.
Michiel Broek <mbroek@mbse.eu>
parents:
6
diff
changeset
|
515 | vTaskDelay(10 / portTICK_PERIOD_MS); |
3
e5d91caa6ab4
Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
1
diff
changeset
|
516 | break; |
e5d91caa6ab4
Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
1
diff
changeset
|
517 | |
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
|
518 | case State_Measure: getVoltsCurrent(); |
3
e5d91caa6ab4
Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
1
diff
changeset
|
519 | // if (isnan(Temperature)) { |
e5d91caa6ab4
Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
1
diff
changeset
|
520 | // getTempHumi(); |
e5d91caa6ab4
Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
1
diff
changeset
|
521 | // } |
7
2b337dd92f25
Added volts/current loop calculations. Added millis() timer running on the hardware clock. Completed most of the main state loop. Added MQTT wait for disconnect. MQTT disconnect in two passes, disconnect and stop.
Michiel Broek <mbroek@mbse.eu>
parents:
6
diff
changeset
|
522 | gTimeNext = millis() + ST_INTERVAL; |
2b337dd92f25
Added volts/current loop calculations. Added millis() timer running on the hardware clock. Completed most of the main state loop. Added MQTT wait for disconnect. MQTT disconnect in two passes, disconnect and stop.
Michiel Broek <mbroek@mbse.eu>
parents:
6
diff
changeset
|
523 | if (loopno >= ST_LOOPS) { |
2b337dd92f25
Added volts/current loop calculations. Added millis() timer running on the hardware clock. Completed most of the main state loop. Added MQTT wait for disconnect. MQTT disconnect in two passes, disconnect and stop.
Michiel Broek <mbroek@mbse.eu>
parents:
6
diff
changeset
|
524 | ESP_LOGI(TAG, "Enough loops, do connect"); |
3
e5d91caa6ab4
Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
1
diff
changeset
|
525 | // getLightValues(); |
7
2b337dd92f25
Added volts/current loop calculations. Added millis() timer running on the hardware clock. Completed most of the main state loop. Added MQTT wait for disconnect. MQTT disconnect in two passes, disconnect and stop.
Michiel Broek <mbroek@mbse.eu>
parents:
6
diff
changeset
|
526 | State = State_Connect; |
2b337dd92f25
Added volts/current loop calculations. Added millis() timer running on the hardware clock. Completed most of the main state loop. Added MQTT wait for disconnect. MQTT disconnect in two passes, disconnect and stop.
Michiel Broek <mbroek@mbse.eu>
parents:
6
diff
changeset
|
527 | DisCounter = 0; |
2b337dd92f25
Added volts/current loop calculations. Added millis() timer running on the hardware clock. Completed most of the main state loop. Added MQTT wait for disconnect. MQTT disconnect in two passes, disconnect and stop.
Michiel Broek <mbroek@mbse.eu>
parents:
6
diff
changeset
|
528 | request_WiFi(true); |
2b337dd92f25
Added volts/current loop calculations. Added millis() timer running on the hardware clock. Completed most of the main state loop. Added MQTT wait for disconnect. MQTT disconnect in two passes, disconnect and stop.
Michiel Broek <mbroek@mbse.eu>
parents:
6
diff
changeset
|
529 | } else { |
2b337dd92f25
Added volts/current loop calculations. Added millis() timer running on the hardware clock. Completed most of the main state loop. Added MQTT wait for disconnect. MQTT disconnect in two passes, disconnect and stop.
Michiel Broek <mbroek@mbse.eu>
parents:
6
diff
changeset
|
530 | State = State_Wait; |
2b337dd92f25
Added volts/current loop calculations. Added millis() timer running on the hardware clock. Completed most of the main state loop. Added MQTT wait for disconnect. MQTT disconnect in two passes, disconnect and stop.
Michiel Broek <mbroek@mbse.eu>
parents:
6
diff
changeset
|
531 | } |
3
e5d91caa6ab4
Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
1
diff
changeset
|
532 | break; |
e5d91caa6ab4
Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
1
diff
changeset
|
533 | |
7
2b337dd92f25
Added volts/current loop calculations. Added millis() timer running on the hardware clock. Completed most of the main state loop. Added MQTT wait for disconnect. MQTT disconnect in two passes, disconnect and stop.
Michiel Broek <mbroek@mbse.eu>
parents:
6
diff
changeset
|
534 | case State_GoSleep: ESP_LOGE(TAG, "Entered GoSleep -- not supported"); |
2b337dd92f25
Added volts/current loop calculations. Added millis() timer running on the hardware clock. Completed most of the main state loop. Added MQTT wait for disconnect. MQTT disconnect in two passes, disconnect and stop.
Michiel Broek <mbroek@mbse.eu>
parents:
6
diff
changeset
|
535 | // ds_time = EEPROM.read(EM_DS_Time); |
3
e5d91caa6ab4
Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
1
diff
changeset
|
536 | // #if Debug == true |
e5d91caa6ab4
Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
1
diff
changeset
|
537 | // Serial.printf("Going to deep-sleep for %d seconds\n", ds_time); |
e5d91caa6ab4
Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
1
diff
changeset
|
538 | // #endif |
e5d91caa6ab4
Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
1
diff
changeset
|
539 | // ESP.deepSleep(ds_time * 1e6); |
e5d91caa6ab4
Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
1
diff
changeset
|
540 | break; |
e5d91caa6ab4
Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
1
diff
changeset
|
541 | } |
e5d91caa6ab4
Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents:
1
diff
changeset
|
542 | vTaskDelay(20 / portTICK_PERIOD_MS); |
1
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
543 | } |
1c9894662795
Added esp-idf-lib for a lot of sensors. Added the basic design for the BMP280 task.
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
544 | // Not reached. |
0 | 545 | } |