Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <ESPAsyncWebServer.h>
- #include <ArduinoJson.h>
- #include <SPIFFS.h>
- void handleUpload(AsyncWebServerRequest *request, String filename, size_t index, uint8_t *data, size_t len, bool final) {
- String logmessage = "Client:" + request->client()->remoteIP().toString() + " " + request->url();
- Serial.println(logmessage);
- if (!index) {
- logmessage = "Upload Start: " + String(filename);
- // open the file on first call and store the file handle in the request object
- request->_tempFile = SPIFFS.open("/" + filename, "w");
- Serial.println(logmessage);
- }
- if (len) {
- // stream the incoming chunk to the opened file
- request->_tempFile.write(data, len);
- logmessage = "Writing file: " + String(filename) + " index=" + String(index) + " len=" + String(len);
- Serial.println(logmessage);
- }
- if (final) {
- logmessage = "Upload Complete: " + String(filename) + ",size: " + String(index + len);
- // close the file handle as the upload is now done
- request->_tempFile.close();
- Serial.println(logmessage);
- request->redirect("/");
- }
- }
- String humanReadableSize(const 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";
- }
- String listFiles() {
- String returnText = "";
- Serial.println("Listing files stored on SPIFFS");
- File root = SPIFFS.open("/");
- File foundfile = root.openNextFile();
- returnText += "<table><tr><th align='left'>Name</th><th align='left'>Size</th></tr>";
- while (foundfile) {
- returnText += "<tr align='left'><td><a href='/www/" + String(foundfile.name()) + "'>" + String(foundfile.name()) + "</a></td><td>" + humanReadableSize(foundfile.size()) + "</td></tr>";
- foundfile = root.openNextFile();
- }
- returnText += "</table>";
- returnText += "<br><a href=\"/upload\">Upload new file...</a>";
- root.close();
- foundfile.close();
- return returnText;
- }
- void appendFile(fs::FS &fs, const char * path, const char * message) {
- Serial.printf("Appending to file: %s\r\n", path);
- File file = fs.open(path, FILE_APPEND);
- if (!file) {
- Serial.println("- failed to open file for appending");
- return;
- }
- if (file.print(message)) {
- Serial.println("- message appended");
- } else {
- Serial.println("- append failed");
- }
- file.close();
- }
- AsyncWebServer server(80);
- const char* ssid = "iPhone (Bartek)";
- const char* password = "bartek12";
- void setup() {
- Serial.begin(115200);
- WiFi.mode(WIFI_STA);
- WiFi.begin(ssid, password);
- if (WiFi.waitForConnectResult() != WL_CONNECTED) {
- Serial.printf("WiFi Failed!\n");
- return;
- }
- if (!SPIFFS.begin()) {
- Serial.println("LittleFS Mount Failed, formatting!");
- SPIFFS.format();
- if (!SPIFFS.begin()) {
- Serial.println("LittleFS Mount Failed after formatting, throw.");
- return;
- }
- }
- Serial.print("IP Address: ");
- Serial.println(WiFi.localIP());
- server.on("/update", HTTP_GET, [](AsyncWebServerRequest * request) {
- DynamicJsonDocument doc(512);
- File f;
- if (SPIFFS.exists("/data.json")) {
- f = SPIFFS.open("/data.json", "r");
- deserializeJson(doc, f);
- f.close();
- }
- JsonObject e = doc.createNestedObject();
- e["timestamp"] = millis();
- e["data"] = random(0, 100);
- f = SPIFFS.open("/data.json", "w");
- serializeJson(doc, f);
- f.close();
- request->send(200, "text/plain", "OK");
- });
- server.on("/file", HTTP_GET, [](AsyncWebServerRequest * request) {
- request->send(SPIFFS, "/data.json", "application/json");
- });
- server.on("/upload", HTTP_GET, [](AsyncWebServerRequest * request) {
- request->send(200, "text/html", "<form method='POST' action='/upload' enctype='multipart/form-data'><input type='file' name='upload'><input type='submit' value='upload'></form>");
- });
- server.on("/upload", HTTP_POST, [](AsyncWebServerRequest * request) {
- request->send(200);
- }, handleUpload);
- server.on("/", HTTP_GET, [](AsyncWebServerRequest * request) {
- request->send(200, "text/html", listFiles());
- });
- server.serveStatic("/www/", SPIFFS, "/");
- server.begin();
- }
- void loop() {
- }
Advertisement
Add Comment
Please, Sign In to add comment