main.cpp 4.6 KB

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