main.cpp 6.6 KB

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