main.cpp 7.1 KB

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