Skip to content

Commit 4ec8820

Browse files
committed
Adding example-01
1 parent cbd57d5 commit 4ec8820

File tree

1 file changed

+199
-0
lines changed

1 file changed

+199
-0
lines changed

example-01/example-01.ino

Lines changed: 199 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,199 @@
1+
#include <WiFi.h>
2+
#include <AsyncTCP.h>
3+
#include <ESPAsyncWebServer.h>
4+
#include <SPIFFS.h>
5+
6+
7+
const String default_ssid = "something";
8+
const String default_wifipassword = "somepass";
9+
const int default_webserverporthttp = 80;
10+
11+
const char index_html[] PROGMEM = R"rawliteral(
12+
<!DOCTYPE HTML>
13+
<html lang="en">
14+
<head>
15+
<meta name="viewport" content="width=device-width, initial-scale=1">
16+
<meta charset="UTF-8">
17+
</head>
18+
<body>
19+
<p>File Upload</p>
20+
<p>Free Storage: %FREESPIFFS% | Used Storage: %USEDSPIFFS% | Total Storage: %TOTALSPIFFS%</p>
21+
<form method="POST" action="/upload" enctype="multipart/form-data"><input type="file" name="data"/><input type="submit" name="upload" value="Upload" title="Upload File"></form>
22+
<p>%FILELIST%</p>
23+
</body>
24+
</html>
25+
)rawliteral";
26+
27+
28+
String listFiles(bool ishtml = false);
29+
30+
// configuration structure
31+
struct Config {
32+
String ssid; // wifi ssid
33+
String wifipassword; // wifi password
34+
int webserverporthttp; // http port number for web admin
35+
};
36+
37+
// variables
38+
Config config; // configuration
39+
AsyncWebServer *server; // initialise webserver
40+
41+
42+
void setup() {
43+
Serial.begin(115200);
44+
45+
Serial.println("Booting ...");
46+
47+
Serial.println("Mounting SPIFFS ...");
48+
if (!SPIFFS.begin(true)) {
49+
// if you have not used SPIFFS before on a ESP32, it will show this error.
50+
// after a reboot SPIFFS will be configured and will happily work.
51+
Serial.println("ERROR: Cannot mount SPIFFS, Rebooting");
52+
rebootESP("ERROR: Cannot mount SPIFFS, Rebooting");
53+
}
54+
55+
Serial.print("SPIFFS Free: "); Serial.println(humanReadableSize((SPIFFS.totalBytes() - SPIFFS.usedBytes())));
56+
Serial.print("SPIFFS Used: "); Serial.println(humanReadableSize(SPIFFS.usedBytes()));
57+
Serial.print("SPIFFS Total: "); Serial.println(humanReadableSize(SPIFFS.totalBytes()));
58+
59+
Serial.println(listFiles());
60+
61+
Serial.println("Loading Configuration ...");
62+
63+
config.ssid = default_ssid;
64+
config.wifipassword = default_wifipassword;
65+
config.webserverporthttp = default_webserverporthttp;
66+
67+
Serial.print("\nConnecting to Wifi: ");
68+
WiFi.begin(config.ssid.c_str(), config.wifipassword.c_str());
69+
while (WiFi.status() != WL_CONNECTED) {
70+
delay(500);
71+
Serial.print(".");
72+
}
73+
74+
Serial.println("\n\nNetwork Configuration:");
75+
Serial.println("----------------------");
76+
Serial.print(" SSID: "); Serial.println(WiFi.SSID());
77+
Serial.print(" Wifi Status: "); Serial.println(WiFi.status());
78+
Serial.print("Wifi Strength: "); Serial.print(WiFi.RSSI()); Serial.println(" dBm");
79+
Serial.print(" MAC: "); Serial.println(WiFi.macAddress());
80+
Serial.print(" IP: "); Serial.println(WiFi.localIP());
81+
Serial.print(" Subnet: "); Serial.println(WiFi.subnetMask());
82+
Serial.print(" Gateway: "); Serial.println(WiFi.gatewayIP());
83+
Serial.print(" DNS 1: "); Serial.println(WiFi.dnsIP(0));
84+
Serial.print(" DNS 2: "); Serial.println(WiFi.dnsIP(1));
85+
Serial.print(" DNS 3: "); Serial.println(WiFi.dnsIP(2));
86+
Serial.println();
87+
88+
// configure web server
89+
Serial.println("Configuring Webserver ...");
90+
server = new AsyncWebServer(config.webserverporthttp);
91+
configureWebServer();
92+
93+
// startup web server
94+
Serial.println("Starting Webserver ...");
95+
server->begin();
96+
}
97+
98+
void loop() {
99+
}
100+
101+
void rebootESP(String message) {
102+
Serial.print("Rebooting ESP32: "); Serial.println(message);
103+
ESP.restart();
104+
}
105+
106+
// list all of the files, if ishtml=true, return html rather than simple text
107+
String listFiles(bool ishtml) {
108+
String returnText = "";
109+
Serial.println("Listing files stored on SPIFFS");
110+
File root = SPIFFS.open("/");
111+
File foundfile = root.openNextFile();
112+
if (ishtml) {
113+
returnText += "<table><tr><th align='left'>Name</th><th align='left'>Size</th></tr>";
114+
}
115+
while (foundfile) {
116+
if (ishtml) {
117+
returnText += "<tr align='left'><td>" + String(foundfile.name()) + "</td><td>" + humanReadableSize(foundfile.size()) + "</td></tr>";
118+
} else {
119+
returnText += "File: " + String(foundfile.name()) + "\n";
120+
}
121+
foundfile = root.openNextFile();
122+
}
123+
if (ishtml) {
124+
returnText += "</table>";
125+
}
126+
root.close();
127+
foundfile.close();
128+
return returnText;
129+
}
130+
131+
// Make size of files human readable
132+
// source: https://github.com/CelliesProjects/minimalUploadAuthESP32
133+
String humanReadableSize(const size_t bytes) {
134+
if (bytes < 1024) return String(bytes) + " B";
135+
else if (bytes < (1024 * 1024)) return String(bytes / 1024.0) + " KB";
136+
else if (bytes < (1024 * 1024 * 1024)) return String(bytes / 1024.0 / 1024.0) + " MB";
137+
else return String(bytes / 1024.0 / 1024.0 / 1024.0) + " GB";
138+
}
139+
140+
void configureWebServer() {
141+
server->on("/", HTTP_GET, [](AsyncWebServerRequest * request) {
142+
String logmessage = "Client:" + request->client()->remoteIP().toString() + + " " + request->url();
143+
Serial.println(logmessage);
144+
request->send_P(200, "text/html", index_html, processor);
145+
});
146+
147+
// run handleUpload function when any file is uploaded
148+
//server->onFileUpload(handleUpload);
149+
server->on("/upload", HTTP_POST, [](AsyncWebServerRequest *request) {
150+
request->send(200);
151+
}, handleUpload);
152+
}
153+
154+
// handles uploads
155+
void handleUpload(AsyncWebServerRequest *request, String filename, size_t index, uint8_t *data, size_t len, bool final) {
156+
String logmessage = "Client:" + request->client()->remoteIP().toString() + " " + request->url();
157+
Serial.println(logmessage);
158+
159+
if (!index) {
160+
logmessage = "Upload Start: " + String(filename);
161+
// open the file on first call and store the file handle in the request object
162+
request->_tempFile = SPIFFS.open("/" + filename, "w");
163+
Serial.println(logmessage);
164+
}
165+
166+
if (len) {
167+
// stream the incoming chunk to the opened file
168+
request->_tempFile.write(data, len);
169+
logmessage = "Writing file: " + String(filename) + " index=" + String(index) + " len=" + String(len);
170+
Serial.println(logmessage);
171+
}
172+
173+
if (final) {
174+
logmessage = "Upload Complete: " + String(filename) + ",size: " + String(index + len);
175+
// close the file handle as the upload is now done
176+
request->_tempFile.close();
177+
Serial.println(logmessage);
178+
request->redirect("/");
179+
}
180+
}
181+
182+
String processor(const String& var) {
183+
if (var == "FILELIST") {
184+
return listFiles(true);
185+
}
186+
if (var == "FREESPIFFS") {
187+
return humanReadableSize((SPIFFS.totalBytes() - SPIFFS.usedBytes()));
188+
}
189+
190+
if (var == "USEDSPIFFS") {
191+
return humanReadableSize(SPIFFS.usedBytes());
192+
}
193+
194+
if (var == "TOTALSPIFFS") {
195+
return humanReadableSize(SPIFFS.totalBytes());
196+
}
197+
198+
return String();
199+
}

0 commit comments

Comments
 (0)