Skip to content

Been trying to put this on a ESP32 #61

Description

@GeorgeFlorian

Hello there ! I've been tinkering with the Arduino IDE + ESP32 for the past week.

I tried the FSBrowser + AutoConnect from here but with no luck.

Hardware:

Board: ESP32 DEVKIT1

Core Installation version: 1.0.1-git this one

IDE name: Arduino IDE

Flash Frequency: 80Mhz

PSRAM enabled: no

Upload Speed: 115200

Computer OS: Linux Mint 19.1 Mate

This is my sketch:

/*
  FSWebServer - Example WebServer with SPIFFS backend for esp8266
  Copyright (c) 2015 Hristo Gochkov. All rights reserved.
  This file is part of the ESP8266WebServer library for Arduino environment.
*/

#include <FS.h>
#include <WiFi.h>
#include <WiFiClient.h>
#include <WebServer.h>
#include <ESPmDNS.h>
#include <SPIFFS.h>
#include <AutoConnect.h>

#define DBG_OUTPUT_PORT Serial
#define LED 2

const char* ssid = "wifi-ssid";
const char* password = "wifi-password";
const char* host = "esp32fs";

WebServer server(80);


AutoConnect portal(server);
//holds the current upload
File fsUploadFile;

bool startCP(IPAddress ip) {
    digitalWrite(LED, HIGH);
    DBG_OUTPUT_PORT.println("Captive Portal started ! IP: " + WiFi.localIP().toString());
    return true;
  }

//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";
  }
}

String getContentType(String filename){
  if(server.hasArg("download")) return "application/octet-stream";
  else if(filename.endsWith(".htm")) return "text/html";
  else if(filename.endsWith(".html")) return "text/html";
  else if(filename.endsWith(".css")) return "text/css";
  else if(filename.endsWith(".js")) return "application/javascript";
  else if(filename.endsWith(".png")) return "image/png";
  else if(filename.endsWith(".gif")) return "image/gif";
  else if(filename.endsWith(".jpg")) return "image/jpeg";
  else if(filename.endsWith(".ico")) return "image/x-icon";
  else if(filename.endsWith(".xml")) return "text/xml";
  else if(filename.endsWith(".pdf")) return "application/x-pdf";
  else if(filename.endsWith(".zip")) return "application/x-zip";
  else if(filename.endsWith(".gz")) return "application/x-gzip";
  return "text/plain";
}

bool handleFileRead(String path){
  DBG_OUTPUT_PORT.println("handleFileRead: " + path);
  if(path.endsWith("/")) path += "index.htm";
  String contentType = getContentType(path);
  String pathWithGz = path + ".gz";
  if(SPIFFS.exists(pathWithGz) || SPIFFS.exists(path)){
    if(SPIFFS.exists(pathWithGz))
      path += ".gz";
    File file = SPIFFS.open(path, "r");
    size_t sent = server.streamFile(file, contentType);
    file.close();
    return true;
  }
  return false;
}

void handleFileUpload(){
  if(server.uri() != "/edit") return;
  HTTPUpload& upload = server.upload();
  if(upload.status == UPLOAD_FILE_START){
    String filename = upload.filename;
    if(!filename.startsWith("/")) filename = "/"+filename;
    DBG_OUTPUT_PORT.print("handleFileUpload Name: "); DBG_OUTPUT_PORT.println(filename);
    fsUploadFile = SPIFFS.open(filename, "w");
    filename = String();
  } else if(upload.status == UPLOAD_FILE_WRITE){
    //DBG_OUTPUT_PORT.print("handleFileUpload Data: "); DBG_OUTPUT_PORT.println(upload.currentSize);
    if(fsUploadFile)
      fsUploadFile.write(upload.buf, upload.currentSize);
  } else if(upload.status == UPLOAD_FILE_END){
    if(fsUploadFile)
      fsUploadFile.close();
    DBG_OUTPUT_PORT.print("handleFileUpload Size: "); DBG_OUTPUT_PORT.println(upload.totalSize);
  }
}

void handleFileDelete(){
  if(server.args() == 0) return server.send(500, "text/plain", "BAD ARGS");
  String path = server.arg(0);
  DBG_OUTPUT_PORT.println("handleFileDelete: " + path);
  if(path == "/")
    return server.send(500, "text/plain", "BAD PATH");
  if(!SPIFFS.exists(path))
    return server.send(404, "text/plain", "FileNotFound");
  SPIFFS.remove(path);
  server.send(200, "text/plain", "");
  path = String();
}

void handleFileCreate(){
  if(server.args() == 0)
    return server.send(500, "text/plain", "BAD ARGS");
  String path = server.arg(0);
  DBG_OUTPUT_PORT.println("handleFileCreate: " + path);
  if(path == "/")
    return server.send(500, "text/plain", "BAD PATH");
  if(SPIFFS.exists(path))
    return server.send(500, "text/plain", "FILE EXISTS");
  File file = SPIFFS.open(path, "w");
  if(file)
    file.close();
  else
    return server.send(500, "text/plain", "CREATE FAILED");
  server.send(200, "text/plain", "");
  path = String();
}

void handleFileList() {
  if (!server.hasArg("dir")) {
    server.send(500, "text/plain", "BAD ARGS");
    return;
  }
  
  String path = server.arg("dir");
  DBG_OUTPUT_PORT.println("handleFileList: " + path);
  File root = SPIFFS.open(path);

  path = String();

  String output = "[";

  if(root.isDirectory()){
    File file = root.openNextFile();
    while(file){
      if (output != "[") {
        output += ',';
      }
      output += "{\"type\":\"";
      output += (file.isDirectory()) ? "dir" : "file";
      output += "\",\"name\":\"";
      output += String(file.name()).substring(1);
      output += "\"}";
      file = root.openNextFile();
    }
  }
  
  output += "]";
  server.send(200, "text/json", output);
}

void setup(void){
  delay(1000);
  DBG_OUTPUT_PORT.begin(115200);
  DBG_OUTPUT_PORT.print("\n");
  DBG_OUTPUT_PORT.setDebugOutput(true);
  
  SPIFFS.begin();
  {
    File root = SPIFFS.open("/");
    File file = root.openNextFile();
    while(file){
      String fileName = file.name();
      size_t fileSize = file.size();
      DBG_OUTPUT_PORT.printf("FS File: %s, size: %s\n", fileName.c_str(), formatBytes(fileSize).c_str());
      file = root.openNextFile();
    }
    DBG_OUTPUT_PORT.printf("\n");
  }
  

  //WIFI INIT
  //DBG_OUTPUT_PORT.printf("Connecting to %s\n", ssid);

  //SERVER INIT
  //list directory
  server.on("/list", HTTP_GET, handleFileList);
  //load editor
  server.on("/edit", HTTP_GET, [](){
    if(!handleFileRead("/edit.htm")) server.send(404, "text/plain", "FileNotFound");
  });
  //create file
  server.on("/edit", HTTP_PUT, handleFileCreate);
  //delete file
  server.on("/edit", HTTP_DELETE, handleFileDelete);
  //first callback is called after the request has ended with all parsed arguments
  //second callback handles file uploads at that location
  server.on("/edit", HTTP_POST, [](){ server.send(200, "text/plain", ""); }, handleFileUpload);

  //called when the url is not defined here
  //use it to load content from SPIFFS
  portal.onNotFound([](){
    if(!handleFileRead(server.uri()))
      server.send(404, "text/plain", "FileNotFound");
  });

  //get heap status, analog input value and all GPIO statuses in one json call
  server.on("/all", HTTP_GET, [](){
    String json = "{";
    json += "\"heap\":"+String(ESP.getFreeHeap());
    json += ", \"analog\":"+String(analogRead(A0));

    json += ", \"gpio\":" + String((uint32_t)(0));
    
    json += "}";
    server.send(200, "text/json", json);
    json = String();
  });

  pinMode(LED,OUTPUT);
  digitalWrite(LED, LOW);
  portal.onDetect(startCP);
  
  if (portal.begin()) {
    digitalWrite(LED, LOW);
  }
  
  DBG_OUTPUT_PORT.println("HTTP server started");

  //Relocation as follows to make AutoConnect recognition.
  DBG_OUTPUT_PORT.println("");
  DBG_OUTPUT_PORT.print("Connected! IP address: ");
  DBG_OUTPUT_PORT.println(WiFi.localIP());

  //Relocation as follows to make AutoConnect recognition.
  MDNS.begin(host);
  DBG_OUTPUT_PORT.print("Open http://");
  DBG_OUTPUT_PORT.print(host);
  DBG_OUTPUT_PORT.println(".local/edit to see the file browser");
}
 
void loop(void){

  portal.handleClient();
}

My http://192.168.1.103/edit returns FileNotFound.

/list doesn't work like that. It works like this: http://192.168.1.103/list?dir=/ and it returns:

[{"type":"file","name":"login.css"},{"type":"file","name":"index.html"},{"type":"file","name":"logo.png"},{"type":"file","name":"back-image.jpg"}]

Here is a Serial Monitor output:

10:26:01.651 ->
10:26:01.651 -> rst:0x10 (RTCWDT_RTC_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
10:26:01.651 -> configsip: 0, SPIWP:0xee
10:26:01.651 -> clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
10:26:01.685 -> mode:DIO, clock div:1
10:26:01.685 -> load:0x3fff0018,len:4
10:26:01.685 -> load:0x3fff001c,len:1100
10:26:01.685 -> load:0x40078000,len:10088
10:26:01.685 -> load:0x40080400,len:6380
10:26:01.685 -> entry 0x400806a4
10:26:02.979 ->
10:26:03.145 -> FS File: /logo.png, size: 3.80KB
10:26:03.145 -> FS File: /login.css, size: 1.62KB
10:26:03.145 -> FS File: /index.html, size: 880B
10:26:03.178 -> FS File: /back-image.jpg, size: 146.42KB
10:26:03.212 ->
10:26:03.378 -> [D][WiFiGeneric.cpp:342] _eventCallback(): Event: 0 - WIFI_READY
10:26:03.378 -> [D][WiFiGeneric.cpp:342] _eventCallback(): Event: 2 - STA_START
10:26:03.477 -> [AC] WiFi.config(IP=0.0.0.0, Gateway=0.0.0.0, Subnetmask=0.0.0.0, DNS1=0.0.0.0, DNS2=0.0.0.0) [AC] WiFi.begin()
10:26:03.510 -> [AC] Connecting....[D][WiFiGeneric.cpp:342] _eventCallback(): Event: 4 - STA_CONNECTED
10:26:04.440 -> [D][WiFiGeneric.cpp:342] _eventCallback(): Event: 7 - STA_GOT_IP
10:26:04.440 -> [D][WiFiGeneric.cpp:385] _eventCallback(): STA IP: 192.168.1.103, MASK: 255.255.255.0, GW: 192.168.1.1
10:26:04.706 -> [AC] established IP:192.168.1.103
10:26:04.706 -> [AC] http server started
10:26:04.706 -> HTTP server started
10:26:04.706 ->
10:26:04.706 -> Connected! IP address: 192.168.1.103
10:26:04.706 -> Open http://esp32fs.local/edit to see the file browser
10:26:22.267 -> [AC] /_ac
10:26:22.267 -> [AC] Page[/_ac] allocated
10:26:22.267 -> [AC] 192.168.1.103/_ac
10:26:22.267 -> [PB] at leaving build: 260260 free
10:26:22.267 -> [PB] Res:200, Chunked:0
10:26:22.267 -> [PB] Free heap:262948, content len.:6643
10:26:22.300 -> [PB] Transfer-Encoding:chunked
10:26:22.300 -> [D][WiFiClient.cpp:463] connected(): Disconnected: RES: 0, ERR: 128
10:26:45.707 -> handleFileRead: /edit.htm
10:26:45.872 -> [D][WiFiClient.cpp:463] connected(): Disconnected: RES: 0, ERR: 128
10:26:46.106 -> [AC] esp32fs.local/favicon.ico
10:26:46.106 -> [AC] Page[] allocated
10:26:46.106 -> [E][WebServer.cpp:602] _handleRequest(): request handler not found
10:26:46.106 -> [D][WiFiClient.cpp:463] connected(): Disconnected: RES: 0, ERR: 128
10:26:46.106 -> [AC] esp32fs.local/_ac
10:26:46.139 -> [AC] Page[/_ac] allocated
10:26:46.139 -> [AC] 192.168.1.103/_ac
10:26:46.139 -> [PB] at leaving build: 259968 free
10:26:46.139 -> [PB] Res:200, Chunked:0
10:26:46.139 -> [PB] Free heap:262736, content len.:6643
10:26:46.139 -> [PB] Transfer-Encoding:chunked
10:26:46.172 -> [E][WiFiClient.cpp:346] write(): 104
10:27:08.145 -> handleFileRead: /edit.htm
10:27:08.311 -> [D][WiFiClient.cpp:463] connected(): Disconnected: RES: 0, ERR: 128
10:27:08.410 -> [AC] esp32fs.local/favicon.ico
10:27:08.444 -> [AC] Page[] allocated
10:27:08.444 -> [E][WebServer.cpp:602] _handleRequest(): request handler not found
10:27:08.444 -> [D][WiFiClient.cpp:463] connected(): Disconnected: RES: 0, ERR: 128
10:27:08.444 -> [AC] esp32fs.local/_ac
10:27:08.444 -> [AC] Page[/_ac] allocated
10:27:08.444 -> [AC] 192.168.1.103/_ac
10:27:08.477 -> [PB] at leaving build: 259664 free
10:27:08.477 -> [PB] Res:200, Chunked:0
10:27:08.477 -> [PB] Free heap:262440, content len.:6643
10:27:08.477 -> [PB] Transfer-Encoding:chunked
10:27:08.477 -> [E][WiFiClient.cpp:346] write(): 104
10:27:13.455 -> [D][WiFiClient.cpp:463] connected(): Disconnected: RES: 0, ERR: 128
10:27:13.854 -> [AC] esp32fs.local/favicon.ico
10:27:13.854 -> [AC] Page[] allocated
10:27:13.854 -> [E][WebServer.cpp:602] _handleRequest(): request handler not found
10:27:13.854 -> [D][WiFiClient.cpp:463] connected(): Disconnected: RES: 0, ERR: 128
10:27:18.866 -> [AC] esp32fs.local/_ac
10:27:18.866 -> [AC] Page[/_ac] allocated
10:27:18.866 -> [AC] 192.168.1.103/_ac
10:27:18.899 -> [PB] at leaving build: 259372 free
10:27:18.899 -> [PB] Res:200, Chunked:0
10:27:18.899 -> [PB] Free heap:262132, content len.:6643
10:27:18.899 -> [PB] Transfer-Encoding:chunked
10:27:18.899 -> [E][WiFiClient.cpp:346] write(): 104

As you can see I've uploaded 4 files into the ESP32's FlashMemory. I would like to "edit" them. Maybe delete one of them, or create a new file. I can't do that since /edit does nothing.

Do you have any idea ?
Thank you !

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions