main.cpp 5.7 KB

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