Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@

#include <ESPAsyncWebServer.h>

//#define DEBUG

class ESPAsyncHTTPUpdateServer
{
public:
Expand Down
22 changes: 19 additions & 3 deletions library.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,25 @@
],
"license": "LGPL-V2.1",
"homepage": "https://www.ip-af.ir/Projects/Electronics/ESPAsyncHTTPUpdateServer",
"dependencies": {
"me-no-dev/ESP Async WebServer": "*"
},
"dependencies": [
{"me-no-dev/ESP Async WebServer": "*"}
,
{
"name": "SPIFFS",
"frameworks": "arduino",
"platforms": "espressif32"
},
{
"name": "LittleFS",
"frameworks": "arduino",
"platforms": "espressif32"
},
{
"name": "Update",
"frameworks": "arduino",
"platforms": "espressif32"
}
],
"frameworks": "*",
"platforms": "*"
}
4 changes: 2 additions & 2 deletions library.properties
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
name=ESPAsyncHTTPUpdateServer
version=1.0
version=1.0.0
author=IPdotSetAF
maintainer=IPdotSetAF <ipdotsetaf@gmail.com>
sentence=Simple HTTP Update server based on the ESP8266WebServer modifed for ESPAsyncWebServer(by me-no-dev)
paragraph=The library accepts HTTP post requests to the /update url, and updates the ESP firmware.
category=Communication
url=
url=https://github.com/IPdotSetAF/ESPAsyncHTTPUpdateServer
architectures=esp8266, esp32
dot_a_linkage=false
83 changes: 54 additions & 29 deletions src/ESPAsyncHTTPUpdateServer.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
#include <Arduino.h>
#include <flash_hal.h>
#include <FS.h>
#include "StreamString.h"
#include "ESPAsyncHTTPUpdateServer.h"

#ifdef ESP32
#ifdef ESPASYNCHTTPUPDATESERVER_LITTLEFS
#include <LittleFS.h>
#else
#include <SPIFFS.h>
#endif
#include <Update.h>
#elif defined(ESP8266)
#include <flash_hal.h>
#include <FS.h>
#endif

static const char serverIndex[] PROGMEM =
R"(<!DOCTYPE html>
<html lang='en'>
Expand Down Expand Up @@ -61,7 +71,7 @@ void ESPAsyncHTTPUpdateServer::setup(AsyncWebServer *server, const String &path,
_authenticated = (_username == emptyString || _password == emptyString || request -> authenticate(_username.c_str(), _password.c_str()));
if (!_authenticated)
{
#ifdef DEBUG
#ifdef ESPASYNCHTTPUPDATESERVER_DEBUG
Serial.printf("Unauthenticated Update\n");
#endif
return;
Expand All @@ -71,22 +81,24 @@ void ESPAsyncHTTPUpdateServer::setup(AsyncWebServer *server, const String &path,
_server->on(
path.c_str(), HTTP_POST, [&](AsyncWebServerRequest *request)
{
//if requestAuthentication() is false second handler will run, else it wont.
if(!_authenticated)
return request->requestAuthentication();
// if requestAuthentication() is false second handler will run, else it wont.
if (!_authenticated)
return request->requestAuthentication();

if (Update.hasError()) {
AsyncWebServerResponse* response = request->beginResponse(200,F("text/html"), String(F("Update error: ")) + _updaterError);
response->addHeader("Access-Control-Allow-Headers", "*");
response->addHeader("Access-Control-Allow-Origin", "*");
response->addHeader("Connection", "close");
request->send(response);
}
else {
request->send_P(200, PSTR("text/html"), successResponse);
delay(1000);
ESP.restart();
} },
if (Update.hasError())
{
AsyncWebServerResponse *response = request->beginResponse(200, F("text/html"), String(F("Update error: ")) + _updaterError);
response->addHeader("Access-Control-Allow-Headers", "*");
response->addHeader("Access-Control-Allow-Origin", "*");
response->addHeader("Connection", "close");
request->send(response);
}
else
{
request->send_P(200, PSTR("text/html"), successResponse);
delay(1000);
ESP.restart();
} },
[&](AsyncWebServerRequest *request, String filename, size_t index, uint8_t *data, size_t len, bool final)
{
// handler for the file upload, gets the sketch bytes, and writes
Expand All @@ -97,38 +109,51 @@ void ESPAsyncHTTPUpdateServer::setup(AsyncWebServer *server, const String &path,
if (!index)
{
_updaterError.clear();
#ifdef DEBUG
#ifdef ESPASYNCHTTPUPDATESERVER_DEBUG
Serial.setDebugOutput(true);
#endif
_authenticated = (_username == emptyString || _password == emptyString || request->authenticate(_username.c_str(), _password.c_str()));
if (!_authenticated)
{
#ifdef DEBUG
#ifdef ESPASYNCHTTPUPDATESERVER_DEBUG
Serial.printf("Unauthenticated Update\n");
#endif
return;
}
#ifdef DEBUG
#ifdef ESPASYNCHTTPUPDATESERVER_DEBUG
Serial.printf("Update: %s\n", filename.c_str());
#endif
#ifdef ESP8266
Update.runAsync(true);
#endif
if (inputName == "filesystem")
{
#ifdef DEBUG
#ifdef ESPASYNCHTTPUPDATESERVER_DEBUG
Serial.println("updating filesystem");
#endif

#ifdef ESP8266
int command = U_FS;
size_t fsSize = ((size_t)FS_end - (size_t)FS_start);
close_all_fs();
if (!Update.begin(fsSize, U_FS))
#elif defined(ESP32)
int command = U_SPIFFS;
#ifdef ESPASYNCHTTPUPDATESERVER_LITTLEFS
size_t fsSize = LittleFS.totalBytes();
#else
size_t fsSize = SPIFFS.totalBytes();
#endif
#endif
if (!Update.begin(fsSize, command))
{ // start with max available size
#ifdef DEBUG
#ifdef ESPASYNCHTTPUPDATESERVER_DEBUG
Update.printError(Serial);
#endif
}
}
else
{
#ifdef DEBUG
#ifdef ESPASYNCHTTPUPDATESERVER_DEBUG
Serial.println("updating flash");
#endif
uint32_t maxSketchSpace = (ESP.getFreeSketchSpace() - 0x1000) & 0xFFFFF000;
Expand All @@ -141,7 +166,7 @@ void ESPAsyncHTTPUpdateServer::setup(AsyncWebServer *server, const String &path,

if (_authenticated && len && !_updaterError.length())
{
#ifdef DEBUG
#ifdef ESPASYNCHTTPUPDATESERVER_DEBUG
Serial.printf(".");
#endif
if (Update.write(data, len) != len)
Expand All @@ -152,13 +177,13 @@ void ESPAsyncHTTPUpdateServer::setup(AsyncWebServer *server, const String &path,
{
if (Update.end(true))
{ // true to set the size to the current progress
#ifdef DEBUG
#ifdef ESPASYNCHTTPUPDATESERVER_DEBUG
Serial.println("Update Success: \nRebooting...\n");
#endif
}
else
_setUpdaterError();
#ifdef DEBUG
#ifdef ESPASYNCHTTPUPDATESERVER_DEBUG
Serial.setDebugOutput(false);
#endif
}
Expand All @@ -169,7 +194,7 @@ void ESPAsyncHTTPUpdateServer::setup(AsyncWebServer *server, const String &path,

void ESPAsyncHTTPUpdateServer::_setUpdaterError()
{
#ifdef DEBUG
#ifdef ESPASYNCHTTPUPDATESERVER_DEBUG
Update.printError(Serial);
#endif
StreamString str;
Expand Down