main.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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 "Helper.h"
  26. extern const char* GIT_TAG;
  27. extern const char* GIT_REV;
  28. extern const char* GIT_BRANCH;
  29. extern const char* BUILD_TIME;
  30. // #include "jomjol_WS2812Slow.h"
  31. #include "SmartLeds.h"
  32. #define __SD_USE_ONE_LINE_MODE__
  33. #include "server_GPIO.h"
  34. #define BLINK_GPIO GPIO_NUM_33
  35. static const char *TAGMAIN = "main";
  36. //#define FLASH_GPIO GPIO_NUM_4
  37. bool Init_NVS_SDCard()
  38. {
  39. esp_err_t ret = nvs_flash_init();
  40. if (ret == ESP_ERR_NVS_NO_FREE_PAGES) {
  41. ESP_ERROR_CHECK(nvs_flash_erase());
  42. ret = nvs_flash_init();
  43. }
  44. ////////////////////////////////////////////////
  45. ESP_LOGI(TAGMAIN, "Using SDMMC peripheral");
  46. sdmmc_host_t host = SDMMC_HOST_DEFAULT();
  47. // This initializes the slot without card detect (CD) and write protect (WP) signals.
  48. // Modify slot_config.gpio_cd and slot_config.gpio_wp if your board has these signals.
  49. sdmmc_slot_config_t slot_config = SDMMC_SLOT_CONFIG_DEFAULT();
  50. // To use 1-line SD mode, uncomment the following line:
  51. #ifdef __SD_USE_ONE_LINE_MODE__
  52. slot_config.width = 1;
  53. #endif
  54. // GPIOs 15, 2, 4, 12, 13 should have external 10k pull-ups.
  55. // Internal pull-ups are not sufficient. However, enabling internal pull-ups
  56. // does make a difference some boards, so we do that here.
  57. gpio_set_pull_mode(GPIO_NUM_15, GPIO_PULLUP_ONLY); // CMD, needed in 4- and 1- line modes
  58. gpio_set_pull_mode(GPIO_NUM_2, GPIO_PULLUP_ONLY); // D0, needed in 4- and 1-line modes
  59. #ifndef __SD_USE_ONE_LINE_MODE__
  60. gpio_set_pull_mode(GPIO_NUM_4, GPIO_PULLUP_ONLY); // D1, needed in 4-line mode only
  61. gpio_set_pull_mode(GPIO_NUM_12, GPIO_PULLUP_ONLY); // D2, needed in 4-line mode only
  62. #endif
  63. gpio_set_pull_mode(GPIO_NUM_13, GPIO_PULLUP_ONLY); // D3, needed in 4- and 1-line modes
  64. // Options for mounting the filesystem.
  65. // If format_if_mount_failed is set to true, SD card will be partitioned and
  66. // formatted in case when mounting fails.
  67. esp_vfs_fat_sdmmc_mount_config_t mount_config = {
  68. .format_if_mount_failed = false,
  69. .max_files = 7, // anstatt 5 (2022-09-21)
  70. .allocation_unit_size = 16 * 1024
  71. };
  72. // Use settings defined above to initialize SD card and mount FAT filesystem.
  73. // Note: esp_vfs_fat_sdmmc_mount is an all-in-one convenience function.
  74. // Please check its source code and implement error recovery when developing
  75. // production applications.
  76. sdmmc_card_t* card;
  77. ret = esp_vfs_fat_sdmmc_mount("/sdcard", &host, &slot_config, &mount_config, &card);
  78. if (ret != ESP_OK) {
  79. if (ret == ESP_FAIL) {
  80. ESP_LOGE(TAGMAIN, "Failed to mount filesystem. "
  81. "If you want the card to be formatted, set format_if_mount_failed = true.");
  82. } else {
  83. ESP_LOGE(TAGMAIN, "Failed to initialize the card (%s). "
  84. "Make sure SD card lines have pull-up resistors in place.", esp_err_to_name(ret));
  85. }
  86. return false;
  87. }
  88. sdmmc_card_print_info(stdout, card);
  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. TickType_t xDelay;
  110. string versionFormated = "Branch: '" + std::string(GIT_BRANCH) + "', Tag: '" + std::string(GIT_TAG) + \
  111. "', Revision: " + std::string(GIT_REV) +", Date/Time: " + std::string(BUILD_TIME);
  112. printf("=============================================================================================\n");
  113. printf("%s\n", versionFormated.c_str());
  114. printf("=============================================================================================\n");
  115. PowerResetCamera();
  116. esp_err_t cam = Camera.InitCam();
  117. Camera.LightOnOff(false);
  118. xDelay = 2000 / portTICK_PERIOD_MS;
  119. printf("nach init camera: sleep for : %ldms\n", (long) xDelay);
  120. vTaskDelay( xDelay );
  121. if (!Init_NVS_SDCard())
  122. {
  123. xTaskCreate(&task_NoSDBlink, "task_NoSDBlink", configMINIMAL_STACK_SIZE * 64, NULL, tskIDLE_PRIORITY+1, NULL);
  124. return;
  125. };
  126. CheckOTAUpdate();
  127. char *ssid = NULL, *passwd = NULL, *hostname = NULL, *ip = NULL, *gateway = NULL, *netmask = NULL, *dns = NULL;
  128. LoadWlanFromFile("/sdcard/wlan.ini", ssid, passwd, hostname, ip, gateway, netmask, dns);
  129. if (ssid != NULL && passwd != NULL)
  130. printf("\nWLan: %s, %s\n", ssid, passwd);
  131. else
  132. printf("No SSID and PASSWORD set!!!");
  133. if (hostname != NULL)
  134. printf("Hostename: %s\n", hostname);
  135. else
  136. printf("Hostname not set.\n");
  137. if (ip != NULL && gateway != NULL && netmask != NULL)
  138. printf("Fixed IP: %s, Gateway %s, Netmask %s\n", ip, gateway, netmask);
  139. if (dns != NULL)
  140. printf("DNS IP: %s\n", dns);
  141. wifi_init_sta(ssid, passwd, hostname, ip, gateway, netmask, dns);
  142. xDelay = 2000 / portTICK_PERIOD_MS;
  143. printf("main: sleep for : %ldms\n", (long) xDelay);
  144. vTaskDelay( xDelay );
  145. setup_time();
  146. setBootTime();
  147. LogFile.WriteToFile("=============================================================================================");
  148. LogFile.WriteToFile("=================================== Main Started ============================================");
  149. LogFile.WriteToFile("=============================================================================================");
  150. LogFile.WriteToFile(versionFormated);
  151. LogFile.SwitchOnOff(false);
  152. std::string zw = gettimestring("%Y%m%d-%H%M%S");
  153. printf("time %s\n", zw.c_str());
  154. size_t _hsize = getESPHeapSize();
  155. if (_hsize < 4000000)
  156. {
  157. std::string _zws = "Not enought PSRAM available. Expected 4.194.304 MByte - available: " + std::to_string(_hsize);
  158. _zws = _zws + "\nEither not initialzed or too small (2MByte only) or not present at all. Firmware cannot start!!";
  159. printf(_zws.c_str());
  160. LogFile.SwitchOnOff(true);
  161. LogFile.WriteToFile(_zws);
  162. LogFile.SwitchOnOff(false);
  163. } else {
  164. if (cam != ESP_OK) {
  165. ESP_LOGE(TAGMAIN, "Failed to initialize camera module. "
  166. "Check that your camera module is working and connected properly.");
  167. LogFile.SwitchOnOff(true);
  168. LogFile.WriteToFile("Failed to initialize camera module. "
  169. "Check that your camera module is working and connected properly.");
  170. LogFile.SwitchOnOff(false);
  171. } else {
  172. // Test Camera
  173. camera_fb_t * fb = esp_camera_fb_get();
  174. if (!fb) {
  175. ESP_LOGE(TAGMAIN, "esp_camera_fb_get: Camera Capture Failed");
  176. LogFile.SwitchOnOff(true);
  177. LogFile.WriteToFile("Camera cannot be initialzed. "
  178. "System will reboot.");
  179. doReboot();
  180. }
  181. esp_camera_fb_return(fb);
  182. Camera.LightOnOff(false);
  183. }
  184. }
  185. xDelay = 2000 / portTICK_PERIOD_MS;
  186. printf("main: sleep for : %ldms\n", (long) xDelay*10);
  187. vTaskDelay( xDelay );
  188. printf("starting server\n");
  189. server = start_webserver();
  190. register_server_camera_uri(server);
  191. register_server_tflite_uri(server);
  192. register_server_file_uri(server, "/sdcard");
  193. register_server_ota_sdcard_uri(server);
  194. gpio_handler_create(server);
  195. printf("vor reg server main\n");
  196. register_server_main_uri(server, "/sdcard");
  197. printf("vor dotautostart\n");
  198. TFliteDoAutoStart();
  199. }