main/iotbalkon.c

Mon, 17 Apr 2023 14:54:35 +0200

author
Michiel Broek <mbroek@mbse.eu>
date
Mon, 17 Apr 2023 14:54:35 +0200
changeset 31
ec5c7794dcd6
parent 30
7448b8dd4288
child 32
84e54b14e7db
permissions
-rw-r--r--

Added basic BLE code.

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
f8b0268c8d0a Initial project setup
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
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
f8b0268c8d0a Initial project setup
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
7
f8b0268c8d0a Initial project setup
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
8 static const char *TAG = "iotbalkon";
f8b0268c8d0a Initial project setup
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
9
14
2a9f67ecbc72 Fixed wrong voltage and current measurements due to differences between Arduino and ESP-IDF.
Michiel Broek <mbroek@mbse.eu>
parents: 12
diff changeset
10 /*
2a9f67ecbc72 Fixed wrong voltage and current measurements due to differences between Arduino and ESP-IDF.
Michiel Broek <mbroek@mbse.eu>
parents: 12
diff changeset
11 * Main State table.
2a9f67ecbc72 Fixed wrong voltage and current measurements due to differences between Arduino and ESP-IDF.
Michiel Broek <mbroek@mbse.eu>
parents: 12
diff changeset
12 */
2a9f67ecbc72 Fixed wrong voltage and current measurements due to differences between Arduino and ESP-IDF.
Michiel Broek <mbroek@mbse.eu>
parents: 12
diff changeset
13 typedef enum {
2a9f67ecbc72 Fixed wrong voltage and current measurements due to differences between Arduino and ESP-IDF.
Michiel Broek <mbroek@mbse.eu>
parents: 12
diff changeset
14 State_Init = 0,
15
64028e178ff1 Splitted wifi and mqtt connect states to make the connections more robust. Protect the mqtt connection request against requests when a request is already done or in progress.
Michiel Broek <mbroek@mbse.eu>
parents: 14
diff changeset
15 State_Connect_Wifi,
64028e178ff1 Splitted wifi and mqtt connect states to make the connections more robust. Protect the mqtt connection request against requests when a request is already done or in progress.
Michiel Broek <mbroek@mbse.eu>
parents: 14
diff changeset
16 State_Connect_MQTT,
14
2a9f67ecbc72 Fixed wrong voltage and current measurements due to differences between Arduino and ESP-IDF.
Michiel Broek <mbroek@mbse.eu>
parents: 12
diff changeset
17 State_Working,
2a9f67ecbc72 Fixed wrong voltage and current measurements due to differences between Arduino and ESP-IDF.
Michiel Broek <mbroek@mbse.eu>
parents: 12
diff changeset
18 State_WorkDone,
15
64028e178ff1 Splitted wifi and mqtt connect states to make the connections more robust. Protect the mqtt connection request against requests when a request is already done or in progress.
Michiel Broek <mbroek@mbse.eu>
parents: 14
diff changeset
19 State_Disconnect_MQTT,
64028e178ff1 Splitted wifi and mqtt connect states to make the connections more robust. Protect the mqtt connection request against requests when a request is already done or in progress.
Michiel Broek <mbroek@mbse.eu>
parents: 14
diff changeset
20 State_Disconnect_Wifi,
14
2a9f67ecbc72 Fixed wrong voltage and current measurements due to differences between Arduino and ESP-IDF.
Michiel Broek <mbroek@mbse.eu>
parents: 12
diff changeset
21 State_Wait,
2a9f67ecbc72 Fixed wrong voltage and current measurements due to differences between Arduino and ESP-IDF.
Michiel Broek <mbroek@mbse.eu>
parents: 12
diff changeset
22 State_Measure,
2a9f67ecbc72 Fixed wrong voltage and current measurements due to differences between Arduino and ESP-IDF.
Michiel Broek <mbroek@mbse.eu>
parents: 12
diff changeset
23 State_GoSleep
2a9f67ecbc72 Fixed wrong voltage and current measurements due to differences between Arduino and ESP-IDF.
Michiel Broek <mbroek@mbse.eu>
parents: 12
diff changeset
24 } Main_State;
3
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents: 1
diff changeset
25
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
26
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
27 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
28 static TaskHandle_t xTaskINA219 = NULL;
12
bb72d448e282 In the esp-idf-lib the adps9930 driver uses a device descriptor structure instead of just i2c_dev_t. Fixed a linking issue. Added APDS9930 task. Added getLightValues function. Added wifi quality value to the MQTT payload. The payload is complete and will be published.
Michiel Broek <mbroek@mbse.eu>
parents: 9
diff changeset
29 static TaskHandle_t xTaskAPDS9930 = 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
30 static TaskHandle_t xTaskMQTT = NULL;
4
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents: 3
diff changeset
31 static TaskHandle_t xTaskWifi = NULL;
30
7448b8dd4288 Preparations for BLE GATT. Added extra time after INA219 is powered on before measurement. Reduced LEDC frequency to 60 Hz, that makes the LED lights less nervous. Hardware mod on output 4, now needs external pulldown resistor.
Michiel Broek <mbroek@mbse.eu>
parents: 29
diff changeset
32 #ifdef CONFIG_ENABLE_BLE_GATT
7448b8dd4288 Preparations for BLE GATT. Added extra time after INA219 is powered on before measurement. Reduced LEDC frequency to 60 Hz, that makes the LED lights less nervous. Hardware mod on output 4, now needs external pulldown resistor.
Michiel Broek <mbroek@mbse.eu>
parents: 29
diff changeset
33 static TaskHandle_t xTaskBLE = NULL;
7448b8dd4288 Preparations for BLE GATT. Added extra time after INA219 is powered on before measurement. Reduced LEDC frequency to 60 Hz, that makes the LED lights less nervous. Hardware mod on output 4, now needs external pulldown resistor.
Michiel Broek <mbroek@mbse.eu>
parents: 29
diff changeset
34 #endif
9
1659bd3c7a2b Added task_out to drive the relays and led lights. Added NVS namespace to store the state of the outputs. Respond to subscribed MQTT topics to set new output values.
Michiel Broek <mbroek@mbse.eu>
parents: 8
diff changeset
35 static TaskHandle_t xTaskOUT = NULL;
8
115e93bf8796 Added namespace and output variables in NVS.
Michiel Broek <mbroek@mbse.eu>
parents: 7
diff changeset
36
18
12506716211c Added nvsio utilities to make read/write to nvs namespace easier. Added variables for deep sleep to nvs namespace. In task_wifi, removed some init parameters that might add to instable wifi connect problems.
Michiel Broek <mbroek@mbse.eu>
parents: 16
diff changeset
37 #define DS_TIME 60
29
551a53b31373 Final release and installed for production.
Michiel Broek <mbroek@mbse.eu>
parents: 27
diff changeset
38 #define DS_CURRENT 6.6
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
39 #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
40 #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
41
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
42 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
43 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
44 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
45 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
46 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
47 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
48 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
49 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
50 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
51 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
52 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
53 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
54 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
55
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 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
57 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
58 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
59
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
60 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
61
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
62 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
63 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
64 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
65 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
66 extern SemaphoreHandle_t xSemaphoreINA219;
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents: 1
diff changeset
67 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
68 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
69 extern INA219_State *ina219_state;
12
bb72d448e282 In the esp-idf-lib the adps9930 driver uses a device descriptor structure instead of just i2c_dev_t. Fixed a linking issue. Added APDS9930 task. Added getLightValues function. Added wifi quality value to the MQTT payload. The payload is complete and will be published.
Michiel Broek <mbroek@mbse.eu>
parents: 9
diff changeset
70 extern SemaphoreHandle_t xSemaphoreAPDS9930;
bb72d448e282 In the esp-idf-lib the adps9930 driver uses a device descriptor structure instead of just i2c_dev_t. Fixed a linking issue. Added APDS9930 task. Added getLightValues function. Added wifi quality value to the MQTT payload. The payload is complete and will be published.
Michiel Broek <mbroek@mbse.eu>
parents: 9
diff changeset
71 extern apds9930_t apds9930_dev;
bb72d448e282 In the esp-idf-lib the adps9930 driver uses a device descriptor structure instead of just i2c_dev_t. Fixed a linking issue. Added APDS9930 task. Added getLightValues function. Added wifi quality value to the MQTT payload. The payload is complete and will be published.
Michiel Broek <mbroek@mbse.eu>
parents: 9
diff changeset
72 extern APDS9930_State *apds9930_state;
4
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents: 3
diff changeset
73 extern SemaphoreHandle_t xSemaphoreWiFi;
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents: 3
diff changeset
74 extern WIFI_State *wifi_state; ///< WiFi state
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents: 3
diff changeset
75
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
76 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
77
b1f38105ca7e Added task MQTT 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 /*
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
79 * 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
80 */
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
81 #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
82 #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
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 #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
86 #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
87
8
115e93bf8796 Added namespace and output variables in NVS.
Michiel Broek <mbroek@mbse.eu>
parents: 7
diff changeset
88 /*
115e93bf8796 Added namespace and output variables in NVS.
Michiel Broek <mbroek@mbse.eu>
parents: 7
diff changeset
89 * Variables in NVS namespace "balkon"
115e93bf8796 Added namespace and output variables in NVS.
Michiel Broek <mbroek@mbse.eu>
parents: 7
diff changeset
90 */
115e93bf8796 Added namespace and output variables in NVS.
Michiel Broek <mbroek@mbse.eu>
parents: 7
diff changeset
91 uint8_t Relay1;
115e93bf8796 Added namespace and output variables in NVS.
Michiel Broek <mbroek@mbse.eu>
parents: 7
diff changeset
92 uint8_t Relay2;
115e93bf8796 Added namespace and output variables in NVS.
Michiel Broek <mbroek@mbse.eu>
parents: 7
diff changeset
93 uint8_t Dimmer3;
115e93bf8796 Added namespace and output variables in NVS.
Michiel Broek <mbroek@mbse.eu>
parents: 7
diff changeset
94 uint8_t Dimmer4;
18
12506716211c Added nvsio utilities to make read/write to nvs namespace easier. Added variables for deep sleep to nvs namespace. In task_wifi, removed some init parameters that might add to instable wifi connect problems.
Michiel Broek <mbroek@mbse.eu>
parents: 16
diff changeset
95 uint8_t DS_Active;
12506716211c Added nvsio utilities to make read/write to nvs namespace easier. Added variables for deep sleep to nvs namespace. In task_wifi, removed some init parameters that might add to instable wifi connect problems.
Michiel Broek <mbroek@mbse.eu>
parents: 16
diff changeset
96 uint32_t DS_Time;
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
97
b1f38105ca7e Added task MQTT 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
b1f38105ca7e Added task MQTT 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 // 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
100 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
101 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
102 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
103 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
104 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
105 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
106 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
107 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
108
b1f38105ca7e Added task MQTT 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 * 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
112 */
b1f38105ca7e Added task MQTT 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 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
114 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
115
b1f38105ca7e Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents: 4
diff changeset
116 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
117 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
118
b1f38105ca7e Added task MQTT 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 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
120 // 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
121 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
122 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
123 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
124 }
b1f38105ca7e Added task MQTT 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 }
b1f38105ca7e Added task MQTT 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 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
127 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
128
b1f38105ca7e Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents: 4
diff changeset
129 } 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
130 // 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
131 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
132 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
133 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
134 }
b1f38105ca7e Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents: 4
diff changeset
135 }
b1f38105ca7e Added task MQTT 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 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
137 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
138
b1f38105ca7e Added task MQTT 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 } 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
140 // 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
141 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
142 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
143 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
144 }
b1f38105ca7e Added task MQTT 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 }
b1f38105ca7e Added task MQTT 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 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
147 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
148
b1f38105ca7e Added task MQTT 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 } 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
150 // 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
151 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
152 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
153 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
154 }
b1f38105ca7e Added task MQTT 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 }
b1f38105ca7e Added task MQTT 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 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
157 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
158
b1f38105ca7e Added task MQTT 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 } 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
160 // 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
161 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
162 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
163 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
164 }
b1f38105ca7e Added task MQTT 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 }
b1f38105ca7e Added task MQTT 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 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
167 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
168
b1f38105ca7e Added task MQTT 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 } 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
170 // 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
171 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
172 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
173 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
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 }
b1f38105ca7e Added task MQTT 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
176 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
177 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
178
b1f38105ca7e Added task MQTT 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
179 } 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
180 // 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
181 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
182 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
183 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
184 }
b1f38105ca7e Added task MQTT 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
185 }
b1f38105ca7e Added task MQTT 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
186 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
187 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
188
b1f38105ca7e Added task MQTT 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
189 } 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
190 // 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
191 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
192 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
193 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
194 }
b1f38105ca7e Added task MQTT 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
195 }
b1f38105ca7e Added task MQTT 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
196 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
197 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
198 }
b1f38105ca7e Added task MQTT 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
199 }
b1f38105ca7e Added task MQTT 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
200
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
201
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
202
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
203 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
204 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
205 }
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
206
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
207
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
208
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
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 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
211 */
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 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
213 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
214 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
215
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 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
217
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 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
219 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
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
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 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
223 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
224 pressure = bmp280_state->pressure / 100.0;
15
64028e178ff1 Splitted wifi and mqtt connect states to make the connections more robust. Protect the mqtt connection request against requests when a request is already done or in progress.
Michiel Broek <mbroek@mbse.eu>
parents: 14
diff changeset
225 ESP_LOGI(TAG, "Temperature: %.2f Pressure: %.1f hPa", temperature, pressure);
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
226 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
227 } 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
228 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
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
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
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 /*
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 * 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
236 * 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
237 */
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 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
239
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 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
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 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
243 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
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
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 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
247
14
2a9f67ecbc72 Fixed wrong voltage and current measurements due to differences between Arduino and ESP-IDF.
Michiel Broek <mbroek@mbse.eu>
parents: 12
diff changeset
248 s_Volts[loopno] = ina219_state->Solar.volts + ina219_state->Solar.shunt;
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
249 s_Current[loopno] = ina219_state->Solar.current;
14
2a9f67ecbc72 Fixed wrong voltage and current measurements due to differences between Arduino and ESP-IDF.
Michiel Broek <mbroek@mbse.eu>
parents: 12
diff changeset
250 b_Volts[loopno] = ina219_state->Battery.volts + ina219_state->Battery.shunt;
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
251 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
252 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
253 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
254
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
255 /*
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 * 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
257 */
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 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
259 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
260 }
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 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
262 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
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 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
265 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
266 }
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
267 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
268 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
269 }
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
270 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
271 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
272 }
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
273 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
274 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
275 }
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
276 }
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
277
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
278 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
279 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
280 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
281
15
64028e178ff1 Splitted wifi and mqtt connect states to make the connections more robust. Protect the mqtt connection request against requests when a request is already done or in progress.
Michiel Broek <mbroek@mbse.eu>
parents: 14
diff changeset
282 ESP_LOGI(TAG, "Measure: %d Valid: %s Battery: %.3fV %.1fmA Solar: %.3fV %.1fmA time %llu",
64028e178ff1 Splitted wifi and mqtt connect states to make the connections more robust. Protect the mqtt connection request against requests when a request is already done or in progress.
Michiel Broek <mbroek@mbse.eu>
parents: 14
diff changeset
283 loopno, (m_Valid[loopno]) ? "true":"false", b_Volts[loopno], b_Current[loopno], s_Volts[loopno], s_Current[loopno], m_Time[loopno]);
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
284
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
285 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
286 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
287 }
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
288
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
289
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
290
12
bb72d448e282 In the esp-idf-lib the adps9930 driver uses a device descriptor structure instead of just i2c_dev_t. Fixed a linking issue. Added APDS9930 task. Added getLightValues function. Added wifi quality value to the MQTT payload. The payload is complete and will be published.
Michiel Broek <mbroek@mbse.eu>
parents: 9
diff changeset
291 void getLightValues() {
bb72d448e282 In the esp-idf-lib the adps9930 driver uses a device descriptor structure instead of just i2c_dev_t. Fixed a linking issue. Added APDS9930 task. Added getLightValues function. Added wifi quality value to the MQTT payload. The payload is complete and will be published.
Michiel Broek <mbroek@mbse.eu>
parents: 9
diff changeset
292
bb72d448e282 In the esp-idf-lib the adps9930 driver uses a device descriptor structure instead of just i2c_dev_t. Fixed a linking issue. Added APDS9930 task. Added getLightValues function. Added wifi quality value to the MQTT payload. The payload is complete and will be published.
Michiel Broek <mbroek@mbse.eu>
parents: 9
diff changeset
293 request_apds9930();
bb72d448e282 In the esp-idf-lib the adps9930 driver uses a device descriptor structure instead of just i2c_dev_t. Fixed a linking issue. Added APDS9930 task. Added getLightValues function. Added wifi quality value to the MQTT payload. The payload is complete and will be published.
Michiel Broek <mbroek@mbse.eu>
parents: 9
diff changeset
294
bb72d448e282 In the esp-idf-lib the adps9930 driver uses a device descriptor structure instead of just i2c_dev_t. Fixed a linking issue. Added APDS9930 task. Added getLightValues function. Added wifi quality value to the MQTT payload. The payload is complete and will be published.
Michiel Broek <mbroek@mbse.eu>
parents: 9
diff changeset
295 while (! ready_apds9930()) {
bb72d448e282 In the esp-idf-lib the adps9930 driver uses a device descriptor structure instead of just i2c_dev_t. Fixed a linking issue. Added APDS9930 task. Added getLightValues function. Added wifi quality value to the MQTT payload. The payload is complete and will be published.
Michiel Broek <mbroek@mbse.eu>
parents: 9
diff changeset
296 vTaskDelay(10 / portTICK_PERIOD_MS);
bb72d448e282 In the esp-idf-lib the adps9930 driver uses a device descriptor structure instead of just i2c_dev_t. Fixed a linking issue. Added APDS9930 task. Added getLightValues function. Added wifi quality value to the MQTT payload. The payload is complete and will be published.
Michiel Broek <mbroek@mbse.eu>
parents: 9
diff changeset
297 }
bb72d448e282 In the esp-idf-lib the adps9930 driver uses a device descriptor structure instead of just i2c_dev_t. Fixed a linking issue. Added APDS9930 task. Added getLightValues function. Added wifi quality value to the MQTT payload. The payload is complete and will be published.
Michiel Broek <mbroek@mbse.eu>
parents: 9
diff changeset
298 }
bb72d448e282 In the esp-idf-lib the adps9930 driver uses a device descriptor structure instead of just i2c_dev_t. Fixed a linking issue. Added APDS9930 task. Added getLightValues function. Added wifi quality value to the MQTT payload. The payload is complete and will be published.
Michiel Broek <mbroek@mbse.eu>
parents: 9
diff changeset
299
bb72d448e282 In the esp-idf-lib the adps9930 driver uses a device descriptor structure instead of just i2c_dev_t. Fixed a linking issue. Added APDS9930 task. Added getLightValues function. Added wifi quality value to the MQTT payload. The payload is complete and will be published.
Michiel Broek <mbroek@mbse.eu>
parents: 9
diff changeset
300
0
f8b0268c8d0a Initial project setup
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
301 void app_main(void)
f8b0268c8d0a Initial project setup
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
302 {
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
303 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
304
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
305 #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
306 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
307 #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
308 #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
309 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
310 #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
311
8
115e93bf8796 Added namespace and output variables in NVS.
Michiel Broek <mbroek@mbse.eu>
parents: 7
diff changeset
312 /*
115e93bf8796 Added namespace and output variables in NVS.
Michiel Broek <mbroek@mbse.eu>
parents: 7
diff changeset
313 * Initialize NVS
115e93bf8796 Added namespace and output variables in NVS.
Michiel Broek <mbroek@mbse.eu>
parents: 7
diff changeset
314 */
115e93bf8796 Added namespace and output variables in NVS.
Michiel Broek <mbroek@mbse.eu>
parents: 7
diff changeset
315 esp_err_t err = nvs_flash_init();
115e93bf8796 Added namespace and output variables in NVS.
Michiel Broek <mbroek@mbse.eu>
parents: 7
diff changeset
316 if (err == ESP_ERR_NVS_NO_FREE_PAGES || err == ESP_ERR_NVS_NEW_VERSION_FOUND) {
115e93bf8796 Added namespace and output variables in NVS.
Michiel Broek <mbroek@mbse.eu>
parents: 7
diff changeset
317 ESP_ERROR_CHECK(nvs_flash_erase());
115e93bf8796 Added namespace and output variables in NVS.
Michiel Broek <mbroek@mbse.eu>
parents: 7
diff changeset
318 err = nvs_flash_init();
115e93bf8796 Added namespace and output variables in NVS.
Michiel Broek <mbroek@mbse.eu>
parents: 7
diff changeset
319 }
115e93bf8796 Added namespace and output variables in NVS.
Michiel Broek <mbroek@mbse.eu>
parents: 7
diff changeset
320 ESP_ERROR_CHECK(err);
115e93bf8796 Added namespace and output variables in NVS.
Michiel Broek <mbroek@mbse.eu>
parents: 7
diff changeset
321
18
12506716211c Added nvsio utilities to make read/write to nvs namespace easier. Added variables for deep sleep to nvs namespace. In task_wifi, removed some init parameters that might add to instable wifi connect problems.
Michiel Broek <mbroek@mbse.eu>
parents: 16
diff changeset
322 nvsio_init();
12506716211c Added nvsio utilities to make read/write to nvs namespace easier. Added variables for deep sleep to nvs namespace. In task_wifi, removed some init parameters that might add to instable wifi connect problems.
Michiel Broek <mbroek@mbse.eu>
parents: 16
diff changeset
323 Relay1 = nvsio_read_u8((char *)"out1");
12506716211c Added nvsio utilities to make read/write to nvs namespace easier. Added variables for deep sleep to nvs namespace. In task_wifi, removed some init parameters that might add to instable wifi connect problems.
Michiel Broek <mbroek@mbse.eu>
parents: 16
diff changeset
324 Relay2 = nvsio_read_u8((char *)"out2");
12506716211c Added nvsio utilities to make read/write to nvs namespace easier. Added variables for deep sleep to nvs namespace. In task_wifi, removed some init parameters that might add to instable wifi connect problems.
Michiel Broek <mbroek@mbse.eu>
parents: 16
diff changeset
325 Dimmer3 = nvsio_read_u8((char *)"out3");
12506716211c Added nvsio utilities to make read/write to nvs namespace easier. Added variables for deep sleep to nvs namespace. In task_wifi, removed some init parameters that might add to instable wifi connect problems.
Michiel Broek <mbroek@mbse.eu>
parents: 16
diff changeset
326 Dimmer4 = nvsio_read_u8((char *)"out4");
12506716211c Added nvsio utilities to make read/write to nvs namespace easier. Added variables for deep sleep to nvs namespace. In task_wifi, removed some init parameters that might add to instable wifi connect problems.
Michiel Broek <mbroek@mbse.eu>
parents: 16
diff changeset
327 DS_Active = nvsio_read_u8((char *)"ds_active");
12506716211c Added nvsio utilities to make read/write to nvs namespace easier. Added variables for deep sleep to nvs namespace. In task_wifi, removed some init parameters that might add to instable wifi connect problems.
Michiel Broek <mbroek@mbse.eu>
parents: 16
diff changeset
328 DS_Time = nvsio_read_u32((char *)"ds_time");
12506716211c Added nvsio utilities to make read/write to nvs namespace easier. Added variables for deep sleep to nvs namespace. In task_wifi, removed some init parameters that might add to instable wifi connect problems.
Michiel Broek <mbroek@mbse.eu>
parents: 16
diff changeset
329 if (DS_Time == 0) {
12506716211c Added nvsio utilities to make read/write to nvs namespace easier. Added variables for deep sleep to nvs namespace. In task_wifi, removed some init parameters that might add to instable wifi connect problems.
Michiel Broek <mbroek@mbse.eu>
parents: 16
diff changeset
330 DS_Time = DS_TIME;
12506716211c Added nvsio utilities to make read/write to nvs namespace easier. Added variables for deep sleep to nvs namespace. In task_wifi, removed some init parameters that might add to instable wifi connect problems.
Michiel Broek <mbroek@mbse.eu>
parents: 16
diff changeset
331 nvsio_write_u32((char *)"ds_time", DS_Time);
8
115e93bf8796 Added namespace and output variables in NVS.
Michiel Broek <mbroek@mbse.eu>
parents: 7
diff changeset
332 }
115e93bf8796 Added namespace and output variables in NVS.
Michiel Broek <mbroek@mbse.eu>
parents: 7
diff changeset
333
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
334 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
335 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
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 bmp280_init_default_params(&bmp280_params);
27
53d6ecf5829b Adjusted BMP280 default parameters for low power usage.
Michiel Broek <mbroek@mbse.eu>
parents: 26
diff changeset
338 /*
53d6ecf5829b Adjusted BMP280 default parameters for low power usage.
Michiel Broek <mbroek@mbse.eu>
parents: 26
diff changeset
339 * Override some defaults to make the BMP280 use less power
53d6ecf5829b Adjusted BMP280 default parameters for low power usage.
Michiel Broek <mbroek@mbse.eu>
parents: 26
diff changeset
340 * and still provide enough resolution.
53d6ecf5829b Adjusted BMP280 default parameters for low power usage.
Michiel Broek <mbroek@mbse.eu>
parents: 26
diff changeset
341 */
53d6ecf5829b Adjusted BMP280 default parameters for low power usage.
Michiel Broek <mbroek@mbse.eu>
parents: 26
diff changeset
342 bmp280_params.oversampling_pressure = BMP280_LOW_POWER;
53d6ecf5829b Adjusted BMP280 default parameters for low power usage.
Michiel Broek <mbroek@mbse.eu>
parents: 26
diff changeset
343 bmp280_params.oversampling_temperature = BMP280_LOW_POWER;
53d6ecf5829b Adjusted BMP280 default parameters for low power usage.
Michiel Broek <mbroek@mbse.eu>
parents: 26
diff changeset
344 bmp280_params.standby = BMP280_STANDBY_1000;
53d6ecf5829b Adjusted BMP280 default parameters for low power usage.
Michiel Broek <mbroek@mbse.eu>
parents: 26
diff changeset
345
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
346 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
347 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
348 memset(&ina219_s_dev, 0, sizeof(ina219_t));
16
b3e96bbe4ce4 First pass of debugging APDS9930 with a real chip.
Michiel Broek <mbroek@mbse.eu>
parents: 15
diff changeset
349 memset(&apds9930_dev, 0, sizeof(apds9930_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
350
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
351 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
352 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
353 dev.cfg.scl_io_num = CONFIG_I2C_MASTER_SCL;
24
74609f70411e Reduced I2C master speed from 400 to 100 Khz. Hope to get power-on init for BMP280 more reliable. Added warnings for I2C devices that are missing. Some logmessages reduced to debug log messages. Added extra 10 mSec delays before read shunt voltage in the INA219 task. Removed and reduced log levels in the MQTT task. Show received data events. Remove WiFi total time debug logging, it's ok now.
Michiel Broek <mbroek@mbse.eu>
parents: 23
diff changeset
354 dev.cfg.master.clk_speed = 100000;
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
355
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
356 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
357 if (i2c_dev_probe(&dev, I2C_DEV_WRITE) == 0) {
12
bb72d448e282 In the esp-idf-lib the adps9930 driver uses a device descriptor structure instead of just i2c_dev_t. Fixed a linking issue. Added APDS9930 task. Added getLightValues function. Added wifi quality value to the MQTT payload. The payload is complete and will be published.
Michiel Broek <mbroek@mbse.eu>
parents: 9
diff changeset
358 ESP_ERROR_CHECK(apds9930_init_desc(&apds9930_dev, 0x39, 0, CONFIG_I2C_MASTER_SDA, CONFIG_I2C_MASTER_SCL));
bb72d448e282 In the esp-idf-lib the adps9930 driver uses a device descriptor structure instead of just i2c_dev_t. Fixed a linking issue. Added APDS9930 task. Added getLightValues function. Added wifi quality value to the MQTT payload. The payload is complete and will be published.
Michiel Broek <mbroek@mbse.eu>
parents: 9
diff changeset
359 ESP_ERROR_CHECK(apds9930_init(&apds9930_dev));
bb72d448e282 In the esp-idf-lib the adps9930 driver uses a device descriptor structure instead of just i2c_dev_t. Fixed a linking issue. Added APDS9930 task. Added getLightValues function. Added wifi quality value to the MQTT payload. The payload is complete and will be published.
Michiel Broek <mbroek@mbse.eu>
parents: 9
diff changeset
360 ESP_LOGI(TAG, "Found APDS-9930 id: 0x%02x", apds9930_dev.id);
24
74609f70411e Reduced I2C master speed from 400 to 100 Khz. Hope to get power-on init for BMP280 more reliable. Added warnings for I2C devices that are missing. Some logmessages reduced to debug log messages. Added extra 10 mSec delays before read shunt voltage in the INA219 task. Removed and reduced log levels in the MQTT task. Show received data events. Remove WiFi total time debug logging, it's ok now.
Michiel Broek <mbroek@mbse.eu>
parents: 23
diff changeset
361 } else {
74609f70411e Reduced I2C master speed from 400 to 100 Khz. Hope to get power-on init for BMP280 more reliable. Added warnings for I2C devices that are missing. Some logmessages reduced to debug log messages. Added extra 10 mSec delays before read shunt voltage in the INA219 task. Removed and reduced log levels in the MQTT task. Show received data events. Remove WiFi total time debug logging, it's ok now.
Michiel Broek <mbroek@mbse.eu>
parents: 23
diff changeset
362 ESP_LOGW(TAG, "No APDS-9930 found");
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
363 }
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
364 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
365 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
366 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
367 ESP_ERROR_CHECK(ina219_init(&ina219_b_dev));
16
b3e96bbe4ce4 First pass of debugging APDS9930 with a real chip.
Michiel Broek <mbroek@mbse.eu>
parents: 15
diff changeset
368 ESP_LOGI(TAG, "Found INA219 @0x40 Battery");
24
74609f70411e Reduced I2C master speed from 400 to 100 Khz. Hope to get power-on init for BMP280 more reliable. Added warnings for I2C devices that are missing. Some logmessages reduced to debug log messages. Added extra 10 mSec delays before read shunt voltage in the INA219 task. Removed and reduced log levels in the MQTT task. Show received data events. Remove WiFi total time debug logging, it's ok now.
Michiel Broek <mbroek@mbse.eu>
parents: 23
diff changeset
369 } else {
74609f70411e Reduced I2C master speed from 400 to 100 Khz. Hope to get power-on init for BMP280 more reliable. Added warnings for I2C devices that are missing. Some logmessages reduced to debug log messages. Added extra 10 mSec delays before read shunt voltage in the INA219 task. Removed and reduced log levels in the MQTT task. Show received data events. Remove WiFi total time debug logging, it's ok now.
Michiel Broek <mbroek@mbse.eu>
parents: 23
diff changeset
370 ESP_LOGW(TAG, "No INA219 @0x40 found");
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
371 }
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
372 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
373 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
374 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
375 ESP_ERROR_CHECK(ina219_init(&ina219_s_dev));
16
b3e96bbe4ce4 First pass of debugging APDS9930 with a real chip.
Michiel Broek <mbroek@mbse.eu>
parents: 15
diff changeset
376 ESP_LOGI(TAG, "Found INA219 @0x41 Solar");
24
74609f70411e Reduced I2C master speed from 400 to 100 Khz. Hope to get power-on init for BMP280 more reliable. Added warnings for I2C devices that are missing. Some logmessages reduced to debug log messages. Added extra 10 mSec delays before read shunt voltage in the INA219 task. Removed and reduced log levels in the MQTT task. Show received data events. Remove WiFi total time debug logging, it's ok now.
Michiel Broek <mbroek@mbse.eu>
parents: 23
diff changeset
377 } else {
74609f70411e Reduced I2C master speed from 400 to 100 Khz. Hope to get power-on init for BMP280 more reliable. Added warnings for I2C devices that are missing. Some logmessages reduced to debug log messages. Added extra 10 mSec delays before read shunt voltage in the INA219 task. Removed and reduced log levels in the MQTT task. Show received data events. Remove WiFi total time debug logging, it's ok now.
Michiel Broek <mbroek@mbse.eu>
parents: 23
diff changeset
378 ESP_LOGW(TAG, "No INA219 @0x41 found");
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
379 }
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
380 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
381 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
382 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
383 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
384 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
385 } else {
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents: 1
diff changeset
386 dev.addr = 0x77;
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents: 1
diff changeset
387 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
388 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
389 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
390 ESP_LOGI(TAG, "Found BMP280 @ 0x77 id: 0x%02x", bmp280_dev.id);
24
74609f70411e Reduced I2C master speed from 400 to 100 Khz. Hope to get power-on init for BMP280 more reliable. Added warnings for I2C devices that are missing. Some logmessages reduced to debug log messages. Added extra 10 mSec delays before read shunt voltage in the INA219 task. Removed and reduced log levels in the MQTT task. Show received data events. Remove WiFi total time debug logging, it's ok now.
Michiel Broek <mbroek@mbse.eu>
parents: 23
diff changeset
391 } else {
74609f70411e Reduced I2C master speed from 400 to 100 Khz. Hope to get power-on init for BMP280 more reliable. Added warnings for I2C devices that are missing. Some logmessages reduced to debug log messages. Added extra 10 mSec delays before read shunt voltage in the INA219 task. Removed and reduced log levels in the MQTT task. Show received data events. Remove WiFi total time debug logging, it's ok now.
Michiel Broek <mbroek@mbse.eu>
parents: 23
diff changeset
392 ESP_LOGW(TAG, "No BMP280 found");
74609f70411e Reduced I2C master speed from 400 to 100 Khz. Hope to get power-on init for BMP280 more reliable. Added warnings for I2C devices that are missing. Some logmessages reduced to debug log messages. Added extra 10 mSec delays before read shunt voltage in the INA219 task. Removed and reduced log levels in the MQTT task. Show received data events. Remove WiFi total time debug logging, it's ok now.
Michiel Broek <mbroek@mbse.eu>
parents: 23
diff changeset
393 }
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
394 }
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
395
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
396 /*
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
397 * 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
398 */
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
399 xSemaphoreBMP280 = xSemaphoreCreateMutex();
4
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents: 3
diff changeset
400 xSemaphoreINA219 = xSemaphoreCreateMutex();
12
bb72d448e282 In the esp-idf-lib the adps9930 driver uses a device descriptor structure instead of just i2c_dev_t. Fixed a linking issue. Added APDS9930 task. Added getLightValues function. Added wifi quality value to the MQTT payload. The payload is complete and will be published.
Michiel Broek <mbroek@mbse.eu>
parents: 9
diff changeset
401 xSemaphoreAPDS9930 = 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
402
4
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents: 3
diff changeset
403
12
bb72d448e282 In the esp-idf-lib the adps9930 driver uses a device descriptor structure instead of just i2c_dev_t. Fixed a linking issue. Added APDS9930 task. Added getLightValues function. Added wifi quality value to the MQTT payload. The payload is complete and will be published.
Michiel Broek <mbroek@mbse.eu>
parents: 9
diff changeset
404 xTaskCreate(&task_bmp280, "task_bmp280", 2560, NULL, 8, &xTaskBMP280);
bb72d448e282 In the esp-idf-lib the adps9930 driver uses a device descriptor structure instead of just i2c_dev_t. Fixed a linking issue. Added APDS9930 task. Added getLightValues function. Added wifi quality value to the MQTT payload. The payload is complete and will be published.
Michiel Broek <mbroek@mbse.eu>
parents: 9
diff changeset
405 xTaskCreate(&task_ina219, "task_ina219", 2560, NULL, 8, &xTaskINA219);
bb72d448e282 In the esp-idf-lib the adps9930 driver uses a device descriptor structure instead of just i2c_dev_t. Fixed a linking issue. Added APDS9930 task. Added getLightValues function. Added wifi quality value to the MQTT payload. The payload is complete and will be published.
Michiel Broek <mbroek@mbse.eu>
parents: 9
diff changeset
406 xTaskCreate(&task_apds9930, "task_apds9930", 2560, NULL, 8, &xTaskAPDS9930);
bb72d448e282 In the esp-idf-lib the adps9930 driver uses a device descriptor structure instead of just i2c_dev_t. Fixed a linking issue. Added APDS9930 task. Added getLightValues function. Added wifi quality value to the MQTT payload. The payload is complete and will be published.
Michiel Broek <mbroek@mbse.eu>
parents: 9
diff changeset
407 xTaskCreate(&task_out, "task_out", 2560, NULL, 9, &xTaskOUT);
bb72d448e282 In the esp-idf-lib the adps9930 driver uses a device descriptor structure instead of just i2c_dev_t. Fixed a linking issue. Added APDS9930 task. Added getLightValues function. Added wifi quality value to the MQTT payload. The payload is complete and will be published.
Michiel Broek <mbroek@mbse.eu>
parents: 9
diff changeset
408 xTaskCreate(&task_mqtt, "task_mqtt", 4096, NULL, 5, &xTaskMQTT);
bb72d448e282 In the esp-idf-lib the adps9930 driver uses a device descriptor structure instead of just i2c_dev_t. Fixed a linking issue. Added APDS9930 task. Added getLightValues function. Added wifi quality value to the MQTT payload. The payload is complete and will be published.
Michiel Broek <mbroek@mbse.eu>
parents: 9
diff changeset
409 xTaskCreate(&task_wifi, "task_wifi", 4096, NULL, 3, &xTaskWifi);
30
7448b8dd4288 Preparations for BLE GATT. Added extra time after INA219 is powered on before measurement. Reduced LEDC frequency to 60 Hz, that makes the LED lights less nervous. Hardware mod on output 4, now needs external pulldown resistor.
Michiel Broek <mbroek@mbse.eu>
parents: 29
diff changeset
410 #ifdef CONFIG_ENABLE_BLE_GATT
7448b8dd4288 Preparations for BLE GATT. Added extra time after INA219 is powered on before measurement. Reduced LEDC frequency to 60 Hz, that makes the LED lights less nervous. Hardware mod on output 4, now needs external pulldown resistor.
Michiel Broek <mbroek@mbse.eu>
parents: 29
diff changeset
411 xTaskCreate(&task_ble, "task_ble", 4096, NULL, 3, &xTaskBLE);
7448b8dd4288 Preparations for BLE GATT. Added extra time after INA219 is powered on before measurement. Reduced LEDC frequency to 60 Hz, that makes the LED lights less nervous. Hardware mod on output 4, now needs external pulldown resistor.
Michiel Broek <mbroek@mbse.eu>
parents: 29
diff changeset
412 #endif
4
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents: 3
diff changeset
413
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents: 3
diff changeset
414 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
415
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
416 /*
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
417 * 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
418 */
23
2cc30d828d6e Shorter delays during wifi connect timeout. Some code cleanup in the main state table. Report WiFi TX power during init. Removed SSID from WIFI_State because it wasn't used.
Michiel Broek <mbroek@mbse.eu>
parents: 22
diff changeset
419 int State = State_Init;
2cc30d828d6e Shorter delays during wifi connect timeout. Some code cleanup in the main state table. Report WiFi TX power during init. Removed SSID from WIFI_State because it wasn't used.
Michiel Broek <mbroek@mbse.eu>
parents: 22
diff changeset
420 int OldState = State_Init + 1;
18
12506716211c Added nvsio utilities to make read/write to nvs namespace easier. Added variables for deep sleep to nvs namespace. In task_wifi, removed some init parameters that might add to instable wifi connect problems.
Michiel Broek <mbroek@mbse.eu>
parents: 16
diff changeset
421 uint8_t ds_time = DS_Time;
3
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents: 1
diff changeset
422
16
b3e96bbe4ce4 First pass of debugging APDS9930 with a real chip.
Michiel Broek <mbroek@mbse.eu>
parents: 15
diff changeset
423
31
ec5c7794dcd6 Added basic BLE code.
Michiel Broek <mbroek@mbse.eu>
parents: 30
diff changeset
424 while (1) {
ec5c7794dcd6 Added basic BLE code.
Michiel Broek <mbroek@mbse.eu>
parents: 30
diff changeset
425 // request_bmp280();
ec5c7794dcd6 Added basic BLE code.
Michiel Broek <mbroek@mbse.eu>
parents: 30
diff changeset
426 // request_ina219();
ec5c7794dcd6 Added basic BLE code.
Michiel Broek <mbroek@mbse.eu>
parents: 30
diff changeset
427 // request_apds9930();
16
b3e96bbe4ce4 First pass of debugging APDS9930 with a real chip.
Michiel Broek <mbroek@mbse.eu>
parents: 15
diff changeset
428 vTaskDelay(5000 / portTICK_PERIOD_MS);
b3e96bbe4ce4 First pass of debugging APDS9930 with a real chip.
Michiel Broek <mbroek@mbse.eu>
parents: 15
diff changeset
429 }
b3e96bbe4ce4 First pass of debugging APDS9930 with a real chip.
Michiel Broek <mbroek@mbse.eu>
parents: 15
diff changeset
430
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
431 while (1) {
3
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents: 1
diff changeset
432 if (OldState != State) {
24
74609f70411e Reduced I2C master speed from 400 to 100 Khz. Hope to get power-on init for BMP280 more reliable. Added warnings for I2C devices that are missing. Some logmessages reduced to debug log messages. Added extra 10 mSec delays before read shunt voltage in the INA219 task. Removed and reduced log levels in the MQTT task. Show received data events. Remove WiFi total time debug logging, it's ok now.
Michiel Broek <mbroek@mbse.eu>
parents: 23
diff changeset
433 ESP_LOGD(TAG, "Switch to state %d", State);
3
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents: 1
diff changeset
434 OldState = State;
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents: 1
diff changeset
435 }
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
436
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 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
438
3
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents: 1
diff changeset
439 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
440 case State_Init: getTempBaro();
12
bb72d448e282 In the esp-idf-lib the adps9930 driver uses a device descriptor structure instead of just i2c_dev_t. Fixed a linking issue. Added APDS9930 task. Added getLightValues function. Added wifi quality value to the MQTT payload. The payload is complete and will be published.
Michiel Broek <mbroek@mbse.eu>
parents: 9
diff changeset
441 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
442 getVoltsCurrent();
15
64028e178ff1 Splitted wifi and mqtt connect states to make the connections more robust. Protect the mqtt connection request against requests when a request is already done or in progress.
Michiel Broek <mbroek@mbse.eu>
parents: 14
diff changeset
443 State = State_Connect_Wifi;
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
444 DisCounter = 0;
4
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents: 3
diff changeset
445 request_WiFi(true);
3
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents: 1
diff changeset
446 break;
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents: 1
diff changeset
447
15
64028e178ff1 Splitted wifi and mqtt connect states to make the connections more robust. Protect the mqtt connection request against requests when a request is already done or in progress.
Michiel Broek <mbroek@mbse.eu>
parents: 14
diff changeset
448 case State_Connect_Wifi: if (ready_WiFi()) {
64028e178ff1 Splitted wifi and mqtt connect states to make the connections more robust. Protect the mqtt connection request against requests when a request is already done or in progress.
Michiel Broek <mbroek@mbse.eu>
parents: 14
diff changeset
449 State = State_Connect_MQTT;
64028e178ff1 Splitted wifi and mqtt connect states to make the connections more robust. Protect the mqtt connection request against requests when a request is already done or in progress.
Michiel Broek <mbroek@mbse.eu>
parents: 14
diff changeset
450 /*
64028e178ff1 Splitted wifi and mqtt connect states to make the connections more robust. Protect the mqtt connection request against requests when a request is already done or in progress.
Michiel Broek <mbroek@mbse.eu>
parents: 14
diff changeset
451 * Give the network stack a bit more time to setup
64028e178ff1 Splitted wifi and mqtt connect states to make the connections more robust. Protect the mqtt connection request against requests when a request is already done or in progress.
Michiel Broek <mbroek@mbse.eu>
parents: 14
diff changeset
452 */
64028e178ff1 Splitted wifi and mqtt connect states to make the connections more robust. Protect the mqtt connection request against requests when a request is already done or in progress.
Michiel Broek <mbroek@mbse.eu>
parents: 14
diff changeset
453 vTaskDelay(250 / portTICK_PERIOD_MS);
64028e178ff1 Splitted wifi and mqtt connect states to make the connections more robust. Protect the mqtt connection request against requests when a request is already done or in progress.
Michiel Broek <mbroek@mbse.eu>
parents: 14
diff changeset
454 request_mqtt(true);
6
bad3414f7bc4 Added the getTempBaro and getVoltsCurrent functions to process the measured data. Added some main task code. Added subscribe to MQTT events.
Michiel Broek <mbroek@mbse.eu>
parents: 5
diff changeset
455 Alarm &= ~AL_NOWIFI;
24
74609f70411e Reduced I2C master speed from 400 to 100 Khz. Hope to get power-on init for BMP280 more reliable. Added warnings for I2C devices that are missing. Some logmessages reduced to debug log messages. Added extra 10 mSec delays before read shunt voltage in the INA219 task. Removed and reduced log levels in the MQTT task. Show received data events. Remove WiFi total time debug logging, it's ok now.
Michiel Broek <mbroek@mbse.eu>
parents: 23
diff changeset
456 ESP_LOGD(TAG, "Connected counter WiFi %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
457 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
458 } else {
15
64028e178ff1 Splitted wifi and mqtt connect states to make the connections more robust. Protect the mqtt connection request against requests when a request is already done or in progress.
Michiel Broek <mbroek@mbse.eu>
parents: 14
diff changeset
459 /*
23
2cc30d828d6e Shorter delays during wifi connect timeout. Some code cleanup in the main state table. Report WiFi TX power during init. Removed SSID from WIFI_State because it wasn't used.
Michiel Broek <mbroek@mbse.eu>
parents: 22
diff changeset
460 * 15 seconds connection try.
15
64028e178ff1 Splitted wifi and mqtt connect states to make the connections more robust. Protect the mqtt connection request against requests when a request is already done or in progress.
Michiel Broek <mbroek@mbse.eu>
parents: 14
diff changeset
461 */
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
462 DisCounter++;
23
2cc30d828d6e Shorter delays during wifi connect timeout. Some code cleanup in the main state table. Report WiFi TX power during init. Removed SSID from WIFI_State because it wasn't used.
Michiel Broek <mbroek@mbse.eu>
parents: 22
diff changeset
463 vTaskDelay(500 / portTICK_PERIOD_MS);
15
64028e178ff1 Splitted wifi and mqtt connect states to make the connections more robust. Protect the mqtt connection request against requests when a request is already done or in progress.
Michiel Broek <mbroek@mbse.eu>
parents: 14
diff changeset
464 ESP_LOGI(TAG, "* Counter WiFi %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
465 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
466 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
467 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
468 State = State_Init;
15
64028e178ff1 Splitted wifi and mqtt connect states to make the connections more robust. Protect the mqtt connection request against requests when a request is already done or in progress.
Michiel Broek <mbroek@mbse.eu>
parents: 14
diff changeset
469 vTaskDelay(4000 / portTICK_PERIOD_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
470 }
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
471 }
3
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents: 1
diff changeset
472 break;
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents: 1
diff changeset
473
15
64028e178ff1 Splitted wifi and mqtt connect states to make the connections more robust. Protect the mqtt connection request against requests when a request is already done or in progress.
Michiel Broek <mbroek@mbse.eu>
parents: 14
diff changeset
474 case State_Connect_MQTT: if (ready_mqtt()) {
64028e178ff1 Splitted wifi and mqtt connect states to make the connections more robust. Protect the mqtt connection request against requests when a request is already done or in progress.
Michiel Broek <mbroek@mbse.eu>
parents: 14
diff changeset
475 State = State_Working;
64028e178ff1 Splitted wifi and mqtt connect states to make the connections more robust. Protect the mqtt connection request against requests when a request is already done or in progress.
Michiel Broek <mbroek@mbse.eu>
parents: 14
diff changeset
476 ESP_LOGI(TAG, "Connected counter MQTT %d", DisCounter);
64028e178ff1 Splitted wifi and mqtt connect states to make the connections more robust. Protect the mqtt connection request against requests when a request is already done or in progress.
Michiel Broek <mbroek@mbse.eu>
parents: 14
diff changeset
477 DisCounter = 0;
64028e178ff1 Splitted wifi and mqtt connect states to make the connections more robust. Protect the mqtt connection request against requests when a request is already done or in progress.
Michiel Broek <mbroek@mbse.eu>
parents: 14
diff changeset
478 } else {
64028e178ff1 Splitted wifi and mqtt connect states to make the connections more robust. Protect the mqtt connection request against requests when a request is already done or in progress.
Michiel Broek <mbroek@mbse.eu>
parents: 14
diff changeset
479 DisCounter++;
64028e178ff1 Splitted wifi and mqtt connect states to make the connections more robust. Protect the mqtt connection request against requests when a request is already done or in progress.
Michiel Broek <mbroek@mbse.eu>
parents: 14
diff changeset
480 if (DisCounter > 60) {
64028e178ff1 Splitted wifi and mqtt connect states to make the connections more robust. Protect the mqtt connection request against requests when a request is already done or in progress.
Michiel Broek <mbroek@mbse.eu>
parents: 14
diff changeset
481 request_mqtt(false);
64028e178ff1 Splitted wifi and mqtt connect states to make the connections more robust. Protect the mqtt connection request against requests when a request is already done or in progress.
Michiel Broek <mbroek@mbse.eu>
parents: 14
diff changeset
482 request_WiFi(false);
64028e178ff1 Splitted wifi and mqtt connect states to make the connections more robust. Protect the mqtt connection request against requests when a request is already done or in progress.
Michiel Broek <mbroek@mbse.eu>
parents: 14
diff changeset
483 State = State_Init;
64028e178ff1 Splitted wifi and mqtt connect states to make the connections more robust. Protect the mqtt connection request against requests when a request is already done or in progress.
Michiel Broek <mbroek@mbse.eu>
parents: 14
diff changeset
484 vTaskDelay(2000 / portTICK_PERIOD_MS);
64028e178ff1 Splitted wifi and mqtt connect states to make the connections more robust. Protect the mqtt connection request against requests when a request is already done or in progress.
Michiel Broek <mbroek@mbse.eu>
parents: 14
diff changeset
485 }
64028e178ff1 Splitted wifi and mqtt connect states to make the connections more robust. Protect the mqtt connection request against requests when a request is already done or in progress.
Michiel Broek <mbroek@mbse.eu>
parents: 14
diff changeset
486 vTaskDelay(1000 / portTICK_PERIOD_MS);
64028e178ff1 Splitted wifi and mqtt connect states to make the connections more robust. Protect the mqtt connection request against requests when a request is already done or in progress.
Michiel Broek <mbroek@mbse.eu>
parents: 14
diff changeset
487 }
64028e178ff1 Splitted wifi and mqtt connect states to make the connections more robust. Protect the mqtt connection request against requests when a request is already done or in progress.
Michiel Broek <mbroek@mbse.eu>
parents: 14
diff changeset
488 break;
3
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents: 1
diff changeset
489
15
64028e178ff1 Splitted wifi and mqtt connect states to make the connections more robust. Protect the mqtt connection request against requests when a request is already done or in progress.
Michiel Broek <mbroek@mbse.eu>
parents: 14
diff changeset
490 case State_Working: /*
64028e178ff1 Splitted wifi and mqtt connect states to make the connections more robust. Protect the mqtt connection request against requests when a request is already done or in progress.
Michiel Broek <mbroek@mbse.eu>
parents: 14
diff changeset
491 * The final measure power usage.
64028e178ff1 Splitted wifi and mqtt connect states to make the connections more robust. Protect the mqtt connection request against requests when a request is already done or in progress.
Michiel Broek <mbroek@mbse.eu>
parents: 14
diff changeset
492 * This one should include the WiFi usage current.
64028e178ff1 Splitted wifi and mqtt connect states to make the connections more robust. Protect the mqtt connection request against requests when a request is already done or in progress.
Michiel Broek <mbroek@mbse.eu>
parents: 14
diff changeset
493 */
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
494 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
495 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
496 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
497 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
498 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
499 /*
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents: 1
diff changeset
500 * 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
501 * then assume the following:
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents: 1
diff changeset
502 * 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
503 * 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
504 * 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
505 * 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
506 *
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents: 1
diff changeset
507 * 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
508 * 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
509 * 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
510 * 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
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 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
513 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
514 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
515 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
516 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
517 // 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
518 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
519 }
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
520 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
521 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
522 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
523 }
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 }
3
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents: 1
diff changeset
525
18
12506716211c Added nvsio utilities to make read/write to nvs namespace easier. Added variables for deep sleep to nvs namespace. In task_wifi, removed some init parameters that might add to instable wifi connect problems.
Michiel Broek <mbroek@mbse.eu>
parents: 16
diff changeset
526 if (DS_Active) {
12506716211c Added nvsio utilities to make read/write to nvs namespace easier. Added variables for deep sleep to nvs namespace. In task_wifi, removed some init parameters that might add to instable wifi connect problems.
Michiel Broek <mbroek@mbse.eu>
parents: 16
diff changeset
527 totalTime += DS_Time * 1000;
12506716211c Added nvsio utilities to make read/write to nvs namespace easier. Added variables for deep sleep to nvs namespace. In task_wifi, removed some init parameters that might add to instable wifi connect problems.
Michiel Broek <mbroek@mbse.eu>
parents: 16
diff changeset
528 batteryCurrent += DS_CURRENT * DS_Time * 1000;
12506716211c Added nvsio utilities to make read/write to nvs namespace easier. Added variables for deep sleep to nvs namespace. In task_wifi, removed some init parameters that might add to instable wifi connect problems.
Michiel Broek <mbroek@mbse.eu>
parents: 16
diff changeset
529 ESP_LOGI(TAG, "Added %ld totalTime %lld", DS_Time * 1000, totalTime);
12506716211c Added nvsio utilities to make read/write to nvs namespace easier. Added variables for deep sleep to nvs namespace. In task_wifi, removed some init parameters that might add to instable wifi connect problems.
Michiel Broek <mbroek@mbse.eu>
parents: 16
diff changeset
530 }
3
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents: 1
diff changeset
531
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents: 1
diff changeset
532 // 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
533 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
534 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
535 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
536 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
537 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
538 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
539 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
540 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
541
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
542 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
543 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
544
22
9c0bcc91fe1a Decreased WiFi transmit level to 13. Level 15 is unstable, skip 14 just to be sure. It seems to be the bad designed antenna circuit on the ESP32-C3 Lolin v2.1.0 board. The board also benefits from a ground plane connected to the shield of the USB connector. The final implementation needs an external antenna anyway so the onboard antenna will go away. In the meanwhile, the wifi task is now more robust. Also, in testing mode there is no battery alarm, only when building for production.
Michiel Broek <mbroek@mbse.eu>
parents: 21
diff changeset
545 #ifdef CONFIG_CODE_PRODUCTION
9c0bcc91fe1a Decreased WiFi transmit level to 13. Level 15 is unstable, skip 14 just to be sure. It seems to be the bad designed antenna circuit on the ESP32-C3 Lolin v2.1.0 board. The board also benefits from a ground plane connected to the shield of the USB connector. The final implementation needs an external antenna anyway so the onboard antenna will go away. In the meanwhile, the wifi task is now more robust. Also, in testing mode there is no battery alarm, only when building for production.
Michiel Broek <mbroek@mbse.eu>
parents: 21
diff changeset
546 /* Check alarm conditions */
21
df8564c9701e The esp-idf-lib apds9930 component is now working. In nvsio stop logging not updated values. Store gain and aglbit values in nvs.
Michiel Broek <mbroek@mbse.eu>
parents: 20
diff changeset
547 if (batteryState <= 10) {
df8564c9701e The esp-idf-lib apds9930 component is now working. In nvsio stop logging not updated values. Store gain and aglbit values in nvs.
Michiel Broek <mbroek@mbse.eu>
parents: 20
diff changeset
548 Alarm |= AL_ACCULOW;
df8564c9701e The esp-idf-lib apds9930 component is now working. In nvsio stop logging not updated values. Store gain and aglbit values in nvs.
Michiel Broek <mbroek@mbse.eu>
parents: 20
diff changeset
549 } else {
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
550 Alarm &= ~AL_ACCULOW;
21
df8564c9701e The esp-idf-lib apds9930 component is now working. In nvsio stop logging not updated values. Store gain and aglbit values in nvs.
Michiel Broek <mbroek@mbse.eu>
parents: 20
diff changeset
551 }
22
9c0bcc91fe1a Decreased WiFi transmit level to 13. Level 15 is unstable, skip 14 just to be sure. It seems to be the bad designed antenna circuit on the ESP32-C3 Lolin v2.1.0 board. The board also benefits from a ground plane connected to the shield of the USB connector. The final implementation needs an external antenna anyway so the onboard antenna will go away. In the meanwhile, the wifi task is now more robust. Also, in testing mode there is no battery alarm, only when building for production.
Michiel Broek <mbroek@mbse.eu>
parents: 21
diff changeset
552 #endif
9c0bcc91fe1a Decreased WiFi transmit level to 13. Level 15 is unstable, skip 14 just to be sure. It seems to be the bad designed antenna circuit on the ESP32-C3 Lolin v2.1.0 board. The board also benefits from a ground plane connected to the shield of the USB connector. The final implementation needs an external antenna anyway so the onboard antenna will go away. In the meanwhile, the wifi task is now more robust. Also, in testing mode there is no battery alarm, only when building for production.
Michiel Broek <mbroek@mbse.eu>
parents: 21
diff changeset
553 #ifdef CONFIG_CODE_TESTING
9c0bcc91fe1a Decreased WiFi transmit level to 13. Level 15 is unstable, skip 14 just to be sure. It seems to be the bad designed antenna circuit on the ESP32-C3 Lolin v2.1.0 board. The board also benefits from a ground plane connected to the shield of the USB connector. The final implementation needs an external antenna anyway so the onboard antenna will go away. In the meanwhile, the wifi task is now more robust. Also, in testing mode there is no battery alarm, only when building for production.
Michiel Broek <mbroek@mbse.eu>
parents: 21
diff changeset
554 Alarm &= ~AL_ACCULOW;
9c0bcc91fe1a Decreased WiFi transmit level to 13. Level 15 is unstable, skip 14 just to be sure. It seems to be the bad designed antenna circuit on the ESP32-C3 Lolin v2.1.0 board. The board also benefits from a ground plane connected to the shield of the USB connector. The final implementation needs an external antenna anyway so the onboard antenna will go away. In the meanwhile, the wifi task is now more robust. Also, in testing mode there is no battery alarm, only when building for production.
Michiel Broek <mbroek@mbse.eu>
parents: 21
diff changeset
555 #endif
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
556 }
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
557 getTempBaro();
20
c3001154416f Some extra time for the mqtt messages.
Michiel Broek <mbroek@mbse.eu>
parents: 19
diff changeset
558 vTaskDelay(2000 / portTICK_PERIOD_MS);
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
559 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
560 State = State_WorkDone;
20
c3001154416f Some extra time for the mqtt messages.
Michiel Broek <mbroek@mbse.eu>
parents: 19
diff changeset
561 /*
c3001154416f Some extra time for the mqtt messages.
Michiel Broek <mbroek@mbse.eu>
parents: 19
diff changeset
562 * Wait an extra second so the publish message will reach
c3001154416f Some extra time for the mqtt messages.
Michiel Broek <mbroek@mbse.eu>
parents: 19
diff changeset
563 * the broker and the subscriptions are really here.
c3001154416f Some extra time for the mqtt messages.
Michiel Broek <mbroek@mbse.eu>
parents: 19
diff changeset
564 */
c3001154416f Some extra time for the mqtt messages.
Michiel Broek <mbroek@mbse.eu>
parents: 19
diff changeset
565 gTimeNext = millis() + 1000;
3
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents: 1
diff changeset
566 break;
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents: 1
diff changeset
567
23
2cc30d828d6e Shorter delays during wifi connect timeout. Some code cleanup in the main state table. Report WiFi TX power during init. Removed SSID from WIFI_State because it wasn't used.
Michiel Broek <mbroek@mbse.eu>
parents: 22
diff changeset
568 case State_WorkDone: if (gTimeInMillis >= gTimeNext) {
15
64028e178ff1 Splitted wifi and mqtt connect states to make the connections more robust. Protect the mqtt connection request against requests when a request is already done or in progress.
Michiel Broek <mbroek@mbse.eu>
parents: 14
diff changeset
569 State = State_Disconnect_MQTT;
64028e178ff1 Splitted wifi and mqtt connect states to make the connections more robust. Protect the mqtt connection request against requests when a request is already done or in progress.
Michiel Broek <mbroek@mbse.eu>
parents: 14
diff changeset
570 request_mqtt(false);
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
571 }
20
c3001154416f Some extra time for the mqtt messages.
Michiel Broek <mbroek@mbse.eu>
parents: 19
diff changeset
572 vTaskDelay(50 / portTICK_PERIOD_MS);
3
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents: 1
diff changeset
573 break;
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents: 1
diff changeset
574
15
64028e178ff1 Splitted wifi and mqtt connect states to make the connections more robust. Protect the mqtt connection request against requests when a request is already done or in progress.
Michiel Broek <mbroek@mbse.eu>
parents: 14
diff changeset
575 case State_Disconnect_MQTT: wait_mqtt(10000);
64028e178ff1 Splitted wifi and mqtt connect states to make the connections more robust. Protect the mqtt connection request against requests when a request is already done or in progress.
Michiel Broek <mbroek@mbse.eu>
parents: 14
diff changeset
576 State = State_Disconnect_Wifi;
64028e178ff1 Splitted wifi and mqtt connect states to make the connections more robust. Protect the mqtt connection request against requests when a request is already done or in progress.
Michiel Broek <mbroek@mbse.eu>
parents: 14
diff changeset
577 break;
64028e178ff1 Splitted wifi and mqtt connect states to make the connections more robust. Protect the mqtt connection request against requests when a request is already done or in progress.
Michiel Broek <mbroek@mbse.eu>
parents: 14
diff changeset
578
64028e178ff1 Splitted wifi and mqtt connect states to make the connections more robust. Protect the mqtt connection request against requests when a request is already done or in progress.
Michiel Broek <mbroek@mbse.eu>
parents: 14
diff changeset
579 case State_Disconnect_Wifi: request_WiFi(false);
3
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents: 1
diff changeset
580 // // Reset values for average current measurement.
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
581 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
582 gLastTime = millis();
3
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents: 1
diff changeset
583
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents: 1
diff changeset
584 /*
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents: 1
diff changeset
585 * 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
586 * 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
587 */
15
64028e178ff1 Splitted wifi and mqtt connect states to make the connections more robust. Protect the mqtt connection request against requests when a request is already done or in progress.
Michiel Broek <mbroek@mbse.eu>
parents: 14
diff changeset
588 if (Relay1 || Relay2 || Dimmer3 || Dimmer4) {
23
2cc30d828d6e Shorter delays during wifi connect timeout. Some code cleanup in the main state table. Report WiFi TX power during init. Removed SSID from WIFI_State because it wasn't used.
Michiel Broek <mbroek@mbse.eu>
parents: 22
diff changeset
589 DS_Active = 0;
2cc30d828d6e Shorter delays during wifi connect timeout. Some code cleanup in the main state table. Report WiFi TX power during init. Removed SSID from WIFI_State because it wasn't used.
Michiel Broek <mbroek@mbse.eu>
parents: 22
diff changeset
590 nvsio_write_u8((char *)"ds_active", 0);
3
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents: 1
diff changeset
591
18
12506716211c Added nvsio utilities to make read/write to nvs namespace easier. Added variables for deep sleep to nvs namespace. In task_wifi, removed some init parameters that might add to instable wifi connect problems.
Michiel Broek <mbroek@mbse.eu>
parents: 16
diff changeset
592 // Active mode, 60 seconds loop
12506716211c Added nvsio utilities to make read/write to nvs namespace easier. Added variables for deep sleep to nvs namespace. In task_wifi, removed some init parameters that might add to instable wifi connect problems.
Michiel Broek <mbroek@mbse.eu>
parents: 16
diff changeset
593 ST_LOOPS = 6;
12506716211c Added nvsio utilities to make read/write to nvs namespace easier. Added variables for deep sleep to nvs namespace. In task_wifi, removed some init parameters that might add to instable wifi connect problems.
Michiel Broek <mbroek@mbse.eu>
parents: 16
diff changeset
594 gTimeNext = millis() + ST_INTERVAL;
24
74609f70411e Reduced I2C master speed from 400 to 100 Khz. Hope to get power-on init for BMP280 more reliable. Added warnings for I2C devices that are missing. Some logmessages reduced to debug log messages. Added extra 10 mSec delays before read shunt voltage in the INA219 task. Removed and reduced log levels in the MQTT task. Show received data events. Remove WiFi total time debug logging, it's ok now.
Michiel Broek <mbroek@mbse.eu>
parents: 23
diff changeset
595 ESP_LOGD(TAG, "Start sleeploops");
18
12506716211c Added nvsio utilities to make read/write to nvs namespace easier. Added variables for deep sleep to nvs namespace. In task_wifi, removed some init parameters that might add to instable wifi connect problems.
Michiel Broek <mbroek@mbse.eu>
parents: 16
diff changeset
596 State = State_Wait;
12506716211c Added nvsio utilities to make read/write to nvs namespace easier. Added variables for deep sleep to nvs namespace. In task_wifi, removed some init parameters that might add to instable wifi connect problems.
Michiel Broek <mbroek@mbse.eu>
parents: 16
diff changeset
597 } else {
12506716211c Added nvsio utilities to make read/write to nvs namespace easier. Added variables for deep sleep to nvs namespace. In task_wifi, removed some init parameters that might add to instable wifi connect problems.
Michiel Broek <mbroek@mbse.eu>
parents: 16
diff changeset
598 ds_time = DS_TIME;
23
2cc30d828d6e Shorter delays during wifi connect timeout. Some code cleanup in the main state table. Report WiFi TX power during init. Removed SSID from WIFI_State because it wasn't used.
Michiel Broek <mbroek@mbse.eu>
parents: 22
diff changeset
599 if (solarVolts < 6) {
2cc30d828d6e Shorter delays during wifi connect timeout. Some code cleanup in the main state table. Report WiFi TX power during init. Removed SSID from WIFI_State because it wasn't used.
Michiel Broek <mbroek@mbse.eu>
parents: 22
diff changeset
600 // At night, increase the deep-sleep time.
2cc30d828d6e Shorter delays during wifi connect timeout. Some code cleanup in the main state table. Report WiFi TX power during init. Removed SSID from WIFI_State because it wasn't used.
Michiel Broek <mbroek@mbse.eu>
parents: 22
diff changeset
601 ds_time *= 4;
2cc30d828d6e Shorter delays during wifi connect timeout. Some code cleanup in the main state table. Report WiFi TX power during init. Removed SSID from WIFI_State because it wasn't used.
Michiel Broek <mbroek@mbse.eu>
parents: 22
diff changeset
602 }
18
12506716211c Added nvsio utilities to make read/write to nvs namespace easier. Added variables for deep sleep to nvs namespace. In task_wifi, removed some init parameters that might add to instable wifi connect problems.
Michiel Broek <mbroek@mbse.eu>
parents: 16
diff changeset
603 nvsio_write_u8((char *)"ds_active", 1);
12506716211c Added nvsio utilities to make read/write to nvs namespace easier. Added variables for deep sleep to nvs namespace. In task_wifi, removed some init parameters that might add to instable wifi connect problems.
Michiel Broek <mbroek@mbse.eu>
parents: 16
diff changeset
604 DS_Active = 1;
12506716211c Added nvsio utilities to make read/write to nvs namespace easier. Added variables for deep sleep to nvs namespace. In task_wifi, removed some init parameters that might add to instable wifi connect problems.
Michiel Broek <mbroek@mbse.eu>
parents: 16
diff changeset
605 nvsio_write_u32((char *)"ds_time", ds_time);
12506716211c Added nvsio utilities to make read/write to nvs namespace easier. Added variables for deep sleep to nvs namespace. In task_wifi, removed some init parameters that might add to instable wifi connect problems.
Michiel Broek <mbroek@mbse.eu>
parents: 16
diff changeset
606 DS_Time = ds_time;
12506716211c Added nvsio utilities to make read/write to nvs namespace easier. Added variables for deep sleep to nvs namespace. In task_wifi, removed some init parameters that might add to instable wifi connect problems.
Michiel Broek <mbroek@mbse.eu>
parents: 16
diff changeset
607 State = State_GoSleep;
12506716211c Added nvsio utilities to make read/write to nvs namespace easier. Added variables for deep sleep to nvs namespace. In task_wifi, removed some init parameters that might add to instable wifi connect problems.
Michiel Broek <mbroek@mbse.eu>
parents: 16
diff changeset
608 }
3
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents: 1
diff changeset
609 break;
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents: 1
diff changeset
610
23
2cc30d828d6e Shorter delays during wifi connect timeout. Some code cleanup in the main state table. Report WiFi TX power during init. Removed SSID from WIFI_State because it wasn't used.
Michiel Broek <mbroek@mbse.eu>
parents: 22
diff changeset
611 case State_Wait: if (gTimeInMillis >= gTimeNext) {
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
612 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
613 }
23
2cc30d828d6e Shorter delays during wifi connect timeout. Some code cleanup in the main state table. Report WiFi TX power during init. Removed SSID from WIFI_State because it wasn't used.
Michiel Broek <mbroek@mbse.eu>
parents: 22
diff changeset
614 vTaskDelay(50 / portTICK_PERIOD_MS);
3
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents: 1
diff changeset
615 break;
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents: 1
diff changeset
616
23
2cc30d828d6e Shorter delays during wifi connect timeout. Some code cleanup in the main state table. Report WiFi TX power during init. Removed SSID from WIFI_State because it wasn't used.
Michiel Broek <mbroek@mbse.eu>
parents: 22
diff changeset
617 case State_Measure: gTimeNext = millis() + ST_INTERVAL;
2cc30d828d6e Shorter delays during wifi connect timeout. Some code cleanup in the main state table. Report WiFi TX power during init. Removed SSID from WIFI_State because it wasn't used.
Michiel Broek <mbroek@mbse.eu>
parents: 22
diff changeset
618 getVoltsCurrent();
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
619 if (loopno >= ST_LOOPS) {
24
74609f70411e Reduced I2C master speed from 400 to 100 Khz. Hope to get power-on init for BMP280 more reliable. Added warnings for I2C devices that are missing. Some logmessages reduced to debug log messages. Added extra 10 mSec delays before read shunt voltage in the INA219 task. Removed and reduced log levels in the MQTT task. Show received data events. Remove WiFi total time debug logging, it's ok now.
Michiel Broek <mbroek@mbse.eu>
parents: 23
diff changeset
620 ESP_LOGD(TAG, "Enough loops, do connect");
12
bb72d448e282 In the esp-idf-lib the adps9930 driver uses a device descriptor structure instead of just i2c_dev_t. Fixed a linking issue. Added APDS9930 task. Added getLightValues function. Added wifi quality value to the MQTT payload. The payload is complete and will be published.
Michiel Broek <mbroek@mbse.eu>
parents: 9
diff changeset
621 getLightValues();
15
64028e178ff1 Splitted wifi and mqtt connect states to make the connections more robust. Protect the mqtt connection request against requests when a request is already done or in progress.
Michiel Broek <mbroek@mbse.eu>
parents: 14
diff changeset
622 State = State_Connect_Wifi;
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
623 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
624 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
625 } 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
626 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
627 }
3
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents: 1
diff changeset
628 break;
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents: 1
diff changeset
629
18
12506716211c Added nvsio utilities to make read/write to nvs namespace easier. Added variables for deep sleep to nvs namespace. In task_wifi, removed some init parameters that might add to instable wifi connect problems.
Michiel Broek <mbroek@mbse.eu>
parents: 16
diff changeset
630 case State_GoSleep: ESP_LOGI(TAG, "Going to deep-sleep for %ld seconds", DS_Time);
12506716211c Added nvsio utilities to make read/write to nvs namespace easier. Added variables for deep sleep to nvs namespace. In task_wifi, removed some init parameters that might add to instable wifi connect problems.
Michiel Broek <mbroek@mbse.eu>
parents: 16
diff changeset
631 ESP_ERROR_CHECK(esp_sleep_enable_timer_wakeup(DS_Time * 1e6));
12506716211c Added nvsio utilities to make read/write to nvs namespace easier. Added variables for deep sleep to nvs namespace. In task_wifi, removed some init parameters that might add to instable wifi connect problems.
Michiel Broek <mbroek@mbse.eu>
parents: 16
diff changeset
632 esp_deep_sleep_start();
3
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents: 1
diff changeset
633 break;
e5d91caa6ab4 Added begin of INA219 measurements. Added raw main state table.
Michiel Broek <mbroek@mbse.eu>
parents: 1
diff changeset
634 }
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
635 }
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
636 // Not reached.
0
f8b0268c8d0a Initial project setup
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
637 }

mercurial