main.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. bool debug_detail_heap = true;
  25. #define FLASH_GPIO GPIO_NUM_4
  26. void Init_NVS_SDCard()
  27. {
  28. esp_err_t ret = nvs_flash_init();
  29. if (ret == ESP_ERR_NVS_NO_FREE_PAGES) {
  30. ESP_ERROR_CHECK(nvs_flash_erase());
  31. ret = nvs_flash_init();
  32. }
  33. ESP_LOGI(TAGMAIN, "Initializing SD card");
  34. sdmmc_host_t host = SDMMC_HOST_DEFAULT();
  35. host.flags = SDMMC_HOST_FLAG_1BIT;
  36. // sdmmc_host_t host = SDMMC_HOST_SLOT_1();
  37. // host.flags = SDMMC_HOST_FLAG_1BIT;
  38. sdmmc_slot_config_t slot_config = SDMMC_SLOT_CONFIG_DEFAULT();
  39. slot_config.width = 1; // 1 line SD mode
  40. esp_vfs_fat_sdmmc_mount_config_t mount_config = { };
  41. mount_config.format_if_mount_failed = false;
  42. mount_config.max_files = 5;
  43. gpio_set_pull_mode((gpio_num_t) 15, GPIO_PULLUP_ONLY); // CMD, needed in 4- and 1- line modes
  44. gpio_set_pull_mode((gpio_num_t) 2, GPIO_PULLUP_ONLY); // D0, needed in 4- and 1-line modes
  45. sdmmc_card_t* card;
  46. ret = esp_vfs_fat_sdmmc_mount("/sdcard", &host, &slot_config, &mount_config, &card);
  47. if (ret != ESP_OK) {
  48. if (ret == ESP_FAIL) {
  49. ESP_LOGE(TAGMAIN, "Failed to mount filesystem. If you want the card to be formatted, set format_if_mount_failed = true.");
  50. } else {
  51. ESP_LOGE(TAGMAIN, "Failed to initialize the card (%d). Make sure SD card lines have pull-up resistors in place.", ret);
  52. }
  53. return;
  54. }
  55. sdmmc_card_print_info(stdout, card);
  56. // Init the GPIO
  57. // Flash ausschalten
  58. gpio_pad_select_gpio(FLASH_GPIO);
  59. gpio_set_direction(FLASH_GPIO, GPIO_MODE_OUTPUT);
  60. gpio_set_level(FLASH_GPIO, 0);
  61. }
  62. extern "C" void app_main(void)
  63. {
  64. printf("Do Reset Camera\n");
  65. PowerResetCamera();
  66. Camera.InitCam();
  67. Camera.LightOnOff(false);
  68. Init_NVS_SDCard();
  69. // LogFile.WriteToFile("Startsequence 02");
  70. CheckOTAUpdate();
  71. // LogFile.WriteToFile("Startsequence 03");
  72. std::string ssid = "";
  73. std::string password = "";
  74. std::string hostname = "";
  75. std::string ip = "";
  76. std::string gw = "";
  77. std::string netmask = "";
  78. std::string dns = "";
  79. // LoadWlanFromFile("/sdcard/wlan.ini", ssid, password, hostname, ip, gw, netmask, dns);
  80. LoadWlanFromFile("/sdcard/wlan.ini", ssid, password, hostname);
  81. LoadNetConfigFromFile("/sdcard/wlan.ini", ip, gw, netmask, dns);
  82. // LogFile.WriteToFile("Startsequence 04");
  83. printf("To use WLan: %s, %s\n", ssid.c_str(), password.c_str());
  84. printf("To set Hostename: %s\n", hostname.c_str());
  85. printf("Fixed IP: %s, Gateway %s, Netmask %s, DNS %s\n", ip.c_str(), gw.c_str(), netmask.c_str(), dns.c_str());
  86. if (ip.length() == 0 || gw.length() == 0 || netmask.length() == 0)
  87. {
  88. initialise_wifi(ssid, password, hostname);
  89. }
  90. else
  91. {
  92. initialise_wifi_fixed_ip(ip, gw, netmask, ssid, password, hostname, dns);
  93. }
  94. printf("Netparameter: IP: %s - GW: %s - NetMask %s\n", getIPAddress().c_str(), getGW().c_str(), getNetMask().c_str());
  95. // LogFile.WriteToFile("Startsequence 05");
  96. TickType_t xDelay;
  97. xDelay = 2000 / portTICK_PERIOD_MS;
  98. printf("Autoflow: sleep for : %ldms\n", (long) xDelay);
  99. // LogFile.WriteToFile("Startsequence 06");
  100. vTaskDelay( xDelay );
  101. // LogFile.WriteToFile("Startsequence 07");
  102. setup_time();
  103. LogFile.WriteToFile("=============================================================================================");
  104. LogFile.WriteToFile("=================================== Main Started ============================================");
  105. LogFile.WriteToFile("=============================================================================================");
  106. LogFile.SwitchOnOff(false);
  107. std::string zw = gettimestring("%Y%m%d-%H%M%S");
  108. printf("time %s\n", zw.c_str());
  109. // Camera.InitCam();
  110. // Camera.LightOnOff(false);
  111. xDelay = 2000 / portTICK_PERIOD_MS;
  112. printf("Autoflow: sleep for : %ldms\n", (long) xDelay);
  113. vTaskDelay( xDelay );
  114. server = start_webserver();
  115. register_server_camera_uri(server);
  116. register_server_tflite_uri(server);
  117. register_server_file_uri(server, "/sdcard");
  118. register_server_ota_sdcard_uri(server);
  119. register_server_main_uri(server, "/sdcard");
  120. TFliteDoAutoStart();
  121. }