Skip to content

Instantly share code, notes, and snippets.

@gmag11
Last active January 4, 2017 15:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • 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()
{
}
@gmag11
Copy link
Author

gmag11 commented Aug 15, 2016

I'm trying to develop a cusomized web server library to be a start framework for my projects with ESP8266. It initializes some included pages to configure WiFi, NTP, OTA update and some other things. I would like to use OOP to get everything encapsulated. But I have a big mess with callbacks. I've tried to find a way to pass C++ member funtions as callbacks but, until now, have not been able to.
I know that this is a frequent topic among begginers but I've not found a suitable solution.

@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