esp_rest_main.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /* HTTP Restful API Server Example
  2. This example code is in the Public Domain (or CC0 licensed, at your option.)
  3. Unless required by applicable law or agreed to in writing, this
  4. software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  5. CONDITIONS OF ANY KIND, either express or implied.
  6. */
  7. #include "sdkconfig.h"
  8. #include "driver/gpio.h"
  9. #include "esp_vfs_semihost.h"
  10. #include "esp_vfs_fat.h"
  11. #include "esp_spiffs.h"
  12. #include "sdmmc_cmd.h"
  13. #include "nvs_flash.h"
  14. #include "esp_netif.h"
  15. #include "esp_event.h"
  16. #include "esp_log.h"
  17. #include "mdns.h"
  18. #include "lwip/apps/netbiosns.h"
  19. #include "protocol_examples_common.h"
  20. #if CONFIG_EXAMPLE_WEB_DEPLOY_SD
  21. #include "driver/sdmmc_host.h"
  22. #endif
  23. #define MDNS_INSTANCE "esp home web server"
  24. static const char *TAG = "example";
  25. esp_err_t start_rest_server(const char *base_path);
  26. static void initialise_mdns(void)
  27. {
  28. mdns_init();
  29. mdns_hostname_set(CONFIG_EXAMPLE_MDNS_HOST_NAME);
  30. mdns_instance_name_set(MDNS_INSTANCE);
  31. mdns_txt_item_t serviceTxtData[] = {
  32. {"board", "esp32"},
  33. {"path", "/"}
  34. };
  35. ESP_ERROR_CHECK(mdns_service_add("ESP32-WebServer", "_http", "_tcp", 80, serviceTxtData,
  36. sizeof(serviceTxtData) / sizeof(serviceTxtData[0])));
  37. }
  38. #if CONFIG_EXAMPLE_WEB_DEPLOY_SEMIHOST
  39. esp_err_t init_fs(void)
  40. {
  41. esp_err_t ret = esp_vfs_semihost_register(CONFIG_EXAMPLE_WEB_MOUNT_POINT);
  42. if (ret != ESP_OK) {
  43. ESP_LOGE(TAG, "Failed to register semihost driver (%s)!", esp_err_to_name(ret));
  44. return ESP_FAIL;
  45. }
  46. return ESP_OK;
  47. }
  48. #endif
  49. #if CONFIG_EXAMPLE_WEB_DEPLOY_SD
  50. esp_err_t init_fs(void)
  51. {
  52. sdmmc_host_t host = SDMMC_HOST_DEFAULT();
  53. sdmmc_slot_config_t slot_config = SDMMC_SLOT_CONFIG_DEFAULT();
  54. gpio_set_pull_mode(15, GPIO_PULLUP_ONLY); // CMD
  55. gpio_set_pull_mode(2, GPIO_PULLUP_ONLY); // D0
  56. gpio_set_pull_mode(4, GPIO_PULLUP_ONLY); // D1
  57. gpio_set_pull_mode(12, GPIO_PULLUP_ONLY); // D2
  58. gpio_set_pull_mode(13, GPIO_PULLUP_ONLY); // D3
  59. esp_vfs_fat_sdmmc_mount_config_t mount_config = {
  60. .format_if_mount_failed = true,
  61. .max_files = 4,
  62. .allocation_unit_size = 16 * 1024
  63. };
  64. sdmmc_card_t *card;
  65. esp_err_t ret = esp_vfs_fat_sdmmc_mount(CONFIG_EXAMPLE_WEB_MOUNT_POINT, &host, &slot_config, &mount_config, &card);
  66. if (ret != ESP_OK) {
  67. if (ret == ESP_FAIL) {
  68. ESP_LOGE(TAG, "Failed to mount filesystem.");
  69. } else {
  70. ESP_LOGE(TAG, "Failed to initialize the card (%s)", esp_err_to_name(ret));
  71. }
  72. return ESP_FAIL;
  73. }
  74. /* print card info if mount successfully */
  75. sdmmc_card_print_info(stdout, card);
  76. return ESP_OK;
  77. }
  78. #endif
  79. #if CONFIG_EXAMPLE_WEB_DEPLOY_SF
  80. esp_err_t init_fs(void)
  81. {
  82. esp_vfs_spiffs_conf_t conf = {
  83. .base_path = CONFIG_EXAMPLE_WEB_MOUNT_POINT,
  84. .partition_label = NULL,
  85. .max_files = 5,
  86. .format_if_mount_failed = false
  87. };
  88. esp_err_t ret = esp_vfs_spiffs_register(&conf);
  89. if (ret != ESP_OK) {
  90. if (ret == ESP_FAIL) {
  91. ESP_LOGE(TAG, "Failed to mount or format filesystem");
  92. } else if (ret == ESP_ERR_NOT_FOUND) {
  93. ESP_LOGE(TAG, "Failed to find SPIFFS partition");
  94. } else {
  95. ESP_LOGE(TAG, "Failed to initialize SPIFFS (%s)", esp_err_to_name(ret));
  96. }
  97. return ESP_FAIL;
  98. }
  99. size_t total = 0, used = 0;
  100. ret = esp_spiffs_info(NULL, &total, &used);
  101. if (ret != ESP_OK) {
  102. ESP_LOGE(TAG, "Failed to get SPIFFS partition information (%s)", esp_err_to_name(ret));
  103. } else {
  104. ESP_LOGI(TAG, "Partition size: total: %d, used: %d", total, used);
  105. }
  106. return ESP_OK;
  107. }
  108. #endif
  109. void app_main(void)
  110. {
  111. ESP_ERROR_CHECK(nvs_flash_init());
  112. ESP_ERROR_CHECK(esp_netif_init());
  113. ESP_ERROR_CHECK(esp_event_loop_create_default());
  114. initialise_mdns();
  115. netbiosns_init();
  116. netbiosns_set_name(CONFIG_EXAMPLE_MDNS_HOST_NAME);
  117. ESP_ERROR_CHECK(example_connect());
  118. ESP_ERROR_CHECK(init_fs());
  119. ESP_ERROR_CHECK(start_rest_server(CONFIG_EXAMPLE_WEB_MOUNT_POINT));
  120. }