main.cpp 9.1 KB

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