main/co2meter.c

changeset 13
7de246feba5f
parent 11
e33f2d325d15
child 14
deaca7606e23
equal deleted inserted replaced
12:7dc9003f86a8 13:7de246feba5f
28 static TaskHandle_t xTaskDS18B20 = NULL; 28 static TaskHandle_t xTaskDS18B20 = NULL;
29 static TaskHandle_t xTaskADC = NULL; 29 static TaskHandle_t xTaskADC = NULL;
30 static TaskHandle_t xTaskWifi = NULL; 30 static TaskHandle_t xTaskWifi = NULL;
31 static TaskHandle_t xTaskMQTT = NULL; 31 static TaskHandle_t xTaskMQTT = NULL;
32 const esp_app_desc_t *app_desc = NULL; 32 const esp_app_desc_t *app_desc = NULL;
33 u8g2_t u8g2; ///< A structure which will contain all the data for one display
33 34
34 35
35 extern unit_t units[3]; ///< Pressure test units 36 extern unit_t units[3]; ///< Pressure test units
36 extern SemaphoreHandle_t xSemaphoreUnits; ///< Units lock semaphore 37 extern SemaphoreHandle_t xSemaphoreUnits; ///< Units lock semaphore
37 extern DS18B20_State *ds18b20_state; ///< DS18B20 state 38 extern DS18B20_State *ds18b20_state; ///< DS18B20 state
39 extern ADC_State *adc_state; ///< ADC state 40 extern ADC_State *adc_state; ///< ADC state
40 extern SemaphoreHandle_t xSemaphoreADC; ///< ADC lock semaphore 41 extern SemaphoreHandle_t xSemaphoreADC; ///< ADC lock semaphore
41 extern int count_pub; 42 extern int count_pub;
42 43
43 44
45 void screen_main(float t, float p1, float p2, float p3)
46 {
47 char buf[65];
48
49 u8g2_ClearBuffer(&u8g2);
50 u8g2_DrawHLine(&u8g2, 0, 14, 128);
51 u8g2_SetFont(&u8g2, u8g2_font_t0_15_tr);
52 sprintf(buf, "CO2 meter %s", app_desc->version);
53 u8g2_uint_t w = u8g2_GetStrWidth(&u8g2, buf);
54 u8g2_DrawStr(&u8g2, (128 - w) / 2,12, buf);
55
56 u8g2_SetFont(&u8g2, u8g2_font_t0_22b_tf);
57 sprintf(buf, "%.1f °C", t);
58 w = u8g2_GetUTF8Width(&u8g2, buf);
59 u8g2_DrawUTF8(&u8g2, (128 - w) / 2,40, buf);
60 u8g2_SetFont(&u8g2, u8g2_font_t0_18b_tf);
61
62 sprintf(buf, "%.1f", p1);
63 w = u8g2_GetUTF8Width(&u8g2, buf);
64 u8g2_DrawUTF8(&u8g2, ((42 - w) / 2),63, buf);
65
66 sprintf(buf, "%.1f", p2);
67 w = u8g2_GetUTF8Width(&u8g2, buf);
68 u8g2_DrawUTF8(&u8g2, ((42 - w) / 2) + 43,63, buf);
69
70 sprintf(buf, "%.1f", p3);
71 w = u8g2_GetUTF8Width(&u8g2, buf);
72 u8g2_DrawUTF8(&u8g2, ((42 - w) / 2) + 86,63, buf);
73
74 u8g2_SendBuffer(&u8g2);
75 u8g2_SetPowerSave(&u8g2, 0); // wake up display
76 }
77
78
79
80 void screen_fatal(char *e1, char *e2)
81 {
82 u8g2_SetFont(&u8g2, u8g2_font_t0_15_tr);
83 u8g2_DrawStr(&u8g2,2,12,e1);
84 u8g2_DrawStr(&u8g2,2,24,e2);
85 u8g2_SendBuffer(&u8g2);
86 u8g2_SetPowerSave(&u8g2, 0);
87
88 }
89
90
91
92
44 void app_main() 93 void app_main()
45 { 94 {
46 struct timeval now; 95 struct timeval now;
47 gettimeofday(&now, NULL); 96 gettimeofday(&now, NULL);
48 int sleep_time_ms = (now.tv_sec - sleep_enter_time.tv_sec) * 1000 + (now.tv_usec - sleep_enter_time.tv_usec) / 1000; 97 int sleep_time_ms = (now.tv_sec - sleep_enter_time.tv_sec) * 1000 + (now.tv_usec - sleep_enter_time.tv_usec) / 1000;
49 int New_Loop2 = MAIN_LOOP2_INIT; 98 int New_Loop2 = MAIN_LOOP2_INIT;
50 char buf[65];
51 esp_err_t ret; 99 esp_err_t ret;
52 100
53 Main_Loop1 = MAIN_LOOP1_INIT; 101 Main_Loop1 = MAIN_LOOP1_INIT;
54 Main_Loop2 = -1; 102 Main_Loop2 = -1;
55 103
107 u8g2_esp32_hal_t u8g2_esp32_hal = U8G2_ESP32_HAL_DEFAULT; 155 u8g2_esp32_hal_t u8g2_esp32_hal = U8G2_ESP32_HAL_DEFAULT;
108 u8g2_esp32_hal.sda = PIN_SDA; 156 u8g2_esp32_hal.sda = PIN_SDA;
109 u8g2_esp32_hal.scl = PIN_SCL; 157 u8g2_esp32_hal.scl = PIN_SCL;
110 u8g2_esp32_hal_init(u8g2_esp32_hal); 158 u8g2_esp32_hal_init(u8g2_esp32_hal);
111 159
112 u8g2_t u8g2; // a structure which will contain all the data for one display
113 u8g2_Setup_sh1106_i2c_128x64_noname_f(&u8g2, U8G2_R0, u8g2_esp32_i2c_byte_cb, u8g2_esp32_gpio_and_delay_cb); // init u8g2 structure 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
114 u8x8_SetI2CAddress(&u8g2.u8x8, 0x78); 161 u8x8_SetI2CAddress(&u8g2.u8x8, 0x78);
115 ESP_LOGI(TAG, "u8g2_InitDisplay"); 162 ESP_LOGI(TAG, "u8g2_InitDisplay");
116 u8g2_InitDisplay(&u8g2); // send init sequence to the display, display is in sleep mode after this, 163 u8g2_InitDisplay(&u8g2); // send init sequence to the display, display is in sleep mode after this,
117 164
138 } else if (ret == ESP_ERR_NOT_FOUND) { 185 } else if (ret == ESP_ERR_NOT_FOUND) {
139 ESP_LOGE(TAG, "Failed to find SPIFFS partition"); 186 ESP_LOGE(TAG, "Failed to find SPIFFS partition");
140 } else { 187 } else {
141 ESP_LOGE(TAG, "Failed to initialize SPIFFS (%d)", ret); 188 ESP_LOGE(TAG, "Failed to initialize SPIFFS (%d)", ret);
142 } 189 }
143 u8g2_SetFont(&u8g2, u8g2_font_t0_15_tr); 190 screen_fatal("SPIFFS:", "init error");
144 u8g2_DrawStr(&u8g2,2,12,"SPIFFS:");
145 u8g2_DrawStr(&u8g2,2,24,"init error");
146 u8g2_SendBuffer(&u8g2);
147 u8g2_SetPowerSave(&u8g2, 0);
148 return; // Stop application. 191 return; // Stop application.
149 } 192 }
150 193
151 size_t total = 0, used = 0; 194 size_t total = 0, used = 0;
152 ret = esp_spiffs_info(NULL, &total, &used); 195 ret = esp_spiffs_info(NULL, &total, &used);
153 if (ret != ESP_OK) { 196 if (ret != ESP_OK) {
154 ESP_LOGE(TAG, "Failed to get SPIFFS partition information"); 197 ESP_LOGE(TAG, "Failed to get SPIFFS partition information");
155 u8g2_SetFont(&u8g2, u8g2_font_t0_15_tr); 198 screen_fatal("SPIFFS:", "partition error");
156 u8g2_DrawStr(&u8g2,2,12,"SPIFFS:");
157 u8g2_DrawStr(&u8g2,2,24,"partition error");
158 u8g2_SendBuffer(&u8g2);
159 u8g2_SetPowerSave(&u8g2, 0);
160 return; // Stop application. 199 return; // Stop application.
161 } else { 200 } else {
162 ESP_LOGI(TAG, "Partition size: %d, used: %d - %d%%", total, used, (used * 100) / total); 201 ESP_LOGI(TAG, "Partition size: %d, used: %d - %d%%", total, used, (used * 100) / total);
163 } 202 }
164 203
237 // If configured do MAIN_LOOP1_CONNECT 276 // If configured do MAIN_LOOP1_CONNECT
238 Main_Loop1 = MAIN_LOOP1_CONNECT; 277 Main_Loop1 = MAIN_LOOP1_CONNECT;
239 requestWiFi_system(true); 278 requestWiFi_system(true);
240 request_ds18b20(); 279 request_ds18b20();
241 request_adc(); 280 request_adc();
242
243 u8g2_ClearBuffer(&u8g2);
244 u8g2_DrawHLine(&u8g2, 0, 14, 128);
245 u8g2_SetFont(&u8g2, u8g2_font_t0_15_tr);
246 sprintf(buf, "CO2 meter %s", app_desc->version);
247 u8g2_uint_t w = u8g2_GetStrWidth(&u8g2, buf);
248 u8g2_DrawStr(&u8g2, (128 - w) / 2,12, buf);
249
250 break; 281 break;
251 282
252 case MAIN_LOOP1_CONNECT: 283 case MAIN_LOOP1_CONNECT:
253 if (ready_WiFi()) 284 if (ready_WiFi())
254 Main_Loop1 = MAIN_LOOP1_MQTT_CONNECT; 285 Main_Loop1 = MAIN_LOOP1_MQTT_CONNECT;
262 293
263 /* Get global temperature, use for all units. */ 294 /* Get global temperature, use for all units. */
264 uint32_t temp = 0; 295 uint32_t temp = 0;
265 int state = 0; 296 int state = 0;
266 char rom_code[17]; 297 char rom_code[17];
298 float t = 0, p1, p2, p3;
267 if (xSemaphoreTake(xSemaphoreDS18B20, 10) == pdTRUE) { 299 if (xSemaphoreTake(xSemaphoreDS18B20, 10) == pdTRUE) {
268 temp = (ds18b20_state->sensor[0].temperature * 1000); 300 temp = (ds18b20_state->sensor[0].temperature * 1000);
269 state = (ds18b20_state->sensor[0].error == 0) ? 0:1; 301 state = (ds18b20_state->sensor[0].error == 0) ? 0:1;
270 strncpy(rom_code, ds18b20_state->sensor[0].rom_code, 17); 302 strncpy(rom_code, ds18b20_state->sensor[0].rom_code, 17);
271 rom_code[16] = '\0'; 303 rom_code[16] = '\0';
272 xSemaphoreGive(xSemaphoreDS18B20); 304 xSemaphoreGive(xSemaphoreDS18B20);
273 } 305 }
274 306 t = temp / 1000.0;
275 u8g2_SetFont(&u8g2, u8g2_font_t0_22b_tf);
276 sprintf(buf, "%.1f °C", temp / 1000.0);
277 u8g2_uint_t w = u8g2_GetUTF8Width(&u8g2, buf);
278 u8g2_DrawUTF8(&u8g2, (128 - w) / 2,40, buf);
279 u8g2_SetFont(&u8g2, u8g2_font_t0_18b_tf);
280 307
281 /* Copy measured data and calculate results */ 308 /* Copy measured data and calculate results */
282 if (xSemaphoreTake(xSemaphoreUnits, 25) == pdTRUE) { 309 if (xSemaphoreTake(xSemaphoreUnits, 25) == pdTRUE) {
283 for (int i = 0; i < 3; i++) { 310 for (int i = 0; i < 3; i++) {
284 if (i == 0) 311 if (i == 0)
298 units[i].alarm |= ALARM_UNIT_PRESSURE; 325 units[i].alarm |= ALARM_UNIT_PRESSURE;
299 int P = (units[i].pressure_voltage / (adc_state->Batt_voltage / 1000) - units[i].pressure_zero) * 14; // in bar 326 int P = (units[i].pressure_voltage / (adc_state->Batt_voltage / 1000) - units[i].pressure_zero) * 14; // in bar
300 if (P < 0) 327 if (P < 0)
301 P = 0; 328 P = 0;
302 units[i].pressure = P; 329 units[i].pressure = P;
303 sprintf(buf, "%.1f", P / 1000.0); 330 if (i == 0)
304 w = u8g2_GetUTF8Width(&u8g2, buf); 331 p1 = P / 1000.0;
305 u8g2_DrawUTF8(&u8g2, ((42 - w) / 2) + i * 43,63, buf); 332 else if (i == 1)
333 p2 = P / 1000.0;
334 else if (i == 2)
335 p3 = P / 1000.0;
306 printf("%d volt: %d batt: %d scale: %d bar: %d\n", i, units[i].pressure_voltage, adc_state->Batt_voltage, 336 printf("%d volt: %d batt: %d scale: %d bar: %d\n", i, units[i].pressure_voltage, adc_state->Batt_voltage,
307 units[i].pressure_voltage / (adc_state->Batt_voltage / 1000) - units[i].pressure_zero, P); 337 units[i].pressure_voltage / (adc_state->Batt_voltage / 1000) - units[i].pressure_zero, P);
308 // Moet die echt op 5 volt? 338 // Moet die echt op 5 volt?
309 // Verbruik 10 mA 339 // Verbruik 10 mA
310 // Setup tijd max 2 mS 340 // Setup tijd max 2 mS
311 xSemaphoreGive(xSemaphoreADC); 341 xSemaphoreGive(xSemaphoreADC);
312 } 342 }
313 } 343 }
314 write_units(); 344 write_units();
315 xSemaphoreGive(xSemaphoreUnits); 345 xSemaphoreGive(xSemaphoreUnits);
316 u8g2_SendBuffer(&u8g2); 346 screen_main(t, p1, p2, p3);
317 u8g2_SetPowerSave(&u8g2, 0); // wake up display
318 } 347 }
319 } 348 }
320 break; 349 break;
321 350
322 case MAIN_LOOP1_WAITCON: 351 case MAIN_LOOP1_WAITCON:

mercurial