Sun, 14 Jun 2020 14:53:06 +0200
Code cleanup, doxygen comments added.
0
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
1 | /** |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
2 | * @file task_mqtt.c |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
3 | * @brief The FreeRTOS task to maintain MQTT connections. |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
4 | */ |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
5 | |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
6 | |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
7 | #include "config.h" |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
8 | |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
9 | |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
10 | static const char *TAG = "task_mqtt"; |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
11 | |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
12 | EventGroupHandle_t xEventGroupMQTT; ///< Events MQTT task |
11
e33f2d325d15
Experimental mqtt published messages state counter
Michiel Broek <mbroek@mbse.eu>
parents:
9
diff
changeset
|
13 | SemaphoreHandle_t xSemaphorePcounter; ///< Publish counter semaphore. |
e33f2d325d15
Experimental mqtt published messages state counter
Michiel Broek <mbroek@mbse.eu>
parents:
9
diff
changeset
|
14 | int count_pub = 0; ///< Outstanding published messages. |
e33f2d325d15
Experimental mqtt published messages state counter
Michiel Broek <mbroek@mbse.eu>
parents:
9
diff
changeset
|
15 | esp_mqtt_client_handle_t client; ///< MQTT client handle |
0
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
16 | |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
17 | const int TASK_MQTT_CONNECT = BIT0; ///< Request MQTT connection |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
18 | const int TASK_MQTT_DISCONNECT = BIT1; ///< Request MQTT disconnect |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
19 | const int TASK_MQTT_CONNECTED = BIT2; ///< MQTT is connected |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
20 | |
57
232f318a6b51
Code cleanup, doxygen comments added.
Michiel Broek <mbroek@mbse.eu>
parents:
54
diff
changeset
|
21 | const char *sensState[] = { "OK", "ERROR" }; ///< Sensor state strings |
232f318a6b51
Code cleanup, doxygen comments added.
Michiel Broek <mbroek@mbse.eu>
parents:
54
diff
changeset
|
22 | const char *unitMode[] = { "OFF", "ON" }; ///< Units state strings |
0
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
23 | |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
24 | extern DS18B20_State *ds18b20_state; ///< DS18B20 state |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
25 | extern SemaphoreHandle_t xSemaphoreDS18B20; ///< DS18B20 lock semaphore |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
26 | extern ADC_State *adc_state; ///< ADC state |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
27 | extern SemaphoreHandle_t xSemaphoreADC; ///< ADC lock semaphore |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
28 | extern WIFI_State *wifi_state; ///< WiFi state |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
29 | extern SemaphoreHandle_t xSemaphoreWiFi; ///< WiFi lock semaphore |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
30 | extern unit_t units[3]; |
4
2a57c466bf45
FreeRTOS scheduler from 100 Hz to 200 Hz. All units data is now protected with a semaphore.
Michiel Broek <mbroek@mbse.eu>
parents:
3
diff
changeset
|
31 | extern SemaphoreHandle_t xSemaphoreUnits; |
0
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
32 | extern const esp_app_desc_t *app_desc; |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
33 | |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
34 | |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
35 | |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
36 | void connect_mqtt(bool state) |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
37 | { |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
38 | if (state) |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
39 | xEventGroupSetBits(xEventGroupMQTT, TASK_MQTT_CONNECT); |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
40 | else |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
41 | xEventGroupSetBits(xEventGroupMQTT, TASK_MQTT_DISCONNECT); |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
42 | } |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
43 | |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
44 | |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
45 | |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
46 | bool ready_mqtt(void) |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
47 | { |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
48 | if (xEventGroupGetBits(xEventGroupMQTT) & TASK_MQTT_CONNECTED) |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
49 | return true; |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
50 | return false; |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
51 | } |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
52 | |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
53 | |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
54 | |
44
e52d11b8f252
Removed dead code. Added more doxygen documentation.
Michiel Broek <mbroek@mbse.eu>
parents:
37
diff
changeset
|
55 | /** |
e52d11b8f252
Removed dead code. Added more doxygen documentation.
Michiel Broek <mbroek@mbse.eu>
parents:
37
diff
changeset
|
56 | * @brief Generate the mqtt payload header. |
e52d11b8f252
Removed dead code. Added more doxygen documentation.
Michiel Broek <mbroek@mbse.eu>
parents:
37
diff
changeset
|
57 | * @return Allocated character string with the header. |
e52d11b8f252
Removed dead code. Added more doxygen documentation.
Michiel Broek <mbroek@mbse.eu>
parents:
37
diff
changeset
|
58 | */ |
0
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
59 | char *payload_header(void) |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
60 | { |
47
1ab1f4a8c328
Version 0.2.2 Changed to use a permanent network and WiFi connection. Removed three mainloop stages. Removed MQTT sequence counter that was not used. Update WiFi rssi status during eacht measure cycle. Changed FreeRTOS schedulng to 500 Hz.
Michiel Broek <mbroek@mbse.eu>
parents:
45
diff
changeset
|
61 | char *tmp; |
0
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
62 | |
47
1ab1f4a8c328
Version 0.2.2 Changed to use a permanent network and WiFi connection. Removed three mainloop stages. Removed MQTT sequence counter that was not used. Update WiFi rssi status during eacht measure cycle. Changed FreeRTOS schedulng to 500 Hz.
Michiel Broek <mbroek@mbse.eu>
parents:
45
diff
changeset
|
63 | tmp = xstrcpy((char *)"{\"metric\":"); |
0
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
64 | return tmp; |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
65 | } |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
66 | |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
67 | |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
68 | |
44
e52d11b8f252
Removed dead code. Added more doxygen documentation.
Michiel Broek <mbroek@mbse.eu>
parents:
37
diff
changeset
|
69 | /** |
e52d11b8f252
Removed dead code. Added more doxygen documentation.
Michiel Broek <mbroek@mbse.eu>
parents:
37
diff
changeset
|
70 | * @brief Generate the mqtt topic base part. |
e52d11b8f252
Removed dead code. Added more doxygen documentation.
Michiel Broek <mbroek@mbse.eu>
parents:
37
diff
changeset
|
71 | * @param msgtype The message type part of the topic. |
e52d11b8f252
Removed dead code. Added more doxygen documentation.
Michiel Broek <mbroek@mbse.eu>
parents:
37
diff
changeset
|
72 | * @return The topic string allocated in memory. |
e52d11b8f252
Removed dead code. Added more doxygen documentation.
Michiel Broek <mbroek@mbse.eu>
parents:
37
diff
changeset
|
73 | */ |
0
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
74 | char *topic_base(char *msgtype) |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
75 | { |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
76 | char *tmp; |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
77 | |
8 | 78 | tmp = xstrcpy((char *)"mbv1.0/co2meters/"); |
0
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
79 | tmp = xstrcat(tmp, msgtype); |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
80 | tmp = xstrcat(tmp, (char *)"/"); |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
81 | tmp = xstrcat(tmp, config.hostname); |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
82 | return tmp; |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
83 | } |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
84 | |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
85 | |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
86 | |
44
e52d11b8f252
Removed dead code. Added more doxygen documentation.
Michiel Broek <mbroek@mbse.eu>
parents:
37
diff
changeset
|
87 | /** |
e52d11b8f252
Removed dead code. Added more doxygen documentation.
Michiel Broek <mbroek@mbse.eu>
parents:
37
diff
changeset
|
88 | * @brief The mqtt generic publish function. |
e52d11b8f252
Removed dead code. Added more doxygen documentation.
Michiel Broek <mbroek@mbse.eu>
parents:
37
diff
changeset
|
89 | * @param topic The topic of the mqtt message. |
e52d11b8f252
Removed dead code. Added more doxygen documentation.
Michiel Broek <mbroek@mbse.eu>
parents:
37
diff
changeset
|
90 | * @param payload The payload of the mqtt message. |
e52d11b8f252
Removed dead code. Added more doxygen documentation.
Michiel Broek <mbroek@mbse.eu>
parents:
37
diff
changeset
|
91 | */ |
0
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
92 | void publisher(char *topic, char *payload) |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
93 | { |
12
7dc9003f86a8
ADC1 source code cleanup. Switched to 12 bit resolution again. Multisample set to 128. Improved adc-state structure locking. Improved ADC read error detection. DS18B20 extra error logging. MQTT better publish counter locking.
Michiel Broek <mbroek@mbse.eu>
parents:
11
diff
changeset
|
94 | /* |
7dc9003f86a8
ADC1 source code cleanup. Switched to 12 bit resolution again. Multisample set to 128. Improved adc-state structure locking. Improved ADC read error detection. DS18B20 extra error logging. MQTT better publish counter locking.
Michiel Broek <mbroek@mbse.eu>
parents:
11
diff
changeset
|
95 | * First count, then sent the data. |
7dc9003f86a8
ADC1 source code cleanup. Switched to 12 bit resolution again. Multisample set to 128. Improved adc-state structure locking. Improved ADC read error detection. DS18B20 extra error logging. MQTT better publish counter locking.
Michiel Broek <mbroek@mbse.eu>
parents:
11
diff
changeset
|
96 | */ |
7dc9003f86a8
ADC1 source code cleanup. Switched to 12 bit resolution again. Multisample set to 128. Improved adc-state structure locking. Improved ADC read error detection. DS18B20 extra error logging. MQTT better publish counter locking.
Michiel Broek <mbroek@mbse.eu>
parents:
11
diff
changeset
|
97 | if (xSemaphoreTake(xSemaphorePcounter, 10) == pdTRUE) { |
7dc9003f86a8
ADC1 source code cleanup. Switched to 12 bit resolution again. Multisample set to 128. Improved adc-state structure locking. Improved ADC read error detection. DS18B20 extra error logging. MQTT better publish counter locking.
Michiel Broek <mbroek@mbse.eu>
parents:
11
diff
changeset
|
98 | count_pub++; |
7dc9003f86a8
ADC1 source code cleanup. Switched to 12 bit resolution again. Multisample set to 128. Improved adc-state structure locking. Improved ADC read error detection. DS18B20 extra error logging. MQTT better publish counter locking.
Michiel Broek <mbroek@mbse.eu>
parents:
11
diff
changeset
|
99 | xSemaphoreGive(xSemaphorePcounter); |
7dc9003f86a8
ADC1 source code cleanup. Switched to 12 bit resolution again. Multisample set to 128. Improved adc-state structure locking. Improved ADC read error detection. DS18B20 extra error logging. MQTT better publish counter locking.
Michiel Broek <mbroek@mbse.eu>
parents:
11
diff
changeset
|
100 | } else { |
23
58a328e91881
Added error logging for failed locks
Michiel Broek <mbroek@mbse.eu>
parents:
16
diff
changeset
|
101 | ESP_LOGE(TAG, "publisher() counter lock"); |
12
7dc9003f86a8
ADC1 source code cleanup. Switched to 12 bit resolution again. Multisample set to 128. Improved adc-state structure locking. Improved ADC read error detection. DS18B20 extra error logging. MQTT better publish counter locking.
Michiel Broek <mbroek@mbse.eu>
parents:
11
diff
changeset
|
102 | } |
7dc9003f86a8
ADC1 source code cleanup. Switched to 12 bit resolution again. Multisample set to 128. Improved adc-state structure locking. Improved ADC read error detection. DS18B20 extra error logging. MQTT better publish counter locking.
Michiel Broek <mbroek@mbse.eu>
parents:
11
diff
changeset
|
103 | |
0
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
104 | if (payload) |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
105 | esp_mqtt_client_publish(client, topic, payload, strlen(payload), 1, 0); |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
106 | else |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
107 | esp_mqtt_client_publish(client, topic, NULL, 0, 1, 0); |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
108 | } |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
109 | |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
110 | |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
111 | |
44
e52d11b8f252
Removed dead code. Added more doxygen documentation.
Michiel Broek <mbroek@mbse.eu>
parents:
37
diff
changeset
|
112 | /** |
e52d11b8f252
Removed dead code. Added more doxygen documentation.
Michiel Broek <mbroek@mbse.eu>
parents:
37
diff
changeset
|
113 | * @brief Generate json data for the given unit. |
e52d11b8f252
Removed dead code. Added more doxygen documentation.
Michiel Broek <mbroek@mbse.eu>
parents:
37
diff
changeset
|
114 | * @param i The unit record number. |
e52d11b8f252
Removed dead code. Added more doxygen documentation.
Michiel Broek <mbroek@mbse.eu>
parents:
37
diff
changeset
|
115 | * @return The json string allocated in memory. |
e52d11b8f252
Removed dead code. Added more doxygen documentation.
Michiel Broek <mbroek@mbse.eu>
parents:
37
diff
changeset
|
116 | */ |
0
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
117 | char *unit_data(int i) |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
118 | { |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
119 | char *payload = NULL; |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
120 | char buf[128]; |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
121 | |
4
2a57c466bf45
FreeRTOS scheduler from 100 Hz to 200 Hz. All units data is now protected with a semaphore.
Michiel Broek <mbroek@mbse.eu>
parents:
3
diff
changeset
|
122 | if (xSemaphoreTake(xSemaphoreUnits, 25) == pdTRUE) { |
2a57c466bf45
FreeRTOS scheduler from 100 Hz to 200 Hz. All units data is now protected with a semaphore.
Michiel Broek <mbroek@mbse.eu>
parents:
3
diff
changeset
|
123 | payload = xstrcpy((char *)"{\"uuid\":\""); |
2a57c466bf45
FreeRTOS scheduler from 100 Hz to 200 Hz. All units data is now protected with a semaphore.
Michiel Broek <mbroek@mbse.eu>
parents:
3
diff
changeset
|
124 | payload = xstrcat(payload, units[i].uuid); |
2a57c466bf45
FreeRTOS scheduler from 100 Hz to 200 Hz. All units data is now protected with a semaphore.
Michiel Broek <mbroek@mbse.eu>
parents:
3
diff
changeset
|
125 | payload = xstrcat(payload, (char *)"\",\"alias\":\""); |
2a57c466bf45
FreeRTOS scheduler from 100 Hz to 200 Hz. All units data is now protected with a semaphore.
Michiel Broek <mbroek@mbse.eu>
parents:
3
diff
changeset
|
126 | payload = xstrcat(payload, units[i].alias); |
8 | 127 | payload = xstrcat(payload, (char *)"\",\"mode\":\""); |
128 | payload = xstrcat(payload, (char *)unitMode[units[i].mode]); | |
129 | payload = xstrcat(payload, (char *)"\",\"alarm\":"); | |
130 | sprintf(buf, "%d", units[i].alarm); | |
131 | payload = xstrcat(payload, buf); | |
0
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
132 | |
4
2a57c466bf45
FreeRTOS scheduler from 100 Hz to 200 Hz. All units data is now protected with a semaphore.
Michiel Broek <mbroek@mbse.eu>
parents:
3
diff
changeset
|
133 | // temperature_state temperature_address temperature |
8 | 134 | payload = xstrcat(payload, (char *)",\"temperature\":{\"state\":\""); |
4
2a57c466bf45
FreeRTOS scheduler from 100 Hz to 200 Hz. All units data is now protected with a semaphore.
Michiel Broek <mbroek@mbse.eu>
parents:
3
diff
changeset
|
135 | payload = xstrcat(payload, (char *)sensState[units[i].temperature_state]); |
2a57c466bf45
FreeRTOS scheduler from 100 Hz to 200 Hz. All units data is now protected with a semaphore.
Michiel Broek <mbroek@mbse.eu>
parents:
3
diff
changeset
|
136 | payload = xstrcat(payload, (char *)"\",\"address\":\""); |
2a57c466bf45
FreeRTOS scheduler from 100 Hz to 200 Hz. All units data is now protected with a semaphore.
Michiel Broek <mbroek@mbse.eu>
parents:
3
diff
changeset
|
137 | payload = xstrcat(payload, (char *)units[i].temperature_rom_code); |
2a57c466bf45
FreeRTOS scheduler from 100 Hz to 200 Hz. All units data is now protected with a semaphore.
Michiel Broek <mbroek@mbse.eu>
parents:
3
diff
changeset
|
138 | payload = xstrcat(payload, (char *)"\",\"temperature\":"); |
2a57c466bf45
FreeRTOS scheduler from 100 Hz to 200 Hz. All units data is now protected with a semaphore.
Michiel Broek <mbroek@mbse.eu>
parents:
3
diff
changeset
|
139 | sprintf(buf, "%.3f", units[i].temperature / 1000.0); |
2a57c466bf45
FreeRTOS scheduler from 100 Hz to 200 Hz. All units data is now protected with a semaphore.
Michiel Broek <mbroek@mbse.eu>
parents:
3
diff
changeset
|
140 | payload = xstrcat(payload, buf); |
0
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
141 | |
4
2a57c466bf45
FreeRTOS scheduler from 100 Hz to 200 Hz. All units data is now protected with a semaphore.
Michiel Broek <mbroek@mbse.eu>
parents:
3
diff
changeset
|
142 | // pressure_state pressure_channel pressure_voltage pressure_zero pressure |
2a57c466bf45
FreeRTOS scheduler from 100 Hz to 200 Hz. All units data is now protected with a semaphore.
Michiel Broek <mbroek@mbse.eu>
parents:
3
diff
changeset
|
143 | payload = xstrcat(payload, (char *)"},\"pressure\":{\"state\":\""); |
2a57c466bf45
FreeRTOS scheduler from 100 Hz to 200 Hz. All units data is now protected with a semaphore.
Michiel Broek <mbroek@mbse.eu>
parents:
3
diff
changeset
|
144 | payload = xstrcat(payload, (char *)sensState[units[i].pressure_state]); |
2a57c466bf45
FreeRTOS scheduler from 100 Hz to 200 Hz. All units data is now protected with a semaphore.
Michiel Broek <mbroek@mbse.eu>
parents:
3
diff
changeset
|
145 | payload = xstrcat(payload, (char *)"\",\"channel\":"); |
2a57c466bf45
FreeRTOS scheduler from 100 Hz to 200 Hz. All units data is now protected with a semaphore.
Michiel Broek <mbroek@mbse.eu>
parents:
3
diff
changeset
|
146 | sprintf(buf, "%d", units[i].pressure_channel); |
2a57c466bf45
FreeRTOS scheduler from 100 Hz to 200 Hz. All units data is now protected with a semaphore.
Michiel Broek <mbroek@mbse.eu>
parents:
3
diff
changeset
|
147 | payload = xstrcat(payload, buf); |
2a57c466bf45
FreeRTOS scheduler from 100 Hz to 200 Hz. All units data is now protected with a semaphore.
Michiel Broek <mbroek@mbse.eu>
parents:
3
diff
changeset
|
148 | payload = xstrcat(payload, (char *)",\"voltage\":"); |
2a57c466bf45
FreeRTOS scheduler from 100 Hz to 200 Hz. All units data is now protected with a semaphore.
Michiel Broek <mbroek@mbse.eu>
parents:
3
diff
changeset
|
149 | sprintf(buf, "%.3f", units[i].pressure_voltage / 1000.0); |
2a57c466bf45
FreeRTOS scheduler from 100 Hz to 200 Hz. All units data is now protected with a semaphore.
Michiel Broek <mbroek@mbse.eu>
parents:
3
diff
changeset
|
150 | payload = xstrcat(payload, buf); |
2a57c466bf45
FreeRTOS scheduler from 100 Hz to 200 Hz. All units data is now protected with a semaphore.
Michiel Broek <mbroek@mbse.eu>
parents:
3
diff
changeset
|
151 | payload = xstrcat(payload, (char *)",\"zero\":"); |
2a57c466bf45
FreeRTOS scheduler from 100 Hz to 200 Hz. All units data is now protected with a semaphore.
Michiel Broek <mbroek@mbse.eu>
parents:
3
diff
changeset
|
152 | sprintf(buf, "%.3f", units[i].pressure_zero / 1000.0); |
2a57c466bf45
FreeRTOS scheduler from 100 Hz to 200 Hz. All units data is now protected with a semaphore.
Michiel Broek <mbroek@mbse.eu>
parents:
3
diff
changeset
|
153 | payload = xstrcat(payload, buf); |
2a57c466bf45
FreeRTOS scheduler from 100 Hz to 200 Hz. All units data is now protected with a semaphore.
Michiel Broek <mbroek@mbse.eu>
parents:
3
diff
changeset
|
154 | payload = xstrcat(payload, (char *)",\"bar\":"); |
2a57c466bf45
FreeRTOS scheduler from 100 Hz to 200 Hz. All units data is now protected with a semaphore.
Michiel Broek <mbroek@mbse.eu>
parents:
3
diff
changeset
|
155 | sprintf(buf, "%.2f", units[i].pressure / 1000.0); |
2a57c466bf45
FreeRTOS scheduler from 100 Hz to 200 Hz. All units data is now protected with a semaphore.
Michiel Broek <mbroek@mbse.eu>
parents:
3
diff
changeset
|
156 | payload = xstrcat(payload, buf); |
8 | 157 | payload = xstrcat(payload, (char *)"}}"); |
4
2a57c466bf45
FreeRTOS scheduler from 100 Hz to 200 Hz. All units data is now protected with a semaphore.
Michiel Broek <mbroek@mbse.eu>
parents:
3
diff
changeset
|
158 | xSemaphoreGive(xSemaphoreUnits); |
23
58a328e91881
Added error logging for failed locks
Michiel Broek <mbroek@mbse.eu>
parents:
16
diff
changeset
|
159 | } else { |
58a328e91881
Added error logging for failed locks
Michiel Broek <mbroek@mbse.eu>
parents:
16
diff
changeset
|
160 | ESP_LOGE(TAG, "unit_data(%d) lock error", i); |
4
2a57c466bf45
FreeRTOS scheduler from 100 Hz to 200 Hz. All units data is now protected with a semaphore.
Michiel Broek <mbroek@mbse.eu>
parents:
3
diff
changeset
|
161 | } |
0
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
162 | return payload; |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
163 | } |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
164 | |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
165 | |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
166 | |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
167 | void publishUnits(void) |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
168 | { |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
169 | char *topic = NULL, *payload = NULL, *payloadu = NULL; |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
170 | int i; |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
171 | bool comma = false; |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
172 | |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
173 | payload = payload_header(); |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
174 | payload = xstrcat(payload, (char *)"{\"units\":["); |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
175 | for (i = 0; i < 3; i++) { |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
176 | if (comma) |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
177 | payload = xstrcat(payload, (char *)","); |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
178 | payloadu = unit_data(i); |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
179 | payload = xstrcat(payload, payloadu); |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
180 | comma = true; |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
181 | free(payloadu); |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
182 | payloadu = NULL; |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
183 | } |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
184 | payload = xstrcat(payload, (char *)"]}}"); |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
185 | topic = topic_base((char *)"DBIRTH"); |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
186 | publisher(topic, payload); |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
187 | free(topic); |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
188 | topic = NULL; |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
189 | free(payload); |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
190 | payload = NULL; |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
191 | } |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
192 | |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
193 | |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
194 | |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
195 | void publishNode(void) |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
196 | { |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
197 | char *topic = NULL, *payload = NULL, buf[64]; |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
198 | esp_chip_info_t chip_info; |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
199 | |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
200 | esp_chip_info(&chip_info); |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
201 | payload = payload_header(); |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
202 | payload = xstrcat(payload, (char *)"{\"uuid\":\""); |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
203 | payload = xstrcat(payload, config.uuid); |
54
3b1834482899
Version 0.2.5. Added interval to the NBIRTH json data. Added global define 60 seconds.
Michiel Broek <mbroek@mbse.eu>
parents:
47
diff
changeset
|
204 | payload = xstrcat(payload, (char *)"\",\"interval\":"); |
3b1834482899
Version 0.2.5. Added interval to the NBIRTH json data. Added global define 60 seconds.
Michiel Broek <mbroek@mbse.eu>
parents:
47
diff
changeset
|
205 | sprintf(buf, "%d", MAINLOOP_TIMER); |
3b1834482899
Version 0.2.5. Added interval to the NBIRTH json data. Added global define 60 seconds.
Michiel Broek <mbroek@mbse.eu>
parents:
47
diff
changeset
|
206 | payload = xstrcat(payload, buf); |
3b1834482899
Version 0.2.5. Added interval to the NBIRTH json data. Added global define 60 seconds.
Michiel Broek <mbroek@mbse.eu>
parents:
47
diff
changeset
|
207 | payload = xstrcat(payload, (char *)",\"properties\":{\"hardwaremake\":\"Unknown\",\"hardwaremodel\":\""); |
11
e33f2d325d15
Experimental mqtt published messages state counter
Michiel Broek <mbroek@mbse.eu>
parents:
9
diff
changeset
|
208 | sprintf(buf, "ESP32 %d cores rev %d, WiFi bgn", chip_info.cores, chip_info.revision); |
0
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
209 | payload = xstrcat(payload, buf); |
45
61a106fd9d9e
Changed os and os_version information to esp-idf.
Michiel Broek <mbroek@mbse.eu>
parents:
44
diff
changeset
|
210 | payload = xstrcat(payload, (char *)"\",\"os\":\"esp-idf\",\"os_version\":\""); |
61a106fd9d9e
Changed os and os_version information to esp-idf.
Michiel Broek <mbroek@mbse.eu>
parents:
44
diff
changeset
|
211 | payload = xstrcat(payload, (char *)esp_get_idf_version()); |
61a106fd9d9e
Changed os and os_version information to esp-idf.
Michiel Broek <mbroek@mbse.eu>
parents:
44
diff
changeset
|
212 | payload = xstrcat(payload, (char *)"\",\"FW\":\""); |
0
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
213 | payload = xstrcat(payload, (char *)app_desc->version); |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
214 | payload = xstrcat(payload, (char *)"\"}"); |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
215 | |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
216 | if (xSemaphoreTake(xSemaphoreDS18B20, 10) == pdTRUE) { |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
217 | payload = xstrcat(payload, (char *)",\"THB\":{\"temperature\":"); |
2
c0184362d48c
Prepare ds18b20 sensors for multiple sensors on the onewire bus.
Michiel Broek <mbroek@mbse.eu>
parents:
1
diff
changeset
|
218 | sprintf(buf, "%.3f", ds18b20_state->sensor[0].temperature); |
0
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
219 | payload = xstrcat(payload, buf); |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
220 | payload = xstrcat(payload, (char *)"}"); |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
221 | xSemaphoreGive(xSemaphoreDS18B20); |
23
58a328e91881
Added error logging for failed locks
Michiel Broek <mbroek@mbse.eu>
parents:
16
diff
changeset
|
222 | } else { |
58a328e91881
Added error logging for failed locks
Michiel Broek <mbroek@mbse.eu>
parents:
16
diff
changeset
|
223 | ESP_LOGE(TAG, "publishNode() lock DS18B20 error"); |
0
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
224 | } |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
225 | |
24
64078aa15512
Finished the editor using the rotary switch. Added editing of the MQTT server parameters.
Michiel Broek <mbroek@mbse.eu>
parents:
23
diff
changeset
|
226 | if (xSemaphoreTake(xSemaphoreWiFi, 25) == pdTRUE) { |
0
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
227 | payload = xstrcat(payload, (char *)",\"net\":{\"address\":\""); |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
228 | payload = xstrcat(payload, wifi_state->STA_ip); |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
229 | payload = xstrcat(payload, (char *)"\",\"ifname\":\"sta\",\"rssi\":"); |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
230 | sprintf(buf, "%d", wifi_state->STA_rssi); |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
231 | payload = xstrcat(payload, buf); |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
232 | payload = xstrcat(payload, (char *)"}"); |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
233 | xSemaphoreGive(xSemaphoreWiFi); |
23
58a328e91881
Added error logging for failed locks
Michiel Broek <mbroek@mbse.eu>
parents:
16
diff
changeset
|
234 | } else { |
58a328e91881
Added error logging for failed locks
Michiel Broek <mbroek@mbse.eu>
parents:
16
diff
changeset
|
235 | ESP_LOGE(TAG, "publishNode() lock WiFi error"); |
0
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
236 | } |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
237 | |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
238 | payload = xstrcat(payload, (char *)"}}"); |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
239 | // Only NBIRTH messages, no NDATA in this project. |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
240 | topic = topic_base((char *)"NBIRTH"); |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
241 | publisher(topic, payload); |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
242 | free(topic); |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
243 | topic = NULL; |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
244 | free(payload); |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
245 | payload = NULL; |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
246 | } |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
247 | |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
248 | |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
249 | |
9
a85995941d0d
Added publish logs. Another fix for DS18B20 rom address copy.
Michiel Broek <mbroek@mbse.eu>
parents:
8
diff
changeset
|
250 | void publishLogs(void) |
a85995941d0d
Added publish logs. Another fix for DS18B20 rom address copy.
Michiel Broek <mbroek@mbse.eu>
parents:
8
diff
changeset
|
251 | { |
a85995941d0d
Added publish logs. Another fix for DS18B20 rom address copy.
Michiel Broek <mbroek@mbse.eu>
parents:
8
diff
changeset
|
252 | char *topic = NULL, *payload = NULL, buf[64]; |
a85995941d0d
Added publish logs. Another fix for DS18B20 rom address copy.
Michiel Broek <mbroek@mbse.eu>
parents:
8
diff
changeset
|
253 | |
a85995941d0d
Added publish logs. Another fix for DS18B20 rom address copy.
Michiel Broek <mbroek@mbse.eu>
parents:
8
diff
changeset
|
254 | for (int i = 0; i < 3; i++) { |
a85995941d0d
Added publish logs. Another fix for DS18B20 rom address copy.
Michiel Broek <mbroek@mbse.eu>
parents:
8
diff
changeset
|
255 | if (units[i].mode && ! units[i].alarm) { |
a85995941d0d
Added publish logs. Another fix for DS18B20 rom address copy.
Michiel Broek <mbroek@mbse.eu>
parents:
8
diff
changeset
|
256 | if (xSemaphoreTake(xSemaphoreUnits, 25) == pdTRUE) { |
a85995941d0d
Added publish logs. Another fix for DS18B20 rom address copy.
Michiel Broek <mbroek@mbse.eu>
parents:
8
diff
changeset
|
257 | payload = payload_header(); |
a85995941d0d
Added publish logs. Another fix for DS18B20 rom address copy.
Michiel Broek <mbroek@mbse.eu>
parents:
8
diff
changeset
|
258 | payload = xstrcat(payload, (char *)"{\"uuid\":\""); |
a85995941d0d
Added publish logs. Another fix for DS18B20 rom address copy.
Michiel Broek <mbroek@mbse.eu>
parents:
8
diff
changeset
|
259 | payload = xstrcat(payload, units[i].uuid); |
a85995941d0d
Added publish logs. Another fix for DS18B20 rom address copy.
Michiel Broek <mbroek@mbse.eu>
parents:
8
diff
changeset
|
260 | payload = xstrcat(payload, (char *)"\",\"temperature\":"); |
a85995941d0d
Added publish logs. Another fix for DS18B20 rom address copy.
Michiel Broek <mbroek@mbse.eu>
parents:
8
diff
changeset
|
261 | sprintf(buf, "%.3f", units[i].temperature / 1000.0); |
a85995941d0d
Added publish logs. Another fix for DS18B20 rom address copy.
Michiel Broek <mbroek@mbse.eu>
parents:
8
diff
changeset
|
262 | payload = xstrcat(payload, buf); |
a85995941d0d
Added publish logs. Another fix for DS18B20 rom address copy.
Michiel Broek <mbroek@mbse.eu>
parents:
8
diff
changeset
|
263 | payload = xstrcat(payload, (char *)",\"pressure\":"); |
a85995941d0d
Added publish logs. Another fix for DS18B20 rom address copy.
Michiel Broek <mbroek@mbse.eu>
parents:
8
diff
changeset
|
264 | sprintf(buf, "%.3f", units[i].pressure / 1000.0); |
a85995941d0d
Added publish logs. Another fix for DS18B20 rom address copy.
Michiel Broek <mbroek@mbse.eu>
parents:
8
diff
changeset
|
265 | payload = xstrcat(payload, buf); |
a85995941d0d
Added publish logs. Another fix for DS18B20 rom address copy.
Michiel Broek <mbroek@mbse.eu>
parents:
8
diff
changeset
|
266 | payload = xstrcat(payload, (char *)"}}"); |
a85995941d0d
Added publish logs. Another fix for DS18B20 rom address copy.
Michiel Broek <mbroek@mbse.eu>
parents:
8
diff
changeset
|
267 | topic = topic_base((char *)"DLOG"); |
a85995941d0d
Added publish logs. Another fix for DS18B20 rom address copy.
Michiel Broek <mbroek@mbse.eu>
parents:
8
diff
changeset
|
268 | topic = xstrcat(topic, (char *)"/"); |
a85995941d0d
Added publish logs. Another fix for DS18B20 rom address copy.
Michiel Broek <mbroek@mbse.eu>
parents:
8
diff
changeset
|
269 | topic = xstrcat(topic, units[i].alias); |
a85995941d0d
Added publish logs. Another fix for DS18B20 rom address copy.
Michiel Broek <mbroek@mbse.eu>
parents:
8
diff
changeset
|
270 | publisher(topic, payload); |
a85995941d0d
Added publish logs. Another fix for DS18B20 rom address copy.
Michiel Broek <mbroek@mbse.eu>
parents:
8
diff
changeset
|
271 | free(topic); |
a85995941d0d
Added publish logs. Another fix for DS18B20 rom address copy.
Michiel Broek <mbroek@mbse.eu>
parents:
8
diff
changeset
|
272 | topic = NULL; |
a85995941d0d
Added publish logs. Another fix for DS18B20 rom address copy.
Michiel Broek <mbroek@mbse.eu>
parents:
8
diff
changeset
|
273 | free(payload); |
a85995941d0d
Added publish logs. Another fix for DS18B20 rom address copy.
Michiel Broek <mbroek@mbse.eu>
parents:
8
diff
changeset
|
274 | payload = NULL; |
a85995941d0d
Added publish logs. Another fix for DS18B20 rom address copy.
Michiel Broek <mbroek@mbse.eu>
parents:
8
diff
changeset
|
275 | xSemaphoreGive(xSemaphoreUnits); |
23
58a328e91881
Added error logging for failed locks
Michiel Broek <mbroek@mbse.eu>
parents:
16
diff
changeset
|
276 | } else { |
58a328e91881
Added error logging for failed locks
Michiel Broek <mbroek@mbse.eu>
parents:
16
diff
changeset
|
277 | ESP_LOGE(TAG, "publishLogs() lock error unit %d", i); |
9
a85995941d0d
Added publish logs. Another fix for DS18B20 rom address copy.
Michiel Broek <mbroek@mbse.eu>
parents:
8
diff
changeset
|
278 | } |
a85995941d0d
Added publish logs. Another fix for DS18B20 rom address copy.
Michiel Broek <mbroek@mbse.eu>
parents:
8
diff
changeset
|
279 | } |
a85995941d0d
Added publish logs. Another fix for DS18B20 rom address copy.
Michiel Broek <mbroek@mbse.eu>
parents:
8
diff
changeset
|
280 | } |
a85995941d0d
Added publish logs. Another fix for DS18B20 rom address copy.
Michiel Broek <mbroek@mbse.eu>
parents:
8
diff
changeset
|
281 | } |
a85995941d0d
Added publish logs. Another fix for DS18B20 rom address copy.
Michiel Broek <mbroek@mbse.eu>
parents:
8
diff
changeset
|
282 | |
a85995941d0d
Added publish logs. Another fix for DS18B20 rom address copy.
Michiel Broek <mbroek@mbse.eu>
parents:
8
diff
changeset
|
283 | |
a85995941d0d
Added publish logs. Another fix for DS18B20 rom address copy.
Michiel Broek <mbroek@mbse.eu>
parents:
8
diff
changeset
|
284 | |
0
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
285 | static esp_err_t mqtt_event_handler_cb(esp_mqtt_event_handle_t event) |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
286 | { |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
287 | switch (event->event_id) { |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
288 | |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
289 | case MQTT_EVENT_CONNECTED: |
37
358bbd5b608e
menuconfig settings now work for I2C display setup. Lot's of code cleanup.
Michiel Broek <mbroek@mbse.eu>
parents:
28
diff
changeset
|
290 | ESP_LOGD(TAG, "MQTT_EVENT_CONNECTED"); |
0
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
291 | xEventGroupSetBits(xEventGroupMQTT, TASK_MQTT_CONNECTED); |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
292 | break; |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
293 | |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
294 | case MQTT_EVENT_DISCONNECTED: |
37
358bbd5b608e
menuconfig settings now work for I2C display setup. Lot's of code cleanup.
Michiel Broek <mbroek@mbse.eu>
parents:
28
diff
changeset
|
295 | ESP_LOGD(TAG, "MQTT_EVENT_DISCONNECTED"); |
0
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
296 | xEventGroupClearBits(xEventGroupMQTT, TASK_MQTT_CONNECTED); |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
297 | break; |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
298 | |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
299 | case MQTT_EVENT_SUBSCRIBED: |
37
358bbd5b608e
menuconfig settings now work for I2C display setup. Lot's of code cleanup.
Michiel Broek <mbroek@mbse.eu>
parents:
28
diff
changeset
|
300 | ESP_LOGD(TAG, "MQTT_EVENT_SUBSCRIBED, msg_id=%d", event->msg_id); |
0
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
301 | break; |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
302 | |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
303 | case MQTT_EVENT_UNSUBSCRIBED: |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
304 | ESP_LOGI(TAG, "MQTT_EVENT_UNSUBSCRIBED, msg_id=%d", event->msg_id); |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
305 | break; |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
306 | |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
307 | case MQTT_EVENT_PUBLISHED: |
37
358bbd5b608e
menuconfig settings now work for I2C display setup. Lot's of code cleanup.
Michiel Broek <mbroek@mbse.eu>
parents:
28
diff
changeset
|
308 | ESP_LOGD(TAG, "MQTT_EVENT_PUBLISHED, msg_id=%d", event->msg_id); |
11
e33f2d325d15
Experimental mqtt published messages state counter
Michiel Broek <mbroek@mbse.eu>
parents:
9
diff
changeset
|
309 | if (xSemaphoreTake(xSemaphorePcounter, 10) == pdTRUE) { |
e33f2d325d15
Experimental mqtt published messages state counter
Michiel Broek <mbroek@mbse.eu>
parents:
9
diff
changeset
|
310 | if (count_pub) { |
e33f2d325d15
Experimental mqtt published messages state counter
Michiel Broek <mbroek@mbse.eu>
parents:
9
diff
changeset
|
311 | count_pub--; |
e33f2d325d15
Experimental mqtt published messages state counter
Michiel Broek <mbroek@mbse.eu>
parents:
9
diff
changeset
|
312 | } |
e33f2d325d15
Experimental mqtt published messages state counter
Michiel Broek <mbroek@mbse.eu>
parents:
9
diff
changeset
|
313 | xSemaphoreGive(xSemaphorePcounter); |
e33f2d325d15
Experimental mqtt published messages state counter
Michiel Broek <mbroek@mbse.eu>
parents:
9
diff
changeset
|
314 | } else { |
23
58a328e91881
Added error logging for failed locks
Michiel Broek <mbroek@mbse.eu>
parents:
16
diff
changeset
|
315 | ESP_LOGE(TAG, "mqtt_event_handler_cb(() lock error event"); |
11
e33f2d325d15
Experimental mqtt published messages state counter
Michiel Broek <mbroek@mbse.eu>
parents:
9
diff
changeset
|
316 | } |
0
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
317 | break; |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
318 | |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
319 | case MQTT_EVENT_DATA: |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
320 | ESP_LOGI(TAG, "MQTT_EVENT_DATA"); |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
321 | printf("TOPIC=%.*s\r\n", event->topic_len, event->topic); |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
322 | printf("DATA=%.*s\r\n", event->data_len, event->data); |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
323 | break; |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
324 | |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
325 | case MQTT_EVENT_ERROR: |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
326 | ESP_LOGI(TAG, "MQTT_EVENT_ERROR"); |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
327 | break; |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
328 | |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
329 | case MQTT_EVENT_BEFORE_CONNECT: |
3 | 330 | //ESP_LOGI(TAG, "MQTT_EVENT_BEFORE_CONNECT"); |
0
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
331 | // Configure connection can be here. |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
332 | break; |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
333 | |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
334 | default: |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
335 | ESP_LOGI(TAG, "Other event id:%d", event->event_id); |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
336 | break; |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
337 | } |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
338 | return ESP_OK; |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
339 | } |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
340 | |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
341 | |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
342 | |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
343 | static void mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_t event_id, void *event_data) { |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
344 | mqtt_event_handler_cb(event_data); |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
345 | } |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
346 | |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
347 | |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
348 | |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
349 | /* |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
350 | * Task to read temperature sensors on request. |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
351 | */ |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
352 | void task_mqtt(void *pvParameter) |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
353 | { |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
354 | esp_err_t err; |
28
6d825e2962e4
Code cleanup. Set MQTT uri from the configuration.
Michiel Broek <mbroek@mbse.eu>
parents:
24
diff
changeset
|
355 | char *uri = NULL, port[11]; |
0
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
356 | |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
357 | ESP_LOGI(TAG, "Starting MQTT task"); |
11
e33f2d325d15
Experimental mqtt published messages state counter
Michiel Broek <mbroek@mbse.eu>
parents:
9
diff
changeset
|
358 | xSemaphorePcounter = xSemaphoreCreateMutex(); |
0
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
359 | |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
360 | /* event handler and event group for the wifi driver */ |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
361 | xEventGroupMQTT = xEventGroupCreate(); |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
362 | EventBits_t uxBits; |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
363 | esp_mqtt_client_config_t mqtt_cfg = { |
28
6d825e2962e4
Code cleanup. Set MQTT uri from the configuration.
Michiel Broek <mbroek@mbse.eu>
parents:
24
diff
changeset
|
364 | .uri = "mqtt://localhost", |
0
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
365 | }; |
11
e33f2d325d15
Experimental mqtt published messages state counter
Michiel Broek <mbroek@mbse.eu>
parents:
9
diff
changeset
|
366 | client = esp_mqtt_client_init(&mqtt_cfg); |
0
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
367 | esp_mqtt_client_register_event(client, ESP_EVENT_ANY_ID, mqtt_event_handler, client); |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
368 | |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
369 | /* |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
370 | * Task loop forever. |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
371 | */ |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
372 | while (1) { |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
373 | |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
374 | uxBits = xEventGroupWaitBits(xEventGroupMQTT, TASK_MQTT_CONNECT | TASK_MQTT_DISCONNECT, pdFALSE, pdFALSE, portMAX_DELAY ); |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
375 | |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
376 | if (uxBits & TASK_MQTT_CONNECT) { |
28
6d825e2962e4
Code cleanup. Set MQTT uri from the configuration.
Michiel Broek <mbroek@mbse.eu>
parents:
24
diff
changeset
|
377 | if (strlen(config.mqtt_server)) { |
6d825e2962e4
Code cleanup. Set MQTT uri from the configuration.
Michiel Broek <mbroek@mbse.eu>
parents:
24
diff
changeset
|
378 | uri = xstrcpy((char *)"mqtt://"); |
6d825e2962e4
Code cleanup. Set MQTT uri from the configuration.
Michiel Broek <mbroek@mbse.eu>
parents:
24
diff
changeset
|
379 | if (strlen(config.mqtt_user) && strlen(config.mqtt_pwd)) { |
6d825e2962e4
Code cleanup. Set MQTT uri from the configuration.
Michiel Broek <mbroek@mbse.eu>
parents:
24
diff
changeset
|
380 | uri = xstrcat(uri, config.mqtt_user); |
6d825e2962e4
Code cleanup. Set MQTT uri from the configuration.
Michiel Broek <mbroek@mbse.eu>
parents:
24
diff
changeset
|
381 | uri = xstrcat(uri, (char *)":"); |
6d825e2962e4
Code cleanup. Set MQTT uri from the configuration.
Michiel Broek <mbroek@mbse.eu>
parents:
24
diff
changeset
|
382 | uri = xstrcat(uri, config.mqtt_pwd); |
6d825e2962e4
Code cleanup. Set MQTT uri from the configuration.
Michiel Broek <mbroek@mbse.eu>
parents:
24
diff
changeset
|
383 | uri = xstrcat(uri, (char *)"@"); |
6d825e2962e4
Code cleanup. Set MQTT uri from the configuration.
Michiel Broek <mbroek@mbse.eu>
parents:
24
diff
changeset
|
384 | } |
6d825e2962e4
Code cleanup. Set MQTT uri from the configuration.
Michiel Broek <mbroek@mbse.eu>
parents:
24
diff
changeset
|
385 | uri = xstrcat(uri, config.mqtt_server); |
6d825e2962e4
Code cleanup. Set MQTT uri from the configuration.
Michiel Broek <mbroek@mbse.eu>
parents:
24
diff
changeset
|
386 | if (config.mqtt_port != 1883) { |
6d825e2962e4
Code cleanup. Set MQTT uri from the configuration.
Michiel Broek <mbroek@mbse.eu>
parents:
24
diff
changeset
|
387 | uri = xstrcat(uri, (char *)":"); |
6d825e2962e4
Code cleanup. Set MQTT uri from the configuration.
Michiel Broek <mbroek@mbse.eu>
parents:
24
diff
changeset
|
388 | sprintf(port, "%d", config.mqtt_port); |
6d825e2962e4
Code cleanup. Set MQTT uri from the configuration.
Michiel Broek <mbroek@mbse.eu>
parents:
24
diff
changeset
|
389 | uri = xstrcat(uri, port); |
6d825e2962e4
Code cleanup. Set MQTT uri from the configuration.
Michiel Broek <mbroek@mbse.eu>
parents:
24
diff
changeset
|
390 | } |
6d825e2962e4
Code cleanup. Set MQTT uri from the configuration.
Michiel Broek <mbroek@mbse.eu>
parents:
24
diff
changeset
|
391 | } else { |
6d825e2962e4
Code cleanup. Set MQTT uri from the configuration.
Michiel Broek <mbroek@mbse.eu>
parents:
24
diff
changeset
|
392 | uri = xstrcpy((char *)"mqtt://iot.eclipse.org:1883"); |
6d825e2962e4
Code cleanup. Set MQTT uri from the configuration.
Michiel Broek <mbroek@mbse.eu>
parents:
24
diff
changeset
|
393 | } |
6d825e2962e4
Code cleanup. Set MQTT uri from the configuration.
Michiel Broek <mbroek@mbse.eu>
parents:
24
diff
changeset
|
394 | ESP_LOGI(TAG, "Request MQTT connect %s", uri); |
6d825e2962e4
Code cleanup. Set MQTT uri from the configuration.
Michiel Broek <mbroek@mbse.eu>
parents:
24
diff
changeset
|
395 | err = esp_mqtt_client_set_uri(client, uri); |
6d825e2962e4
Code cleanup. Set MQTT uri from the configuration.
Michiel Broek <mbroek@mbse.eu>
parents:
24
diff
changeset
|
396 | if (err != ESP_OK) |
6d825e2962e4
Code cleanup. Set MQTT uri from the configuration.
Michiel Broek <mbroek@mbse.eu>
parents:
24
diff
changeset
|
397 | ESP_LOGE(TAG, "Set uri %s", esp_err_to_name(err)); |
0
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
398 | err = esp_mqtt_client_start(client); |
11
e33f2d325d15
Experimental mqtt published messages state counter
Michiel Broek <mbroek@mbse.eu>
parents:
9
diff
changeset
|
399 | if (err != ESP_OK) |
23
58a328e91881
Added error logging for failed locks
Michiel Broek <mbroek@mbse.eu>
parents:
16
diff
changeset
|
400 | ESP_LOGE(TAG, "Result %s", esp_err_to_name(err)); |
28
6d825e2962e4
Code cleanup. Set MQTT uri from the configuration.
Michiel Broek <mbroek@mbse.eu>
parents:
24
diff
changeset
|
401 | if (uri) |
6d825e2962e4
Code cleanup. Set MQTT uri from the configuration.
Michiel Broek <mbroek@mbse.eu>
parents:
24
diff
changeset
|
402 | free(uri); |
6d825e2962e4
Code cleanup. Set MQTT uri from the configuration.
Michiel Broek <mbroek@mbse.eu>
parents:
24
diff
changeset
|
403 | uri = NULL; |
0
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
404 | xEventGroupClearBits(xEventGroupMQTT, TASK_MQTT_CONNECT); |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
405 | |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
406 | } else if (uxBits & TASK_MQTT_DISCONNECT) { |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
407 | ESP_LOGI(TAG, "Request MQTT disconnect"); |
11
e33f2d325d15
Experimental mqtt published messages state counter
Michiel Broek <mbroek@mbse.eu>
parents:
9
diff
changeset
|
408 | esp_mqtt_client_stop(client); |
0
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
409 | xEventGroupClearBits(xEventGroupMQTT, TASK_MQTT_DISCONNECT); |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
410 | xEventGroupClearBits(xEventGroupMQTT, TASK_MQTT_CONNECTED); |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
411 | } |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
412 | } |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
413 | } |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
414 |