2018-12-16 13:10:24 -05:00
|
|
|
/* HTTP File 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/param.h>
|
|
|
|
|
|
|
|
#include "esp_wifi.h"
|
2019-07-05 04:58:04 -04:00
|
|
|
#include "esp_event.h"
|
2018-12-16 13:10:24 -05:00
|
|
|
#include "esp_log.h"
|
|
|
|
#include "esp_system.h"
|
|
|
|
#include "esp_spiffs.h"
|
|
|
|
#include "nvs_flash.h"
|
2019-08-31 10:19:21 -04:00
|
|
|
#include "esp_netif.h"
|
2018-11-20 11:42:37 -05:00
|
|
|
#include "protocol_examples_common.h"
|
2018-12-16 13:10:24 -05:00
|
|
|
|
|
|
|
/* This example demonstrates how to create file server
|
|
|
|
* using esp_http_server. This file has only startup code.
|
|
|
|
* Look in file_server.c for the implementation */
|
|
|
|
|
|
|
|
static const char *TAG="example";
|
|
|
|
|
|
|
|
/* Function to initialize SPIFFS */
|
|
|
|
static esp_err_t init_spiffs(void)
|
|
|
|
{
|
|
|
|
ESP_LOGI(TAG, "Initializing SPIFFS");
|
|
|
|
|
|
|
|
esp_vfs_spiffs_conf_t conf = {
|
|
|
|
.base_path = "/spiffs",
|
|
|
|
.partition_label = NULL,
|
|
|
|
.max_files = 5, // This decides the maximum number of files that can be created on the storage
|
|
|
|
.format_if_mount_failed = true
|
|
|
|
};
|
|
|
|
|
|
|
|
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));
|
|
|
|
return ESP_FAIL;
|
|
|
|
}
|
|
|
|
|
|
|
|
ESP_LOGI(TAG, "Partition size: total: %d, used: %d", total, used);
|
|
|
|
return ESP_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Declare the function which starts the file server.
|
|
|
|
* Implementation of this function is to be found in
|
|
|
|
* file_server.c */
|
|
|
|
esp_err_t start_file_server(const char *base_path);
|
|
|
|
|
2019-07-16 05:33:30 -04:00
|
|
|
void app_main(void)
|
2018-12-16 13:10:24 -05:00
|
|
|
{
|
2018-11-20 11:42:37 -05:00
|
|
|
ESP_ERROR_CHECK(nvs_flash_init());
|
2019-11-29 04:54:02 -05:00
|
|
|
ESP_ERROR_CHECK(esp_netif_init());
|
2018-11-20 11:42:37 -05:00
|
|
|
ESP_ERROR_CHECK(esp_event_loop_create_default());
|
|
|
|
|
|
|
|
/* This helper function configures Wi-Fi or Ethernet, as selected in menuconfig.
|
|
|
|
* Read "Establishing Wi-Fi or Ethernet Connection" section in
|
|
|
|
* examples/protocols/README.md for more information about this function.
|
|
|
|
*/
|
|
|
|
ESP_ERROR_CHECK(example_connect());
|
2018-12-16 13:10:24 -05:00
|
|
|
|
|
|
|
/* Initialize file storage */
|
|
|
|
ESP_ERROR_CHECK(init_spiffs());
|
|
|
|
|
|
|
|
/* Start the file server */
|
|
|
|
ESP_ERROR_CHECK(start_file_server("/spiffs"));
|
|
|
|
}
|