main/co2meter.c

Tue, 08 Oct 2019 12:00:31 +0200

author
Michiel Broek <mbroek@mbse.eu>
date
Tue, 08 Oct 2019 12:00:31 +0200
changeset 0
88d965579617
child 2
c0184362d48c
permissions
-rw-r--r--

Initial import of the CO2 meter application.

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 * co2meter project.
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
3 */
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 #include "config.h"
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 static const char *TAG = "co2meter";
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 #define PIN_SDA (CONFIG_I2C_MASTER_SDA)
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
10 #define PIN_SCL (CONFIG_I2C_MASTER_SCL)
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
11 #define ROT_ENC_A_GPIO (CONFIG_ROT_ENC_A_GPIO)
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
12 #define ROT_ENC_B_GPIO (CONFIG_ROT_ENC_B_GPIO)
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
13 #define ROT_ENC_SW_GPIO (CONFIG_ROT_ENC_SW_GPIO)
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
14
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
15
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
16 #define ENABLE_HALF_STEPS false ///< Set to true to enable tracking of rotary encoder at half step resolution
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
17 #define RESET_AT 0 ///< Set to a positive non-zero number to reset the position if this value is exceeded
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
18 #define FLIP_DIRECTION false ///< Set to true to reverse the clockwise/counterclockwise sense
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
19
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
20
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
21 int Main_Loop1 = MAIN_LOOP1_INIT; ///< Loop 1 init
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
22 int Main_Loop2 = -1; ///< Loop 2 invalid
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
23 bool System_TimeOk = false; ///< System time status
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
24 time_t now; ///< Current time
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
25 struct tm timeinfo; ///< Current time structure
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
26 char strftime_buf[64]; ///< Time buffer
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
27 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
28 static TaskHandle_t xTaskDS18B20 = NULL;
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
29 static TaskHandle_t xTaskADC = NULL;
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
30 static TaskHandle_t xTaskWifi = NULL;
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
31 static TaskHandle_t xTaskMQTT = NULL;
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
32 const esp_app_desc_t *app_desc = NULL;
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
33 unit_t units[3]; ///< Pressure test units
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
34
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
35
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
36 extern DS18B20_State *ds18b20_state; ///< DS18B20 state
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
37 extern SemaphoreHandle_t xSemaphoreDS18B20; ///< DS18B20 lock semaphore
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
38 extern ADC_State *adc_state; ///< ADC state
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
39 extern SemaphoreHandle_t xSemaphoreADC; ///< ADC lock semaphore
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
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
42 void app_main()
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
43 {
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
44 struct timeval now;
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
45 gettimeofday(&now, NULL);
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
46 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
47 int New_Loop2 = MAIN_LOOP2_INIT;
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
48 esp_err_t ret;
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
49
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
50 Main_Loop1 = MAIN_LOOP1_INIT;
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
51 Main_Loop2 = -1;
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
52
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
53 switch (esp_sleep_get_wakeup_cause()) {
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
54 case ESP_SLEEP_WAKEUP_EXT1: {
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
55 uint64_t wakeup_pin_mask = esp_sleep_get_ext1_wakeup_status();
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
56 if (wakeup_pin_mask != 0) {
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
57 int pin = __builtin_ffsll(wakeup_pin_mask) - 1;
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
58 printf("Wake up from GPIO %d\n", pin);
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
59 } else {
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
60 printf("Wake up from GPIO\n");
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
61 }
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
62 break;
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 case ESP_SLEEP_WAKEUP_TIMER: {
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
65 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
66 break;
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
67 }
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
68 case ESP_SLEEP_WAKEUP_UNDEFINED:
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
69 default:
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
70 ESP_LOGI(TAG, "Starting from hard reset");
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 const int wakeup_time_sec = 20;
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
74 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
75 esp_sleep_enable_timer_wakeup(wakeup_time_sec * 1000000);
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 // const int ext_wakeup_pin_1 = ROT_ENC_SW_GPIO; // 25 in example, redefine to rotary name.
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
78 // const uint64_t ext_wakeup_pin_1_mask = 1ULL << ext_wakeup_pin_1;
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
79
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
80 // printf("Enabling EXT1 wakeup on pins GPIO%d\n", ext_wakeup_pin_1);
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
81 // esp_sleep_enable_ext1_wakeup(ext_wakeup_pin_1_mask, ESP_EXT1_WAKEUP_ANY_HIGH); // TODO: what is the logic of the rotary button.
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
82
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
83 // 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
84 // 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
85 // to minimize current consumption.
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
86 // rtc_gpio_isolate(GPIO_NUM_12);
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 app_desc = esp_ota_get_app_description();
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 /*
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
91 * Initialize NVS
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
92 */
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
93 ret = nvs_flash_init();
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
94 if (ret == ESP_ERR_NVS_NO_FREE_PAGES) {
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
95 ESP_ERROR_CHECK(nvs_flash_erase());
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
96 ret = nvs_flash_init();
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 ESP_ERROR_CHECK(ret);
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
99
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 * Setup the OLED display.
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
102 * See: https://github.com/nkolban/esp32-snippets/blob/master/hardware/displays/U8G2/
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
103 */
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
104 // u8g2_esp32_hal_t u8g2_esp32_hal = U8G2_ESP32_HAL_DEFAULT;
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
105 // u8g2_esp32_hal.sda = PIN_SDA;
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
106 // u8g2_esp32_hal.scl = PIN_SCL;
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
107 // u8g2_esp32_hal_init(u8g2_esp32_hal);
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
108
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
109 // u8g2_t u8g2; // a structure which will contain all the data for one display
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
110 //// u8g2_Setup_ssd1306_i2c_128x32_univision_f(
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
111 // u8g2_Setup_sh1106_i2c_128x64_noname_f(
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
112 // &u8g2,
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
113 // U8G2_R0,
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
114 // //u8x8_byte_sw_i2c,
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
115 // u8g2_esp32_i2c_byte_cb,
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
116 // u8g2_esp32_gpio_and_delay_cb); // init u8g2 structure
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
117 // u8x8_SetI2CAddress(&u8g2.u8x8,0x78);
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
118
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
119 // ESP_LOGI(TAG, "u8g2_InitDisplay");
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
120 // u8g2_InitDisplay(&u8g2); // send init sequence to the display, display is in sleep mode after this,
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
121
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
122 // ESP_LOGI(TAG, "u8g2_SetPowerSave");
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
123 // u8g2_SetPowerSave(&u8g2, 0); // wake up display
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
124 // ESP_LOGI(TAG, "u8g2_ClearBuffer");
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
125 // u8g2_ClearBuffer(&u8g2);
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
126 // ESP_LOGI(TAG, "u8g2_DrawBox");
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
127 // u8g2_DrawBox(&u8g2, 0, 26, 80,6);
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
128 // u8g2_DrawFrame(&u8g2, 0,26,100,6);
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
129
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
130 // ESP_LOGI(TAG, "u8g2_SetFont");
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
131 // u8g2_SetFont(&u8g2, u8g2_font_ncenB14_tr);
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
132 // ESP_LOGI(TAG, "u8g2_DrawStr");
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
133 // u8g2_DrawStr(&u8g2, 2,17,"Hi nkolban!");
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
134 // ESP_LOGI(TAG, "u8g2_SendBuffer");
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
135 // u8g2_SendBuffer(&u8g2);
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
136
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
137
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
138 ESP_LOGI(TAG, "Initializing SPIFFS");
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
139
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
140 esp_vfs_spiffs_conf_t conf = {
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
141 .base_path = "/spiffs",
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
142 .partition_label = NULL,
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
143 .max_files = 5,
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
144 .format_if_mount_failed = true
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 // Use settings defined above to initialize and mount SPIFFS filesystem.
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
148 // Note: esp_vfs_spiffs_register is an all-in-one convenience function.
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
149 ret = esp_vfs_spiffs_register(&conf);
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
150
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
151 if (ret != ESP_OK) {
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
152 if (ret == ESP_FAIL) {
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
153 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
154 } else if (ret == ESP_ERR_NOT_FOUND) {
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
155 ESP_LOGE(TAG, "Failed to find SPIFFS partition");
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
156 } else {
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
157 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
158 }
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
159 // _fg = TFT_RED;
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
160 // TFT_print((char *)"error\r\n", LASTX, LASTY);
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
161 return; // Stop application.
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
162 }
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 size_t total = 0, used = 0;
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
165 ret = esp_spiffs_info(NULL, &total, &used);
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
166 if (ret != ESP_OK) {
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
167 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
168 // _fg = TFT_RED;
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
169 // TFT_print((char *)"error\r\n", LASTX, LASTY);
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
170 return; // Stop application.
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
171 } else {
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
172 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
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 // Just to debug, list the /spiffs filesystem.
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
176 #if 1
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
177 DIR *dir = opendir("/spiffs");
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
178 struct dirent* de = readdir(dir);
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
179 while (de) {
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
180 if (de->d_type == DT_REG) {
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
181 printf("F ");
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
182 }
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
183 if (de->d_type == DT_DIR) {
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
184 printf("D ");
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
185 }
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
186 printf("%s\n", de->d_name);
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
187 de = readdir(dir);
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
188 }
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
189 closedir(dir);
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
190 #endif
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
191
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
192 /*
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
193 * Read or create configuration
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 // TFT_print((char *)"Ophalen configuratie ", LASTX, LASTY);
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
196 read_config();
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
197 read_units();
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 //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
200 //remove_station((uint8_t *)"MBSE_WLP");
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
201 //sprintf(config.lastSSID, "%s", "BREWER");
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
202 //write_config();
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
203
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
204 xSemaphoreDS18B20 = xSemaphoreCreateMutex();
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
205 xSemaphoreADC = xSemaphoreCreateMutex();
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
206
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
207 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
208 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
209 esp_log_level_set("wifi", ESP_LOG_ERROR);
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
210 xTaskCreate(&task_wifi, "task_wifi", 4096, NULL, 3, &xTaskWifi);
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
211 vTaskDelay( (TickType_t)10);
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
212 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
213
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
214 // esp32-rotary-encoder requires that the GPIO ISR service is installed before calling rotary_encoder_register()
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
215 // ESP_ERROR_CHECK(gpio_install_isr_service(0));
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
216
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
217 // Initialise the rotary encoder device with the GPIOs for A and B signals
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
218 // rotary_encoder_info_t info = { 0 };
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
219 // ESP_ERROR_CHECK(rotary_encoder_init(&info, ROT_ENC_A_GPIO, ROT_ENC_B_GPIO));
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
220 // ESP_ERROR_CHECK(rotary_encoder_enable_half_steps(&info, ENABLE_HALF_STEPS));
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
221 #ifdef FLIP_DIRECTION
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
222 // ESP_ERROR_CHECK(rotary_encoder_flip_direction(&info));
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
223 #endif
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
224
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
225 // Create a queue for events from the rotary encoder driver.
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
226 // Tasks can read from this queue to receive up to date position information.
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
227 // QueueHandle_t event_queue = rotary_encoder_create_queue();
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
228 // ESP_ERROR_CHECK(rotary_encoder_set_queue(&info, event_queue));
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
229
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
230
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
231 /* Print chip information */
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
232 // esp_chip_info_t chip_info;
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
233 // esp_chip_info(&chip_info);
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
234 // printf("This is ESP32 chip with %d CPU cores, WiFi%s%s, ",
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
235 // chip_info.cores,
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
236 // (chip_info.features & CHIP_FEATURE_BT) ? "/BT" : "",
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
237 // (chip_info.features & CHIP_FEATURE_BLE) ? "/BLE" : "");
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
238
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
239 // printf("silicon revision %d, ", chip_info.revision);
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
240
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
241 // printf("%dMB %s flash\n", spi_flash_get_chip_size() / (1024 * 1024),
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
242 // (chip_info.features & CHIP_FEATURE_EMB_FLASH) ? "embedded" : "external");
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
243
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
244 // esp_err_t status = adc2_vref_to_gpio(GPIO_NUM_26);
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
245 // if (status == ESP_OK) {
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
246 // printf("v_ref routed to GPIO\n");
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
247 // } else {
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
248 // printf("failed to route v_ref\n");
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
249 // }
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
250 // vTaskDelay(1000 * wakeup_time_sec / portTICK_PERIOD_MS);
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
251
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
252 /*
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
253 * Main application loop.
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
254 */
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
255 while (1) {
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
256
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
257 ESP_LOGI(TAG, "Entered app loop");
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
258
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
259 /* Measure process or user input via rotary switch */
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
260 while (1) {
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
261 switch (Main_Loop1) {
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
262 case MAIN_LOOP1_INIT:
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
263 ESP_LOGI(TAG, "Loop timer: Init");
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
264 // If configured do MAIN_LOOP1_CONNECT
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
265 Main_Loop1 = MAIN_LOOP1_CONNECT;
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
266 requestWiFi_system(true);
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
267 request_ds18b20();
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
268 request_adc();
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
269 break;
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
270
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
271 case MAIN_LOOP1_CONNECT:
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
272 if (ready_WiFi())
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
273 Main_Loop1 = MAIN_LOOP1_MQTT_CONNECT;
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 MAIN_LOOP1_MQTT_CONNECT:
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
277 if (ready_ds18b20() && ready_adc()) {
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
278 connect_mqtt(true);
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
279 Main_Loop1 = MAIN_LOOP1_WAITCON;
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
280 ESP_LOGI(TAG, "Loop timer: Wait MQTT");
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
281
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
282 /* Get global temperature, use for all units. */
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
283 uint32_t temp = 0;
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
284 int state = 0;
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
285 if (xSemaphoreTake(xSemaphoreDS18B20, 10) == pdTRUE) {
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
286 temp = (ds18b20_state->bottle_temperature * 1000);
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
287 state = (ds18b20_state->bottle_error == 0) ? 0:1;
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
288 xSemaphoreGive(xSemaphoreDS18B20);
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
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
291 /* Copy measured data and calculate results */
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
292 for (int i = 0; i < 3; i++) {
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
293 units[i].temperature = temp;
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
294 units[i].temperature_state = state;
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
295 if (xSemaphoreTake(xSemaphoreADC, 10) == pdTRUE) {
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
296 units[i].pressure_state = adc_state->Pressure[i].error;
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
297 units[i].pressure_channel = adc_state->Pressure[i].channel;
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
298 units[i].pressure_voltage = adc_state->Pressure[i].voltage;
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
299 units[i].pressure_zero = 110;
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
300 int P = (units[i].pressure_voltage / (adc_state->Batt_voltage / 1000) - units[i].pressure_zero) * 14; // in bar
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
301 if (P < 0)
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
302 P = 0;
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
303 units[i].pressure = P;
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
304 printf("%d volt: %d batt: %d scale: %d bar: %d\n", i, units[i].pressure_voltage, adc_state->Batt_voltage,
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
305 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
306 // Moet die echt op 5 volt?
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
307 // Verbruik 10 mA
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
308 // Setup tijd max 2 mS
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
309 xSemaphoreGive(xSemaphoreADC);
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 }
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
312 write_units();
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 break;
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 // calculate stap en data copy
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
317
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
318 case MAIN_LOOP1_WAITCON:
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
319 if (ready_mqtt())
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
320 Main_Loop1 = MAIN_LOOP1_SEND;
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
321 break;
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 case MAIN_LOOP1_SEND:
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
324 ESP_LOGI(TAG, "Loop timer: Send MQTT");
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
325 publishNode();
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
326 publishUnits();
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 Main_Loop1 = MAIN_LOOP1_MQTT_DISCONNECT;
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
329 break;
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
330
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
331 case MAIN_LOOP1_WAITACK:
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
332 break;
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
333
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
334 case MAIN_LOOP1_MQTT_DISCONNECT:
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
335 ESP_LOGI(TAG, "Loop timer: Disconnect MQTT");
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
336 connect_mqtt(false);
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
337 Main_Loop1 = MAIN_LOOP1_DISCONNECT;
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
338 break;
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
339
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
340 case MAIN_LOOP1_DISCONNECT:
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
341 if (! ready_mqtt()) {
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
342 ESP_LOGI(TAG, "Loop timer: WiFi off");
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
343 requestWiFi_system(false);
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
344 Main_Loop1 = MAIN_LOOP1_WIFI_OFF;
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 break;
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 case MAIN_LOOP1_WIFI_OFF:
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
349 if (! ready_WiFi()) {
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
350 ESP_LOGI(TAG, "Loop timer: Done");
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
351 Main_Loop1 = MAIN_LOOP1_DONE;
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
352 }
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
353 break;
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
354
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
355 case MAIN_LOOP1_DONE:
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
356 break;
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
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 * One time actions
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 if (New_Loop2 != Main_Loop2) {
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 Main_Loop2 = New_Loop2;
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
365
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
366 switch (Main_Loop2) {
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
367 case MAIN_LOOP2_INIT:
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
368 ESP_LOGI(TAG, "Loop user: Init");
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
369 // u8g2_SetPowerSave(&u8g2, 0); // wake up display
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
370 // u8g2_ClearBuffer(&u8g2);
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
371 // New_Loop2 = MAIN_LOOP2_INACTIVE;
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
372 New_Loop2 = MAIN_LOOP2_DONE;
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
373 break;
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
374
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
375 case MAIN_LOOP2_INACTIVE:
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
376 // u8g2_SetPowerSave(&u8g2, 1); // powersave display
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
377 New_Loop2 = MAIN_LOOP2_DONE;
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
378 break;
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
379
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
380 default:
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
381 break;
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
382 }
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 /*
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
386 * Action process.
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
387 */
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
388 switch (Main_Loop2) {
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
389 // If wakeup from GPIO -- state machine 2
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
390 // Init OLED
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
391 // If not configured, start configure
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
392 // If configured select first unit
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
393 // New rotate position, set screen, reset waittimer
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
394 // Handle screen (first is show measured values)
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
395 // Count inactivity
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
396 // flag if inactive and OLED lowpower.
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
397
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
398 // Break if all done and inactive.
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
399 default:
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
400 break;
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
401 }
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
402
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
403 if (Main_Loop1 == MAIN_LOOP1_DONE && Main_Loop2 == MAIN_LOOP2_DONE)
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
404 break;
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
405
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
406 vTaskDelay(10 / portTICK_PERIOD_MS);
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
407 }
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
408
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
409 // printf("Simulate deep sleep\n");
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
410 // vTaskDelay(1000 * wakeup_time_sec / portTICK_PERIOD_MS);
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
411
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
412 printf("Entering deep sleep\n");
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
413 gettimeofday(&sleep_enter_time, NULL);
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
414 esp_deep_sleep_start();
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
415
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
416 Main_Loop1 = MAIN_LOOP1_INIT;
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
417 New_Loop2 = MAIN_LOOP2_INIT;
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
418 }
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
419
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
420 }
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
421

mercurial