Last active
October 18, 2020 17:56
-
-
Save cotestatnt/7d0f2b1f02f9b31a98a47419211aba7d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <FS.h> | |
#include <FFat.h> | |
#include "myWebserver.h" | |
const char* ssid = "xxxxxxxxxx"; | |
const char* password = "xxxxxxxxxxx"; | |
const char * hostName = "esp-async"; | |
const char* http_username = "admin"; | |
const char* http_password = "admin"; | |
myWebserver myServer(80, http_username, http_password); | |
void doNothing(AsyncWebServerRequest *request){ | |
String args; | |
Serial.print("Do nothing with this request args:\n"); | |
for(int i=0; i<request->args(); i++){ | |
args += request->argName(i); | |
args += " : "; | |
args += request->arg(request->argName(i)); | |
args += "\n"; | |
} | |
Serial.println(args); | |
args.replace("\n", "<br>"); | |
String response = "<!DOCTYPE html><html><head><title>Test HTML response</title></head><body>"; | |
response += "<p>This is HTML out from doNothing() callback function.</p><div>"; | |
response += "<p>This is the arguments you passed me: <br></p><div>"; | |
response += args; | |
response += "</div></body></html>"; | |
request->send(200, "text/html", response); | |
} | |
void setup(){ | |
Serial.begin(115200); | |
Serial.setDebugOutput(true); | |
WiFi.mode(WIFI_STA); | |
WiFi.begin(ssid, password); | |
if (WiFi.waitForConnectResult() != WL_CONNECTED) { | |
Serial.printf("STA: Failed!\n"); | |
WiFi.disconnect(false); | |
delay(1000); | |
WiFi.begin(ssid, password); | |
} | |
Serial.print("\nConnected! IP address: "); | |
Serial.println(WiFi.localIP()); | |
MDNS.begin(hostName); | |
FFat.begin(true); | |
myServer.addCustomHandler("/getData", doNothing ); | |
myServer.startWebserver(FFat); | |
} | |
void loop(){ | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <FS.h> | |
#ifdef ESP32 | |
#include <ESPmDNS.h> | |
#include <WiFi.h> | |
#include <AsyncTCP.h> | |
#elif defined(ESP8266) | |
#include <ESP8266WiFi.h> | |
#include <ESPAsyncTCP.h> | |
#include <ESP8266mDNS.h> | |
#endif | |
#include <ESPAsyncWebServer.h> | |
#include <SPIFFSEditor.h> | |
#define DEBUG_WESERVER | |
#ifdef DEBUG_WESERVER | |
#define SERIAL_DEBUG(...) Serial.printf(__VA_ARGS__) | |
#else | |
#define SERIAL_DEBUG(...) | |
#endif | |
using function_cb = std::function<void(AsyncWebServerRequest *req)>; | |
class CustomRequestHandler : public AsyncWebHandler { | |
public: | |
CustomRequestHandler() {} | |
virtual ~CustomRequestHandler() {} | |
function_cb _function ; | |
const char* _uri; | |
bool canHandle(AsyncWebServerRequest *request){ | |
if(request->url().equalsIgnoreCase(_uri)) | |
return true; | |
else | |
return false; | |
} | |
void handleRequest(AsyncWebServerRequest *request) { | |
_function(request); | |
} | |
}; | |
// This is just a wrapper around ESPAsyncWebServer | |
class myWebserver : public AsyncWebServer | |
{ | |
public: | |
myWebserver(uint16_t port, const char* user, const char* pass ):AsyncWebServer( port){ | |
_port = port; | |
_user = user; | |
_pass = pass; | |
} | |
void startWebserver(fs::FS &fs); | |
void addCustomHandler(const char* uri, function_cb func); | |
String list(fs::FS &fs, const char* dirname ); | |
private: | |
uint16_t _port; | |
const char* _user; | |
const char* _pass; | |
}; | |
void myWebserver::addCustomHandler(const char* uri, function_cb func){ | |
CustomRequestHandler *handler = new CustomRequestHandler(); | |
handler->_function = func; | |
handler->_uri = uri; | |
this->addHandler(handler); | |
} | |
void myWebserver::startWebserver(fs::FS &fs){ | |
this->addHandler(new SPIFFSEditor(fs, _user, _pass)); | |
this->on("/heap", HTTP_GET, [](AsyncWebServerRequest *request){ | |
request->send(200, "text/plain", String(ESP.getFreeHeap())); | |
}); | |
this->serveStatic("/", fs, "/").setDefaultFile("index.htm"); | |
this->onNotFound([](AsyncWebServerRequest *request){ | |
SERIAL_DEBUG("NOT_FOUND: "); | |
if(request->method() == HTTP_GET) | |
SERIAL_DEBUG("GET"); | |
else if(request->method() == HTTP_POST) | |
SERIAL_DEBUG("POST"); | |
else if(request->method() == HTTP_DELETE) | |
SERIAL_DEBUG("DELETE"); | |
else if(request->method() == HTTP_PUT) | |
SERIAL_DEBUG("PUT"); | |
else if(request->method() == HTTP_PATCH) | |
SERIAL_DEBUG("PATCH"); | |
else if(request->method() == HTTP_HEAD) | |
SERIAL_DEBUG("HEAD"); | |
else if(request->method() == HTTP_OPTIONS) | |
SERIAL_DEBUG("OPTIONS"); | |
else | |
SERIAL_DEBUG("UNKNOWN"); | |
SERIAL_DEBUG(" http://%s%s\n", request->host().c_str(), request->url().c_str()); | |
if(request->contentLength()){ | |
SERIAL_DEBUG("_CONTENT_TYPE: %s\n", request->contentType().c_str()); | |
SERIAL_DEBUG("_CONTENT_LENGTH: %u\n", request->contentLength()); | |
} | |
int headers = request->headers(); | |
int i; | |
for(i=0;i<headers;i++){ | |
AsyncWebHeader* h = request->getHeader(i); | |
SERIAL_DEBUG("_HEADER[%s]: %s\n", h->name().c_str(), h->value().c_str()); | |
} | |
int params = request->params(); | |
for(i=0; i<params; i++){ | |
AsyncWebParameter* p = request->getParam(i); | |
if(p->isFile()){ | |
SERIAL_DEBUG("_FILE[%s]: %s, size: %u\n", p->name().c_str(), p->value().c_str(), p->size()); | |
} else if(p->isPost()){ | |
SERIAL_DEBUG("_POST[%s]: %s\n", p->name().c_str(), p->value().c_str()); | |
} else { | |
SERIAL_DEBUG("_GET[%s]: %s\n", p->name().c_str(), p->value().c_str()); | |
} | |
} | |
request->send(404); | |
}); | |
this->onFileUpload([](AsyncWebServerRequest *request, const String& filename, size_t index, uint8_t *data, size_t len, bool final){ | |
if(!index) | |
SERIAL_DEBUG("UploadStart: %s\n", filename.c_str()); | |
SERIAL_DEBUG("%s", (const char*)data); | |
if(final) | |
SERIAL_DEBUG("UploadEnd: %s (%u)\n", filename.c_str(), index+len); | |
}); | |
this->onRequestBody([](AsyncWebServerRequest *request, uint8_t *data, size_t len, size_t index, size_t total){ | |
if(!index) | |
SERIAL_DEBUG("BodyStart: %u\n", total); | |
SERIAL_DEBUG("%s", (const char*)data); | |
if(index + len == total) | |
SERIAL_DEBUG("BodyEnd: %u\n", total); | |
}); | |
this->begin(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment