mikolajmki

arduino_hacker

Nov 10th, 2024
239
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.24 KB | None | 0 0
  1. #include <ESPAsyncWebServer.h>
  2. #include <ArduinoJson.h>
  3. #include <SPIFFS.h>
  4.  
  5. void handleUpload(AsyncWebServerRequest *request, String filename, size_t index, uint8_t *data, size_t len, bool final) {
  6.   String logmessage = "Client:" + request->client()->remoteIP().toString() + " " + request->url();
  7.   Serial.println(logmessage);
  8.  
  9.   if (!index) {
  10.     logmessage = "Upload Start: " + String(filename);
  11.     // open the file on first call and store the file handle in the request object
  12.     request->_tempFile = SPIFFS.open("/" + filename, "w");
  13.     Serial.println(logmessage);
  14.   }
  15.  
  16.   if (len) {
  17.     // stream the incoming chunk to the opened file
  18.     request->_tempFile.write(data, len);
  19.     logmessage = "Writing file: " + String(filename) + " index=" + String(index) + " len=" + String(len);
  20.     Serial.println(logmessage);
  21.   }
  22.  
  23.   if (final) {
  24.     logmessage = "Upload Complete: " + String(filename) + ",size: " + String(index + len);
  25.     // close the file handle as the upload is now done
  26.     request->_tempFile.close();
  27.     Serial.println(logmessage);
  28.     request->redirect("/");
  29.   }
  30. }
  31.  
  32. String humanReadableSize(const size_t bytes) {
  33.   if (bytes < 1024) return String(bytes) + " B";
  34.   else if (bytes < (1024 * 1024)) return String(bytes / 1024.0) + " KB";
  35.   else if (bytes < (1024 * 1024 * 1024)) return String(bytes / 1024.0 / 1024.0) + " MB";
  36.   else return String(bytes / 1024.0 / 1024.0 / 1024.0) + " GB";
  37. }
  38.  
  39. String listFiles() {
  40.   String returnText = "";
  41.   Serial.println("Listing files stored on SPIFFS");
  42.   File root = SPIFFS.open("/");
  43.   File foundfile = root.openNextFile();
  44.   returnText += "<table><tr><th align='left'>Name</th><th align='left'>Size</th></tr>";
  45.  
  46.   while (foundfile) {
  47.     returnText += "<tr align='left'><td><a href='/www/" + String(foundfile.name()) + "'>"  + String(foundfile.name()) + "</a></td><td>" + humanReadableSize(foundfile.size()) + "</td></tr>";
  48.     foundfile = root.openNextFile();
  49.   }
  50.  
  51.   returnText += "</table>";
  52.  
  53.   returnText += "<br><a href=\"/upload\">Upload new file...</a>";
  54.  
  55.   root.close();
  56.   foundfile.close();
  57.   return returnText;
  58. }
  59.  
  60. void appendFile(fs::FS &fs, const char * path, const char * message) {
  61.   Serial.printf("Appending to file: %s\r\n", path);
  62.   File file = fs.open(path, FILE_APPEND);
  63.   if (!file) {
  64.     Serial.println("- failed to open file for appending");
  65.     return;
  66.   }
  67.   if (file.print(message)) {
  68.     Serial.println("- message appended");
  69.   } else {
  70.     Serial.println("- append failed");
  71.   }
  72.   file.close();
  73. }
  74.  
  75. AsyncWebServer server(80);
  76. const char* ssid = "iPhone (Bartek)";
  77. const char* password = "bartek12";
  78. void setup() {
  79. Serial.begin(115200);
  80. WiFi.mode(WIFI_STA);
  81. WiFi.begin(ssid, password);
  82.  
  83. if (WiFi.waitForConnectResult() != WL_CONNECTED) {
  84.   Serial.printf("WiFi Failed!\n");
  85.   return;
  86. }
  87.  
  88. if (!SPIFFS.begin()) {
  89.   Serial.println("LittleFS Mount Failed, formatting!");
  90.   SPIFFS.format();
  91.   if (!SPIFFS.begin()) {
  92.     Serial.println("LittleFS Mount Failed after formatting, throw.");
  93.     return;
  94.   }
  95. }
  96.  
  97. Serial.print("IP Address: ");
  98. Serial.println(WiFi.localIP());
  99.  
  100. server.on("/update", HTTP_GET, [](AsyncWebServerRequest * request) {
  101.   DynamicJsonDocument doc(512);
  102.   File f;
  103.   if (SPIFFS.exists("/data.json")) {
  104.     f = SPIFFS.open("/data.json", "r");
  105.     deserializeJson(doc, f);
  106.     f.close();
  107.   }
  108.   JsonObject e = doc.createNestedObject();
  109.   e["timestamp"] = millis();
  110.   e["data"] = random(0, 100);
  111.   f = SPIFFS.open("/data.json", "w");
  112.   serializeJson(doc, f);
  113.   f.close();
  114.   request->send(200, "text/plain", "OK");
  115. });
  116.  
  117. server.on("/file", HTTP_GET, [](AsyncWebServerRequest * request) {
  118.   request->send(SPIFFS, "/data.json", "application/json");
  119. });
  120.  
  121. server.on("/upload", HTTP_GET, [](AsyncWebServerRequest * request) {
  122.     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>");
  123.   });
  124.  
  125. server.on("/upload", HTTP_POST, [](AsyncWebServerRequest * request) {
  126.   request->send(200);
  127. }, handleUpload);
  128.  
  129. server.on("/", HTTP_GET, [](AsyncWebServerRequest * request) {
  130.     request->send(200, "text/html", listFiles());
  131. });
  132.  
  133. server.serveStatic("/www/", SPIFFS, "/");
  134. server.begin();
  135. }
  136.  
  137. void loop() {
  138. }
  139.  
Advertisement
Add Comment
Please, Sign In to add comment