main.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. #define __SD_USE_ONE_LINE_MODE__
  24. #ifdef __SD_USE_ONE_LINE_MODE__
  25. #include "server_GPIO.h"
  26. #endif
  27. #define BLINK_GPIO GPIO_NUM_33
  28. static const char *TAGMAIN = "connect_wlan_main";
  29. #define FLASH_GPIO GPIO_NUM_4
  30. bool Init_NVS_SDCard()
  31. {
  32. esp_err_t ret = nvs_flash_init();
  33. if (ret == ESP_ERR_NVS_NO_FREE_PAGES) {
  34. ESP_ERROR_CHECK(nvs_flash_erase());
  35. ret = nvs_flash_init();
  36. }
  37. ////////////////////////////////////////////////
  38. ESP_LOGI(TAG, "Using SDMMC peripheral");
  39. sdmmc_host_t host = SDMMC_HOST_DEFAULT();
  40. // This initializes the slot without card detect (CD) and write protect (WP) signals.
  41. // Modify slot_config.gpio_cd and slot_config.gpio_wp if your board has these signals.
  42. sdmmc_slot_config_t slot_config = SDMMC_SLOT_CONFIG_DEFAULT();
  43. // To use 1-line SD mode, uncomment the following line:
  44. #ifdef __SD_USE_ONE_LINE_MODE__
  45. slot_config.width = 1;
  46. #endif
  47. // GPIOs 15, 2, 4, 12, 13 should have external 10k pull-ups.
  48. // Internal pull-ups are not sufficient. However, enabling internal pull-ups
  49. // does make a difference some boards, so we do that here.
  50. gpio_set_pull_mode(GPIO_NUM_15, GPIO_PULLUP_ONLY); // CMD, needed in 4- and 1- line modes
  51. gpio_set_pull_mode(GPIO_NUM_2, GPIO_PULLUP_ONLY); // D0, needed in 4- and 1-line modes
  52. gpio_set_pull_mode(GPIO_NUM_4, GPIO_PULLUP_ONLY); // D1, needed in 4-line mode only
  53. gpio_set_pull_mode(GPIO_NUM_12, GPIO_PULLUP_ONLY); // D2, needed in 4-line mode only
  54. gpio_set_pull_mode(GPIO_NUM_13, GPIO_PULLUP_ONLY); // D3, needed in 4- and 1-line modes
  55. // Options for mounting the filesystem.
  56. // If format_if_mount_failed is set to true, SD card will be partitioned and
  57. // formatted in case when mounting fails.
  58. esp_vfs_fat_sdmmc_mount_config_t mount_config = {
  59. .format_if_mount_failed = false,
  60. .max_files = 5,
  61. .allocation_unit_size = 16 * 1024
  62. };
  63. // Use settings defined above to initialize SD card and mount FAT filesystem.
  64. // Note: esp_vfs_fat_sdmmc_mount is an all-in-one convenience function.
  65. // Please check its source code and implement error recovery when developing
  66. // production applications.
  67. sdmmc_card_t* card;
  68. ret = esp_vfs_fat_sdmmc_mount("/sdcard", &host, &slot_config, &mount_config, &card);
  69. if (ret != ESP_OK) {
  70. if (ret == ESP_FAIL) {
  71. ESP_LOGE(TAG, "Failed to mount filesystem. "
  72. "If you want the card to be formatted, set format_if_mount_failed = true.");
  73. } else {
  74. ESP_LOGE(TAG, "Failed to initialize the card (%s). "
  75. "Make sure SD card lines have pull-up resistors in place.", esp_err_to_name(ret));
  76. }
  77. return false;
  78. }
  79. // Card has been initialized, print its properties
  80. sdmmc_card_print_info(stdout, card);
  81. // Init the GPIO
  82. // Flash ausschalten
  83. gpio_pad_select_gpio(FLASH_GPIO);
  84. gpio_set_direction(FLASH_GPIO, GPIO_MODE_OUTPUT);
  85. gpio_set_level(FLASH_GPIO, 0);
  86. return true;
  87. }
  88. void task_NoSDBlink(void *pvParameter)
  89. {
  90. gpio_pad_select_gpio(BLINK_GPIO);
  91. gpio_set_direction(BLINK_GPIO, GPIO_MODE_OUTPUT);
  92. TickType_t xDelay;
  93. xDelay = 100 / portTICK_PERIOD_MS;
  94. printf("SD-Card could not be inialized - STOP THE PROGRAMM HERE\n");
  95. while (1)
  96. {
  97. gpio_set_level(BLINK_GPIO, 1);
  98. vTaskDelay( xDelay );
  99. gpio_set_level(BLINK_GPIO, 0);
  100. vTaskDelay( xDelay );
  101. }
  102. vTaskDelete(NULL); //Delete this task if it exits from the loop above
  103. }
  104. extern "C" void app_main(void)
  105. {
  106. printf("Do Reset Camera\n");
  107. PowerResetCamera();
  108. Camera.InitCam();
  109. Camera.LightOnOff(false);
  110. if (!Init_NVS_SDCard())
  111. {
  112. xTaskCreate(&task_NoSDBlink, "task_NoSDBlink", configMINIMAL_STACK_SIZE * 64, NULL, tskIDLE_PRIORITY+1, NULL);
  113. return;
  114. };
  115. CheckOTAUpdate();
  116. LoadWlanFromFile("/sdcard/wlan.ini");
  117. ConnectToWLAN();
  118. printf("\nNetparameter: IP: %s - GW: %s - NetMask %s\n", getIPAddress().c_str(), getGW().c_str(), getNetMask().c_str());
  119. TickType_t xDelay;
  120. xDelay = 2000 / portTICK_PERIOD_MS;
  121. printf("Autoflow: sleep for : %ldms\n", (long) xDelay);
  122. // LogFile.WriteToFile("Startsequence 06");
  123. vTaskDelay( xDelay );
  124. // LogFile.WriteToFile("Startsequence 07");
  125. setup_time();
  126. LogFile.WriteToFile("=============================================================================================");
  127. LogFile.WriteToFile("=================================== Main Started ============================================");
  128. LogFile.WriteToFile("=============================================================================================");
  129. LogFile.SwitchOnOff(false);
  130. std::string zw = gettimestring("%Y%m%d-%H%M%S");
  131. printf("time %s\n", zw.c_str());
  132. // Camera.InitCam();
  133. // Camera.LightOnOff(false);
  134. xDelay = 2000 / portTICK_PERIOD_MS;
  135. printf("Autoflow: sleep for : %ldms\n", (long) xDelay);
  136. vTaskDelay( xDelay );
  137. server = start_webserver();
  138. register_server_camera_uri(server);
  139. register_server_tflite_uri(server);
  140. register_server_file_uri(server, "/sdcard");
  141. register_server_ota_sdcard_uri(server);
  142. #ifdef __SD_USE_ONE_LINE_MODE__
  143. register_server_GPIO_uri(server);
  144. #endif
  145. register_server_main_uri(server, "/sdcard");
  146. TFliteDoAutoStart();
  147. }