这个是来自ESP32官方示例的改版,官方的示例由于存在一些问题所以我进行了修改
原本的示例有点逻辑上的问题,所以进行了一些修改
主要修改有:
1.新增SD卡测试部分 复制自官方SD卡示例
2.新增一个根目录页,访问根目录就可以看到
3.修改了目录展示页,可以通过"ip地址/list"看到,原本这个页面只会返回"BAD ARGS"
4.新增Wifi设置页,可以通过Wifi设置页更改链接的Wifi(但是建议不要随便更改,除非连着串口,不然你看不到新的IP地址)
5.新增文件上传页,可以通过网页进行上传(调用原本的"/edit"的链接)
下面是页面展示:
主页
Index.PNG (29.16 KB, 下载次数: 16)
2018-8-27 15:10 上传
Wifi设置页
WifiSetting.PNG (20.95 KB, 下载次数: 14)
2018-8-27 15:10 上传
WIFI信息页
WIFIINFO.PNG (7.28 KB, 下载次数: 19)
2018-8-27 15:10 上传
文件目录页
File List.PNG (43.39 KB, 下载次数: 16)
2018-8-27 15:10 上传
文件上传页
UPLOAD.PNG (3.88 KB, 下载次数: 17)
2018-8-27 15:10 上传
主要代码:
[mw_shl_code=cpp,true]/*
SDWebServer - Example WebServer with SD Card backend for esp8266
Copyright (c) 2015 Hristo Gochkov. All rights reserved.
This file is part of the WebServer library for Arduino environment.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Have a FAT Formatted SD Card connected to the SPI port of the ESP8266
The web root is the SD Card root folder
File extensions with more than 3 charecters are not supported by the SD Library
File Names longer than 8 charecters will be truncated by the SD library, so keep filenames shorter
index.htm is the default index (works on subfolders as well)
upload the contents of SdRoot to the root of the SDcard and access the editor by going to http://esp8266sd.local/edit
Modified by Maoweicao 2018.8.27
*/
#include
#include
#include
#include
#include
#include
#include "FS.h"
#define DBG_OUTPUT_PORT Serial
const char* ssid = "................."; //Replace youself wifi ssid
const char* password = "......................"; //Replace youself wifi password
const char* host = "esp32sd";
String wifissid = "";
String wifipwd = "";
WebServer server(80);
static bool hasSD = false;
File uploadFile;
//format bytes
String formatBytes(size_t bytes) {
if (bytes < 1024) {
return String(bytes) + "B";
} else if (bytes < (1024 * 1024)) {
return String(bytes / 1024.0) + "KB";
} else if (bytes < (1024 * 1024 * 1024)) {
return String(bytes / 1024.0 / 1024.0) + "MB";
} else {
return String(bytes / 1024.0 / 1024.0 / 1024.0) + "GB";
}
}
void listDir(fs::FS &fs, const char * dirname, uint8_t levels){
Serial.printf("Listing directory: %s\n", dirname);
File root = fs.open(dirname);
if(!root){
Serial.println("Failed to open directory");
return;
}
if(!root.isDirectory()){
Serial.println("Not a directory");
return;
}
File file = root.openNextFile();
while(file){
if(file.isDirectory()){
Serial.print(" DIR : ");
Serial.println(file.name());
if(levels){
listDir(fs, file.name(), levels -1);
}
} else {
Serial.print(" FILE: ");
Serial.print(file.name());
Serial.print(" SIZE: ");
Serial.println(file.size());
}
file = root.openNextFile();
}
}
void createDir(fs::FS &fs, const char * path){
Serial.printf("Creating Dir: %s\n", path);
if(fs.mkdir(path)){