Thu, 07 Nov 2019 15:09:51 +0100
Added error logging for failed locks
17 | 1 | /** |
2 | * @file co2meter.c | |
3 | * @brief co2meter project. | |
0
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 | #include "config.h" |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
7 | |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
8 | static const char *TAG = "co2meter"; |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
9 | |
16
e38ffa806e84
Initial code for the rotary switch and some menus. Changed default pushbutton pin from 12 to 14 and swapped the rotary pins. Disabled always create a new units file, it should be safe to keep it now.
Michiel Broek <mbroek@mbse.eu>
parents:
14
diff
changeset
|
10 | |
21
043ae27633f8
Moved the user interface into a separate task. Added a real seconds timer to this task.
Michiel Broek <mbroek@mbse.eu>
parents:
20
diff
changeset
|
11 | const esp_app_desc_t *app_desc = NULL; ///< Application description |
16
e38ffa806e84
Initial code for the rotary switch and some menus. Changed default pushbutton pin from 12 to 14 and swapped the rotary pins. Disabled always create a new units file, it should be safe to keep it now.
Michiel Broek <mbroek@mbse.eu>
parents:
14
diff
changeset
|
12 | int Main_Loop1 = ML1_INIT; ///< Loop 1 init |
0
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
13 | bool System_TimeOk = false; ///< System time status |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
14 | time_t now; ///< Current time |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
15 | struct tm timeinfo; ///< Current time structure |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
16 | char strftime_buf[64]; ///< Time buffer |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
17 | static RTC_DATA_ATTR struct timeval sleep_enter_time; |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
18 | static TaskHandle_t xTaskDS18B20 = NULL; |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
19 | static TaskHandle_t xTaskADC = NULL; |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
20 | static TaskHandle_t xTaskWifi = NULL; |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
21 | static TaskHandle_t xTaskMQTT = NULL; |
21
043ae27633f8
Moved the user interface into a separate task. Added a real seconds timer to this task.
Michiel Broek <mbroek@mbse.eu>
parents:
20
diff
changeset
|
22 | static TaskHandle_t xTaskUser = NULL; |
0
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
23 | |
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:
2
diff
changeset
|
24 | extern unit_t units[3]; ///< Pressure test units |
2a57c466bf45
FreeRTOS scheduler from 100 Hz to 200 Hz. All units data is now protected with a semaphore.
Michiel Broek <mbroek@mbse.eu>
parents:
2
diff
changeset
|
25 | extern SemaphoreHandle_t xSemaphoreUnits; ///< Units lock semaphore |
0
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
26 | extern DS18B20_State *ds18b20_state; ///< DS18B20 state |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
27 | extern SemaphoreHandle_t xSemaphoreDS18B20; ///< DS18B20 lock semaphore |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
28 | extern ADC_State *adc_state; ///< ADC state |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
29 | extern SemaphoreHandle_t xSemaphoreADC; ///< ADC lock semaphore |
20
7c1dacafed03
Attempt to create a rotary editor
Michiel Broek <mbroek@mbse.eu>
parents:
19
diff
changeset
|
30 | extern WIFI_State *wifi_state; ///< WiFi state |
21
043ae27633f8
Moved the user interface into a separate task. Added a real seconds timer to this task.
Michiel Broek <mbroek@mbse.eu>
parents:
20
diff
changeset
|
31 | extern EventGroupHandle_t xEventGroupUser; |
16
e38ffa806e84
Initial code for the rotary switch and some menus. Changed default pushbutton pin from 12 to 14 and swapped the rotary pins. Disabled always create a new units file, it should be safe to keep it now.
Michiel Broek <mbroek@mbse.eu>
parents:
14
diff
changeset
|
32 | extern int count_pub; ///< Published MQTT messages in transit |
e38ffa806e84
Initial code for the rotary switch and some menus. Changed default pushbutton pin from 12 to 14 and swapped the rotary pins. Disabled always create a new units file, it should be safe to keep it now.
Michiel Broek <mbroek@mbse.eu>
parents:
14
diff
changeset
|
33 | |
18
d969e0fe05dc
Added splash screen and unit zero set menu.
Michiel Broek <mbroek@mbse.eu>
parents:
17
diff
changeset
|
34 | |
d969e0fe05dc
Added splash screen and unit zero set menu.
Michiel Broek <mbroek@mbse.eu>
parents:
17
diff
changeset
|
35 | |
19 | 36 | |
0
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
37 | void app_main() |
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 | struct timeval now; |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
40 | gettimeofday(&now, NULL); |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
41 | int sleep_time_ms = (now.tv_sec - sleep_enter_time.tv_sec) * 1000 + (now.tv_usec - sleep_enter_time.tv_usec) / 1000; |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
42 | esp_err_t ret; |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
43 | |
16
e38ffa806e84
Initial code for the rotary switch and some menus. Changed default pushbutton pin from 12 to 14 and swapped the rotary pins. Disabled always create a new units file, it should be safe to keep it now.
Michiel Broek <mbroek@mbse.eu>
parents:
14
diff
changeset
|
44 | Main_Loop1 = ML1_INIT; |
18
d969e0fe05dc
Added splash screen and unit zero set menu.
Michiel Broek <mbroek@mbse.eu>
parents:
17
diff
changeset
|
45 | app_desc = esp_ota_get_app_description(); |
21
043ae27633f8
Moved the user interface into a separate task. Added a real seconds timer to this task.
Michiel Broek <mbroek@mbse.eu>
parents:
20
diff
changeset
|
46 | /* event handler and event group for the user interface */ |
043ae27633f8
Moved the user interface into a separate task. Added a real seconds timer to this task.
Michiel Broek <mbroek@mbse.eu>
parents:
20
diff
changeset
|
47 | xEventGroupUser = xEventGroupCreate(); |
18
d969e0fe05dc
Added splash screen and unit zero set menu.
Michiel Broek <mbroek@mbse.eu>
parents:
17
diff
changeset
|
48 | |
0
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
49 | switch (esp_sleep_get_wakeup_cause()) { |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
50 | case ESP_SLEEP_WAKEUP_EXT1: { |
16
e38ffa806e84
Initial code for the rotary switch and some menus. Changed default pushbutton pin from 12 to 14 and swapped the rotary pins. Disabled always create a new units file, it should be safe to keep it now.
Michiel Broek <mbroek@mbse.eu>
parents:
14
diff
changeset
|
51 | ESP_LOGI(TAG, "Starting from deep sleep, Rotary switch pressed"); |
21
043ae27633f8
Moved the user interface into a separate task. Added a real seconds timer to this task.
Michiel Broek <mbroek@mbse.eu>
parents:
20
diff
changeset
|
52 | user_wakeup(); |
0
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
53 | break; |
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 | case ESP_SLEEP_WAKEUP_TIMER: { |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
56 | ESP_LOGI(TAG, "Starting from deep sleep, timer wakeup after %dms", sleep_time_ms); |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
57 | break; |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
58 | } |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
59 | case ESP_SLEEP_WAKEUP_UNDEFINED: |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
60 | default: |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
61 | ESP_LOGI(TAG, "Starting from hard reset"); |
21
043ae27633f8
Moved the user interface into a separate task. Added a real seconds timer to this task.
Michiel Broek <mbroek@mbse.eu>
parents:
20
diff
changeset
|
62 | user_cold(); |
0
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
63 | } |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
64 | |
7
6eafc4c2bf3d
Sleep time is now 55 seconds. Code cleanup. Decrease MQTT library logging to Error.
Michiel Broek <mbroek@mbse.eu>
parents:
6
diff
changeset
|
65 | const int wakeup_time_sec = 55; |
0
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
66 | ESP_LOGI(TAG, "Enabling timer wakeup, %ds", wakeup_time_sec); |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
67 | esp_sleep_enable_timer_wakeup(wakeup_time_sec * 1000000); |
16
e38ffa806e84
Initial code for the rotary switch and some menus. Changed default pushbutton pin from 12 to 14 and swapped the rotary pins. Disabled always create a new units file, it should be safe to keep it now.
Michiel Broek <mbroek@mbse.eu>
parents:
14
diff
changeset
|
68 | const uint64_t ext_wakeup_pin_1_mask = 1ULL << ROT_ENC_SW_GPIO; |
0
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
69 | |
16
e38ffa806e84
Initial code for the rotary switch and some menus. Changed default pushbutton pin from 12 to 14 and swapped the rotary pins. Disabled always create a new units file, it should be safe to keep it now.
Michiel Broek <mbroek@mbse.eu>
parents:
14
diff
changeset
|
70 | ESP_LOGI(TAG, "Enabling EXT1 wakeup on pin GPIO%d", ROT_ENC_SW_GPIO); |
e38ffa806e84
Initial code for the rotary switch and some menus. Changed default pushbutton pin from 12 to 14 and swapped the rotary pins. Disabled always create a new units file, it should be safe to keep it now.
Michiel Broek <mbroek@mbse.eu>
parents:
14
diff
changeset
|
71 | esp_sleep_enable_ext1_wakeup(ext_wakeup_pin_1_mask, ESP_EXT1_WAKEUP_ALL_LOW); |
0
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 | // Isolate GPIO12 pin from external circuits. This is needed for modules |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
74 | // which have an external pull-up resistor on GPIO12 (such as ESP32-WROVER) |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
75 | // to minimize current consumption. |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
76 | // rtc_gpio_isolate(GPIO_NUM_12); |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
77 | |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
78 | /* |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
79 | * Initialize NVS |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
80 | */ |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
81 | ret = nvs_flash_init(); |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
82 | if (ret == ESP_ERR_NVS_NO_FREE_PAGES) { |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
83 | ESP_ERROR_CHECK(nvs_flash_erase()); |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
84 | ret = nvs_flash_init(); |
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 | ESP_ERROR_CHECK(ret); |
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 | /* |
6
06a5028dbcdf
Hookup OLED I2C display to temorary display some data.
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
89 | * Setup SPIFFS filesystem |
06a5028dbcdf
Hookup OLED I2C display to temorary display some data.
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
90 | */ |
0
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
91 | ESP_LOGI(TAG, "Initializing SPIFFS"); |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
92 | esp_vfs_spiffs_conf_t conf = { |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
93 | .base_path = "/spiffs", |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
94 | .partition_label = NULL, |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
95 | .max_files = 5, |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
96 | .format_if_mount_failed = true |
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 | |
6
06a5028dbcdf
Hookup OLED I2C display to temorary display some data.
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
99 | /* |
06a5028dbcdf
Hookup OLED I2C display to temorary display some data.
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
100 | * Use settings defined above to initialize and mount SPIFFS filesystem. |
06a5028dbcdf
Hookup OLED I2C display to temorary display some data.
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
101 | * Note: esp_vfs_spiffs_register is an all-in-one convenience function. |
06a5028dbcdf
Hookup OLED I2C display to temorary display some data.
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
102 | */ |
0
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
103 | ret = esp_vfs_spiffs_register(&conf); |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
104 | |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
105 | if (ret != ESP_OK) { |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
106 | if (ret == ESP_FAIL) { |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
107 | ESP_LOGE(TAG, "Failed to mount or format filesystem"); |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
108 | } else if (ret == ESP_ERR_NOT_FOUND) { |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
109 | ESP_LOGE(TAG, "Failed to find SPIFFS partition"); |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
110 | } else { |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
111 | ESP_LOGE(TAG, "Failed to initialize SPIFFS (%d)", ret); |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
112 | } |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
113 | return; // Stop application. |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
114 | } |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
115 | |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
116 | size_t total = 0, used = 0; |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
117 | ret = esp_spiffs_info(NULL, &total, &used); |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
118 | if (ret != ESP_OK) { |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
119 | ESP_LOGE(TAG, "Failed to get SPIFFS partition information"); |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
120 | return; // Stop application. |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
121 | } else { |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
122 | ESP_LOGI(TAG, "Partition size: %d, used: %d - %d%%", total, used, (used * 100) / total); |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
123 | } |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
124 | |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
125 | // Just to debug, list the /spiffs filesystem. |
6
06a5028dbcdf
Hookup OLED I2C display to temorary display some data.
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
126 | #if 0 |
0
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
127 | DIR *dir = opendir("/spiffs"); |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
128 | struct dirent* de = readdir(dir); |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
129 | while (de) { |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
130 | if (de->d_type == DT_REG) { |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
131 | printf("F "); |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
132 | } |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
133 | if (de->d_type == DT_DIR) { |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
134 | printf("D "); |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
135 | } |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
136 | printf("%s\n", de->d_name); |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
137 | de = readdir(dir); |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
138 | } |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
139 | closedir(dir); |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
140 | #endif |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
141 | |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
142 | /* |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
143 | * Read or create configuration |
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 | read_config(); |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
146 | read_units(); |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
147 | |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
148 | //add_station((uint8_t *)"MBSE_WLR", (uint8_t *)"abcjkltuv"); |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
149 | //remove_station((uint8_t *)"MBSE_WLP"); |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
150 | |
6
06a5028dbcdf
Hookup OLED I2C display to temorary display some data.
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
151 | /* |
06a5028dbcdf
Hookup OLED I2C display to temorary display some data.
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
152 | * Create FreeRTOS tasks |
06a5028dbcdf
Hookup OLED I2C display to temorary display some data.
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
153 | */ |
0
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
154 | xSemaphoreDS18B20 = xSemaphoreCreateMutex(); |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
155 | xSemaphoreADC = xSemaphoreCreateMutex(); |
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:
2
diff
changeset
|
156 | xSemaphoreUnits = xSemaphoreCreateMutex(); |
0
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
157 | |
21
043ae27633f8
Moved the user interface into a separate task. Added a real seconds timer to this task.
Michiel Broek <mbroek@mbse.eu>
parents:
20
diff
changeset
|
158 | xTaskCreate(&task_user, "task_user", 2048, NULL,10, &xTaskUser); |
0
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
159 | xTaskCreate(&task_ds18b20, "task_ds18b20", 2560, NULL, 8, &xTaskDS18B20); |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
160 | xTaskCreate(&task_adc, "task_adc", 2560, NULL, 8, &xTaskADC); |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
161 | esp_log_level_set("wifi", ESP_LOG_ERROR); |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
162 | xTaskCreate(&task_wifi, "task_wifi", 4096, NULL, 3, &xTaskWifi); |
6
06a5028dbcdf
Hookup OLED I2C display to temorary display some data.
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
163 | vTaskDelay(10 / portTICK_PERIOD_MS); |
7
6eafc4c2bf3d
Sleep time is now 55 seconds. Code cleanup. Decrease MQTT library logging to Error.
Michiel Broek <mbroek@mbse.eu>
parents:
6
diff
changeset
|
164 | esp_log_level_set("MQTT_CLIENT", ESP_LOG_ERROR); |
0
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
165 | xTaskCreate(&task_mqtt, "task_mqtt", 4096, NULL, 5, &xTaskMQTT); |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
166 | |
7
6eafc4c2bf3d
Sleep time is now 55 seconds. Code cleanup. Decrease MQTT library logging to Error.
Michiel Broek <mbroek@mbse.eu>
parents:
6
diff
changeset
|
167 | /* |
0
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
168 | * Main application loop. |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
169 | */ |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
170 | while (1) { |
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 | ESP_LOGI(TAG, "Entered app loop"); |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
173 | |
21
043ae27633f8
Moved the user interface into a separate task. Added a real seconds timer to this task.
Michiel Broek <mbroek@mbse.eu>
parents:
20
diff
changeset
|
174 | /* Measure process */ |
0
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
175 | while (1) { |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
176 | switch (Main_Loop1) { |
16
e38ffa806e84
Initial code for the rotary switch and some menus. Changed default pushbutton pin from 12 to 14 and swapped the rotary pins. Disabled always create a new units file, it should be safe to keep it now.
Michiel Broek <mbroek@mbse.eu>
parents:
14
diff
changeset
|
177 | case ML1_INIT: |
0
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
178 | ESP_LOGI(TAG, "Loop timer: Init"); |
16
e38ffa806e84
Initial code for the rotary switch and some menus. Changed default pushbutton pin from 12 to 14 and swapped the rotary pins. Disabled always create a new units file, it should be safe to keep it now.
Michiel Broek <mbroek@mbse.eu>
parents:
14
diff
changeset
|
179 | // If configured do ML1_CONNECT |
e38ffa806e84
Initial code for the rotary switch and some menus. Changed default pushbutton pin from 12 to 14 and swapped the rotary pins. Disabled always create a new units file, it should be safe to keep it now.
Michiel Broek <mbroek@mbse.eu>
parents:
14
diff
changeset
|
180 | Main_Loop1 = ML1_CONNECT; |
0
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
181 | requestWiFi_system(true); |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
182 | request_ds18b20(); |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
183 | request_adc(); |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
184 | break; |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
185 | |
16
e38ffa806e84
Initial code for the rotary switch and some menus. Changed default pushbutton pin from 12 to 14 and swapped the rotary pins. Disabled always create a new units file, it should be safe to keep it now.
Michiel Broek <mbroek@mbse.eu>
parents:
14
diff
changeset
|
186 | case ML1_CONNECT: |
20
7c1dacafed03
Attempt to create a rotary editor
Michiel Broek <mbroek@mbse.eu>
parents:
19
diff
changeset
|
187 | if (ready_WiFi()) { |
16
e38ffa806e84
Initial code for the rotary switch and some menus. Changed default pushbutton pin from 12 to 14 and swapped the rotary pins. Disabled always create a new units file, it should be safe to keep it now.
Michiel Broek <mbroek@mbse.eu>
parents:
14
diff
changeset
|
188 | Main_Loop1 = ML1_MQTT_CONNECT; |
22
cceb36fd3a2a
Better update of the user screens. Added more internal documentation.
Michiel Broek <mbroek@mbse.eu>
parents:
21
diff
changeset
|
189 | user_refresh(); |
20
7c1dacafed03
Attempt to create a rotary editor
Michiel Broek <mbroek@mbse.eu>
parents:
19
diff
changeset
|
190 | } |
0
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
191 | break; |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
192 | |
16
e38ffa806e84
Initial code for the rotary switch and some menus. Changed default pushbutton pin from 12 to 14 and swapped the rotary pins. Disabled always create a new units file, it should be safe to keep it now.
Michiel Broek <mbroek@mbse.eu>
parents:
14
diff
changeset
|
193 | case ML1_MQTT_CONNECT: |
0
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
194 | if (ready_ds18b20() && ready_adc()) { |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
195 | connect_mqtt(true); |
16
e38ffa806e84
Initial code for the rotary switch and some menus. Changed default pushbutton pin from 12 to 14 and swapped the rotary pins. Disabled always create a new units file, it should be safe to keep it now.
Michiel Broek <mbroek@mbse.eu>
parents:
14
diff
changeset
|
196 | Main_Loop1 = ML1_WAITCON; |
0
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
197 | ESP_LOGI(TAG, "Loop timer: Wait MQTT"); |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
198 | |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
199 | /* Get global temperature, use for all units. */ |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
200 | uint32_t temp = 0; |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
201 | int state = 0; |
2
c0184362d48c
Prepare ds18b20 sensors for multiple sensors on the onewire bus.
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
202 | char rom_code[17]; |
0
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
203 | if (xSemaphoreTake(xSemaphoreDS18B20, 10) == pdTRUE) { |
2
c0184362d48c
Prepare ds18b20 sensors for multiple sensors on the onewire bus.
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
204 | temp = (ds18b20_state->sensor[0].temperature * 1000); |
c0184362d48c
Prepare ds18b20 sensors for multiple sensors on the onewire bus.
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
205 | state = (ds18b20_state->sensor[0].error == 0) ? 0:1; |
9
a85995941d0d
Added publish logs. Another fix for DS18B20 rom address copy.
Michiel Broek <mbroek@mbse.eu>
parents:
8
diff
changeset
|
206 | strncpy(rom_code, ds18b20_state->sensor[0].rom_code, 17); |
a85995941d0d
Added publish logs. Another fix for DS18B20 rom address copy.
Michiel Broek <mbroek@mbse.eu>
parents:
8
diff
changeset
|
207 | rom_code[16] = '\0'; |
0
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
208 | xSemaphoreGive(xSemaphoreDS18B20); |
23
58a328e91881
Added error logging for failed locks
Michiel Broek <mbroek@mbse.eu>
parents:
22
diff
changeset
|
209 | } else { |
58a328e91881
Added error logging for failed locks
Michiel Broek <mbroek@mbse.eu>
parents:
22
diff
changeset
|
210 | ESP_LOGE(TAG, "ML1_MQTT_CONNECT DS18B20 lock error"); |
58a328e91881
Added error logging for failed locks
Michiel Broek <mbroek@mbse.eu>
parents:
22
diff
changeset
|
211 | } |
6
06a5028dbcdf
Hookup OLED I2C display to temorary display some data.
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
212 | |
0
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
213 | /* Copy measured data and calculate results */ |
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:
2
diff
changeset
|
214 | 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:
2
diff
changeset
|
215 | for (int i = 0; i < 3; i++) { |
2a57c466bf45
FreeRTOS scheduler from 100 Hz to 200 Hz. All units data is now protected with a semaphore.
Michiel Broek <mbroek@mbse.eu>
parents:
2
diff
changeset
|
216 | units[i].temperature = temp; |
2a57c466bf45
FreeRTOS scheduler from 100 Hz to 200 Hz. All units data is now protected with a semaphore.
Michiel Broek <mbroek@mbse.eu>
parents:
2
diff
changeset
|
217 | units[i].temperature_state = state; |
8 | 218 | units[i].alarm = 0; |
219 | if (state) | |
220 | units[i].alarm |= ALARM_SYS_TEMPERATURE & ALARM_UNIT_TEMPERATURE; | |
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:
2
diff
changeset
|
221 | strncpy(units[i].temperature_rom_code, rom_code, 17); |
2a57c466bf45
FreeRTOS scheduler from 100 Hz to 200 Hz. All units data is now protected with a semaphore.
Michiel Broek <mbroek@mbse.eu>
parents:
2
diff
changeset
|
222 | if (xSemaphoreTake(xSemaphoreADC, 10) == 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:
2
diff
changeset
|
223 | units[i].pressure_state = adc_state->Pressure[i].error; |
2a57c466bf45
FreeRTOS scheduler from 100 Hz to 200 Hz. All units data is now protected with a semaphore.
Michiel Broek <mbroek@mbse.eu>
parents:
2
diff
changeset
|
224 | units[i].pressure_channel = adc_state->Pressure[i].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:
2
diff
changeset
|
225 | units[i].pressure_voltage = adc_state->Pressure[i].voltage; |
8 | 226 | if (units[i].pressure_state || units[i].pressure_voltage < 80) |
227 | units[i].alarm |= ALARM_UNIT_PRESSURE; | |
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:
2
diff
changeset
|
228 | int P = (units[i].pressure_voltage / (adc_state->Batt_voltage / 1000) - units[i].pressure_zero) * 14; // in 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:
2
diff
changeset
|
229 | if (P < 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:
2
diff
changeset
|
230 | P = 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:
2
diff
changeset
|
231 | units[i].pressure = P; |
18
d969e0fe05dc
Added splash screen and unit zero set menu.
Michiel Broek <mbroek@mbse.eu>
parents:
17
diff
changeset
|
232 | printf("%d volt: %d batt: %d scale: %d mbar: %d\n", i, units[i].pressure_voltage, adc_state->Batt_voltage, |
0
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
233 | units[i].pressure_voltage / (adc_state->Batt_voltage / 1000) - units[i].pressure_zero, P); |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
234 | // Moet die echt op 5 volt? |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
235 | // Verbruik 10 mA |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
236 | // Setup tijd max 2 mS |
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:
2
diff
changeset
|
237 | xSemaphoreGive(xSemaphoreADC); |
23
58a328e91881
Added error logging for failed locks
Michiel Broek <mbroek@mbse.eu>
parents:
22
diff
changeset
|
238 | } else { |
58a328e91881
Added error logging for failed locks
Michiel Broek <mbroek@mbse.eu>
parents:
22
diff
changeset
|
239 | ESP_LOGE(TAG, "ML1_MQTT_CONNECT ADC lock error"); |
58a328e91881
Added error logging for failed locks
Michiel Broek <mbroek@mbse.eu>
parents:
22
diff
changeset
|
240 | } |
0
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
241 | } |
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:
2
diff
changeset
|
242 | write_units(); |
2a57c466bf45
FreeRTOS scheduler from 100 Hz to 200 Hz. All units data is now protected with a semaphore.
Michiel Broek <mbroek@mbse.eu>
parents:
2
diff
changeset
|
243 | xSemaphoreGive(xSemaphoreUnits); |
21
043ae27633f8
Moved the user interface into a separate task. Added a real seconds timer to this task.
Michiel Broek <mbroek@mbse.eu>
parents:
20
diff
changeset
|
244 | user_refresh(); |
23
58a328e91881
Added error logging for failed locks
Michiel Broek <mbroek@mbse.eu>
parents:
22
diff
changeset
|
245 | } else { |
58a328e91881
Added error logging for failed locks
Michiel Broek <mbroek@mbse.eu>
parents:
22
diff
changeset
|
246 | ESP_LOGE(TAG, "ML1_MQTT_CONNECT units lock error"); |
0
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 | break; |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
250 | |
16
e38ffa806e84
Initial code for the rotary switch and some menus. Changed default pushbutton pin from 12 to 14 and swapped the rotary pins. Disabled always create a new units file, it should be safe to keep it now.
Michiel Broek <mbroek@mbse.eu>
parents:
14
diff
changeset
|
251 | case ML1_WAITCON: |
0
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
252 | if (ready_mqtt()) |
16
e38ffa806e84
Initial code for the rotary switch and some menus. Changed default pushbutton pin from 12 to 14 and swapped the rotary pins. Disabled always create a new units file, it should be safe to keep it now.
Michiel Broek <mbroek@mbse.eu>
parents:
14
diff
changeset
|
253 | Main_Loop1 = ML1_SEND; |
0
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
254 | break; |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
255 | |
16
e38ffa806e84
Initial code for the rotary switch and some menus. Changed default pushbutton pin from 12 to 14 and swapped the rotary pins. Disabled always create a new units file, it should be safe to keep it now.
Michiel Broek <mbroek@mbse.eu>
parents:
14
diff
changeset
|
256 | case ML1_SEND: |
0
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
257 | ESP_LOGI(TAG, "Loop timer: Send MQTT"); |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
258 | publishNode(); |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
259 | publishUnits(); |
9
a85995941d0d
Added publish logs. Another fix for DS18B20 rom address copy.
Michiel Broek <mbroek@mbse.eu>
parents:
8
diff
changeset
|
260 | publishLogs(); |
16
e38ffa806e84
Initial code for the rotary switch and some menus. Changed default pushbutton pin from 12 to 14 and swapped the rotary pins. Disabled always create a new units file, it should be safe to keep it now.
Michiel Broek <mbroek@mbse.eu>
parents:
14
diff
changeset
|
261 | Main_Loop1 = ML1_WAITACK; |
0
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
262 | break; |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
263 | |
16
e38ffa806e84
Initial code for the rotary switch and some menus. Changed default pushbutton pin from 12 to 14 and swapped the rotary pins. Disabled always create a new units file, it should be safe to keep it now.
Michiel Broek <mbroek@mbse.eu>
parents:
14
diff
changeset
|
264 | case ML1_WAITACK: |
11
e33f2d325d15
Experimental mqtt published messages state counter
Michiel Broek <mbroek@mbse.eu>
parents:
9
diff
changeset
|
265 | if (count_pub == 0) // Wait until all published messages are sent. |
16
e38ffa806e84
Initial code for the rotary switch and some menus. Changed default pushbutton pin from 12 to 14 and swapped the rotary pins. Disabled always create a new units file, it should be safe to keep it now.
Michiel Broek <mbroek@mbse.eu>
parents:
14
diff
changeset
|
266 | Main_Loop1 = ML1_MQTT_DISCONNECT; |
0
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
267 | break; |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
268 | |
16
e38ffa806e84
Initial code for the rotary switch and some menus. Changed default pushbutton pin from 12 to 14 and swapped the rotary pins. Disabled always create a new units file, it should be safe to keep it now.
Michiel Broek <mbroek@mbse.eu>
parents:
14
diff
changeset
|
269 | case ML1_MQTT_DISCONNECT: |
0
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
270 | ESP_LOGI(TAG, "Loop timer: Disconnect MQTT"); |
11
e33f2d325d15
Experimental mqtt published messages state counter
Michiel Broek <mbroek@mbse.eu>
parents:
9
diff
changeset
|
271 | connect_mqtt(false); // Doesn't really disconnect. |
16
e38ffa806e84
Initial code for the rotary switch and some menus. Changed default pushbutton pin from 12 to 14 and swapped the rotary pins. Disabled always create a new units file, it should be safe to keep it now.
Michiel Broek <mbroek@mbse.eu>
parents:
14
diff
changeset
|
272 | Main_Loop1 = ML1_DISCONNECT; |
0
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
273 | break; |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
274 | |
16
e38ffa806e84
Initial code for the rotary switch and some menus. Changed default pushbutton pin from 12 to 14 and swapped the rotary pins. Disabled always create a new units file, it should be safe to keep it now.
Michiel Broek <mbroek@mbse.eu>
parents:
14
diff
changeset
|
275 | case ML1_DISCONNECT: |
0
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
276 | if (! ready_mqtt()) { |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
277 | ESP_LOGI(TAG, "Loop timer: WiFi off"); |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
278 | requestWiFi_system(false); |
16
e38ffa806e84
Initial code for the rotary switch and some menus. Changed default pushbutton pin from 12 to 14 and swapped the rotary pins. Disabled always create a new units file, it should be safe to keep it now.
Michiel Broek <mbroek@mbse.eu>
parents:
14
diff
changeset
|
279 | Main_Loop1 = ML1_WIFI_OFF; |
0
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
280 | } |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
281 | break; |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
282 | |
16
e38ffa806e84
Initial code for the rotary switch and some menus. Changed default pushbutton pin from 12 to 14 and swapped the rotary pins. Disabled always create a new units file, it should be safe to keep it now.
Michiel Broek <mbroek@mbse.eu>
parents:
14
diff
changeset
|
283 | case ML1_WIFI_OFF: |
0
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
284 | if (! ready_WiFi()) { |
21
043ae27633f8
Moved the user interface into a separate task. Added a real seconds timer to this task.
Michiel Broek <mbroek@mbse.eu>
parents:
20
diff
changeset
|
285 | ESP_LOGI(TAG, "Loop timer: Done %s", user_busy() ? "true":"false"); |
16
e38ffa806e84
Initial code for the rotary switch and some menus. Changed default pushbutton pin from 12 to 14 and swapped the rotary pins. Disabled always create a new units file, it should be safe to keep it now.
Michiel Broek <mbroek@mbse.eu>
parents:
14
diff
changeset
|
286 | Main_Loop1 = ML1_DONE; |
22
cceb36fd3a2a
Better update of the user screens. Added more internal documentation.
Michiel Broek <mbroek@mbse.eu>
parents:
21
diff
changeset
|
287 | user_refresh(); |
0
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 | break; |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
290 | |
16
e38ffa806e84
Initial code for the rotary switch and some menus. Changed default pushbutton pin from 12 to 14 and swapped the rotary pins. Disabled always create a new units file, it should be safe to keep it now.
Michiel Broek <mbroek@mbse.eu>
parents:
14
diff
changeset
|
291 | case ML1_DONE: |
0
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 | |
21
043ae27633f8
Moved the user interface into a separate task. Added a real seconds timer to this task.
Michiel Broek <mbroek@mbse.eu>
parents:
20
diff
changeset
|
295 | if (Main_Loop1 == ML1_DONE && ! user_busy()) |
0
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
296 | break; |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
297 | |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
298 | vTaskDelay(10 / portTICK_PERIOD_MS); |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
299 | } |
6
06a5028dbcdf
Hookup OLED I2C display to temorary display some data.
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
300 | |
17 | 301 | ESP_LOGI(TAG, "Entering deep sleep"); |
0
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
302 | gettimeofday(&sleep_enter_time, NULL); |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
303 | esp_deep_sleep_start(); |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
304 | |
21
043ae27633f8
Moved the user interface into a separate task. Added a real seconds timer to this task.
Michiel Broek <mbroek@mbse.eu>
parents:
20
diff
changeset
|
305 | // ESP_LOGI(TAG, "Do nothing loop"); |
043ae27633f8
Moved the user interface into a separate task. Added a real seconds timer to this task.
Michiel Broek <mbroek@mbse.eu>
parents:
20
diff
changeset
|
306 | // vTaskDelay(55000 / portTICK_PERIOD_MS); |
043ae27633f8
Moved the user interface into a separate task. Added a real seconds timer to this task.
Michiel Broek <mbroek@mbse.eu>
parents:
20
diff
changeset
|
307 | |
16
e38ffa806e84
Initial code for the rotary switch and some menus. Changed default pushbutton pin from 12 to 14 and swapped the rotary pins. Disabled always create a new units file, it should be safe to keep it now.
Michiel Broek <mbroek@mbse.eu>
parents:
14
diff
changeset
|
308 | Main_Loop1 = ML1_INIT; |
0
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
309 | } |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
310 | } |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
311 |