Skip to content

Instantly share code, notes, and snippets.

@gmag11
Last active January 4, 2017 15:47
Show Gist options
  • Save gmag11/bd3f845c3eba2543df04d6de93f0474f to your computer and use it in GitHub Desktop.
Save gmag11/bd3f845c3eba2543df04d6de93f0474f to your computer and use it in GitHub Desktop.
MyAsyncWebServer prototype. Some code errors inside.
#include <ESPAsyncTCP.h>
#include <Ticker.h>
#include <ESPAsyncWebServer.h>
#include <ESP8266WiFi.h>
#include <functional>
#include <Hash.h>
using namespace std;
using namespace placeholders;
class MyServer : public AsyncWebServer {
public:
MyServer(int port) : AsyncWebServer(port) {
}
void begin() {
_periodicTrigger.attach(10.0f, &MyServer::s_periodicTask, static_cast<void*>(this));
on("/index.html", std::bind(&MyServer::indexHandler, this, _1));
on("/", [](AsyncWebServerRequest *request) {
AsyncWebServerResponse *response = request->beginResponse(302);
response->addHeader("Location", "/index.html");
response->addHeader("Cache-Control","no-cache");
request->send(response);
});
onNotFound([](AsyncWebServerRequest *request) {
request->send(404,"text/plain","Not Found");
});
AsyncWebServer::begin();
}
protected:
Ticker _periodicTrigger;
int value = 0;
void indexHandler(AsyncWebServerRequest* request) {
char* text = (char*)malloc(20 * sizeof(char));
sprintf(text, "Value is %d", value);
Serial.printf("%s\r\n", text);
request->send(200,"text/plain",text);
delete(text);
}
void periodicTask() {
Serial.printf("Periodic task. Result = %d\n", value);
value++;
}
static void s_periodicTask(void* arg) {
MyServer* self = reinterpret_cast<MyServer*>(arg);
self->periodicTask();
}
};
MyServer server(80);
void additionalHandler(AsyncWebServerRequest* request) {
request->send(200, "text/plain", "normal handler");
}
void setup()
{
Serial.begin(115200);
WiFi.begin("rohde", "RS0MADRID28160869");
server.on("/other.html", additionalHandler);
server.begin();
Serial.println(ESP.getFreeSketchSpace());
}
void loop()
{
}
@marvinroger
Copy link

periodicTask takes one argument, so you need one placeholder: std::bind(&MyServer::indexHandler, this, std::placeholders::_1). For two arguments you add a 4th parameter std::placeholders::_2 and so on.

@marvinroger
Copy link

And make the base class public: class MyServer : public AsyncWebServer

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment