main/co2meter.c

Fri, 25 Oct 2019 23:27:57 +0200

author
Michiel Broek <mbroek@mbse.eu>
date
Fri, 25 Oct 2019 23:27:57 +0200
changeset 14
deaca7606e23
parent 13
7de246feba5f
child 16
e38ffa806e84
permissions
-rw-r--r--

Added some menu ideas

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;
13
7de246feba5f Updated esp-idf. The first two global screen functions created.
Michiel Broek <mbroek@mbse.eu>
parents: 11
diff changeset
33 u8g2_t u8g2; ///< A structure which will contain all the data for one display
0
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
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
36 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
37 extern SemaphoreHandle_t xSemaphoreUnits; ///< Units lock semaphore
0
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
38 extern DS18B20_State *ds18b20_state; ///< DS18B20 state
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
39 extern SemaphoreHandle_t xSemaphoreDS18B20; ///< DS18B20 lock semaphore
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
40 extern ADC_State *adc_state; ///< ADC state
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
41 extern SemaphoreHandle_t xSemaphoreADC; ///< ADC lock semaphore
11
e33f2d325d15 Experimental mqtt published messages state counter
Michiel Broek <mbroek@mbse.eu>
parents: 9
diff changeset
42 extern int count_pub;
0
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
13
7de246feba5f Updated esp-idf. The first two global screen functions created.
Michiel Broek <mbroek@mbse.eu>
parents: 11
diff changeset
45 void screen_main(float t, float p1, float p2, float p3)
7de246feba5f Updated esp-idf. The first two global screen functions created.
Michiel Broek <mbroek@mbse.eu>
parents: 11
diff changeset
46 {
7de246feba5f Updated esp-idf. The first two global screen functions created.
Michiel Broek <mbroek@mbse.eu>
parents: 11
diff changeset
47 char buf[65];
7de246feba5f Updated esp-idf. The first two global screen functions created.
Michiel Broek <mbroek@mbse.eu>
parents: 11
diff changeset
48
7de246feba5f Updated esp-idf. The first two global screen functions created.
Michiel Broek <mbroek@mbse.eu>
parents: 11
diff changeset
49 u8g2_ClearBuffer(&u8g2);
7de246feba5f Updated esp-idf. The first two global screen functions created.
Michiel Broek <mbroek@mbse.eu>
parents: 11
diff changeset
50 u8g2_DrawHLine(&u8g2, 0, 14, 128);
7de246feba5f Updated esp-idf. The first two global screen functions created.
Michiel Broek <mbroek@mbse.eu>
parents: 11
diff changeset
51 u8g2_SetFont(&u8g2, u8g2_font_t0_15_tr);
7de246feba5f Updated esp-idf. The first two global screen functions created.
Michiel Broek <mbroek@mbse.eu>
parents: 11
diff changeset
52 sprintf(buf, "CO2 meter %s", app_desc->version);
7de246feba5f Updated esp-idf. The first two global screen functions created.
Michiel Broek <mbroek@mbse.eu>
parents: 11
diff changeset
53 u8g2_uint_t w = u8g2_GetStrWidth(&u8g2, buf);
7de246feba5f Updated esp-idf. The first two global screen functions created.
Michiel Broek <mbroek@mbse.eu>
parents: 11
diff changeset
54 u8g2_DrawStr(&u8g2, (128 - w) / 2,12, buf);
7de246feba5f Updated esp-idf. The first two global screen functions created.
Michiel Broek <mbroek@mbse.eu>
parents: 11
diff changeset
55
7de246feba5f Updated esp-idf. The first two global screen functions created.
Michiel Broek <mbroek@mbse.eu>
parents: 11
diff changeset
56 u8g2_SetFont(&u8g2, u8g2_font_t0_22b_tf);
7de246feba5f Updated esp-idf. The first two global screen functions created.
Michiel Broek <mbroek@mbse.eu>
parents: 11
diff changeset
57 sprintf(buf, "%.1f °C", t);
7de246feba5f Updated esp-idf. The first two global screen functions created.
Michiel Broek <mbroek@mbse.eu>
parents: 11
diff changeset
58 w = u8g2_GetUTF8Width(&u8g2, buf);
7de246feba5f Updated esp-idf. The first two global screen functions created.
Michiel Broek <mbroek@mbse.eu>
parents: 11
diff changeset
59 u8g2_DrawUTF8(&u8g2, (128 - w) / 2,40, buf);
7de246feba5f Updated esp-idf. The first two global screen functions created.
Michiel Broek <mbroek@mbse.eu>
parents: 11
diff changeset
60 u8g2_SetFont(&u8g2, u8g2_font_t0_18b_tf);
7de246feba5f Updated esp-idf. The first two global screen functions created.
Michiel Broek <mbroek@mbse.eu>
parents: 11
diff changeset
61
7de246feba5f Updated esp-idf. The first two global screen functions created.
Michiel Broek <mbroek@mbse.eu>
parents: 11
diff changeset
62 sprintf(buf, "%.1f", p1);
7de246feba5f Updated esp-idf. The first two global screen functions created.
Michiel Broek <mbroek@mbse.eu>
parents: 11
diff changeset
63 w = u8g2_GetUTF8Width(&u8g2, buf);
7de246feba5f Updated esp-idf. The first two global screen functions created.
Michiel Broek <mbroek@mbse.eu>
parents: 11
diff changeset
64 u8g2_DrawUTF8(&u8g2, ((42 - w) / 2),63, buf);
7de246feba5f Updated esp-idf. The first two global screen functions created.
Michiel Broek <mbroek@mbse.eu>
parents: 11
diff changeset
65
7de246feba5f Updated esp-idf. The first two global screen functions created.
Michiel Broek <mbroek@mbse.eu>
parents: 11
diff changeset
66 sprintf(buf, "%.1f", p2);
7de246feba5f Updated esp-idf. The first two global screen functions created.
Michiel Broek <mbroek@mbse.eu>
parents: 11
diff changeset
67 w = u8g2_GetUTF8Width(&u8g2, buf);
7de246feba5f Updated esp-idf. The first two global screen functions created.
Michiel Broek <mbroek@mbse.eu>
parents: 11
diff changeset
68 u8g2_DrawUTF8(&u8g2, ((42 - w) / 2) + 43,63, buf);
7de246feba5f Updated esp-idf. The first two global screen functions created.
Michiel Broek <mbroek@mbse.eu>
parents: 11
diff changeset
69
7de246feba5f Updated esp-idf. The first two global screen functions created.
Michiel Broek <mbroek@mbse.eu>
parents: 11
diff changeset
70 sprintf(buf, "%.1f", p3);
7de246feba5f Updated esp-idf. The first two global screen functions created.
Michiel Broek <mbroek@mbse.eu>
parents: 11
diff changeset
71 w = u8g2_GetUTF8Width(&u8g2, buf);
7de246feba5f Updated esp-idf. The first two global screen functions created.
Michiel Broek <mbroek@mbse.eu>
parents: 11
diff changeset
72 u8g2_DrawUTF8(&u8g2, ((42 - w) / 2) + 86,63, buf);
7de246feba5f Updated esp-idf. The first two global screen functions created.
Michiel Broek <mbroek@mbse.eu>
parents: 11
diff changeset
73
7de246feba5f Updated esp-idf. The first two global screen functions created.
Michiel Broek <mbroek@mbse.eu>
parents: 11
diff changeset
74 u8g2_SendBuffer(&u8g2);
7de246feba5f Updated esp-idf. The first two global screen functions created.
Michiel Broek <mbroek@mbse.eu>
parents: 11
diff changeset
75 u8g2_SetPowerSave(&u8g2, 0); // wake up display
7de246feba5f Updated esp-idf. The first two global screen functions created.
Michiel Broek <mbroek@mbse.eu>
parents: 11
diff changeset
76 }
7de246feba5f Updated esp-idf. The first two global screen functions created.
Michiel Broek <mbroek@mbse.eu>
parents: 11
diff changeset
77
7de246feba5f Updated esp-idf. The first two global screen functions created.
Michiel Broek <mbroek@mbse.eu>
parents: 11
diff changeset
78
7de246feba5f Updated esp-idf. The first two global screen functions created.
Michiel Broek <mbroek@mbse.eu>
parents: 11
diff changeset
79
7de246feba5f Updated esp-idf. The first two global screen functions created.
Michiel Broek <mbroek@mbse.eu>
parents: 11
diff changeset
80 void screen_fatal(char *e1, char *e2)
7de246feba5f Updated esp-idf. The first two global screen functions created.
Michiel Broek <mbroek@mbse.eu>
parents: 11
diff changeset
81 {
7de246feba5f Updated esp-idf. The first two global screen functions created.
Michiel Broek <mbroek@mbse.eu>
parents: 11
diff changeset
82 u8g2_SetFont(&u8g2, u8g2_font_t0_15_tr);
7de246feba5f Updated esp-idf. The first two global screen functions created.
Michiel Broek <mbroek@mbse.eu>
parents: 11
diff changeset
83 u8g2_DrawStr(&u8g2,2,12,e1);
7de246feba5f Updated esp-idf. The first two global screen functions created.
Michiel Broek <mbroek@mbse.eu>
parents: 11
diff changeset
84 u8g2_DrawStr(&u8g2,2,24,e2);
7de246feba5f Updated esp-idf. The first two global screen functions created.
Michiel Broek <mbroek@mbse.eu>
parents: 11
diff changeset
85 u8g2_SendBuffer(&u8g2);
7de246feba5f Updated esp-idf. The first two global screen functions created.
Michiel Broek <mbroek@mbse.eu>
parents: 11
diff changeset
86 u8g2_SetPowerSave(&u8g2, 0);
7de246feba5f Updated esp-idf. The first two global screen functions created.
Michiel Broek <mbroek@mbse.eu>
parents: 11
diff changeset
87
7de246feba5f Updated esp-idf. The first two global screen functions created.
Michiel Broek <mbroek@mbse.eu>
parents: 11
diff changeset
88 }
7de246feba5f Updated esp-idf. The first two global screen functions created.
Michiel Broek <mbroek@mbse.eu>
parents: 11
diff changeset
89
7de246feba5f Updated esp-idf. The first two global screen functions created.
Michiel Broek <mbroek@mbse.eu>
parents: 11
diff changeset
90
7de246feba5f Updated esp-idf. The first two global screen functions created.
Michiel Broek <mbroek@mbse.eu>
parents: 11
diff changeset
91
7de246feba5f Updated esp-idf. The first two global screen functions created.
Michiel Broek <mbroek@mbse.eu>
parents: 11
diff changeset
92
0
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
93 void app_main()
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
94 {
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
95 struct timeval now;
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
96 gettimeofday(&now, NULL);
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
97 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
98 int New_Loop2 = MAIN_LOOP2_INIT;
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
99 esp_err_t ret;
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 Main_Loop1 = MAIN_LOOP1_INIT;
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
102 Main_Loop2 = -1;
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 switch (esp_sleep_get_wakeup_cause()) {
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
105 case ESP_SLEEP_WAKEUP_EXT1: {
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
106 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
107 if (wakeup_pin_mask != 0) {
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
108 int pin = __builtin_ffsll(wakeup_pin_mask) - 1;
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
109 printf("Wake up from GPIO %d\n", pin);
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 printf("Wake up from GPIO\n");
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 break;
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 case ESP_SLEEP_WAKEUP_TIMER: {
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
116 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
117 break;
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 case ESP_SLEEP_WAKEUP_UNDEFINED:
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
120 default:
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
121 ESP_LOGI(TAG, "Starting from hard reset");
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
122 }
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
123
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
124 const int wakeup_time_sec = 55;
0
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
125 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
126 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
127
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
128 // 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
129 // 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
130
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
131 // 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
132 // 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
133
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
134 // 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
135 // 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
136 // to minimize current consumption.
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
137 // rtc_gpio_isolate(GPIO_NUM_12);
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 app_desc = esp_ota_get_app_description();
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
140
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 * Initialize NVS
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
143 */
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
144 ret = nvs_flash_init();
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
145 if (ret == ESP_ERR_NVS_NO_FREE_PAGES) {
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
146 ESP_ERROR_CHECK(nvs_flash_erase());
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
147 ret = nvs_flash_init();
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
148 }
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
149 ESP_ERROR_CHECK(ret);
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 /*
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
152 * Setup the OLED display.
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
153 * 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
154 */
6
06a5028dbcdf Hookup OLED I2C display to temorary display some data.
Michiel Broek <mbroek@mbse.eu>
parents: 4
diff changeset
155 u8g2_esp32_hal_t u8g2_esp32_hal = U8G2_ESP32_HAL_DEFAULT;
06a5028dbcdf Hookup OLED I2C display to temorary display some data.
Michiel Broek <mbroek@mbse.eu>
parents: 4
diff changeset
156 u8g2_esp32_hal.sda = PIN_SDA;
06a5028dbcdf Hookup OLED I2C display to temorary display some data.
Michiel Broek <mbroek@mbse.eu>
parents: 4
diff changeset
157 u8g2_esp32_hal.scl = PIN_SCL;
06a5028dbcdf Hookup OLED I2C display to temorary display some data.
Michiel Broek <mbroek@mbse.eu>
parents: 4
diff changeset
158 u8g2_esp32_hal_init(u8g2_esp32_hal);
0
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
159
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
160 u8g2_Setup_sh1106_i2c_128x64_noname_f(&u8g2, U8G2_R0, u8g2_esp32_i2c_byte_cb, u8g2_esp32_gpio_and_delay_cb); // init u8g2 structure
6
06a5028dbcdf Hookup OLED I2C display to temorary display some data.
Michiel Broek <mbroek@mbse.eu>
parents: 4
diff changeset
161 u8x8_SetI2CAddress(&u8g2.u8x8, 0x78);
06a5028dbcdf Hookup OLED I2C display to temorary display some data.
Michiel Broek <mbroek@mbse.eu>
parents: 4
diff changeset
162 ESP_LOGI(TAG, "u8g2_InitDisplay");
06a5028dbcdf Hookup OLED I2C display to temorary display some data.
Michiel Broek <mbroek@mbse.eu>
parents: 4
diff changeset
163 u8g2_InitDisplay(&u8g2); // send init sequence to the display, display is in sleep mode after this,
0
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
164
6
06a5028dbcdf Hookup OLED I2C display to temorary display some data.
Michiel Broek <mbroek@mbse.eu>
parents: 4
diff changeset
165 /*
06a5028dbcdf Hookup OLED I2C display to temorary display some data.
Michiel Broek <mbroek@mbse.eu>
parents: 4
diff changeset
166 * Setup SPIFFS filesystem
06a5028dbcdf Hookup OLED I2C display to temorary display some data.
Michiel Broek <mbroek@mbse.eu>
parents: 4
diff changeset
167 */
0
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
168 ESP_LOGI(TAG, "Initializing SPIFFS");
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
169 esp_vfs_spiffs_conf_t conf = {
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
170 .base_path = "/spiffs",
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
171 .partition_label = NULL,
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
172 .max_files = 5,
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
173 .format_if_mount_failed = true
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
6
06a5028dbcdf Hookup OLED I2C display to temorary display some data.
Michiel Broek <mbroek@mbse.eu>
parents: 4
diff changeset
176 /*
06a5028dbcdf Hookup OLED I2C display to temorary display some data.
Michiel Broek <mbroek@mbse.eu>
parents: 4
diff changeset
177 * 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
178 * 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
179 */
0
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
180 ret = esp_vfs_spiffs_register(&conf);
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
181
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
182 if (ret != ESP_OK) {
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
183 if (ret == ESP_FAIL) {
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
184 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
185 } else if (ret == ESP_ERR_NOT_FOUND) {
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
186 ESP_LOGE(TAG, "Failed to find SPIFFS partition");
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
187 } else {
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
188 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
189 }
13
7de246feba5f Updated esp-idf. The first two global screen functions created.
Michiel Broek <mbroek@mbse.eu>
parents: 11
diff changeset
190 screen_fatal("SPIFFS:", "init error");
0
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
191 return; // Stop application.
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
192 }
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
193
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
194 size_t total = 0, used = 0;
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
195 ret = esp_spiffs_info(NULL, &total, &used);
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
196 if (ret != ESP_OK) {
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
197 ESP_LOGE(TAG, "Failed to get SPIFFS partition information");
13
7de246feba5f Updated esp-idf. The first two global screen functions created.
Michiel Broek <mbroek@mbse.eu>
parents: 11
diff changeset
198 screen_fatal("SPIFFS:", "partition error");
0
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
199 return; // Stop application.
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
200 } else {
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
201 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
202 }
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 // 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
205 #if 0
0
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
206 DIR *dir = opendir("/spiffs");
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
207 struct dirent* de = readdir(dir);
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
208 while (de) {
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
209 if (de->d_type == DT_REG) {
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
210 printf("F ");
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
211 }
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
212 if (de->d_type == DT_DIR) {
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
213 printf("D ");
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
214 }
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
215 printf("%s\n", de->d_name);
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
216 de = readdir(dir);
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
217 }
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
218 closedir(dir);
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
219 #endif
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
220
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
221 /*
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
222 * Read or create configuration
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
223 */
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
224 read_config();
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
225 read_units();
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
226
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
227 //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
228 //remove_station((uint8_t *)"MBSE_WLP");
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
229
6
06a5028dbcdf Hookup OLED I2C display to temorary display some data.
Michiel Broek <mbroek@mbse.eu>
parents: 4
diff changeset
230 /*
06a5028dbcdf Hookup OLED I2C display to temorary display some data.
Michiel Broek <mbroek@mbse.eu>
parents: 4
diff changeset
231 * Create FreeRTOS tasks
06a5028dbcdf Hookup OLED I2C display to temorary display some data.
Michiel Broek <mbroek@mbse.eu>
parents: 4
diff changeset
232 */
0
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
233 xSemaphoreDS18B20 = xSemaphoreCreateMutex();
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
234 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
235 xSemaphoreUnits = xSemaphoreCreateMutex();
0
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
236
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
237 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
238 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
239 esp_log_level_set("wifi", ESP_LOG_ERROR);
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
240 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
241 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
242 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
243 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
244
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
245 /*
6eafc4c2bf3d Sleep time is now 55 seconds. Code cleanup. Decrease MQTT library logging to Error.
Michiel Broek <mbroek@mbse.eu>
parents: 6
diff changeset
246 * Setup the Rotary Encoder.
6eafc4c2bf3d Sleep time is now 55 seconds. Code cleanup. Decrease MQTT library logging to Error.
Michiel Broek <mbroek@mbse.eu>
parents: 6
diff changeset
247 * esp32-rotary-encoder requires that the GPIO ISR service is
6eafc4c2bf3d Sleep time is now 55 seconds. Code cleanup. Decrease MQTT library logging to Error.
Michiel Broek <mbroek@mbse.eu>
parents: 6
diff changeset
248 * installed before calling rotary_encoder_register()
6eafc4c2bf3d Sleep time is now 55 seconds. Code cleanup. Decrease MQTT library logging to Error.
Michiel Broek <mbroek@mbse.eu>
parents: 6
diff changeset
249 */
6eafc4c2bf3d Sleep time is now 55 seconds. Code cleanup. Decrease MQTT library logging to Error.
Michiel Broek <mbroek@mbse.eu>
parents: 6
diff changeset
250 ESP_ERROR_CHECK(gpio_install_isr_service(0));
6eafc4c2bf3d Sleep time is now 55 seconds. Code cleanup. Decrease MQTT library logging to Error.
Michiel Broek <mbroek@mbse.eu>
parents: 6
diff changeset
251 rotary_encoder_info_t rinfo = { 0 };
6eafc4c2bf3d Sleep time is now 55 seconds. Code cleanup. Decrease MQTT library logging to Error.
Michiel Broek <mbroek@mbse.eu>
parents: 6
diff changeset
252 ESP_ERROR_CHECK(rotary_encoder_init(&rinfo, ROT_ENC_A_GPIO, ROT_ENC_B_GPIO));
6eafc4c2bf3d Sleep time is now 55 seconds. Code cleanup. Decrease MQTT library logging to Error.
Michiel Broek <mbroek@mbse.eu>
parents: 6
diff changeset
253 // ESP_ERROR_CHECK(rotary_encoder_enable_half_steps(&rinfo, ENABLE_HALF_STEPS));
0
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
254 #ifdef FLIP_DIRECTION
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
255 // ESP_ERROR_CHECK(rotary_encoder_flip_direction(&rinfo));
0
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
256 #endif
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
257
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
258 // 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
259 // 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
260 // QueueHandle_t event_queue = rotary_encoder_create_queue();
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
261 // ESP_ERROR_CHECK(rotary_encoder_set_queue(&rinfo, event_queue));
0
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
262
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
263
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
264 /*
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
265 * Main application loop.
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
266 */
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
267 while (1) {
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
268
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
269 ESP_LOGI(TAG, "Entered app loop");
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 /* Measure process or user input via rotary switch */
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
272 while (1) {
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
273 switch (Main_Loop1) {
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
274 case MAIN_LOOP1_INIT:
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
275 ESP_LOGI(TAG, "Loop timer: Init");
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
276 // If configured do MAIN_LOOP1_CONNECT
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
277 Main_Loop1 = MAIN_LOOP1_CONNECT;
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
278 requestWiFi_system(true);
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
279 request_ds18b20();
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
280 request_adc();
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
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
283 case MAIN_LOOP1_CONNECT:
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
284 if (ready_WiFi())
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
285 Main_Loop1 = MAIN_LOOP1_MQTT_CONNECT;
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
286 break;
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
287
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
288 case MAIN_LOOP1_MQTT_CONNECT:
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
289 if (ready_ds18b20() && ready_adc()) {
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
290 connect_mqtt(true);
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
291 Main_Loop1 = MAIN_LOOP1_WAITCON;
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
292 ESP_LOGI(TAG, "Loop timer: Wait MQTT");
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 /* Get global temperature, use for all units. */
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
295 uint32_t temp = 0;
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
296 int state = 0;
2
c0184362d48c Prepare ds18b20 sensors for multiple sensors on the onewire bus.
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
297 char rom_code[17];
13
7de246feba5f Updated esp-idf. The first two global screen functions created.
Michiel Broek <mbroek@mbse.eu>
parents: 11
diff changeset
298 float t = 0, p1, p2, p3;
0
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
299 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
300 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
301 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
302 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
303 rom_code[16] = '\0';
0
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
304 xSemaphoreGive(xSemaphoreDS18B20);
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
305 }
13
7de246feba5f Updated esp-idf. The first two global screen functions created.
Michiel Broek <mbroek@mbse.eu>
parents: 11
diff changeset
306 t = temp / 1000.0;
6
06a5028dbcdf Hookup OLED I2C display to temorary display some data.
Michiel Broek <mbroek@mbse.eu>
parents: 4
diff changeset
307
0
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
308 /* 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
309 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
310 for (int i = 0; i < 3; i++) {
8
c6bbd1380f22 Added alarm flag for units.
Michiel Broek <mbroek@mbse.eu>
parents: 7
diff changeset
311 if (i == 0)
c6bbd1380f22 Added alarm flag for units.
Michiel Broek <mbroek@mbse.eu>
parents: 7
diff changeset
312 units[i].mode = 1;
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
313 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
314 units[i].temperature_state = state;
8
c6bbd1380f22 Added alarm flag for units.
Michiel Broek <mbroek@mbse.eu>
parents: 7
diff changeset
315 units[i].alarm = 0;
c6bbd1380f22 Added alarm flag for units.
Michiel Broek <mbroek@mbse.eu>
parents: 7
diff changeset
316 if (state)
c6bbd1380f22 Added alarm flag for units.
Michiel Broek <mbroek@mbse.eu>
parents: 7
diff changeset
317 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
318 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
319 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
320 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
321 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
322 units[i].pressure_voltage = adc_state->Pressure[i].voltage;
2a57c466bf45 FreeRTOS scheduler from 100 Hz to 200 Hz. All units data is now protected with a semaphore.
Michiel Broek <mbroek@mbse.eu>
parents: 2
diff changeset
323 units[i].pressure_zero = 110;
8
c6bbd1380f22 Added alarm flag for units.
Michiel Broek <mbroek@mbse.eu>
parents: 7
diff changeset
324 if (units[i].pressure_state || units[i].pressure_voltage < 80)
c6bbd1380f22 Added alarm flag for units.
Michiel Broek <mbroek@mbse.eu>
parents: 7
diff changeset
325 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
326 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
327 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
328 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
329 units[i].pressure = P;
13
7de246feba5f Updated esp-idf. The first two global screen functions created.
Michiel Broek <mbroek@mbse.eu>
parents: 11
diff changeset
330 if (i == 0)
7de246feba5f Updated esp-idf. The first two global screen functions created.
Michiel Broek <mbroek@mbse.eu>
parents: 11
diff changeset
331 p1 = P / 1000.0;
7de246feba5f Updated esp-idf. The first two global screen functions created.
Michiel Broek <mbroek@mbse.eu>
parents: 11
diff changeset
332 else if (i == 1)
7de246feba5f Updated esp-idf. The first two global screen functions created.
Michiel Broek <mbroek@mbse.eu>
parents: 11
diff changeset
333 p2 = P / 1000.0;
7de246feba5f Updated esp-idf. The first two global screen functions created.
Michiel Broek <mbroek@mbse.eu>
parents: 11
diff changeset
334 else if (i == 2)
7de246feba5f Updated esp-idf. The first two global screen functions created.
Michiel Broek <mbroek@mbse.eu>
parents: 11
diff changeset
335 p3 = P / 1000.0;
0
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
336 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
337 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
338 // Moet die echt op 5 volt?
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
339 // Verbruik 10 mA
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
340 // 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
341 xSemaphoreGive(xSemaphoreADC);
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
342 }
0
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
343 }
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
344 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
345 xSemaphoreGive(xSemaphoreUnits);
13
7de246feba5f Updated esp-idf. The first two global screen functions created.
Michiel Broek <mbroek@mbse.eu>
parents: 11
diff changeset
346 screen_main(t, p1, p2, p3);
0
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
347 }
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
348 }
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
349 break;
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
350
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
351 case MAIN_LOOP1_WAITCON:
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
352 if (ready_mqtt())
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
353 Main_Loop1 = MAIN_LOOP1_SEND;
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
354 break;
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
355
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
356 case MAIN_LOOP1_SEND:
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
357 ESP_LOGI(TAG, "Loop timer: Send MQTT");
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
358 publishNode();
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
359 publishUnits();
9
a85995941d0d Added publish logs. Another fix for DS18B20 rom address copy.
Michiel Broek <mbroek@mbse.eu>
parents: 8
diff changeset
360 publishLogs();
11
e33f2d325d15 Experimental mqtt published messages state counter
Michiel Broek <mbroek@mbse.eu>
parents: 9
diff changeset
361 Main_Loop1 = MAIN_LOOP1_WAITACK;
0
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
362 break;
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 case MAIN_LOOP1_WAITACK:
11
e33f2d325d15 Experimental mqtt published messages state counter
Michiel Broek <mbroek@mbse.eu>
parents: 9
diff changeset
365 if (count_pub == 0) // Wait until all published messages are sent.
e33f2d325d15 Experimental mqtt published messages state counter
Michiel Broek <mbroek@mbse.eu>
parents: 9
diff changeset
366 Main_Loop1 = MAIN_LOOP1_MQTT_DISCONNECT;
0
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
367 break;
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
368
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
369 case MAIN_LOOP1_MQTT_DISCONNECT:
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
370 ESP_LOGI(TAG, "Loop timer: Disconnect MQTT");
11
e33f2d325d15 Experimental mqtt published messages state counter
Michiel Broek <mbroek@mbse.eu>
parents: 9
diff changeset
371 connect_mqtt(false); // Doesn't really disconnect.
0
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
372 Main_Loop1 = MAIN_LOOP1_DISCONNECT;
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_LOOP1_DISCONNECT:
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
376 if (! ready_mqtt()) {
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
377 ESP_LOGI(TAG, "Loop timer: WiFi off");
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
378 requestWiFi_system(false);
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
379 Main_Loop1 = MAIN_LOOP1_WIFI_OFF;
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
380 }
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 case MAIN_LOOP1_WIFI_OFF:
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
384 if (! ready_WiFi()) {
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
385 ESP_LOGI(TAG, "Loop timer: Done");
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
386 Main_Loop1 = MAIN_LOOP1_DONE;
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 break;
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
389
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
390 case MAIN_LOOP1_DONE:
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
391 break;
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
392 }
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
393
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
394 /*
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
395 * One time actions
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
396 */
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
397 if (New_Loop2 != Main_Loop2) {
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
398
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
399 Main_Loop2 = New_Loop2;
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
400
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
401 switch (Main_Loop2) {
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
402 case MAIN_LOOP2_INIT:
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
403 ESP_LOGI(TAG, "Loop user: Init");
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
404 // u8g2_SetPowerSave(&u8g2, 0); // wake up display
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
405 // u8g2_ClearBuffer(&u8g2);
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
406 // New_Loop2 = MAIN_LOOP2_INACTIVE;
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
407 New_Loop2 = MAIN_LOOP2_DONE;
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
408 break;
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
409
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
410 case MAIN_LOOP2_INACTIVE:
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
411 // u8g2_SetPowerSave(&u8g2, 1); // powersave display
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
412 New_Loop2 = MAIN_LOOP2_DONE;
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
413 break;
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
414
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
415 default:
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
416 break;
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
417 }
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 * Action process.
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
422 */
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
423 switch (Main_Loop2) {
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
424 // If wakeup from GPIO -- state machine 2
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
425 // Init OLED
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
426 // If not configured, start configure
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
427 // If configured select first unit
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
428 // New rotate position, set screen, reset waittimer
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
429 // Handle screen (first is show measured values)
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
430 // Count inactivity
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
431 // flag if inactive and OLED lowpower.
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
432
14
deaca7606e23 Added some menu ideas
Michiel Broek <mbroek@mbse.eu>
parents: 13
diff changeset
433 // Generic display all units at once. Press is xxx? Bold/Italic is selected.
deaca7606e23 Added some menu ideas
Michiel Broek <mbroek@mbse.eu>
parents: 13
diff changeset
434 // Display per unit. Temp + Pressure + state. Press is setup this sensor.
deaca7606e23 Added some menu ideas
Michiel Broek <mbroek@mbse.eu>
parents: 13
diff changeset
435 // Setup menu: Sensors
deaca7606e23 Added some menu ideas
Michiel Broek <mbroek@mbse.eu>
parents: 13
diff changeset
436 // WiFi
deaca7606e23 Added some menu ideas
Michiel Broek <mbroek@mbse.eu>
parents: 13
diff changeset
437 // MQTT
deaca7606e23 Added some menu ideas
Michiel Broek <mbroek@mbse.eu>
parents: 13
diff changeset
438 // System (timers etc)
deaca7606e23 Added some menu ideas
Michiel Broek <mbroek@mbse.eu>
parents: 13
diff changeset
439 // Update OTA
deaca7606e23 Added some menu ideas
Michiel Broek <mbroek@mbse.eu>
parents: 13
diff changeset
440 // Return
deaca7606e23 Added some menu ideas
Michiel Broek <mbroek@mbse.eu>
parents: 13
diff changeset
441
deaca7606e23 Added some menu ideas
Michiel Broek <mbroek@mbse.eu>
parents: 13
diff changeset
442 // Sensors menu: Assignments, turn to choose one.
deaca7606e23 Added some menu ideas
Michiel Broek <mbroek@mbse.eu>
parents: 13
diff changeset
443 // Sensors setup menu: DS18B20 addr Press is assign
deaca7606e23 Added some menu ideas
Michiel Broek <mbroek@mbse.eu>
parents: 13
diff changeset
444 // DS18B20 addr
deaca7606e23 Added some menu ideas
Michiel Broek <mbroek@mbse.eu>
parents: 13
diff changeset
445
0
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
446 // Break if all done and inactive.
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
447 default:
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
448 break;
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
449 }
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
450
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
451 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
452 break;
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
453
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
454 vTaskDelay(10 / portTICK_PERIOD_MS);
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
455 }
6
06a5028dbcdf Hookup OLED I2C display to temorary display some data.
Michiel Broek <mbroek@mbse.eu>
parents: 4
diff changeset
456
06a5028dbcdf Hookup OLED I2C display to temorary display some data.
Michiel Broek <mbroek@mbse.eu>
parents: 4
diff changeset
457 // u8g2_ClearBuffer(&u8g2);
06a5028dbcdf Hookup OLED I2C display to temorary display some data.
Michiel Broek <mbroek@mbse.eu>
parents: 4
diff changeset
458 // u8g2_SendBuffer(&u8g2);
06a5028dbcdf Hookup OLED I2C display to temorary display some data.
Michiel Broek <mbroek@mbse.eu>
parents: 4
diff changeset
459 // u8g2_SetPowerSave(&u8g2, 1);
06a5028dbcdf Hookup OLED I2C display to temorary display some data.
Michiel Broek <mbroek@mbse.eu>
parents: 4
diff changeset
460
0
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
461 // printf("Simulate deep sleep\n");
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
462 // vTaskDelay(1000 * wakeup_time_sec / portTICK_PERIOD_MS);
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
463
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
464 printf("Entering deep sleep\n");
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
465 gettimeofday(&sleep_enter_time, NULL);
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
466 esp_deep_sleep_start();
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
467
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
468 Main_Loop1 = MAIN_LOOP1_INIT;
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
469 New_Loop2 = MAIN_LOOP2_INIT;
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
470 }
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
471
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
472 }
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
473

mercurial