| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680 |
- #include <iostream>
- #include <string>
- #include <vector>
- #include <regex>
- #include "esp_psram.h"
- #include "esp_pm.h"
- #include "psram.h"
- #include "esp_chip_info.h"
- #include "esp_vfs_fat.h"
- #include "ffconf.h"
- #include "driver/sdmmc_host.h"
- #include "ClassLogFile.h"
- #include "connect_wlan.h"
- #include "read_wlanini.h"
- #include "server_main.h"
- #include "MainFlowControl.h"
- #include "server_file.h"
- #include "server_ota.h"
- #include "time_sntp.h"
- #include "configFile.h"
- #include "server_main.h"
- #include "server_camera.h"
- #include "basic_auth.h"
- #ifdef ENABLE_MQTT
- #include "server_mqtt.h"
- #endif // ENABLE_MQTT
- #include "Helper.h"
- #include "statusled.h"
- #include "sdcard_check.h"
- #include "../../include/defines.h"
- #ifdef ENABLE_SOFTAP
- #include "softAP.h"
- #endif // ENABLE_SOFTAP
- #ifdef DISABLE_BROWNOUT_DETECTOR
- #include "soc/soc.h"
- #include "soc/rtc_cntl_reg.h"
- #endif
- #ifdef DEBUG_ENABLE_SYSINFO
- #if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(4, 0, 0)
- #include "esp_sys.h"
- #endif
- #endif // DEBUG_ENABLE_SYSINFO
- // define `gpio_pad_select_gpip` for newer versions of IDF
- #if (ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(4, 3, 0))
- #include "esp_rom_gpio.h"
- #define gpio_pad_select_gpio esp_rom_gpio_pad_select_gpio
- #endif
- #ifdef USE_HIMEM_IF_AVAILABLE
- #include "esp32/himem.h"
- #ifdef DEBUG_HIMEM_MEMORY_CHECK
- #include "himem_memory_check.h"
- #endif
- #endif
- // #ifdef CONFIG_HEAP_TRACING_STANDALONE
- #if defined HEAP_TRACING_MAIN_WIFI || defined HEAP_TRACING_MAIN_START
- #include <esp_heap_trace.h>
- #define NUM_RECORDS 300
- static heap_trace_record_t trace_record[NUM_RECORDS]; // This buffer must be in internal RAM
- #endif
- extern const char *GIT_TAG;
- extern const char *GIT_REV;
- extern const char *GIT_BRANCH;
- extern const char *BUILD_TIME;
- extern std::string getFwVersion(void);
- extern std::string getHTMLversion(void);
- extern std::string getHTMLcommit(void);
- std::vector<std::string> splitString(const std::string &str);
- bool setCpuFrequency(void);
- static const char *TAG = "MAIN";
- #define MOUNT_POINT "/sdcard"
- bool Init_NVS_SDCard()
- {
- esp_err_t ret = nvs_flash_init();
- if (ret == ESP_ERR_NVS_NO_FREE_PAGES)
- {
- ESP_ERROR_CHECK(nvs_flash_erase());
- ret = nvs_flash_init();
- }
- ESP_LOGD(TAG, "Using SDMMC peripheral");
- sdmmc_host_t host = SDMMC_HOST_DEFAULT();
- host.max_freq_khz = SDMMC_FREQ_HIGHSPEED;
- // For SoCs where the SD power can be supplied both via an internal or external (e.g. on-board LDO) power supply.
- // When using specific IO pins (which can be used for ultra high-speed SDMMC) to connect to the SD card
- // and the internal LDO power supply, we need to initialize the power supply first.
- #if SD_PWR_CTRL_LDO_INTERNAL_IO
- sd_pwr_ctrl_ldo_config_t ldo_config = {
- .ldo_chan_id = CONFIG_EXAMPLE_SD_PWR_CTRL_LDO_IO_ID,
- };
- sd_pwr_ctrl_handle_t pwr_ctrl_handle = NULL;
- ret = sd_pwr_ctrl_new_on_chip_ldo(&ldo_config, &pwr_ctrl_handle);
- if (ret != ESP_OK)
- {
- ESP_LOGE(TAG, "Failed to create a new on-chip LDO power control driver");
- return false;
- }
- host.pwr_ctrl_handle = pwr_ctrl_handle;
- #endif
- // This initializes the slot without card detect (CD) and write protect (WP) signals.
- // Modify slot_config.gpio_cd and slot_config.gpio_wp if your board has these signals.
- #ifdef CONFIG_SOC_SDMMC_USE_GPIO_MATRIX
- sdmmc_slot_config_t slot_config = {
- .cd = SDMMC_SLOT_NO_CD,
- .wp = SDMMC_SLOT_NO_WP,
- };
- #else
- sdmmc_slot_config_t slot_config = SDMMC_SLOT_CONFIG_DEFAULT();
- #endif
- // Set bus width to use:
- #ifdef __SD_USE_ONE_LINE_MODE__
- slot_config.width = 1;
- #ifdef CONFIG_SOC_SDMMC_USE_GPIO_MATRIX
- slot_config.clk = GPIO_SDCARD_CLK;
- slot_config.cmd = GPIO_SDCARD_CMD;
- slot_config.d0 = GPIO_SDCARD_D0;
- #endif // end CONFIG_SOC_SDMMC_USE_GPIO_MATRIX
- #else // else __SD_USE_ONE_LINE_MODE__
- slot_config.width = 4;
- #ifdef CONFIG_SOC_SDMMC_USE_GPIO_MATRIX
- slot_config.d1 = GPIO_SDCARD_D1;
- slot_config.d2 = GPIO_SDCARD_D2;
- slot_config.d3 = GPIO_SDCARD_D3;
- #endif // end CONFIG_SOC_SDMMC_USE_GPIO_MATRIX
- #endif // end __SD_USE_ONE_LINE_MODE__
- // Enable internal pullups on enabled pins. The internal pullups
- // are insufficient however, please make sure 10k external pullups are
- // connected on the bus. This is for debug / example purpose only.
- slot_config.flags |= SDMMC_SLOT_FLAG_INTERNAL_PULLUP;
- // Der PullUp des GPIO13 wird durch slot_config.flags |= SDMMC_SLOT_FLAG_INTERNAL_PULLUP;
- // nicht gesetzt, da er eigentlich nicht benötigt wird,
- // dies führt jedoch bei schlechten Kopien des AI_THINKER Boards
- // zu Problemen mit der SD Initialisierung und eventuell sogar zur reboot-loops.
- // Um diese Probleme zu kompensieren, wird der PullUp manuel gesetzt.
- gpio_set_pull_mode(GPIO_SDCARD_D3, GPIO_PULLUP_ONLY); // HS2_D3
- // Options for mounting the filesystem.
- // If format_if_mount_failed is set to true, SD card will be partitioned and
- // formatted in case when mounting fails.
- esp_vfs_fat_sdmmc_mount_config_t mount_config = {
- .format_if_mount_failed = false,
- .max_files = 8,
- .allocation_unit_size = 0, // 0 = auto
- .disk_status_check_enable = false,
- .use_one_fat = false,
- };
- sdmmc_card_t *card;
- const char mount_point[] = MOUNT_POINT;
- // Use settings defined above to initialize SD card and mount FAT filesystem.
- // Note: esp_vfs_fat_sdmmc_mount is an all-in-one convenience function.
- // Please check its source code and implement error recovery when developing
- // production applications.
- ret = esp_vfs_fat_sdmmc_mount(mount_point, &host, &slot_config, &mount_config, &card);
- if (ret != ESP_OK)
- {
- if (ret == ESP_FAIL)
- {
- ESP_LOGE(TAG, "Failed to mount FAT filesystem on SD card. Check SD card filesystem (only FAT supported) or try another card");
- StatusLED(SDCARD_INIT, 1, true);
- }
- else if (ret == 263)
- {
- // Error code: 0x107 --> usually: SD not found
- ESP_LOGE(TAG, "SD card init failed. Check if SD card is properly inserted into SD card slot or try another card");
- StatusLED(SDCARD_INIT, 2, true);
- }
- else
- {
- ESP_LOGE(TAG, "SD card init failed. Check error code or try another card");
- StatusLED(SDCARD_INIT, 3, true);
- }
- return false;
- }
- SaveSDCardInfo(card);
- return true;
- }
- extern "C" void app_main(void)
- {
- // #ifdef CONFIG_HEAP_TRACING_STANDALONE
- #if defined HEAP_TRACING_MAIN_WIFI || defined HEAP_TRACING_MAIN_START
- // register a buffer to record the memory trace
- ESP_ERROR_CHECK(heap_trace_init_standalone(trace_record, NUM_RECORDS));
- #endif
- #ifdef DISABLE_BROWNOUT_DETECTOR
- WRITE_PERI_REG(RTC_CNTL_BROWN_OUT_REG, 0); // disable brownout detector
- #endif
- #ifdef HEAP_TRACING_MAIN_START
- ESP_ERROR_CHECK(heap_trace_start(HEAP_TRACE_LEAKS));
- #endif
- // ********************************************
- // Highlight start of app_main
- // ********************************************
- ESP_LOGI(TAG, "\n\n\n\n================ Start app_main =================");
- // Init SD card
- // ********************************************
- if (!Init_NVS_SDCard())
- {
- ESP_LOGE(TAG, "Device init aborted!");
- vTaskDelay(pdMS_TO_TICKS(2000));
- return; // No way to continue without working SD card!
- }
- // SD card: Create log directories (if not already existing)
- // ********************************************
- LogFile.CreateLogDirectories(); // mandatory for logging + image saving
- // ********************************************
- // Highlight start of logfile logging
- // Default Log Level: INFO -> Everything which needs to be logged during boot should be have level INFO, WARN OR ERROR
- // ********************************************
- LogFile.WriteToFile(ESP_LOG_INFO, TAG, "=================================================");
- LogFile.WriteToFile(ESP_LOG_INFO, TAG, "==================== Start ======================");
- LogFile.WriteToFile(ESP_LOG_INFO, TAG, "=================================================");
- // SD card: basic R/W check
- // ********************************************
- int iSDCardStatus = SDCardCheckRW();
- if (iSDCardStatus < 0)
- {
- if (iSDCardStatus <= -1 && iSDCardStatus >= -2)
- {
- // write error
- StatusLED(SDCARD_CHECK, 1, true);
- }
- else if (iSDCardStatus <= -3 && iSDCardStatus >= -5)
- {
- // read error
- StatusLED(SDCARD_CHECK, 2, true);
- }
- else if (iSDCardStatus == -6)
- {
- // delete error
- StatusLED(SDCARD_CHECK, 3, true);
- }
-
- setSystemStatusFlag(SYSTEM_STATUS_SDCARD_CHECK_BAD); // reduced web interface going to be loaded
- vTaskDelay(pdMS_TO_TICKS(2000));
- return; // No way to continue without working SD card!
- }
- // SD card: Create further mandatory directories (if not already existing)
- // Correct creation of these folders will be checked with function "SDCardCheckFolderFilePresence"
- // ********************************************
- MakeDir("/sdcard/firmware"); // mandatory for OTA firmware update
- MakeDir("/sdcard/img_tmp"); // mandatory for setting up alignment marks
- MakeDir("/sdcard/demo"); // mandatory for demo mode
- MakeDir("/sdcard/config/certs"); // mandatory for mqtt certificates
- // Init external PSRAM
- // ********************************************
- esp_err_t PSRAMStatus = esp_psram_init();
- if (PSRAMStatus != ESP_OK)
- {
- // ESP_FAIL -> Failed to init PSRAM
- LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "PSRAM init failed (" + std::to_string(PSRAMStatus) + ")! PSRAM not found or defective");
- setSystemStatusFlag(SYSTEM_STATUS_PSRAM_BAD);
- StatusLED(PSRAM_INIT, 1, true);
- vTaskDelay(pdMS_TO_TICKS(2000));
- return; // No way to continue without psram
- }
- else
- {
- // ESP_OK -> PSRAM init OK --> continue to check PSRAM size
- size_t psram_size = esp_psram_get_size(); // size_t psram_size = esp_psram_get_size(); // comming in IDF 5.0
- LogFile.WriteToFile(ESP_LOG_INFO, TAG, "PSRAM size: " + std::to_string(psram_size) + " byte (" + std::to_string(psram_size / 1024 / 1024) + "MB / " + std::to_string(psram_size / 1024 / 1024 * 8) + "MBit)");
- // Check PSRAM size
- // ********************************************
- if (psram_size < (4 * 1024 * 1024))
- {
- // PSRAM is below 4 MBytes (32Mbit)
- LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "PSRAM size >= 4MB (32Mbit) is mandatory to run this application");
- setSystemStatusFlag(SYSTEM_STATUS_PSRAM_BAD);
- StatusLED(PSRAM_INIT, 2, true);
- vTaskDelay(pdMS_TO_TICKS(2000));
- return; // No way to continue without psram
- }
- else
- {
- // PSRAM size OK --> continue to check heap size
- size_t _hsize = getESPHeapSize();
- LogFile.WriteToFile(ESP_LOG_INFO, TAG, "Total heap: " + std::to_string(_hsize) + " byte");
- // Check heap memory
- // ********************************************
- if (_hsize < 4000000)
- {
- // Check available Heap memory for a bit less than 4 MB (a test on a good device showed 4187558 bytes to be available)
- LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Total heap >= 4000000 byte is mandatory to run this application");
- setSystemStatusFlag(SYSTEM_STATUS_HEAP_TOO_SMALL);
- StatusLED(PSRAM_INIT, 3, true);
- vTaskDelay(pdMS_TO_TICKS(2000));
- return; // No way to continue without psram
- }
- else
- {
- // HEAP size OK --> continue to reserve shared memory block and check camera init
- /* Allocate static PSRAM memory regions */
- if (!reserve_psram_shared_region())
- {
- setSystemStatusFlag(SYSTEM_STATUS_HEAP_TOO_SMALL);
- StatusLED(PSRAM_INIT, 3, true);
- vTaskDelay(pdMS_TO_TICKS(2000));
- return; // No way to continue without psram
- }
- }
- }
- }
- // Check for updates
- // ********************************************
- CheckOTAUpdate();
- CheckUpdate();
-
- // Init time (as early as possible, but SD card needs to be initialized)
- // ********************************************
- setupTime(); // NTP time service: Status of time synchronization will be checked after every round (server_tflite.cpp)
- // Set CPU Frequency
- // ********************************************
- setCpuFrequency();
- // Start SoftAP for initial remote setup
- // Note: Start AP if no wlan.ini and/or config.ini available, e.g. SD card empty; function does not exit anymore until reboot
- // ********************************************
- #ifdef ENABLE_SOFTAP
- CheckStartAPMode();
- #endif
- // SD card: Check presence of some mandatory folders / files
- // ********************************************
- if (!SDCardCheckFolderFilePresence())
- {
- StatusLED(SDCARD_CHECK, 4, true);
- setSystemStatusFlag(SYSTEM_STATUS_FOLDER_CHECK_BAD); // reduced web interface going to be loaded
- }
- // Check version information
- // ********************************************
- std::string versionFormated = getFwVersion() + ", Date/Time: " + std::string(BUILD_TIME) + ", Web UI: " + getHTMLversion();
- if (std::string(GIT_TAG) != "")
- {
- // We are on a tag, add it as prefix
- versionFormated = "Tag: '" + std::string(GIT_TAG) + "', " + versionFormated;
- }
- LogFile.WriteToFile(ESP_LOG_INFO, TAG, versionFormated);
- if (getHTMLcommit().substr(0, 7) == "?")
- {
- LogFile.WriteToFile(ESP_LOG_WARN, TAG, std::string("Failed to read file html/version.txt to parse Web UI version"));
- }
- if (getHTMLcommit().substr(0, 7) != std::string(GIT_REV).substr(0, 7))
- {
- // Compare the first 7 characters of both hashes
- LogFile.WriteToFile(ESP_LOG_WARN, TAG, "Web UI version (" + getHTMLcommit() + ") does not match firmware version (" + std::string(GIT_REV) + ")");
- LogFile.WriteToFile(ESP_LOG_WARN, TAG, "Recommendation: Repeat installation using AI-on-the-edge-device__update__*.zip");
- }
- // Check reboot reason
- // ********************************************
- CheckIsPlannedReboot();
-
- if (!getIsPlannedReboot() && (esp_reset_reason() == ESP_RST_PANIC))
- {
- // If system reboot was not triggered by user and reboot was caused by execption
- LogFile.WriteToFile(ESP_LOG_WARN, TAG, "Reset reason: " + getResetReason());
- LogFile.WriteToFile(ESP_LOG_WARN, TAG, "Device was rebooted due to a software exception! Log level is set to DEBUG until the next reboot. "
- "Flow init is delayed by 5 minutes to check the logs or do an OTA update");
- LogFile.WriteToFile(ESP_LOG_WARN, TAG, "Keep device running until crash occurs again and check logs after device is up again");
- LogFile.setLogLevel(ESP_LOG_DEBUG);
- }
- else
- {
- LogFile.WriteToFile(ESP_LOG_INFO, TAG, "Reset reason: " + getResetReason());
- }
- #ifdef HEAP_TRACING_MAIN_START
- ESP_ERROR_CHECK(heap_trace_stop());
- heap_trace_dump();
- #endif
- #ifdef HEAP_TRACING_MAIN_WIFI
- ESP_ERROR_CHECK(heap_trace_start(HEAP_TRACE_LEAKS));
- #endif
- // Read WLAN parameter and start WIFI
- // ********************************************
- int iWLANStatus = LoadWlanFromFile(WLAN_CONFIG_FILE);
- if (iWLANStatus == 0)
- {
- LogFile.WriteToFile(ESP_LOG_INFO, TAG, "WLAN config loaded, init WIFI...");
- if (wifi_init_sta() != ESP_OK)
- {
- LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "WIFI init failed. Device init aborted!");
- StatusLED(WLAN_INIT, 3, true);
- vTaskDelay(pdMS_TO_TICKS(2000));
- return;
- }
- init_basic_auth();
- }
- else if (iWLANStatus == -1)
- {
- // wlan.ini not available, potentially empty or content not readable
- StatusLED(WLAN_INIT, 1, true);
- vTaskDelay(pdMS_TO_TICKS(2000));
- return; // No way to continue without reading the wlan.ini
- }
- else if (iWLANStatus == -2)
- {
- // SSID or password not configured
- StatusLED(WLAN_INIT, 2, true);
- vTaskDelay(pdMS_TO_TICKS(2000));
- return; // No way to continue with empty SSID or password!
- }
- // Set log level for wifi component to WARN level (default: INFO; only relevant for serial console)
- // ********************************************
- esp_log_level_set("wifi", ESP_LOG_WARN);
- #ifdef HEAP_TRACING_MAIN_WIFI
- ESP_ERROR_CHECK(heap_trace_stop());
- heap_trace_dump();
- #endif
- #ifdef DEBUG_ENABLE_SYSINFO
- #if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(4, 0, 0)
- LogFile.WriteToFile(ESP_LOG_INFO, TAG, "Device Info : " + get_device_info());
- ESP_LOGD(TAG, "Device infos %s", get_device_info().c_str());
- #endif
- #endif // DEBUG_ENABLE_SYSINFO
- #ifdef USE_HIMEM_IF_AVAILABLE
- #ifdef DEBUG_HIMEM_MEMORY_CHECK
- LogFile.WriteToFile(ESP_LOG_INFO, TAG, "Himem mem check : " + himem_memory_check());
- ESP_LOGD(TAG, "Himem mem check %s", himem_memory_check().c_str());
- #endif
- #endif
- // Init camera
- // ********************************************
- esp_err_t camStatus = ESP_OK;
- Camera.LightOnOff(true);
- for (int i = 0; i < 3; i++)
- {
- camStatus = Camera.InitCam();
- if (camStatus == ESP_OK)
- {
- break;
- }
- // De-init in case it was already initialized
- esp_camera_deinit();
- vTaskDelay(pdMS_TO_TICKS(200));
- }
- if (camStatus != ESP_OK)
- {
- // Camera init failed, retry to init
- char camStatusHex[33];
- sprintf(camStatusHex, "0x%02x", camStatus);
- LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Camera init failed (" + std::string(camStatusHex) + ")! Check camera module and/or proper electrical connection");
- setSystemStatusFlag(SYSTEM_STATUS_CAM_BAD);
- StatusLED(CAM_INIT, 1, true);
- vTaskDelay(pdMS_TO_TICKS(2000));
- return; // No way to continue without working Camera!
- }
- // Camera framebuffer check
- // ********************************************
- else if (!Camera.testCamera())
- {
- LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Camera framebuffer check failed");
- // Easiest would be to simply restart here and try again,
- // how ever there seem to be systems where it fails at startup but still work correctly later.
- // Therefore we treat it still as successed! */
- setSystemStatusFlag(SYSTEM_STATUS_CAM_FB_BAD);
- StatusLED(CAM_INIT, 2, false);
- // vTaskDelay(pdMS_TO_TICKS(2000));
- // return; // No way to continue without working Camera!
- }
- else
- {
- // Print camera infos
- // ********************************************
- char caminfo[50];
- sensor_t *sensor = esp_camera_sensor_get();
- sprintf(caminfo, "PID: 0x%02x, VER: 0x%02x, MIDL: 0x%02x, MIDH: 0x%02x", sensor->id.PID, sensor->id.VER, sensor->id.MIDH, sensor->id.MIDL);
- LogFile.WriteToFile(ESP_LOG_INFO, TAG, "Camera info: " + std::string(caminfo));
- }
- Camera.LightOnOff(false); // make sure flashlight is off before start of flow
-
- // Print Device info
- // ********************************************
- esp_chip_info_t chipInfo;
- esp_chip_info(&chipInfo);
- LogFile.WriteToFile(ESP_LOG_INFO, TAG, "Device info: CPU cores: " + std::to_string(chipInfo.cores) + ", Chip revision: " + std::to_string(chipInfo.revision));
- // Print SD-Card info
- // ********************************************
- LogFile.WriteToFile(ESP_LOG_INFO, TAG, "SD card info: Name: " + getSDCardName() + ", Capacity: " + getSDCardCapacity() + "MB, Free: " + getSDCardFreePartitionSpace() + "MB");
- // Start webserver + register handler
- // ********************************************
- ESP_LOGD(TAG, "starting servers");
- server = start_webserver();
-
- register_server_camera_uri(server);
- register_server_main_flow_task_uri(server);
- register_server_file_uri(server, "/sdcard");
- register_server_ota_sdcard_uri(server);
- #ifdef ENABLE_MQTT
- register_server_mqtt_uri(server);
- #endif // ENABLE_MQTT
- gpio_handler_create(server);
- ESP_LOGD(TAG, "Before reg server main");
- register_server_main_uri(server, "/sdcard");
- // manual reset the time
- // ********************************************
- if (!time_manual_reset_sync())
- {
- LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Manual Time Sync failed during startup");
- }
-
- // Check main init + start TFlite task
- // ********************************************
- if (getSystemStatus() == 0)
- {
- // No error flag is set
- LogFile.WriteToFile(ESP_LOG_INFO, TAG, "Initialization completed successfully");
- InitializeFlowTask();
- }
- else if (isSetSystemStatusFlag(SYSTEM_STATUS_CAM_FB_BAD) || isSetSystemStatusFlag(SYSTEM_STATUS_NTP_BAD))
- {
- // Non critical errors occured, we try to continue...
- LogFile.WriteToFile(ESP_LOG_WARN, TAG, "Initialization completed with non-critical errors!");
- InitializeFlowTask();
- }
- else
- {
- // Any other error is critical and makes running the flow impossible. Init is going to abort.
- LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Initialization failed. Flow task start aborted. Loading reduced web interface...");
- }
- }
- std::vector<std::string> splitString(const std::string &str)
- {
- std::vector<std::string> tokens;
- std::stringstream ss(str);
- std::string token;
- while (std::getline(ss, token, '\n'))
- {
- tokens.push_back(token);
- }
- return tokens;
- }
- bool setCpuFrequency(void)
- {
- ConfigFile configFile = ConfigFile(CONFIG_FILE);
- string cpuFrequency = "160";
- esp_pm_config_t pm_config;
- if (!configFile.ConfigFileExists())
- {
- LogFile.WriteToFile(ESP_LOG_WARN, TAG, "No ConfigFile defined - exit setCpuFrequency()!");
- return false;
- }
- std::vector<std::string> splitted;
- std::string line = "";
- bool disabledLine = false;
- bool eof = false;
- /* Load config from config file */
- while ((!configFile.GetNextParagraph(line, disabledLine, eof) || (line.compare("[System]") != 0)) && !eof)
- {
- }
- if (eof || disabledLine)
- {
- return false;
- }
- while (configFile.getNextLine(&line, disabledLine, eof) && !configFile.isNewParagraph(line))
- {
- splitted = ZerlegeZeile(line);
- if (toUpper(splitted[0]) == "CPUFREQUENCY")
- {
- if (splitted.size() < 2)
- {
- cpuFrequency = "160";
- }
- else
- {
- cpuFrequency = splitted[1];
- }
- break;
- }
- }
- if (esp_pm_get_configuration(&pm_config) != ESP_OK)
- {
- LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Failed to read CPU Frequency!");
- return false;
- }
- if (cpuFrequency == "160")
- { // 160 is the default
- // No change needed
- }
- else if (cpuFrequency == "240")
- {
- pm_config.max_freq_mhz = 240;
- pm_config.min_freq_mhz = pm_config.max_freq_mhz;
- if (esp_pm_configure(&pm_config) != ESP_OK)
- {
- LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Failed to set new CPU frequency!");
- return false;
- }
- }
- else
- {
- LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Unknown CPU frequency: " + cpuFrequency + "! "
- "It must be 160 or 240!");
- return false;
- }
- if (esp_pm_get_configuration(&pm_config) == ESP_OK)
- {
- LogFile.WriteToFile(ESP_LOG_INFO, TAG, string("CPU frequency: ") + to_string(pm_config.max_freq_mhz) + " MHz");
- }
- return true;
- }
|