2019-05-10 05:21:14 +00:00
|
|
|
/* HTTP Restful API Server Example
|
|
|
|
|
|
|
|
This example code is in the Public Domain (or CC0 licensed, at your option.)
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, this
|
|
|
|
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
|
|
|
CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
*/
|
2019-10-14 05:41:46 +00:00
|
|
|
#include "sdkconfig.h"
|
2019-05-10 05:21:14 +00:00
|
|
|
#include "driver/gpio.h"
|
|
|
|
#include "esp_vfs_semihost.h"
|
|
|
|
#include "esp_vfs_fat.h"
|
|
|
|
#include "esp_spiffs.h"
|
|
|
|
#include "sdmmc_cmd.h"
|
|
|
|
#include "nvs_flash.h"
|
2019-08-31 14:19:21 +00:00
|
|
|
#include "esp_netif.h"
|
2019-05-10 05:21:14 +00:00
|
|
|
#include "esp_event.h"
|
|
|
|
#include "esp_log.h"
|
|
|
|
#include "mdns.h"
|
2019-10-14 05:41:46 +00:00
|
|
|
#include "lwip/apps/netbiosns.h"
|
2019-05-10 05:21:14 +00:00
|
|
|
#include "protocol_examples_common.h"
|
2019-10-31 06:42:29 +00:00
|
|
|
#if CONFIG_EXAMPLE_WEB_DEPLOY_SD
|
|
|
|
#include "driver/sdmmc_host.h"
|
|
|
|
#endif
|
2019-05-10 05:21:14 +00:00
|
|
|
|
|
|
|
#define MDNS_INSTANCE "esp home web server"
|
|
|
|
|
|
|
|
static const char *TAG = "example";
|
|
|
|
|
|
|
|
esp_err_t start_rest_server(const char *base_path);
|
|
|
|
|
|
|
|
static void initialise_mdns(void)
|
|
|
|
{
|
|
|
|
mdns_init();
|
2019-05-09 14:43:06 +00:00
|
|
|
mdns_hostname_set(CONFIG_EXAMPLE_MDNS_HOST_NAME);
|
2019-05-10 05:21:14 +00:00
|
|
|
mdns_instance_name_set(MDNS_INSTANCE);
|
|
|
|
|
|
|
|
mdns_txt_item_t serviceTxtData[] = {
|
|
|
|
{"board", "esp32"},
|
|
|
|
{"path", "/"}
|
|
|
|
};
|
|
|
|
|
|
|
|
ESP_ERROR_CHECK(mdns_service_add("ESP32-WebServer", "_http", "_tcp", 80, serviceTxtData,
|
|
|
|
sizeof(serviceTxtData) / sizeof(serviceTxtData[0])));
|
|
|
|
}
|
|
|
|
|
2019-05-09 14:43:06 +00:00
|
|
|
#if CONFIG_EXAMPLE_WEB_DEPLOY_SEMIHOST
|
2019-05-10 05:21:14 +00:00
|
|
|
esp_err_t init_fs(void)
|
|
|
|
{
|
2019-05-09 14:43:06 +00:00
|
|
|
esp_err_t ret = esp_vfs_semihost_register(CONFIG_EXAMPLE_WEB_MOUNT_POINT, CONFIG_EXAMPLE_HOST_PATH_TO_MOUNT);
|
2019-05-10 05:21:14 +00:00
|
|
|
if (ret != ESP_OK) {
|
|
|
|
ESP_LOGE(TAG, "Failed to register semihost driver (%s)!", esp_err_to_name(ret));
|
|
|
|
return ESP_FAIL;
|
|
|
|
}
|
|
|
|
return ESP_OK;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2019-05-09 14:43:06 +00:00
|
|
|
#if CONFIG_EXAMPLE_WEB_DEPLOY_SD
|
2019-05-10 05:21:14 +00:00
|
|
|
esp_err_t init_fs(void)
|
|
|
|
{
|
|
|
|
sdmmc_host_t host = SDMMC_HOST_DEFAULT();
|
|
|
|
sdmmc_slot_config_t slot_config = SDMMC_SLOT_CONFIG_DEFAULT();
|
|
|
|
|
|
|
|
gpio_set_pull_mode(15, GPIO_PULLUP_ONLY); // CMD
|
|
|
|
gpio_set_pull_mode(2, GPIO_PULLUP_ONLY); // D0
|
|
|
|
gpio_set_pull_mode(4, GPIO_PULLUP_ONLY); // D1
|
|
|
|
gpio_set_pull_mode(12, GPIO_PULLUP_ONLY); // D2
|
|
|
|
gpio_set_pull_mode(13, GPIO_PULLUP_ONLY); // D3
|
|
|
|
|
|
|
|
esp_vfs_fat_sdmmc_mount_config_t mount_config = {
|
|
|
|
.format_if_mount_failed = true,
|
|
|
|
.max_files = 4,
|
|
|
|
.allocation_unit_size = 16 * 1024
|
|
|
|
};
|
|
|
|
|
|
|
|
sdmmc_card_t *card;
|
2019-05-09 14:43:06 +00:00
|
|
|
esp_err_t ret = esp_vfs_fat_sdmmc_mount(CONFIG_EXAMPLE_WEB_MOUNT_POINT, &host, &slot_config, &mount_config, &card);
|
2019-05-10 05:21:14 +00:00
|
|
|
if (ret != ESP_OK) {
|
|
|
|
if (ret == ESP_FAIL) {
|
|
|
|
ESP_LOGE(TAG, "Failed to mount filesystem.");
|
|
|
|
} else {
|
|
|
|
ESP_LOGE(TAG, "Failed to initialize the card (%s)", esp_err_to_name(ret));
|
|
|
|
}
|
|
|
|
return ESP_FAIL;
|
|
|
|
}
|
|
|
|
/* print card info if mount successfully */
|
|
|
|
sdmmc_card_print_info(stdout, card);
|
|
|
|
return ESP_OK;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2019-05-09 14:43:06 +00:00
|
|
|
#if CONFIG_EXAMPLE_WEB_DEPLOY_SF
|
2019-05-10 05:21:14 +00:00
|
|
|
esp_err_t init_fs(void)
|
|
|
|
{
|
|
|
|
esp_vfs_spiffs_conf_t conf = {
|
2019-05-09 14:43:06 +00:00
|
|
|
.base_path = CONFIG_EXAMPLE_WEB_MOUNT_POINT,
|
2019-05-10 05:21:14 +00:00
|
|
|
.partition_label = NULL,
|
|
|
|
.max_files = 5,
|
|
|
|
.format_if_mount_failed = false
|
|
|
|
};
|
|
|
|
esp_err_t ret = esp_vfs_spiffs_register(&conf);
|
|
|
|
|
|
|
|
if (ret != ESP_OK) {
|
|
|
|
if (ret == ESP_FAIL) {
|
|
|
|
ESP_LOGE(TAG, "Failed to mount or format filesystem");
|
|
|
|
} else if (ret == ESP_ERR_NOT_FOUND) {
|
|
|
|
ESP_LOGE(TAG, "Failed to find SPIFFS partition");
|
|
|
|
} else {
|
|
|
|
ESP_LOGE(TAG, "Failed to initialize SPIFFS (%s)", esp_err_to_name(ret));
|
|
|
|
}
|
|
|
|
return ESP_FAIL;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t total = 0, used = 0;
|
|
|
|
ret = esp_spiffs_info(NULL, &total, &used);
|
|
|
|
if (ret != ESP_OK) {
|
|
|
|
ESP_LOGE(TAG, "Failed to get SPIFFS partition information (%s)", esp_err_to_name(ret));
|
|
|
|
} else {
|
|
|
|
ESP_LOGI(TAG, "Partition size: total: %d, used: %d", total, used);
|
|
|
|
}
|
|
|
|
return ESP_OK;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2019-07-16 09:33:30 +00:00
|
|
|
void app_main(void)
|
2019-05-10 05:21:14 +00:00
|
|
|
{
|
|
|
|
ESP_ERROR_CHECK(nvs_flash_init());
|
2019-11-29 09:54:02 +00:00
|
|
|
ESP_ERROR_CHECK(esp_netif_init());
|
2019-05-10 05:21:14 +00:00
|
|
|
ESP_ERROR_CHECK(esp_event_loop_create_default());
|
|
|
|
initialise_mdns();
|
2019-10-14 05:41:46 +00:00
|
|
|
netbiosns_init();
|
|
|
|
netbiosns_set_name(CONFIG_EXAMPLE_MDNS_HOST_NAME);
|
2019-05-10 05:21:14 +00:00
|
|
|
|
|
|
|
ESP_ERROR_CHECK(example_connect());
|
|
|
|
ESP_ERROR_CHECK(init_fs());
|
2019-05-09 14:43:06 +00:00
|
|
|
ESP_ERROR_CHECK(start_rest_server(CONFIG_EXAMPLE_WEB_MOUNT_POINT));
|
2019-05-10 05:21:14 +00:00
|
|
|
}
|