|
1 /** |
|
2 * @file config.h |
|
3 * @brief The 'co2meter' configuration data. These are stored in the |
|
4 * spiffs filesystem in a flash partition. |
|
5 */ |
|
6 |
|
7 #ifndef _CONFIG_H |
|
8 #define _CONFIG_H |
|
9 |
|
10 // Global includes for the project |
|
11 |
|
12 #include <stdio.h> |
|
13 #include <stdlib.h> |
|
14 #include <string.h> |
|
15 #include <stdbool.h> |
|
16 #include <time.h> |
|
17 #include <errno.h> |
|
18 #include <sys/unistd.h> |
|
19 #include <sys/fcntl.h> |
|
20 #include <sys/time.h> |
|
21 |
|
22 #include "freertos/FreeRTOS.h" |
|
23 #include "freertos/task.h" |
|
24 #include "freertos/semphr.h" |
|
25 #include "freertos/event_groups.h" |
|
26 #include "freertos/queue.h" |
|
27 #include "driver/gpio.h" |
|
28 #include "driver/adc.h" |
|
29 #include "driver/rtc_io.h" |
|
30 #include "soc/sens_periph.h" |
|
31 #include "soc/rtc.h" |
|
32 #include "esp_adc_cal.h" |
|
33 #include "esp_sleep.h" |
|
34 #include "esp_log.h" |
|
35 #include "esp_spiffs.h" |
|
36 #include "esp_vfs.h" |
|
37 #include "esp_system.h" |
|
38 #include "esp_wifi.h" |
|
39 //#include "esp_wifi_types.h" |
|
40 #include "esp_wpa2.h" |
|
41 #include "esp_event.h" |
|
42 #include "esp_spi_flash.h" |
|
43 #include "esp_ota_ops.h" |
|
44 #include "esp_http_client.h" |
|
45 #include "nvs.h" |
|
46 #include "nvs_flash.h" |
|
47 #include "lwip/apps/sntp.h" |
|
48 #include "lwip/sockets.h" |
|
49 #include "lwip/dns.h" |
|
50 #include "lwip/netdb.h" |
|
51 #include "tcpip_adapter.h" |
|
52 #include "mqtt_client.h" |
|
53 |
|
54 #include "rotary_encoder.h" |
|
55 #include "u8g2_esp32_hal.h" |
|
56 |
|
57 #include "task_ds18b20.h" |
|
58 #include "task_adc.h" |
|
59 #include "task_wifi.h" |
|
60 #include "task_mqtt.h" |
|
61 #include "xutil.h" |
|
62 |
|
63 |
|
64 /** |
|
65 * @brief Mainloop 1 timed actions. |
|
66 */ |
|
67 typedef enum |
|
68 { |
|
69 MAIN_LOOP1_INIT = 0, ///< Init fase |
|
70 MAIN_LOOP1_CONNECT, ///< Connect WiFi |
|
71 MAIN_LOOP1_MQTT_CONNECT, ///< Connect MQTT if WiFi |
|
72 MAIN_LOOP1_WAITCON, ///< Wait for connection and measurements |
|
73 MAIN_LOOP1_SEND, ///< Send MQTT node and units messages |
|
74 MAIN_LOOP1_WAITACK, ///< MQTT messages received |
|
75 MAIN_LOOP1_MQTT_DISCONNECT, ///< MQTT disconnect |
|
76 MAIN_LOOP1_DISCONNECT, ///< Disconnect WiFi |
|
77 MAIN_LOOP1_WIFI_OFF, ///< WiFi power off |
|
78 MAIN_LOOP1_DONE ///< All done |
|
79 } MAIN_LOOP1; |
|
80 |
|
81 |
|
82 |
|
83 /** |
|
84 * @brief Mainloop 2 user actions. |
|
85 */ |
|
86 typedef enum |
|
87 { |
|
88 MAIN_LOOP2_INIT = 0, |
|
89 MAIN_LOOP2_UNIT1, ///< Unit 1 |
|
90 MAIN_LOOP2_UNIT2, ///< Unit 2 |
|
91 MAIN_LOOP2_UNIT3, ///< Unit 3 |
|
92 MAIN_LOOP2_UNIT4, ///< Unit 4 |
|
93 MAIN_LOOP2_SET_WIFI, ///< WiFi stations setup |
|
94 MAIN_LOOP2_SET_NETWORK, ///< Network setup |
|
95 MAIN_LOOP2_SET_MQTT, ///< MQTT setup |
|
96 MAIN_LOOP2_UPDATE, ///< Update |
|
97 MAIN_LOOP2_INACTIVE, ///< Inactive reached, cleanup |
|
98 MAIN_LOOP2_DONE ///< All done |
|
99 } MAIN_LOOP2; |
|
100 |
|
101 |
|
102 |
|
103 /** |
|
104 * @brief Global configuration. File /spiffs/config.conf |
|
105 */ |
|
106 struct strConfig { |
|
107 uint8_t Version; ///< Record version number for updates. |
|
108 char ntp_server[32]; ///< Preffered NTP server. |
|
109 char hostname[32]; ///< Our hostname. |
|
110 char uuid[37]; ///< Sort of uuid code. |
|
111 char xlastSSID[32]; ///< Last connected station. |
|
112 char mqtt_server[32]; ///< MQTT server. |
|
113 uint16_t mqtt_port; ///< MQTT TCP port. |
|
114 char mqtt_user[32]; ///< MQTT user name if needed. |
|
115 char mqtt_pwd[62]; ///< MQTT password. |
|
116 } config; ///< Config record. |
|
117 |
|
118 |
|
119 /** |
|
120 * @brief Write configuration to disk. |
|
121 */ |
|
122 void write_config(void); |
|
123 |
|
124 /** @brief Read configuration file. If it doesn't exist create and |
|
125 * write a new configuration file with sane defaults. |
|
126 */ |
|
127 void read_config(void); |
|
128 |
|
129 |
|
130 |
|
131 /** |
|
132 * @brief Records with WiFi stations we have succesfully connected. |
|
133 */ |
|
134 struct strStations { |
|
135 char SSID[32]; ///< Station SSID |
|
136 char Password[64]; ///< Station password |
|
137 bool hide; ///< Hide from AP scan. |
|
138 } wifiStation; ///< Station record. |
|
139 |
|
140 /** |
|
141 * @brief Add a new station record. |
|
142 * @param SSID The SSID |
|
143 * @param Password The password for this SSID |
|
144 * @return The record number, or -1 if error. |
|
145 */ |
|
146 int add_station(uint8_t *SSID, uint8_t *Password); |
|
147 |
|
148 /** |
|
149 * @brief Read station info record. |
|
150 * @param SSID Search for the SSID and load the record if found. |
|
151 * @return Return -1 if not found, else the record number and the wifiStation record is filled. |
|
152 */ |
|
153 int read_station(uint8_t *SSID); |
|
154 |
|
155 /** |
|
156 * @brief Remove station record. |
|
157 * @param SSID The SSID to remove. |
|
158 */ |
|
159 void remove_station(uint8_t *SSID); |
|
160 |
|
161 |
|
162 |
|
163 /** |
|
164 * @brief Records that describes the carbonation units, |
|
165 */ |
|
166 typedef struct strUnit { |
|
167 char uuid[37]; ///< Unit uuid |
|
168 char product_uuid[37]; ///< Beer product uuid |
|
169 char product_code[33]; ///< Beer product code |
|
170 char product_name[129]; ///< Beer product name |
|
171 char alias[33]; ///< Alias name 'unit1' |
|
172 int temperature_state; ///< Reading status |
|
173 char temperature_address[33]; ///< DS18B20 address |
|
174 uint32_t temperature; ///< Temperature in C * 1000 |
|
175 int pressure_state; ///< Reading status |
|
176 uint8_t pressure_channel; ///< ADC channel |
|
177 uint32_t pressure_voltage; ///< Measured voltage in mV. |
|
178 uint32_t pressure_zero; ///< Zero offset in mV. |
|
179 uint32_t pressure; ///< Pressure in bar * 1000; |
|
180 int mode; ///< Unit mode |
|
181 } unit_t; |
|
182 |
|
183 /** |
|
184 * @brief Write units to disk. |
|
185 */ |
|
186 void write_units(void); |
|
187 |
|
188 /** @brief Read units file. If it doesn't exist create and |
|
189 * write a new units file with sane defaults. |
|
190 */ |
|
191 void read_units(void); |
|
192 |
|
193 |
|
194 |
|
195 #endif |