# HG changeset patch # User Michiel Broek # Date 1592215701 -7200 # Node ID 52d9405474e10db2fe5c579e5d3232f0861824ba # Parent e949f41034eb8e26be33d19ed78ef613a3906dd7 Better check for a corrupted units file. Added a small boot delay to prevent units file corruption. diff -r e949f41034eb -r 52d9405474e1 main/co2meter.c --- a/main/co2meter.c Sun Jun 14 22:35:24 2020 +0200 +++ b/main/co2meter.c Mon Jun 15 12:08:21 2020 +0200 @@ -115,6 +115,7 @@ */ read_config(); read_units(); + vTaskDelay(500 / portTICK_PERIOD_MS); /* * Create FreeRTOS tasks diff -r e949f41034eb -r 52d9405474e1 main/config.c --- a/main/config.c Sun Jun 14 22:35:24 2020 +0200 +++ b/main/config.c Mon Jun 15 12:08:21 2020 +0200 @@ -86,23 +86,32 @@ if (f == NULL) { // No units yet, create them. - esp_efuse_mac_get_default(mac_addr); - for (int i = 0; i < 3; i++) { - memset(&units[i], 0, sizeof(unit_t)); - sprintf(units[i].uuid, "c0ffeeee-dead-beef-caf%d-%02x%02x%02x%02x%02x%02x", i & 3, - mac_addr[0], mac_addr[1], mac_addr[2], mac_addr[3], mac_addr[4], mac_addr[5]); - sprintf(units[i].alias, "unit%d", (i + 1) & 3); - units[i].pressure_zero = 110; - } - write_units(); + ESP_LOGE(TAG, "/spiffs/etc/units.conf not found, create new"); + goto u_error; } else { dst = (uint8_t*)&units; size_t bytes = fread(dst, 1, sizeof(units), f); fclose(f); + if (bytes != sizeof(units)) { + ESP_LOGE(TAG, "/spiffs/etc/units.conf read %d of %d bytes", bytes, sizeof(units)); + goto u_error; + } ESP_LOGI(TAG, "/spiffs/etc/units.conf read %d bytes", bytes); for (int i = 0; i < 3; i++) ESP_LOGI(TAG, "%d %s %d %4d %3d", i, units[i].alias, units[i].pressure_channel, units[i].pressure_voltage, units[i].pressure_zero); } + return; + +u_error: + esp_efuse_mac_get_default(mac_addr); + for (int i = 0; i < 3; i++) { + memset(&units[i], 0, sizeof(unit_t)); + sprintf(units[i].uuid, "c0ffeeee-dead-beef-caf%d-%02x%02x%02x%02x%02x%02x", i & 3, + mac_addr[0], mac_addr[1], mac_addr[2], mac_addr[3], mac_addr[4], mac_addr[5]); + sprintf(units[i].alias, "unit%d", (i + 1) & 3); + units[i].pressure_zero = 110; + } + f = fopen("/spiffs/etc/units.conf", "w+"); }