main.cpp 4.9 KB

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