main.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. #include <string>
  2. #include "freertos/FreeRTOS.h"
  3. #include "freertos/task.h"
  4. #include "freertos/event_groups.h"
  5. #include "driver/gpio.h"
  6. #include "sdkconfig.h"
  7. // SD-Card ////////////////////
  8. #include "nvs_flash.h"
  9. #include "esp_vfs_fat.h"
  10. #include "sdmmc_cmd.h"
  11. #include "driver/sdmmc_host.h"
  12. #include "driver/sdmmc_defs.h"
  13. ///////////////////////////////
  14. #include "ClassLogFile.h"
  15. #include "connect_wlan.h"
  16. #include "server_tflite.h"
  17. #include "server_file.h"
  18. #include "server_ota.h"
  19. #include "time_sntp.h"
  20. #include "ClassControllCamera.h"
  21. #include "server_main.h"
  22. #include "server_camera.h"
  23. static const char *TAGMAIN = "connect_wlan_main";
  24. #define FLASH_GPIO GPIO_NUM_4
  25. void Init_NVS_SDCard()
  26. {
  27. esp_err_t ret = nvs_flash_init();
  28. if (ret == ESP_ERR_NVS_NO_FREE_PAGES) {
  29. ESP_ERROR_CHECK(nvs_flash_erase());
  30. ret = nvs_flash_init();
  31. }
  32. ESP_LOGI(TAGMAIN, "Initializing SD card");
  33. sdmmc_host_t host = SDMMC_HOST_DEFAULT();
  34. host.flags = SDMMC_HOST_FLAG_1BIT;
  35. // sdmmc_host_t host = SDMMC_HOST_SLOT_1();
  36. // host.flags = SDMMC_HOST_FLAG_1BIT;
  37. sdmmc_slot_config_t slot_config = SDMMC_SLOT_CONFIG_DEFAULT();
  38. esp_vfs_fat_sdmmc_mount_config_t mount_config = { };
  39. mount_config.format_if_mount_failed = false;
  40. mount_config.max_files = 5;
  41. sdmmc_card_t* card;
  42. ret = esp_vfs_fat_sdmmc_mount("/sdcard", &host, &slot_config, &mount_config, &card);
  43. if (ret != ESP_OK) {
  44. if (ret == ESP_FAIL) {
  45. ESP_LOGE(TAGMAIN, "Failed to mount filesystem. If you want the card to be formatted, set format_if_mount_failed = true.");
  46. } else {
  47. ESP_LOGE(TAGMAIN, "Failed to initialize the card (%d). Make sure SD card lines have pull-up resistors in place.", ret);
  48. }
  49. return;
  50. }
  51. sdmmc_card_print_info(stdout, card);
  52. // Init the GPIO
  53. // Flash ausschalten
  54. gpio_pad_select_gpio(FLASH_GPIO);
  55. gpio_set_direction(FLASH_GPIO, GPIO_MODE_OUTPUT);
  56. gpio_set_level(FLASH_GPIO, 0);
  57. }
  58. extern "C" void app_main(void)
  59. {
  60. printf("Do Reset Camera\n");
  61. PowerResetCamera();
  62. Camera.InitCam();
  63. Camera.LightOnOff(false);
  64. Init_NVS_SDCard();
  65. // LogFile.WriteToFile("Startsequence 02");
  66. CheckOTAUpdate();
  67. // LogFile.WriteToFile("Startsequence 03");
  68. std::string ssid = "";
  69. std::string password = "";
  70. std::string hostname = "";
  71. std::string ip = "";
  72. std::string gw = "";
  73. std::string netmask = "";
  74. std::string dns = "";
  75. // LoadWlanFromFile("/sdcard/wlan.ini", ssid, password, hostname, ip, gw, netmask, dns);
  76. LoadWlanFromFile("/sdcard/wlan.ini", ssid, password, hostname);
  77. // LogFile.WriteToFile("Startsequence 04");
  78. printf("To use WLan: %s, %s\n", ssid.c_str(), password.c_str());
  79. printf("To set Hostename: %s\n", hostname.c_str());
  80. printf("Fixed IP: %s, Gateway %s, Netmask %s\n", ip.c_str(), gw.c_str(), netmask.c_str());
  81. if (ip.length() == 0 || gw.length() == 0 || netmask.length() == 0)
  82. {
  83. initialise_wifi(ssid, password, hostname);
  84. }
  85. else
  86. {
  87. initialise_wifi_fixed_ip(ip, gw, netmask, ssid, password, hostname, dns);
  88. }
  89. printf("Netparameter: IP: %s - GW: %s - NetMask %s\n", getIPAddress().c_str(), getGW().c_str(), getNetMask().c_str());
  90. // LogFile.WriteToFile("Startsequence 05");
  91. TickType_t xDelay;
  92. xDelay = 2000 / portTICK_PERIOD_MS;
  93. printf("Autoflow: sleep for : %ldms\n", (long) xDelay);
  94. // LogFile.WriteToFile("Startsequence 06");
  95. vTaskDelay( xDelay );
  96. // LogFile.WriteToFile("Startsequence 07");
  97. setup_time();
  98. LogFile.WriteToFile("============================== Main Started =======================================");
  99. LogFile.SwitchOnOff(false);
  100. std::string zw = gettimestring("%Y%m%d-%H%M%S");
  101. printf("time %s\n", zw.c_str());
  102. // Camera.InitCam();
  103. // Camera.LightOnOff(false);
  104. xDelay = 2000 / portTICK_PERIOD_MS;
  105. printf("Autoflow: sleep for : %ldms\n", (long) xDelay);
  106. vTaskDelay( xDelay );
  107. server = start_webserver();
  108. register_server_camera_uri(server);
  109. register_server_tflite_uri(server);
  110. register_server_file_uri(server, "/sdcard");
  111. register_server_ota_sdcard_uri(server);
  112. register_server_main_uri(server, "/sdcard");
  113. TFliteDoAutoStart();
  114. }