main.cpp 8.9 KB

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