Mon, 17 Apr 2023 16:20:58 +0200
Version 0.4.1 Measure internal chip temperature, range -10 to 80. Result available in mqtt json payload.
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:
diff
changeset
|
1 | /** |
b1f38105ca7e
Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
2 | * @file task_mqtt.c |
b1f38105ca7e
Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
3 | * @brief The FreeRTOS task to maintain MQTT connections. |
b1f38105ca7e
Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
4 | */ |
b1f38105ca7e
Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
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:
diff
changeset
|
6 | |
b1f38105ca7e
Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
7 | #include "config.h" |
b1f38105ca7e
Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
8 | |
b1f38105ca7e
Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
9 | |
b1f38105ca7e
Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
10 | static const char *TAG = "task_mqtt"; |
b1f38105ca7e
Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
11 | |
b1f38105ca7e
Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
12 | EventGroupHandle_t xEventGroupMQTT; ///< Events MQTT task |
b1f38105ca7e
Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
13 | SemaphoreHandle_t xSemaphorePcounter; ///< Publish counter semaphore. |
b1f38105ca7e
Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
14 | int count_pub = 0; ///< Outstanding published messages. |
b1f38105ca7e
Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
15 | esp_mqtt_client_handle_t client; ///< MQTT client handle |
b1f38105ca7e
Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
16 | |
b1f38105ca7e
Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
17 | const int TASK_MQTT_CONNECT = BIT0; ///< Request MQTT connection |
b1f38105ca7e
Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
18 | const int TASK_MQTT_DISCONNECT = BIT1; ///< Request MQTT disconnect |
b1f38105ca7e
Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
19 | const int TASK_MQTT_CONNECTED = BIT2; ///< MQTT is connected |
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
|
20 | const int TASK_MQTT_DISCONNECTED = BIT3; |
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:
diff
changeset
|
21 | |
b1f38105ca7e
Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
22 | const char *sensState[] = { "OK", "ERROR" }; ///< Sensor state strings |
b1f38105ca7e
Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
23 | const char *unitMode[] = { "OFF", "ON" }; ///< Units state strings |
b1f38105ca7e
Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
24 | |
32
84e54b14e7db
Version 0.4.1 Measure internal chip temperature, range -10 to 80. Result available in mqtt json payload.
Michiel Broek <mbroek@mbse.eu>
parents:
26
diff
changeset
|
25 | extern TEMP_State *temp_state; ///< Internal temperature state |
84e54b14e7db
Version 0.4.1 Measure internal chip temperature, range -10 to 80. Result available in mqtt json payload.
Michiel Broek <mbroek@mbse.eu>
parents:
26
diff
changeset
|
26 | extern SemaphoreHandle_t xSemaphoreTEMP; ///< Internal temperature semaphore |
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:
diff
changeset
|
27 | extern BMP280_State *bmp280_state; ///< BMP280 state |
b1f38105ca7e
Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
28 | extern SemaphoreHandle_t xSemaphoreBMP280; ///< BMP280 lock semaphore |
b1f38105ca7e
Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
29 | extern INA219_State *ina219_state; ///< INA219 state |
b1f38105ca7e
Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
30 | extern SemaphoreHandle_t xSemaphoreINA219; ///< INA219 lock semaphore |
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:
10
diff
changeset
|
31 | extern APDS9930_State *apds9930_state; ///< APDS9930 state |
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:
10
diff
changeset
|
32 | extern SemaphoreHandle_t xSemaphoreAPDS9930; ///< APDS9930 lock semaphore |
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:
diff
changeset
|
33 | extern WIFI_State *wifi_state; ///< WiFi state |
b1f38105ca7e
Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
34 | extern SemaphoreHandle_t xSemaphoreWiFi; ///< WiFi lock semaphore |
b1f38105ca7e
Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
35 | extern const esp_app_desc_t *app_desc; |
b1f38105ca7e
Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
36 | |
b1f38105ca7e
Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
37 | extern uint32_t Alarm; |
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:
10
diff
changeset
|
38 | extern int batteryState; |
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:
diff
changeset
|
39 | extern float batteryVolts; |
b1f38105ca7e
Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
40 | extern float batteryCurrent; |
b1f38105ca7e
Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
41 | extern float batteryPower; |
b1f38105ca7e
Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
42 | extern float solarVolts; |
b1f38105ca7e
Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
43 | extern float solarCurrent; |
b1f38105ca7e
Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
44 | extern float solarPower; |
b1f38105ca7e
Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
45 | |
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:
7
diff
changeset
|
46 | extern uint8_t Relay1; |
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:
7
diff
changeset
|
47 | extern uint8_t Relay2; |
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:
7
diff
changeset
|
48 | extern uint8_t Dimmer3; |
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:
7
diff
changeset
|
49 | extern uint8_t Dimmer4; |
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:
7
diff
changeset
|
50 | |
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:
diff
changeset
|
51 | |
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:
12
diff
changeset
|
52 | void request_mqtt(bool state) |
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:
diff
changeset
|
53 | { |
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:
12
diff
changeset
|
54 | if (state) { |
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:
12
diff
changeset
|
55 | if (xEventGroupGetBits(xEventGroupMQTT) & TASK_MQTT_CONNECTED) { |
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:
12
diff
changeset
|
56 | ESP_LOGW(TAG, "request_mqtt(true) but already connected"); |
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:
12
diff
changeset
|
57 | } 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:
12
diff
changeset
|
58 | xEventGroupSetBits(xEventGroupMQTT, TASK_MQTT_CONNECT); |
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:
12
diff
changeset
|
59 | } |
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:
12
diff
changeset
|
60 | } 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:
12
diff
changeset
|
61 | if (xEventGroupGetBits(xEventGroupMQTT) & TASK_MQTT_DISCONNECT) { |
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:
12
diff
changeset
|
62 | ESP_LOGW(TAG, "request_mqtt(false) already in progress"); |
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:
12
diff
changeset
|
63 | } else if (! (xEventGroupGetBits(xEventGroupMQTT) & TASK_MQTT_CONNECTED)) { |
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:
12
diff
changeset
|
64 | ESP_LOGW(TAG, "request_mqtt(false) but already disconnected"); |
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:
12
diff
changeset
|
65 | } 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:
12
diff
changeset
|
66 | xEventGroupSetBits(xEventGroupMQTT, TASK_MQTT_DISCONNECT); |
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:
12
diff
changeset
|
67 | } |
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:
12
diff
changeset
|
68 | } |
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:
diff
changeset
|
69 | } |
b1f38105ca7e
Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
70 | |
b1f38105ca7e
Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
71 | |
b1f38105ca7e
Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
72 | |
b1f38105ca7e
Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
73 | bool ready_mqtt(void) |
b1f38105ca7e
Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
74 | { |
b1f38105ca7e
Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
75 | if (xEventGroupGetBits(xEventGroupMQTT) & TASK_MQTT_CONNECTED) |
b1f38105ca7e
Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
76 | return true; |
b1f38105ca7e
Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
77 | return false; |
b1f38105ca7e
Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
78 | } |
b1f38105ca7e
Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
79 | |
b1f38105ca7e
Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
80 | |
b1f38105ca7e
Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
81 | |
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
|
82 | void wait_mqtt(int 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
|
83 | { |
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:
18
diff
changeset
|
84 | // EventBits_t uxBits; |
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
|
85 | |
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
|
86 | if (xEventGroupGetBits(xEventGroupMQTT) & TASK_MQTT_CONNECTED) { |
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:
18
diff
changeset
|
87 | /*uxBits =*/ xEventGroupWaitBits(xEventGroupMQTT, TASK_MQTT_DISCONNECTED, pdTRUE, pdFALSE, time / portTICK_PERIOD_MS); |
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:
18
diff
changeset
|
88 | // ESP_LOGI(TAG, "wait_mqtt(%d) 2 %lu", time, uxBits & TASK_MQTT_DISCONNECTED); |
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:
18
diff
changeset
|
89 | // } 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:
18
diff
changeset
|
90 | // ESP_LOGI(TAG, "wait_mqtt(%d) 3 not connected", time); |
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
|
91 | } |
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
|
92 | } |
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
|
93 | |
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
|
94 | |
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
|
95 | |
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:
diff
changeset
|
96 | /** |
b1f38105ca7e
Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
97 | * @brief Generate the mqtt topic base part. |
b1f38105ca7e
Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
98 | * @return The topic string allocated in memory. |
b1f38105ca7e
Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
99 | */ |
b1f38105ca7e
Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
100 | char *topic_base(void) |
b1f38105ca7e
Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
101 | { |
b1f38105ca7e
Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
102 | char *tmp; |
b1f38105ca7e
Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
103 | |
b1f38105ca7e
Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
104 | #ifdef CONFIG_CODE_PRODUCTION |
b1f38105ca7e
Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
105 | tmp = xstrcpy((char *)"balkon/"); |
b1f38105ca7e
Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
106 | #endif |
b1f38105ca7e
Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
107 | #ifdef CONFIG_CODE_TESTING |
b1f38105ca7e
Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
108 | tmp = xstrcpy((char *)"wemos/"); |
b1f38105ca7e
Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
109 | #endif |
b1f38105ca7e
Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents:
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:
diff
changeset
|
111 | return tmp; |
b1f38105ca7e
Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents:
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:
diff
changeset
|
113 | |
b1f38105ca7e
Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
114 | |
b1f38105ca7e
Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents:
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:
diff
changeset
|
116 | /** |
b1f38105ca7e
Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
117 | * @brief The mqtt generic publish function. |
b1f38105ca7e
Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
118 | * @param topic The topic of the mqtt message. |
b1f38105ca7e
Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
119 | * @param payload The payload of the mqtt message. |
b1f38105ca7e
Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
120 | */ |
b1f38105ca7e
Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
121 | void publisher(char *topic, char *payload) |
b1f38105ca7e
Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
122 | { |
b1f38105ca7e
Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
123 | /* |
b1f38105ca7e
Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
124 | * First count, then sent the data. |
b1f38105ca7e
Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents:
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:
diff
changeset
|
126 | if (xSemaphoreTake(xSemaphorePcounter, 10) == pdTRUE) { |
b1f38105ca7e
Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
127 | count_pub++; |
b1f38105ca7e
Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
128 | xSemaphoreGive(xSemaphorePcounter); |
b1f38105ca7e
Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
129 | } 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:
diff
changeset
|
130 | ESP_LOGE(TAG, "publisher() counter lock"); |
b1f38105ca7e
Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
131 | } |
b1f38105ca7e
Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
132 | |
b1f38105ca7e
Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
133 | if (payload) |
b1f38105ca7e
Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
134 | esp_mqtt_client_publish(client, topic, payload, strlen(payload), 1, 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:
diff
changeset
|
135 | 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:
diff
changeset
|
136 | esp_mqtt_client_publish(client, topic, NULL, 0, 1, 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:
diff
changeset
|
137 | } |
b1f38105ca7e
Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents:
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:
diff
changeset
|
139 | |
b1f38105ca7e
Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
140 | |
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:
7
diff
changeset
|
141 | bool do_event_data(char *check, char *topic, char *data) { |
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:
7
diff
changeset
|
142 | return false; |
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:
7
diff
changeset
|
143 | } |
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:
7
diff
changeset
|
144 | |
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:
7
diff
changeset
|
145 | |
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:
diff
changeset
|
146 | void publish(void) |
b1f38105ca7e
Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
147 | { |
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
|
148 | char *topic = NULL, *payload = NULL, buf[64]; |
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
|
149 | const esp_app_desc_t *app_desc = esp_app_get_description(); |
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:
10
diff
changeset
|
150 | int Quality = 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:
diff
changeset
|
151 | |
10
eee990609da7
Fixed start of json status message.
Michiel Broek <mbroek@mbse.eu>
parents:
9
diff
changeset
|
152 | // {"system":{"battery":40,"alarm":0,"version":"0.3.1","rssi":-77,"wifi":46,"light":{"lux":0.1,"gain":3,"agl":0}},"solar":{"voltage":0.31,"current":0,"power":0},"battery":{"voltage":12.27,"current":53.5,"power":0.657},"real":{"current":-53.5},"TH":{"temperature":8.88,"humidity":0},"output":{"relay1":0,"relay2":0,"dimmer3":90,"dimmer4":80}} |
eee990609da7
Fixed start of json status message.
Michiel Broek <mbroek@mbse.eu>
parents:
9
diff
changeset
|
153 | payload = xstrcpy((char *)"{\"system\":{\"battery\":"); |
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:
10
diff
changeset
|
154 | sprintf(buf, "%d", batteryState); |
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:
diff
changeset
|
155 | payload = xstrcat(payload, buf); |
b1f38105ca7e
Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
156 | payload = xstrcat(payload, (char *)",\"alarm\":"); |
b1f38105ca7e
Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
157 | sprintf(buf, "%ld", Alarm); |
b1f38105ca7e
Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
158 | payload = xstrcat(payload, buf); |
b1f38105ca7e
Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
159 | payload = xstrcat(payload, (char *)",\"version\":\""); |
b1f38105ca7e
Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
160 | payload = xstrcat(payload, (char *)app_desc->version); |
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
|
161 | payload = xstrcat(payload, (char *)"\",\"rssi\":"); |
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
|
162 | if (xSemaphoreTake(xSemaphoreWiFi, 25) == pdTRUE) { |
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
|
163 | sprintf(buf, "%d", wifi_state->STA_rssi); |
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
|
164 | payload = xstrcat(payload, buf); |
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:
10
diff
changeset
|
165 | payload = xstrcat(payload, (char *)",\"wifi\":"); |
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:
10
diff
changeset
|
166 | if (wifi_state->STA_rssi <= -100) { |
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:
10
diff
changeset
|
167 | Quality = 0; |
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:
10
diff
changeset
|
168 | } else if (wifi_state->STA_rssi >= -50) { |
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:
10
diff
changeset
|
169 | Quality = 100; |
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:
10
diff
changeset
|
170 | } else { |
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:
10
diff
changeset
|
171 | Quality = 2 * (wifi_state->STA_rssi + 100); |
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:
10
diff
changeset
|
172 | } |
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:
10
diff
changeset
|
173 | sprintf(buf, "%d", Quality); |
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:
10
diff
changeset
|
174 | payload = xstrcat(payload, buf); |
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
|
175 | xSemaphoreGive(xSemaphoreWiFi); |
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
|
176 | } |
32
84e54b14e7db
Version 0.4.1 Measure internal chip temperature, range -10 to 80. Result available in mqtt json payload.
Michiel Broek <mbroek@mbse.eu>
parents:
26
diff
changeset
|
177 | if (xSemaphoreTake(xSemaphoreTEMP, 25) == pdTRUE) { |
84e54b14e7db
Version 0.4.1 Measure internal chip temperature, range -10 to 80. Result available in mqtt json payload.
Michiel Broek <mbroek@mbse.eu>
parents:
26
diff
changeset
|
178 | payload = xstrcat(payload, (char *)",\"temperature\":"); |
84e54b14e7db
Version 0.4.1 Measure internal chip temperature, range -10 to 80. Result available in mqtt json payload.
Michiel Broek <mbroek@mbse.eu>
parents:
26
diff
changeset
|
179 | sprintf(buf, "%.1f", temp_state->temperature); |
84e54b14e7db
Version 0.4.1 Measure internal chip temperature, range -10 to 80. Result available in mqtt json payload.
Michiel Broek <mbroek@mbse.eu>
parents:
26
diff
changeset
|
180 | payload = xstrcat(payload, buf); |
84e54b14e7db
Version 0.4.1 Measure internal chip temperature, range -10 to 80. Result available in mqtt json payload.
Michiel Broek <mbroek@mbse.eu>
parents:
26
diff
changeset
|
181 | xSemaphoreGive(xSemaphoreTEMP); |
84e54b14e7db
Version 0.4.1 Measure internal chip temperature, range -10 to 80. Result available in mqtt json payload.
Michiel Broek <mbroek@mbse.eu>
parents:
26
diff
changeset
|
182 | } |
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:
diff
changeset
|
183 | payload = xstrcat(payload, (char *)",\"light\":{\"lux\":"); |
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:
10
diff
changeset
|
184 | if (xSemaphoreTake(xSemaphoreAPDS9930, 25) == pdTRUE) { |
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:
10
diff
changeset
|
185 | sprintf(buf, "%.1f", apds9930_state->ambient_light); |
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:
10
diff
changeset
|
186 | payload = xstrcat(payload, buf); |
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:
10
diff
changeset
|
187 | payload = xstrcat(payload, (char *)",\"gain\":"); |
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:
10
diff
changeset
|
188 | sprintf(buf, "%d", apds9930_state->gain); |
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:
10
diff
changeset
|
189 | payload = xstrcat(payload, buf); |
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:
10
diff
changeset
|
190 | payload = xstrcat(payload, (char *)",\"agl\":"); |
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:
10
diff
changeset
|
191 | sprintf(buf, "%d", apds9930_state->aglbit); |
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:
10
diff
changeset
|
192 | payload = xstrcat(payload, buf); |
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:
10
diff
changeset
|
193 | xSemaphoreGive(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:
10
diff
changeset
|
194 | } |
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:
diff
changeset
|
195 | payload = xstrcat(payload, (char *)"}},\"solar\":{\"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:
diff
changeset
|
196 | sprintf(buf, "%.2f", solarVolts); |
b1f38105ca7e
Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
197 | payload = xstrcat(payload, buf); |
b1f38105ca7e
Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
198 | payload = xstrcat(payload, (char *)",\"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:
diff
changeset
|
199 | sprintf(buf, "%.1f", solarCurrent); |
b1f38105ca7e
Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
200 | payload = xstrcat(payload, buf); |
b1f38105ca7e
Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
201 | payload = xstrcat(payload, (char *)",\"power\":"); |
b1f38105ca7e
Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
202 | sprintf(buf, "%.3f", solarPower / 1000.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:
diff
changeset
|
203 | payload = xstrcat(payload, buf); |
b1f38105ca7e
Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
204 | payload = xstrcat(payload, (char *)"},\"battery\":{\"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:
diff
changeset
|
205 | sprintf(buf, "%.2f", batteryVolts); |
b1f38105ca7e
Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
206 | payload = xstrcat(payload, buf); |
b1f38105ca7e
Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
207 | payload = xstrcat(payload, (char *)",\"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:
diff
changeset
|
208 | sprintf(buf, "%.1f", batteryCurrent); |
b1f38105ca7e
Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
209 | payload = xstrcat(payload, buf); |
b1f38105ca7e
Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
210 | payload = xstrcat(payload, (char *)",\"power\":"); |
b1f38105ca7e
Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
211 | sprintf(buf, "%.3f", batteryPower / 1000.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:
diff
changeset
|
212 | payload = xstrcat(payload, buf); |
b1f38105ca7e
Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
213 | payload = xstrcat(payload, (char *)"},\"real\":{\"current\":"); |
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
|
214 | sprintf(buf, "%.1f", (0 - batteryCurrent) + 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
|
215 | payload = xstrcat(payload, buf); |
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:
diff
changeset
|
216 | payload = xstrcat(payload, (char *)"},\"TB\":{\"temperature\":"); |
b1f38105ca7e
Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
217 | if (xSemaphoreTake(xSemaphoreBMP280, 25) == pdTRUE) { |
b1f38105ca7e
Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
218 | sprintf(buf, "%.2f", bmp280_state->temperature); |
b1f38105ca7e
Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
219 | payload = xstrcat(payload, buf); |
b1f38105ca7e
Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
220 | payload = xstrcat(payload, (char *)",\"pressure\":"); |
b1f38105ca7e
Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
221 | sprintf(buf, "%.1f", bmp280_state->pressure / 100.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:
diff
changeset
|
222 | payload = xstrcat(payload, buf); |
b1f38105ca7e
Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
223 | xSemaphoreGive(xSemaphoreBMP280); |
b1f38105ca7e
Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
224 | } |
b1f38105ca7e
Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
225 | payload = xstrcat(payload, (char *)"},\"output\":{\"relay1\":"); |
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:
7
diff
changeset
|
226 | sprintf(buf, "%d", Relay1); |
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:
7
diff
changeset
|
227 | payload = xstrcat(payload, buf); |
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:
diff
changeset
|
228 | payload = xstrcat(payload, (char *)",\"relay2\":"); |
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:
7
diff
changeset
|
229 | sprintf(buf, "%d", Relay2); |
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:
7
diff
changeset
|
230 | payload = xstrcat(payload, buf); |
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:
diff
changeset
|
231 | payload = xstrcat(payload, (char *)",\"dimmer3\":"); |
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:
7
diff
changeset
|
232 | sprintf(buf, "%d", Dimmer3); |
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:
7
diff
changeset
|
233 | payload = xstrcat(payload, buf); |
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:
diff
changeset
|
234 | payload = xstrcat(payload, (char *)",\"dimmer4\":"); |
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:
7
diff
changeset
|
235 | sprintf(buf, "%d", Dimmer4); |
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:
7
diff
changeset
|
236 | payload = xstrcat(payload, buf); |
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:
diff
changeset
|
237 | payload = xstrcat(payload, (char *)"}}"); |
b1f38105ca7e
Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
238 | topic = topic_base(); |
b1f38105ca7e
Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
239 | topic = xstrcat(topic, (char *)"status"); |
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
|
240 | ESP_LOGI(TAG, "%s %s", topic, payload); |
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:
10
diff
changeset
|
241 | publisher(topic, payload); |
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:
diff
changeset
|
242 | free(topic); |
b1f38105ca7e
Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
243 | topic = NULL; |
b1f38105ca7e
Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
244 | free(payload); |
b1f38105ca7e
Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
245 | payload = NULL; |
b1f38105ca7e
Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
246 | } |
b1f38105ca7e
Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
247 | |
b1f38105ca7e
Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
248 | |
b1f38105ca7e
Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
249 | |
b1f38105ca7e
Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
250 | static esp_err_t mqtt_event_handler_cb(esp_mqtt_event_handle_t event) |
b1f38105ca7e
Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
251 | { |
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:
18
diff
changeset
|
252 | char *subscr = NULL, *check = NULL; |
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:
7
diff
changeset
|
253 | |
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:
diff
changeset
|
254 | switch (event->event_id) { |
b1f38105ca7e
Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
255 | |
b1f38105ca7e
Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
256 | case MQTT_EVENT_CONNECTED: |
b1f38105ca7e
Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
257 | ESP_LOGI(TAG, "MQTT_EVENT_CONNECTED"); |
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:
18
diff
changeset
|
258 | subscr = topic_base(); |
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:
18
diff
changeset
|
259 | subscr = xstrcat(subscr, (char *)"output/set/#"); |
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:
18
diff
changeset
|
260 | int msgid = esp_mqtt_client_subscribe(client, subscr, 0); |
25
c5a9bde0268f
A bit simpler mqtt event handling.
Michiel Broek <mbroek@mbse.eu>
parents:
24
diff
changeset
|
261 | ESP_LOGD(TAG, "Subscribe `%s' id %d", subscr, msgid); |
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:
18
diff
changeset
|
262 | free(subscr); |
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:
18
diff
changeset
|
263 | subscr = 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:
diff
changeset
|
264 | xEventGroupSetBits(xEventGroupMQTT, TASK_MQTT_CONNECTED); |
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
|
265 | xEventGroupClearBits(xEventGroupMQTT, TASK_MQTT_DISCONNECTED); |
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:
diff
changeset
|
266 | 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:
diff
changeset
|
267 | |
b1f38105ca7e
Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
268 | case MQTT_EVENT_DISCONNECTED: |
b1f38105ca7e
Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
269 | ESP_LOGI(TAG, "MQTT_EVENT_DISCONNECTED"); |
b1f38105ca7e
Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
270 | xEventGroupClearBits(xEventGroupMQTT, TASK_MQTT_CONNECTED); |
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
|
271 | xEventGroupSetBits(xEventGroupMQTT, TASK_MQTT_DISCONNECTED); |
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:
diff
changeset
|
272 | 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:
diff
changeset
|
273 | |
b1f38105ca7e
Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
274 | case MQTT_EVENT_SUBSCRIBED: |
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:
18
diff
changeset
|
275 | ESP_LOGD(TAG, "MQTT_EVENT_SUBSCRIBED, msg_id=%d", event->msg_id); |
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:
diff
changeset
|
276 | 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:
diff
changeset
|
277 | |
b1f38105ca7e
Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
278 | case MQTT_EVENT_UNSUBSCRIBED: |
b1f38105ca7e
Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
279 | ESP_LOGI(TAG, "MQTT_EVENT_UNSUBSCRIBED, msg_id=%d", event->msg_id); |
b1f38105ca7e
Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
280 | 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:
diff
changeset
|
281 | |
b1f38105ca7e
Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
282 | case MQTT_EVENT_PUBLISHED: |
b1f38105ca7e
Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
283 | if (xSemaphoreTake(xSemaphorePcounter, 10) == pdTRUE) { |
b1f38105ca7e
Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
284 | if (count_pub) { |
b1f38105ca7e
Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
285 | count_pub--; |
b1f38105ca7e
Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
286 | } |
b1f38105ca7e
Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
287 | xSemaphoreGive(xSemaphorePcounter); |
b1f38105ca7e
Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
288 | } 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:
diff
changeset
|
289 | ESP_LOGE(TAG, "mqtt_event_handler_cb(() lock error event"); |
b1f38105ca7e
Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
290 | } |
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:
18
diff
changeset
|
291 | ESP_LOGI(TAG, "MQTT_EVENT_PUBLISHED, msg_id=%d, %d msgs in queue", event->msg_id, count_pub); |
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:
diff
changeset
|
292 | 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:
diff
changeset
|
293 | |
b1f38105ca7e
Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
294 | case MQTT_EVENT_DATA: |
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:
18
diff
changeset
|
295 | char data[65], topic[128]; |
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:
7
diff
changeset
|
296 | if (event->data_len < 65) |
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:
7
diff
changeset
|
297 | snprintf(data, 64, "%.*s", event->data_len, event->data); |
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:
7
diff
changeset
|
298 | else |
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:
7
diff
changeset
|
299 | data[0] = '\0'; |
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:
18
diff
changeset
|
300 | if (event->topic_len < 128) |
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:
18
diff
changeset
|
301 | snprintf(topic, 127, "%.*s", event->topic_len, event->topic); |
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:
18
diff
changeset
|
302 | 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:
18
diff
changeset
|
303 | topic[0] = '\0'; |
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:
18
diff
changeset
|
304 | ESP_LOGI(TAG, "MQTT_EVENT_DATA %s %s", topic, data); |
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:
7
diff
changeset
|
305 | |
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:
18
diff
changeset
|
306 | check = topic_base(); |
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:
18
diff
changeset
|
307 | check = xstrcat(check, (char *)"output/set/1"); |
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:
18
diff
changeset
|
308 | if (strncmp(check, event->topic, event->topic_len) == 0) { |
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:
18
diff
changeset
|
309 | ESP_LOGD(TAG, "Got %s `%s' %d", check, data, atoi(data)); |
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:
15
diff
changeset
|
310 | Relay1 = (uint8_t)atoi(data); |
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:
15
diff
changeset
|
311 | nvsio_write_u8((char *)"out1", Relay1); |
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:
7
diff
changeset
|
312 | } |
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:
7
diff
changeset
|
313 | |
25
c5a9bde0268f
A bit simpler mqtt event handling.
Michiel Broek <mbroek@mbse.eu>
parents:
24
diff
changeset
|
314 | check[strlen(check)-1] = '2'; |
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:
18
diff
changeset
|
315 | if (strncmp(check, event->topic, event->topic_len) == 0) { |
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:
18
diff
changeset
|
316 | ESP_LOGD(TAG, "Got %s `%s' %d", check, data, atoi(data)); |
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:
15
diff
changeset
|
317 | Relay2 = (uint8_t)atoi(data); |
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:
15
diff
changeset
|
318 | nvsio_write_u8((char *)"out2", Relay2); |
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:
7
diff
changeset
|
319 | } |
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:
7
diff
changeset
|
320 | |
25
c5a9bde0268f
A bit simpler mqtt event handling.
Michiel Broek <mbroek@mbse.eu>
parents:
24
diff
changeset
|
321 | check[strlen(check)-1] = '3'; |
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:
18
diff
changeset
|
322 | if (strncmp(check, event->topic, event->topic_len) == 0) { |
25
c5a9bde0268f
A bit simpler mqtt event handling.
Michiel Broek <mbroek@mbse.eu>
parents:
24
diff
changeset
|
323 | ESP_LOGD(TAG, "Got %s `%s' %d", check, data, atoi(data)); |
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:
15
diff
changeset
|
324 | Dimmer3 = (uint8_t)atoi(data); |
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:
15
diff
changeset
|
325 | nvsio_write_u8((char *)"out3", Dimmer3); |
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:
7
diff
changeset
|
326 | } |
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:
7
diff
changeset
|
327 | |
25
c5a9bde0268f
A bit simpler mqtt event handling.
Michiel Broek <mbroek@mbse.eu>
parents:
24
diff
changeset
|
328 | check[strlen(check)-1] = '4'; |
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:
18
diff
changeset
|
329 | if (strncmp(check, event->topic, event->topic_len) == 0) { |
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:
18
diff
changeset
|
330 | ESP_LOGD(TAG, "Got %s `%s' %d", check, data, atoi(data)); |
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:
15
diff
changeset
|
331 | Dimmer4 = (uint8_t)atoi(data); |
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:
15
diff
changeset
|
332 | nvsio_write_u8((char *)"out4", Dimmer4); |
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:
7
diff
changeset
|
333 | } |
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:
18
diff
changeset
|
334 | free(check); |
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:
18
diff
changeset
|
335 | check = 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:
diff
changeset
|
336 | 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:
diff
changeset
|
337 | |
b1f38105ca7e
Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
338 | case MQTT_EVENT_ERROR: |
b1f38105ca7e
Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
339 | ESP_LOGI(TAG, "MQTT_EVENT_ERROR"); |
b1f38105ca7e
Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
340 | 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:
diff
changeset
|
341 | |
b1f38105ca7e
Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
342 | case MQTT_EVENT_BEFORE_CONNECT: |
6
bad3414f7bc4
Added the getTempBaro and getVoltsCurrent functions to process the measured data. Added some main task code. Added subscribe to MQTT events.
Michiel Broek <mbroek@mbse.eu>
parents:
5
diff
changeset
|
343 | ESP_LOGD(TAG, "MQTT_EVENT_BEFORE_CONNECT"); |
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:
diff
changeset
|
344 | // Configure connection can be here. |
b1f38105ca7e
Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
345 | 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:
diff
changeset
|
346 | |
b1f38105ca7e
Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
347 | default: |
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
|
348 | ESP_LOGE(TAG, "Other event id:%d", event->event_id); |
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:
diff
changeset
|
349 | 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:
diff
changeset
|
350 | } |
b1f38105ca7e
Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
351 | return ESP_OK; |
b1f38105ca7e
Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
352 | } |
b1f38105ca7e
Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
353 | |
b1f38105ca7e
Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
354 | |
b1f38105ca7e
Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
355 | |
b1f38105ca7e
Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
356 | static void mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_t event_id, void *event_data) { |
b1f38105ca7e
Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
357 | mqtt_event_handler_cb(event_data); |
b1f38105ca7e
Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
358 | } |
b1f38105ca7e
Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
359 | |
b1f38105ca7e
Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
360 | |
b1f38105ca7e
Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
361 | |
b1f38105ca7e
Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
362 | /* |
b1f38105ca7e
Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
363 | * Task to read temperature sensors on request. |
b1f38105ca7e
Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
364 | */ |
b1f38105ca7e
Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
365 | void task_mqtt(void *pvParameter) |
b1f38105ca7e
Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
366 | { |
b1f38105ca7e
Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
367 | esp_err_t err; |
b1f38105ca7e
Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
368 | char *uri = NULL, port[11]; |
b1f38105ca7e
Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
369 | |
b1f38105ca7e
Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
370 | ESP_LOGI(TAG, "Starting MQTT task"); |
26
29dc2064e2ce
Version 0.4.0. Disable normal logging mqtt.
Michiel Broek <mbroek@mbse.eu>
parents:
25
diff
changeset
|
371 | esp_log_level_set("MQTT_CLIENT", ESP_LOG_ERROR); |
5
b1f38105ca7e
Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
372 | xSemaphorePcounter = xSemaphoreCreateMutex(); |
b1f38105ca7e
Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
373 | |
b1f38105ca7e
Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
374 | /* event handler and event group for the wifi driver */ |
b1f38105ca7e
Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
375 | xEventGroupMQTT = xEventGroupCreate(); |
b1f38105ca7e
Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
376 | EventBits_t uxBits; |
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
|
377 | xEventGroupSetBits(xEventGroupMQTT, TASK_MQTT_DISCONNECTED); |
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:
diff
changeset
|
378 | |
b1f38105ca7e
Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
379 | esp_mqtt_client_config_t mqtt_cfg = { |
b1f38105ca7e
Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
380 | .broker.address.uri = "mqtt://localhost", |
b1f38105ca7e
Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
381 | }; |
b1f38105ca7e
Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
382 | client = esp_mqtt_client_init(&mqtt_cfg); |
b1f38105ca7e
Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
383 | esp_mqtt_client_register_event(client, ESP_EVENT_ANY_ID, mqtt_event_handler, client); |
b1f38105ca7e
Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
384 | |
b1f38105ca7e
Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
385 | /* |
b1f38105ca7e
Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
386 | * Task loop forever. |
b1f38105ca7e
Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
387 | */ |
b1f38105ca7e
Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
388 | while (1) { |
b1f38105ca7e
Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
389 | |
b1f38105ca7e
Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
390 | uxBits = xEventGroupWaitBits(xEventGroupMQTT, TASK_MQTT_CONNECT | TASK_MQTT_DISCONNECT, pdFALSE, pdFALSE, portMAX_DELAY ); |
b1f38105ca7e
Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
391 | |
b1f38105ca7e
Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
392 | if (uxBits & TASK_MQTT_CONNECT) { |
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
|
393 | /* |
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
|
394 | * First build the connect uri. |
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
|
395 | */ |
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:
diff
changeset
|
396 | uri = xstrcpy((char *)"mqtt://"); |
b1f38105ca7e
Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
397 | if (strlen(CONFIG_MQTT_USER) && strlen(CONFIG_MQTT_PASS)) { |
b1f38105ca7e
Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
398 | uri = xstrcat(uri, CONFIG_MQTT_USER); |
b1f38105ca7e
Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
399 | uri = xstrcat(uri, (char *)":"); |
b1f38105ca7e
Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
400 | uri = xstrcat(uri, CONFIG_MQTT_PASS); |
b1f38105ca7e
Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
401 | uri = xstrcat(uri, (char *)"@"); |
b1f38105ca7e
Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
402 | } |
b1f38105ca7e
Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
403 | uri = xstrcat(uri, CONFIG_MQTT_SERVER); |
b1f38105ca7e
Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
404 | if (CONFIG_MQTT_PORT != 1883) { |
b1f38105ca7e
Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
405 | uri = xstrcat(uri, (char *)":"); |
b1f38105ca7e
Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
406 | sprintf(port, "%d", CONFIG_MQTT_PORT); |
b1f38105ca7e
Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
407 | uri = xstrcat(uri, port); |
b1f38105ca7e
Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
408 | } |
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
|
409 | ESP_LOGI(TAG, "Request MQTT connect %s", uri); |
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:
diff
changeset
|
410 | |
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
|
411 | /* |
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
|
412 | * Connect to the broker. |
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
|
413 | */ |
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:
diff
changeset
|
414 | err = esp_mqtt_client_set_uri(client, uri); |
b1f38105ca7e
Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
415 | if (err != ESP_OK) |
b1f38105ca7e
Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
416 | ESP_LOGE(TAG, "Set uri %s", esp_err_to_name(err)); |
b1f38105ca7e
Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
417 | err = esp_mqtt_client_start(client); |
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
|
418 | if (err != ESP_OK) { |
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:
diff
changeset
|
419 | ESP_LOGE(TAG, "Result %s", esp_err_to_name(err)); |
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
|
420 | } |
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:
diff
changeset
|
421 | xEventGroupClearBits(xEventGroupMQTT, TASK_MQTT_CONNECT); |
b1f38105ca7e
Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
422 | |
b1f38105ca7e
Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
423 | } else if (uxBits & TASK_MQTT_DISCONNECT) { |
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:
18
diff
changeset
|
424 | ESP_LOGI(TAG, "Request MQTT disconnect"); |
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
|
425 | /* |
2b337dd92f25
Added volts/current loop calculations. Added millis() timer running on the hardware clock. Completed most of the main state loop. Added MQTT wait for disconnect. MQTT disconnect in two passes, disconnect and stop.
Michiel Broek <mbroek@mbse.eu>
parents:
6
diff
changeset
|
426 | * Unsubscribe if connected |
2b337dd92f25
Added volts/current loop calculations. Added millis() timer running on the hardware clock. Completed most of the main state loop. Added MQTT wait for disconnect. MQTT disconnect in two passes, disconnect and stop.
Michiel Broek <mbroek@mbse.eu>
parents:
6
diff
changeset
|
427 | */ |
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
|
428 | if (ready_mqtt()) { |
bad3414f7bc4
Added the getTempBaro and getVoltsCurrent functions to process the measured data. Added some main task code. Added subscribe to MQTT events.
Michiel Broek <mbroek@mbse.eu>
parents:
5
diff
changeset
|
429 | char *topic = topic_base(); |
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
|
430 | topic = xstrcat(topic, (char *)"output/set/#"); |
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
|
431 | esp_mqtt_client_unsubscribe(client, topic); |
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
|
432 | free(topic); |
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
|
433 | } |
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
|
434 | |
2b337dd92f25
Added volts/current loop calculations. Added millis() timer running on the hardware clock. Completed most of the main state loop. Added MQTT wait for disconnect. MQTT disconnect in two passes, disconnect and stop.
Michiel Broek <mbroek@mbse.eu>
parents:
6
diff
changeset
|
435 | /* |
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 | * Disconnect from broker and wait until confirmed. |
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 | */ |
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 | err = esp_mqtt_client_disconnect(client); |
2b337dd92f25
Added volts/current loop calculations. Added millis() timer running on the hardware clock. Completed most of the main state loop. Added MQTT wait for disconnect. MQTT disconnect in two passes, disconnect and stop.
Michiel Broek <mbroek@mbse.eu>
parents:
6
diff
changeset
|
439 | if (err != ESP_OK) { |
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
|
440 | ESP_LOGE(TAG, "Result %s", esp_err_to_name(err)); |
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
|
441 | } |
2b337dd92f25
Added volts/current loop calculations. Added millis() timer running on the hardware clock. Completed most of the main state loop. Added MQTT wait for disconnect. MQTT disconnect in two passes, disconnect and stop.
Michiel Broek <mbroek@mbse.eu>
parents:
6
diff
changeset
|
442 | xEventGroupWaitBits(xEventGroupMQTT, TASK_MQTT_DISCONNECTED, pdTRUE, pdFALSE, 500 / portTICK_PERIOD_MS); |
2b337dd92f25
Added volts/current loop calculations. Added millis() timer running on the hardware clock. Completed most of the main state loop. Added MQTT wait for disconnect. MQTT disconnect in two passes, disconnect and stop.
Michiel Broek <mbroek@mbse.eu>
parents:
6
diff
changeset
|
443 | |
2b337dd92f25
Added volts/current loop calculations. Added millis() timer running on the hardware clock. Completed most of the main state loop. Added MQTT wait for disconnect. MQTT disconnect in two passes, disconnect and stop.
Michiel Broek <mbroek@mbse.eu>
parents:
6
diff
changeset
|
444 | /* |
2b337dd92f25
Added volts/current loop calculations. Added millis() timer running on the hardware clock. Completed most of the main state loop. Added MQTT wait for disconnect. MQTT disconnect in two passes, disconnect and stop.
Michiel Broek <mbroek@mbse.eu>
parents:
6
diff
changeset
|
445 | * Finally stop the client because new connections start |
2b337dd92f25
Added volts/current loop calculations. Added millis() timer running on the hardware clock. Completed most of the main state loop. Added MQTT wait for disconnect. MQTT disconnect in two passes, disconnect and stop.
Michiel Broek <mbroek@mbse.eu>
parents:
6
diff
changeset
|
446 | * with a 'esp_mqtt_client_start()' command. |
2b337dd92f25
Added volts/current loop calculations. Added millis() timer running on the hardware clock. Completed most of the main state loop. Added MQTT wait for disconnect. MQTT disconnect in two passes, disconnect and stop.
Michiel Broek <mbroek@mbse.eu>
parents:
6
diff
changeset
|
447 | * This will take about 5 seconds, but we don't need the network. |
2b337dd92f25
Added volts/current loop calculations. Added millis() timer running on the hardware clock. Completed most of the main state loop. Added MQTT wait for disconnect. MQTT disconnect in two passes, disconnect and stop.
Michiel Broek <mbroek@mbse.eu>
parents:
6
diff
changeset
|
448 | */ |
2b337dd92f25
Added volts/current loop calculations. Added millis() timer running on the hardware clock. Completed most of the main state loop. Added MQTT wait for disconnect. MQTT disconnect in two passes, disconnect and stop.
Michiel Broek <mbroek@mbse.eu>
parents:
6
diff
changeset
|
449 | err = esp_mqtt_client_stop(client); |
2b337dd92f25
Added volts/current loop calculations. Added millis() timer running on the hardware clock. Completed most of the main state loop. Added MQTT wait for disconnect. MQTT disconnect in two passes, disconnect and stop.
Michiel Broek <mbroek@mbse.eu>
parents:
6
diff
changeset
|
450 | if (err != ESP_OK) { |
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:
18
diff
changeset
|
451 | ESP_LOGE(TAG, "esp_mqtt_client_stop() result %s", esp_err_to_name(err)); |
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
|
452 | } |
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
|
453 | |
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:
12
diff
changeset
|
454 | xEventGroupClearBits(xEventGroupMQTT, TASK_MQTT_CONNECTED); |
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:
12
diff
changeset
|
455 | xEventGroupSetBits(xEventGroupMQTT, TASK_MQTT_DISCONNECTED); |
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:
diff
changeset
|
456 | xEventGroupClearBits(xEventGroupMQTT, TASK_MQTT_DISCONNECT); |
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:
12
diff
changeset
|
457 | ESP_LOGI(TAG, "Request MQTT disconnect done"); |
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:
diff
changeset
|
458 | } |
b1f38105ca7e
Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
459 | } |
b1f38105ca7e
Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
460 | } |
b1f38105ca7e
Added task MQTT and some utilities. Added more power measurement variables and code. INA219 measurements are saved in the State record.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
461 |