Arduino example: upload a file using ESPasyncWebServer.

Cellie
Posts: 45
Joined: Sat Feb 27, 2016 9:47 pm

Arduino example: upload a file using ESPasyncWebServer.

Postby Cellie » Thu Oct 12, 2017 8:07 pm

As I struggled with the sparse examples on the GitHub repo I spent some time making a simple example for uploading a file to ESPAsyncWebServer.

Files are uploaded through '/api/upload'.
I used curl to test this code with a command something like this:

Code: Select all

curl --user admin -F 'data=@LOCAL_FILENAME' 'ESP32_IP/api/upload'
The example:

Code: Select all

/****************************************************************************

       Upload a file to esp32 with authorization

       Use curl to upload a file:

       curl --user admin:admin -F 'data=@FILENAME' 'ESP32_IP/api/upload'

 *****************************************************************************/
#include "FS.h"
#include "SD.h"
#include <AsyncTCP.h>
#include <ESPAsyncWebServer.h>

AsyncWebServer server(80);

const char* ssid = "----------";
const char* password = "-----------";
const char* http_username = "admin";
const char* http_password = "admin";
 
void setup() {
  Serial.begin( 115200 );

  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);
  Serial.printf( "Connecting to SSID %s with PSK %s...\n", ssid, password  );
  if (WiFi.waitForConnectResult() != WL_CONNECTED) {
    Serial.printf("WiFi Failed!\n");
    return;
  }
  Serial.println( WiFi.localIP() );
  server.on( "/api/upload", HTTP_POST, []( AsyncWebServerRequest * request )
  {
    if ( request->authenticate( http_username, http_password ) )
    {
      request->send( 200 );
    }
    else
    {
      request->requestAuthentication();
    }
  },
  []( AsyncWebServerRequest * request, String filename, size_t index, uint8_t *data, size_t len, bool final )
  {
    static bool   _authenticated;
    static time_t startTimer;

    if ( !index )
    {
      _authenticated = false;
      if ( request->authenticate(http_username, http_password) )
      {
        startTimer = millis();
        Serial.printf( "UPLOAD: Started to receive '%s'.\n", filename.c_str() );
        _authenticated = true;
      }
      else
      {
        Serial.println( "Unauthorized access." );
        return request->send( 401, "text/plain", "Not logged in." );
      }      
    }

    if ( _authenticated )
    {
      //Serial.printf( "%i bytes received.\n", index );
      //Store or do something with the data...
    }


    if ( final && _authenticated )
    {
      Serial.printf( "UPLOAD: Done. Received %.2f kBytes in %.2fs which is %i kB/s.\n", index / 1024.0, ( millis() - startTimer ) / 1000.0, index / ( millis() - startTimer ) );
    }
  });

  server.onNotFound( []( AsyncWebServerRequest * request )
  {
    Serial.printf("NOT_FOUND: ");
    if (request->method() == HTTP_GET)
      Serial.printf("GET");
    else if (request->method() == HTTP_POST)
      Serial.printf("POST");
    else if (request->method() == HTTP_DELETE)
      Serial.printf("DELETE");
    else if (request->method() == HTTP_PUT)
      Serial.printf("PUT");
    else if (request->method() == HTTP_PATCH)
      Serial.printf("PATCH");
    else if (request->method() == HTTP_HEAD)
      Serial.printf("HEAD");
    else if (request->method() == HTTP_OPTIONS)
      Serial.printf("OPTIONS");
    else
      Serial.printf("UNKNOWN");
    Serial.printf(" http://%s%s\n", request->host().c_str(), request->url().c_str());
    request->send( 404, "text/plain", "Not found." );
  });

  server.begin();

  Serial.println( "Ready for upload" );
}

void loop() {
  // put your main code here, to run repeatedly:

}
See a bit bigger example at https://github.com/CelliesProjects/mini ... dAuthESP32

Who is online

Users browsing this forum: No registered users and 49 guests