main/co2meter.c

changeset 6
06a5028dbcdf
parent 4
2a57c466bf45
child 7
6eafc4c2bf3d
equal deleted inserted replaced
5:4b1c65e4d863 6:06a5028dbcdf
44 { 44 {
45 struct timeval now; 45 struct timeval now;
46 gettimeofday(&now, NULL); 46 gettimeofday(&now, NULL);
47 int sleep_time_ms = (now.tv_sec - sleep_enter_time.tv_sec) * 1000 + (now.tv_usec - sleep_enter_time.tv_usec) / 1000; 47 int sleep_time_ms = (now.tv_sec - sleep_enter_time.tv_sec) * 1000 + (now.tv_usec - sleep_enter_time.tv_usec) / 1000;
48 int New_Loop2 = MAIN_LOOP2_INIT; 48 int New_Loop2 = MAIN_LOOP2_INIT;
49 char buf[65];
49 esp_err_t ret; 50 esp_err_t ret;
50 51
51 Main_Loop1 = MAIN_LOOP1_INIT; 52 Main_Loop1 = MAIN_LOOP1_INIT;
52 Main_Loop2 = -1; 53 Main_Loop2 = -1;
53 54
100 101
101 /* 102 /*
102 * Setup the OLED display. 103 * Setup the OLED display.
103 * See: https://github.com/nkolban/esp32-snippets/blob/master/hardware/displays/U8G2/ 104 * See: https://github.com/nkolban/esp32-snippets/blob/master/hardware/displays/U8G2/
104 */ 105 */
105 // u8g2_esp32_hal_t u8g2_esp32_hal = U8G2_ESP32_HAL_DEFAULT; 106 u8g2_esp32_hal_t u8g2_esp32_hal = U8G2_ESP32_HAL_DEFAULT;
106 // u8g2_esp32_hal.sda = PIN_SDA; 107 u8g2_esp32_hal.sda = PIN_SDA;
107 // u8g2_esp32_hal.scl = PIN_SCL; 108 u8g2_esp32_hal.scl = PIN_SCL;
108 // u8g2_esp32_hal_init(u8g2_esp32_hal); 109 u8g2_esp32_hal_init(u8g2_esp32_hal);
109 110
110 // u8g2_t u8g2; // a structure which will contain all the data for one display 111 u8g2_t u8g2; // a structure which will contain all the data for one display
111 //// u8g2_Setup_ssd1306_i2c_128x32_univision_f( 112 u8g2_Setup_sh1106_i2c_128x64_noname_f(
112 // u8g2_Setup_sh1106_i2c_128x64_noname_f( 113 &u8g2,
113 // &u8g2, 114 U8G2_R0,
114 // U8G2_R0, 115 u8g2_esp32_i2c_byte_cb,
115 // //u8x8_byte_sw_i2c, 116 u8g2_esp32_gpio_and_delay_cb); // init u8g2 structure
116 // u8g2_esp32_i2c_byte_cb, 117 u8x8_SetI2CAddress(&u8g2.u8x8, 0x78);
117 // u8g2_esp32_gpio_and_delay_cb); // init u8g2 structure 118 ESP_LOGI(TAG, "u8g2_InitDisplay");
118 // u8x8_SetI2CAddress(&u8g2.u8x8,0x78); 119 u8g2_InitDisplay(&u8g2); // send init sequence to the display, display is in sleep mode after this,
119 120
120 // ESP_LOGI(TAG, "u8g2_InitDisplay"); 121 /*
121 // u8g2_InitDisplay(&u8g2); // send init sequence to the display, display is in sleep mode after this, 122 * Setup SPIFFS filesystem
122 123 */
123 // ESP_LOGI(TAG, "u8g2_SetPowerSave");
124 // u8g2_SetPowerSave(&u8g2, 0); // wake up display
125 // ESP_LOGI(TAG, "u8g2_ClearBuffer");
126 // u8g2_ClearBuffer(&u8g2);
127 // ESP_LOGI(TAG, "u8g2_DrawBox");
128 // u8g2_DrawBox(&u8g2, 0, 26, 80,6);
129 // u8g2_DrawFrame(&u8g2, 0,26,100,6);
130
131 // ESP_LOGI(TAG, "u8g2_SetFont");
132 // u8g2_SetFont(&u8g2, u8g2_font_ncenB14_tr);
133 // ESP_LOGI(TAG, "u8g2_DrawStr");
134 // u8g2_DrawStr(&u8g2, 2,17,"Hi nkolban!");
135 // ESP_LOGI(TAG, "u8g2_SendBuffer");
136 // u8g2_SendBuffer(&u8g2);
137
138
139 ESP_LOGI(TAG, "Initializing SPIFFS"); 124 ESP_LOGI(TAG, "Initializing SPIFFS");
140
141 esp_vfs_spiffs_conf_t conf = { 125 esp_vfs_spiffs_conf_t conf = {
142 .base_path = "/spiffs", 126 .base_path = "/spiffs",
143 .partition_label = NULL, 127 .partition_label = NULL,
144 .max_files = 5, 128 .max_files = 5,
145 .format_if_mount_failed = true 129 .format_if_mount_failed = true
146 }; 130 };
147 131
148 // Use settings defined above to initialize and mount SPIFFS filesystem. 132 /*
149 // Note: esp_vfs_spiffs_register is an all-in-one convenience function. 133 * Use settings defined above to initialize and mount SPIFFS filesystem.
134 * Note: esp_vfs_spiffs_register is an all-in-one convenience function.
135 */
150 ret = esp_vfs_spiffs_register(&conf); 136 ret = esp_vfs_spiffs_register(&conf);
151 137
152 if (ret != ESP_OK) { 138 if (ret != ESP_OK) {
153 if (ret == ESP_FAIL) { 139 if (ret == ESP_FAIL) {
154 ESP_LOGE(TAG, "Failed to mount or format filesystem"); 140 ESP_LOGE(TAG, "Failed to mount or format filesystem");
155 } else if (ret == ESP_ERR_NOT_FOUND) { 141 } else if (ret == ESP_ERR_NOT_FOUND) {
156 ESP_LOGE(TAG, "Failed to find SPIFFS partition"); 142 ESP_LOGE(TAG, "Failed to find SPIFFS partition");
157 } else { 143 } else {
158 ESP_LOGE(TAG, "Failed to initialize SPIFFS (%d)", ret); 144 ESP_LOGE(TAG, "Failed to initialize SPIFFS (%d)", ret);
159 } 145 }
160 // _fg = TFT_RED; 146 u8g2_SetFont(&u8g2, u8g2_font_t0_15_tr);
161 // TFT_print((char *)"error\r\n", LASTX, LASTY); 147 u8g2_DrawStr(&u8g2,2,12,"SPIFFS:");
148 u8g2_DrawStr(&u8g2,2,24,"init error");
149 u8g2_SendBuffer(&u8g2);
150 u8g2_SetPowerSave(&u8g2, 0);
162 return; // Stop application. 151 return; // Stop application.
163 } 152 }
164 153
165 size_t total = 0, used = 0; 154 size_t total = 0, used = 0;
166 ret = esp_spiffs_info(NULL, &total, &used); 155 ret = esp_spiffs_info(NULL, &total, &used);
167 if (ret != ESP_OK) { 156 if (ret != ESP_OK) {
168 ESP_LOGE(TAG, "Failed to get SPIFFS partition information"); 157 ESP_LOGE(TAG, "Failed to get SPIFFS partition information");
169 // _fg = TFT_RED; 158 u8g2_SetFont(&u8g2, u8g2_font_t0_15_tr);
170 // TFT_print((char *)"error\r\n", LASTX, LASTY); 159 u8g2_DrawStr(&u8g2,2,12,"SPIFFS:");
160 u8g2_DrawStr(&u8g2,2,24,"partition error");
161 u8g2_SendBuffer(&u8g2);
162 u8g2_SetPowerSave(&u8g2, 0);
171 return; // Stop application. 163 return; // Stop application.
172 } else { 164 } else {
173 ESP_LOGI(TAG, "Partition size: %d, used: %d - %d%%", total, used, (used * 100) / total); 165 ESP_LOGI(TAG, "Partition size: %d, used: %d - %d%%", total, used, (used * 100) / total);
174 } 166 }
175 167
176 // Just to debug, list the /spiffs filesystem. 168 // Just to debug, list the /spiffs filesystem.
177 #if 1 169 #if 0
178 DIR *dir = opendir("/spiffs"); 170 DIR *dir = opendir("/spiffs");
179 struct dirent* de = readdir(dir); 171 struct dirent* de = readdir(dir);
180 while (de) { 172 while (de) {
181 if (de->d_type == DT_REG) { 173 if (de->d_type == DT_REG) {
182 printf("F "); 174 printf("F ");
191 #endif 183 #endif
192 184
193 /* 185 /*
194 * Read or create configuration 186 * Read or create configuration
195 */ 187 */
196 // TFT_print((char *)"Ophalen configuratie ", LASTX, LASTY);
197 read_config(); 188 read_config();
198 read_units(); 189 read_units();
199 190
200 //add_station((uint8_t *)"MBSE_WLR", (uint8_t *)"abcjkltuv"); 191 //add_station((uint8_t *)"MBSE_WLR", (uint8_t *)"abcjkltuv");
201 //remove_station((uint8_t *)"MBSE_WLP"); 192 //remove_station((uint8_t *)"MBSE_WLP");
202 //sprintf(config.lastSSID, "%s", "BREWER"); 193
203 //write_config(); 194 /*
204 195 * Create FreeRTOS tasks
196 */
205 xSemaphoreDS18B20 = xSemaphoreCreateMutex(); 197 xSemaphoreDS18B20 = xSemaphoreCreateMutex();
206 xSemaphoreADC = xSemaphoreCreateMutex(); 198 xSemaphoreADC = xSemaphoreCreateMutex();
207 xSemaphoreUnits = xSemaphoreCreateMutex(); 199 xSemaphoreUnits = xSemaphoreCreateMutex();
208 200
209 xTaskCreate(&task_ds18b20, "task_ds18b20", 2560, NULL, 8, &xTaskDS18B20); 201 xTaskCreate(&task_ds18b20, "task_ds18b20", 2560, NULL, 8, &xTaskDS18B20);
210 xTaskCreate(&task_adc, "task_adc", 2560, NULL, 8, &xTaskADC); 202 xTaskCreate(&task_adc, "task_adc", 2560, NULL, 8, &xTaskADC);
211 esp_log_level_set("wifi", ESP_LOG_ERROR); 203 esp_log_level_set("wifi", ESP_LOG_ERROR);
212 xTaskCreate(&task_wifi, "task_wifi", 4096, NULL, 3, &xTaskWifi); 204 xTaskCreate(&task_wifi, "task_wifi", 4096, NULL, 3, &xTaskWifi);
213 vTaskDelay( (TickType_t)10); 205 vTaskDelay(10 / portTICK_PERIOD_MS);
214 xTaskCreate(&task_mqtt, "task_mqtt", 4096, NULL, 5, &xTaskMQTT); 206 xTaskCreate(&task_mqtt, "task_mqtt", 4096, NULL, 5, &xTaskMQTT);
215 207
216 // esp32-rotary-encoder requires that the GPIO ISR service is installed before calling rotary_encoder_register() 208 // esp32-rotary-encoder requires that the GPIO ISR service is installed before calling rotary_encoder_register()
217 // ESP_ERROR_CHECK(gpio_install_isr_service(0)); 209 // ESP_ERROR_CHECK(gpio_install_isr_service(0));
218 210
227 // Create a queue for events from the rotary encoder driver. 219 // Create a queue for events from the rotary encoder driver.
228 // Tasks can read from this queue to receive up to date position information. 220 // Tasks can read from this queue to receive up to date position information.
229 // QueueHandle_t event_queue = rotary_encoder_create_queue(); 221 // QueueHandle_t event_queue = rotary_encoder_create_queue();
230 // ESP_ERROR_CHECK(rotary_encoder_set_queue(&info, event_queue)); 222 // ESP_ERROR_CHECK(rotary_encoder_set_queue(&info, event_queue));
231 223
232
233 /* Print chip information */
234 // esp_chip_info_t chip_info;
235 // esp_chip_info(&chip_info);
236 // printf("This is ESP32 chip with %d CPU cores, WiFi%s%s, ",
237 // chip_info.cores,
238 // (chip_info.features & CHIP_FEATURE_BT) ? "/BT" : "",
239 // (chip_info.features & CHIP_FEATURE_BLE) ? "/BLE" : "");
240
241 // printf("silicon revision %d, ", chip_info.revision);
242
243 // printf("%dMB %s flash\n", spi_flash_get_chip_size() / (1024 * 1024),
244 // (chip_info.features & CHIP_FEATURE_EMB_FLASH) ? "embedded" : "external");
245
246 // esp_err_t status = adc2_vref_to_gpio(GPIO_NUM_26);
247 // if (status == ESP_OK) {
248 // printf("v_ref routed to GPIO\n");
249 // } else {
250 // printf("failed to route v_ref\n");
251 // }
252 // vTaskDelay(1000 * wakeup_time_sec / portTICK_PERIOD_MS);
253 224
254 /* 225 /*
255 * Main application loop. 226 * Main application loop.
256 */ 227 */
257 while (1) { 228 while (1) {
266 // If configured do MAIN_LOOP1_CONNECT 237 // If configured do MAIN_LOOP1_CONNECT
267 Main_Loop1 = MAIN_LOOP1_CONNECT; 238 Main_Loop1 = MAIN_LOOP1_CONNECT;
268 requestWiFi_system(true); 239 requestWiFi_system(true);
269 request_ds18b20(); 240 request_ds18b20();
270 request_adc(); 241 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
271 break; 250 break;
272 251
273 case MAIN_LOOP1_CONNECT: 252 case MAIN_LOOP1_CONNECT:
274 if (ready_WiFi()) 253 if (ready_WiFi())
275 Main_Loop1 = MAIN_LOOP1_MQTT_CONNECT; 254 Main_Loop1 = MAIN_LOOP1_MQTT_CONNECT;
289 temp = (ds18b20_state->sensor[0].temperature * 1000); 268 temp = (ds18b20_state->sensor[0].temperature * 1000);
290 state = (ds18b20_state->sensor[0].error == 0) ? 0:1; 269 state = (ds18b20_state->sensor[0].error == 0) ? 0:1;
291 strncpy(rom_code, ds18b20_state->sensor[0].rom_code, strlen(ds18b20_state->sensor[0].rom_code)); 270 strncpy(rom_code, ds18b20_state->sensor[0].rom_code, strlen(ds18b20_state->sensor[0].rom_code));
292 xSemaphoreGive(xSemaphoreDS18B20); 271 xSemaphoreGive(xSemaphoreDS18B20);
293 } 272 }
273
274 u8g2_SetFont(&u8g2, u8g2_font_t0_22b_tf);
275 sprintf(buf, "%.1f °C", temp / 1000.0);
276 u8g2_uint_t w = u8g2_GetUTF8Width(&u8g2, buf);
277 u8g2_DrawUTF8(&u8g2, (128 - w) / 2,40, buf);
278 u8g2_SetFont(&u8g2, u8g2_font_t0_18b_tf);
294 279
295 /* Copy measured data and calculate results */ 280 /* Copy measured data and calculate results */
296 if (xSemaphoreTake(xSemaphoreUnits, 25) == pdTRUE) { 281 if (xSemaphoreTake(xSemaphoreUnits, 25) == pdTRUE) {
297 for (int i = 0; i < 3; i++) { 282 for (int i = 0; i < 3; i++) {
298 units[i].temperature = temp; 283 units[i].temperature = temp;
305 units[i].pressure_zero = 110; 290 units[i].pressure_zero = 110;
306 int P = (units[i].pressure_voltage / (adc_state->Batt_voltage / 1000) - units[i].pressure_zero) * 14; // in bar 291 int P = (units[i].pressure_voltage / (adc_state->Batt_voltage / 1000) - units[i].pressure_zero) * 14; // in bar
307 if (P < 0) 292 if (P < 0)
308 P = 0; 293 P = 0;
309 units[i].pressure = P; 294 units[i].pressure = P;
295 sprintf(buf, "%.1f", P / 1000.0);
296 w = u8g2_GetUTF8Width(&u8g2, buf);
297 u8g2_DrawUTF8(&u8g2, ((42 - w) / 2) + i * 43,63, buf);
310 printf("%d volt: %d batt: %d scale: %d bar: %d\n", i, units[i].pressure_voltage, adc_state->Batt_voltage, 298 printf("%d volt: %d batt: %d scale: %d bar: %d\n", i, units[i].pressure_voltage, adc_state->Batt_voltage,
311 units[i].pressure_voltage / (adc_state->Batt_voltage / 1000) - units[i].pressure_zero, P); 299 units[i].pressure_voltage / (adc_state->Batt_voltage / 1000) - units[i].pressure_zero, P);
312 // Moet die echt op 5 volt? 300 // Moet die echt op 5 volt?
313 // Verbruik 10 mA 301 // Verbruik 10 mA
314 // Setup tijd max 2 mS 302 // Setup tijd max 2 mS
315 xSemaphoreGive(xSemaphoreADC); 303 xSemaphoreGive(xSemaphoreADC);
316 } 304 }
317 } 305 }
318 write_units(); 306 write_units();
319 xSemaphoreGive(xSemaphoreUnits); 307 xSemaphoreGive(xSemaphoreUnits);
308 u8g2_SendBuffer(&u8g2);
309 u8g2_SetPowerSave(&u8g2, 0); // wake up display
320 } 310 }
321 } 311 }
322 break; 312 break;
323
324 // calculate stap en data copy
325 313
326 case MAIN_LOOP1_WAITCON: 314 case MAIN_LOOP1_WAITCON:
327 if (ready_mqtt()) 315 if (ready_mqtt())
328 Main_Loop1 = MAIN_LOOP1_SEND; 316 Main_Loop1 = MAIN_LOOP1_SEND;
329 break; 317 break;
411 if (Main_Loop1 == MAIN_LOOP1_DONE && Main_Loop2 == MAIN_LOOP2_DONE) 399 if (Main_Loop1 == MAIN_LOOP1_DONE && Main_Loop2 == MAIN_LOOP2_DONE)
412 break; 400 break;
413 401
414 vTaskDelay(10 / portTICK_PERIOD_MS); 402 vTaskDelay(10 / portTICK_PERIOD_MS);
415 } 403 }
416 404
405 // u8g2_ClearBuffer(&u8g2);
406 // u8g2_SendBuffer(&u8g2);
407 // u8g2_SetPowerSave(&u8g2, 1);
408
417 // printf("Simulate deep sleep\n"); 409 // printf("Simulate deep sleep\n");
418 // vTaskDelay(1000 * wakeup_time_sec / portTICK_PERIOD_MS); 410 // vTaskDelay(1000 * wakeup_time_sec / portTICK_PERIOD_MS);
419 411
420 printf("Entering deep sleep\n"); 412 printf("Entering deep sleep\n");
421 gettimeofday(&sleep_enter_time, NULL); 413 gettimeofday(&sleep_enter_time, NULL);

mercurial