Sat, 12 Oct 2019 21:05:09 +0200
One-wire bus can now handle multiple sensors.
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 |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
13 | |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
14 | uint64_t Sequence = 0; ///< Sequence stored in NVS |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
15 | nvs_handle_t seq_handle; ///< NVS handle |
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 | esp_mqtt_client_handle_t client; ///< MQTT client handle |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
18 | 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
|
19 | 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
|
20 | 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
|
21 | |
8 | 22 | const char *sensState[] = { "OK", "ERROR" }; |
23 | const char *unitMode[] = { "OFF", "ON" }; | |
0
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
24 | |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
25 | extern DS18B20_State *ds18b20_state; ///< DS18B20 state |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
26 | extern SemaphoreHandle_t xSemaphoreDS18B20; ///< DS18B20 lock semaphore |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
27 | extern ADC_State *adc_state; ///< ADC state |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
28 | extern SemaphoreHandle_t xSemaphoreADC; ///< ADC lock semaphore |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
29 | extern WIFI_State *wifi_state; ///< WiFi state |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
30 | extern SemaphoreHandle_t xSemaphoreWiFi; ///< WiFi lock semaphore |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
31 | |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
32 | 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
|
33 | extern SemaphoreHandle_t xSemaphoreUnits; |
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
|
34 | |
0
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
35 | extern const esp_app_desc_t *app_desc; |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
36 | |
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 | |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
39 | void connect_mqtt(bool state) |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
40 | { |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
41 | if (state) |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
42 | xEventGroupSetBits(xEventGroupMQTT, TASK_MQTT_CONNECT); |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
43 | else |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
44 | xEventGroupSetBits(xEventGroupMQTT, TASK_MQTT_DISCONNECT); |
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 | |
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 | |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
49 | bool ready_mqtt(void) |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
50 | { |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
51 | if (xEventGroupGetBits(xEventGroupMQTT) & TASK_MQTT_CONNECTED) |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
52 | return true; |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
53 | return false; |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
54 | } |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
55 | |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
56 | |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
57 | |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
58 | char *payload_header(void) |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
59 | { |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
60 | char *tmp, buf[128]; |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
61 | esp_err_t err; |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
62 | |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
63 | tmp = xstrcpy((char *)"{\"seq\":"); |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
64 | sprintf(buf, "%lld", Sequence++); |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
65 | err = nvs_set_u64(seq_handle, "Sequence_cnt", Sequence); |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
66 | if (err != ESP_OK) |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
67 | ESP_LOGE(TAG, "Error %s write Sequence to NVS", esp_err_to_name(err)); |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
68 | tmp = xstrcat(tmp, buf); |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
69 | tmp = xstrcat(tmp, (char *)",\"metric\":"); |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
70 | return tmp; |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
71 | } |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
72 | |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
73 | |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
74 | |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
75 | char *topic_base(char *msgtype) |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
76 | { |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
77 | char *tmp; |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
78 | |
8 | 79 | tmp = xstrcpy((char *)"mbv1.0/co2meters/"); |
0
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
80 | tmp = xstrcat(tmp, msgtype); |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
81 | tmp = xstrcat(tmp, (char *)"/"); |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
82 | tmp = xstrcat(tmp, config.hostname); |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
83 | return tmp; |
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 | |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
87 | |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
88 | void publisher(char *topic, char *payload) |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
89 | { |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
90 | // publish the data |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
91 | if (payload) |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
92 | 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
|
93 | else |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
94 | 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
|
95 | } |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
96 | |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
97 | |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
98 | |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
99 | char *unit_data(int i) |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
100 | { |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
101 | char *payload = NULL; |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
102 | char buf[128]; |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
103 | |
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
|
104 | 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
|
105 | 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
|
106 | 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
|
107 | 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
|
108 | payload = xstrcat(payload, units[i].alias); |
8 | 109 | payload = xstrcat(payload, (char *)"\",\"mode\":\""); |
110 | payload = xstrcat(payload, (char *)unitMode[units[i].mode]); | |
111 | payload = xstrcat(payload, (char *)"\",\"alarm\":"); | |
112 | sprintf(buf, "%d", units[i].alarm); | |
113 | payload = xstrcat(payload, buf); | |
0
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
114 | |
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
|
115 | // temperature_state temperature_address temperature |
8 | 116 | 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
|
117 | 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
|
118 | 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
|
119 | 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
|
120 | 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
|
121 | 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
|
122 | payload = xstrcat(payload, buf); |
0
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
123 | |
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
|
124 | // 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
|
125 | 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
|
126 | 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
|
127 | 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
|
128 | 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
|
129 | 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
|
130 | 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
|
131 | 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
|
132 | 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
|
133 | 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
|
134 | 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
|
135 | 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
|
136 | 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
|
137 | 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
|
138 | payload = xstrcat(payload, buf); |
8 | 139 | 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
|
140 | xSemaphoreGive(xSemaphoreUnits); |
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
|
141 | } |
0
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
142 | return payload; |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
143 | } |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
144 | |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
145 | |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
146 | |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
147 | void publishUnits(void) |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
148 | { |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
149 | char *topic = NULL, *payload = NULL, *payloadu = NULL; |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
150 | int i; |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
151 | bool comma = false; |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
152 | |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
153 | payload = payload_header(); |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
154 | payload = xstrcat(payload, (char *)"{\"units\":["); |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
155 | for (i = 0; i < 3; i++) { |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
156 | if (comma) |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
157 | payload = xstrcat(payload, (char *)","); |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
158 | payloadu = unit_data(i); |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
159 | payload = xstrcat(payload, payloadu); |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
160 | comma = true; |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
161 | free(payloadu); |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
162 | payloadu = NULL; |
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 | payload = xstrcat(payload, (char *)"]}}"); |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
165 | topic = topic_base((char *)"DBIRTH"); |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
166 | publisher(topic, payload); |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
167 | free(topic); |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
168 | topic = NULL; |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
169 | free(payload); |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
170 | payload = NULL; |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
171 | } |
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 | |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
174 | |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
175 | void publishNode(void) |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
176 | { |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
177 | char *topic = NULL, *payload = NULL, buf[64]; |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
178 | esp_chip_info_t chip_info; |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
179 | |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
180 | esp_chip_info(&chip_info); |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
181 | payload = payload_header(); |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
182 | payload = xstrcat(payload, (char *)"{\"uuid\":\""); |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
183 | payload = xstrcat(payload, config.uuid); |
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 | payload = xstrcat(payload, (char *)"\"properties\":{\"hardwaremake\":\"Unknown\",\"hardwaremodel\":\""); |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
186 | sprintf(buf, "ESP32 %d CPU WiFi%s%s rev %d", chip_info.cores, |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
187 | (chip_info.features & CHIP_FEATURE_BT) ? "/BT" : "", |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
188 | (chip_info.features & CHIP_FEATURE_BLE) ? "/BLE" : "", |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
189 | chip_info.revision); |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
190 | payload = xstrcat(payload, buf); |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
191 | payload = xstrcat(payload, (char *)"\",\"os\":\"FreeRTOS\",\"os_version\":\"Unknown\",\"FW\":\""); |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
192 | payload = xstrcat(payload, (char *)app_desc->version); |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
193 | payload = xstrcat(payload, (char *)"\"}"); |
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 | if (xSemaphoreTake(xSemaphoreDS18B20, 10) == pdTRUE) { |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
196 | 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
|
197 | 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
|
198 | payload = xstrcat(payload, buf); |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
199 | payload = xstrcat(payload, (char *)"}"); |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
200 | xSemaphoreGive(xSemaphoreDS18B20); |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
201 | } |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
202 | |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
203 | if (xSemaphoreTake(xSemaphoreWiFi, 10) == pdTRUE) { |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
204 | payload = xstrcat(payload, (char *)",\"net\":{\"address\":\""); |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
205 | payload = xstrcat(payload, wifi_state->STA_ip); |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
206 | payload = xstrcat(payload, (char *)"\",\"ifname\":\"sta\",\"rssi\":"); |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
207 | sprintf(buf, "%d", wifi_state->STA_rssi); |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
208 | payload = xstrcat(payload, buf); |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
209 | payload = xstrcat(payload, (char *)"}"); |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
210 | xSemaphoreGive(xSemaphoreWiFi); |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
211 | } |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
212 | |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
213 | payload = xstrcat(payload, (char *)"}}"); |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
214 | // Only NBIRTH messages, no NDATA in this project. |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
215 | topic = topic_base((char *)"NBIRTH"); |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
216 | publisher(topic, payload); |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
217 | free(topic); |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
218 | topic = NULL; |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
219 | free(payload); |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
220 | payload = NULL; |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
221 | } |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
222 | |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
223 | |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
224 | |
9
a85995941d0d
Added publish logs. Another fix for DS18B20 rom address copy.
Michiel Broek <mbroek@mbse.eu>
parents:
8
diff
changeset
|
225 | void publishLogs(void) |
a85995941d0d
Added publish logs. Another fix for DS18B20 rom address copy.
Michiel Broek <mbroek@mbse.eu>
parents:
8
diff
changeset
|
226 | { |
a85995941d0d
Added publish logs. Another fix for DS18B20 rom address copy.
Michiel Broek <mbroek@mbse.eu>
parents:
8
diff
changeset
|
227 | 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
|
228 | |
a85995941d0d
Added publish logs. Another fix for DS18B20 rom address copy.
Michiel Broek <mbroek@mbse.eu>
parents:
8
diff
changeset
|
229 | 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
|
230 | 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
|
231 | 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
|
232 | payload = payload_header(); |
a85995941d0d
Added publish logs. Another fix for DS18B20 rom address copy.
Michiel Broek <mbroek@mbse.eu>
parents:
8
diff
changeset
|
233 | 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
|
234 | 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
|
235 | 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
|
236 | 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
|
237 | payload = xstrcat(payload, buf); |
a85995941d0d
Added publish logs. Another fix for DS18B20 rom address copy.
Michiel Broek <mbroek@mbse.eu>
parents:
8
diff
changeset
|
238 | 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
|
239 | 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
|
240 | payload = xstrcat(payload, buf); |
a85995941d0d
Added publish logs. Another fix for DS18B20 rom address copy.
Michiel Broek <mbroek@mbse.eu>
parents:
8
diff
changeset
|
241 | payload = xstrcat(payload, (char *)"}}"); |
a85995941d0d
Added publish logs. Another fix for DS18B20 rom address copy.
Michiel Broek <mbroek@mbse.eu>
parents:
8
diff
changeset
|
242 | 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
|
243 | topic = xstrcat(topic, (char *)"/"); |
a85995941d0d
Added publish logs. Another fix for DS18B20 rom address copy.
Michiel Broek <mbroek@mbse.eu>
parents:
8
diff
changeset
|
244 | 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
|
245 | publisher(topic, payload); |
a85995941d0d
Added publish logs. Another fix for DS18B20 rom address copy.
Michiel Broek <mbroek@mbse.eu>
parents:
8
diff
changeset
|
246 | free(topic); |
a85995941d0d
Added publish logs. Another fix for DS18B20 rom address copy.
Michiel Broek <mbroek@mbse.eu>
parents:
8
diff
changeset
|
247 | topic = NULL; |
a85995941d0d
Added publish logs. Another fix for DS18B20 rom address copy.
Michiel Broek <mbroek@mbse.eu>
parents:
8
diff
changeset
|
248 | free(payload); |
a85995941d0d
Added publish logs. Another fix for DS18B20 rom address copy.
Michiel Broek <mbroek@mbse.eu>
parents:
8
diff
changeset
|
249 | payload = NULL; |
a85995941d0d
Added publish logs. Another fix for DS18B20 rom address copy.
Michiel Broek <mbroek@mbse.eu>
parents:
8
diff
changeset
|
250 | xSemaphoreGive(xSemaphoreUnits); |
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 | } |
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 | } |
a85995941d0d
Added publish logs. Another fix for DS18B20 rom address copy.
Michiel Broek <mbroek@mbse.eu>
parents:
8
diff
changeset
|
255 | |
a85995941d0d
Added publish logs. Another fix for DS18B20 rom address copy.
Michiel Broek <mbroek@mbse.eu>
parents:
8
diff
changeset
|
256 | |
a85995941d0d
Added publish logs. Another fix for DS18B20 rom address copy.
Michiel Broek <mbroek@mbse.eu>
parents:
8
diff
changeset
|
257 | |
0
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
258 | 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
|
259 | { |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
260 | switch (event->event_id) { |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
261 | |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
262 | case MQTT_EVENT_CONNECTED: |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
263 | ESP_LOGI(TAG, "MQTT_EVENT_CONNECTED"); |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
264 | xEventGroupSetBits(xEventGroupMQTT, TASK_MQTT_CONNECTED); |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
265 | break; |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
266 | |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
267 | case MQTT_EVENT_DISCONNECTED: |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
268 | ESP_LOGI(TAG, "MQTT_EVENT_DISCONNECTED"); |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
269 | xEventGroupClearBits(xEventGroupMQTT, TASK_MQTT_CONNECTED); |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
270 | break; |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
271 | |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
272 | case MQTT_EVENT_SUBSCRIBED: |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
273 | ESP_LOGI(TAG, "MQTT_EVENT_SUBSCRIBED, msg_id=%d", event->msg_id); |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
274 | break; |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
275 | |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
276 | case MQTT_EVENT_UNSUBSCRIBED: |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
277 | 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
|
278 | break; |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
279 | |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
280 | case MQTT_EVENT_PUBLISHED: |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
281 | ESP_LOGI(TAG, "MQTT_EVENT_PUBLISHED, msg_id=%d", event->msg_id); |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
282 | break; |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
283 | |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
284 | case MQTT_EVENT_DATA: |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
285 | ESP_LOGI(TAG, "MQTT_EVENT_DATA"); |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
286 | 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
|
287 | 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
|
288 | break; |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
289 | |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
290 | case MQTT_EVENT_ERROR: |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
291 | ESP_LOGI(TAG, "MQTT_EVENT_ERROR"); |
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_BEFORE_CONNECT: |
3 | 295 | //ESP_LOGI(TAG, "MQTT_EVENT_BEFORE_CONNECT"); |
0
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
296 | // Configure connection can be here. |
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 | default: |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
300 | 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
|
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 | return ESP_OK; |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
304 | } |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
305 | |
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 | |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
308 | static void mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_t event_id, void *event_data) { |
3 | 309 | // ESP_LOGI(TAG, "Event dispatched from event loop base=%s, event_id=%d", base, event_id); |
0
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
310 | mqtt_event_handler_cb(event_data); |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
311 | } |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
312 | |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
313 | |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
314 | |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
315 | |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
316 | /* |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
317 | * Task to read temperature sensors on request. |
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 | void task_mqtt(void *pvParameter) |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
320 | { |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
321 | esp_err_t err; |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
322 | |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
323 | ESP_LOGI(TAG, "Starting MQTT task"); |
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 | /* |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
326 | * Initialize Sequence counter from NVS |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
327 | */ |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
328 | err = nvs_open("storage", NVS_READWRITE, &seq_handle); |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
329 | if (err != ESP_OK) { |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
330 | ESP_LOGI(TAG, "Error (%s) opening NVS handle", esp_err_to_name(err)); |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
331 | } else { |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
332 | err = nvs_get_u64(seq_handle, "Sequence_cnt", &Sequence); |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
333 | switch (err) { |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
334 | case ESP_OK: |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
335 | ESP_LOGI(TAG, "Sequence counter from NVS = %lld", Sequence); |
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 | case ESP_ERR_NVS_NOT_FOUND: |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
339 | ESP_LOGI(TAG, "Sequence counter not found"); |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
340 | break; |
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 | default: |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
343 | ESP_LOGI(TAG, "Error (%s) init Sequence", esp_err_to_name(err)); |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
344 | break; |
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 | /* 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
|
349 | xEventGroupMQTT = xEventGroupCreate(); |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
350 | EventBits_t uxBits; |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
351 | esp_mqtt_client_config_t mqtt_cfg = { |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
352 | .uri = "mqtt://seaport.mbse.ym", |
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_mqtt_client_handle_t */ client = esp_mqtt_client_init(&mqtt_cfg); |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
355 | 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
|
356 | |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
357 | /* |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
358 | * Task loop forever. |
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 | while (1) { |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
361 | |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
362 | 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
|
363 | |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
364 | if (uxBits & TASK_MQTT_CONNECT) { |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
365 | ESP_LOGI(TAG, "Request MQTT connect"); |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
366 | err = esp_mqtt_client_start(client); |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
367 | ESP_LOGI(TAG, "Result %s", esp_err_to_name(err)); |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
368 | xEventGroupClearBits(xEventGroupMQTT, TASK_MQTT_CONNECT); |
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 | } else if (uxBits & TASK_MQTT_DISCONNECT) { |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
371 | ESP_LOGI(TAG, "Request MQTT disconnect"); |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
372 | // publish disconnect messages |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
373 | err = esp_mqtt_client_stop(client); |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
374 | ESP_LOGI(TAG, "Result %s", esp_err_to_name(err)); |
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 | err = nvs_commit(seq_handle); |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
377 | if (err != ESP_OK) |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
378 | ESP_LOGE(TAG, "Error %s commit NVS", esp_err_to_name(err)); |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
379 | xEventGroupClearBits(xEventGroupMQTT, TASK_MQTT_DISCONNECT); |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
380 | xEventGroupClearBits(xEventGroupMQTT, TASK_MQTT_CONNECTED); |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
381 | } |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
382 | vTaskDelay( (TickType_t)10); |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
383 | } |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
384 | } |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
385 |