HTTPServer.cpp
#include <esp_http_server.h>
/**
* Serveur HTTP.
* Fonctionne avec la bibliothèque ESP-IDF en interne.
*
* Utilisation :
* D'abord appeler http.StartServer()
* Puis appeler http.RegisterHandler(...) pour tous les handlers
*/
class HTTPServer {
public:
HTTPServer(): conf(HTTPD_DEFAULT_CONFIG()) {
}
void StartServer() {
if (httpd_start(&server, &conf) != ESP_OK) {
ESP_LOGE("HTTP server", "Error starting server!");
}
}
void RegisterHandler(const httpd_uri_t *uri_handler) {
httpd_register_uri_handler(this->server, uri_handler);
}
httpd_handle_t server = nullptr;
httpd_config_t conf;
};Exemple d’utilisation :
HTTPServer_usage.cpp
// Déclare le serveur globalement
HTTPServer http;
// Exemple de handler
static const httpd_uri_t rebootHandler = {
.uri = "/api/reboot",
.method = HTTP_GET,
.handler = [](httpd_req_t *req) {
SendStatusOK(req);
delay(20);
ESP.restart();
return ESP_OK;
}
};
void setup() {
// TODO configurer le wifi ou Ethernet
http.StartServer();
http.RegisterHandler(&myHandler);
}
Consultez les articles similaires par catégorie :
C/C++ Embedded ESP8266/ESP32
Si cet article vous a aidé, pensez à m'offrir un café ou à faire un don via PayPal pour soutenir la recherche et la publication de nouveaux articles sur TechOverflow