Last active
January 4, 2017 15:47
-
-
Save gmag11/bd3f845c3eba2543df04d6de93f0474f to your computer and use it in GitHub Desktop.
MyAsyncWebServer prototype. Some code errors inside.
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 <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() | |
{ | |
} |
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.
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
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.