First version of WebApi
This commit is contained in:
parent
153f289724
commit
82c3a914ab
20
include/WebApi.h
Normal file
20
include/WebApi.h
Normal file
@ -0,0 +1,20 @@
|
||||
#pragma once
|
||||
|
||||
#include <ESPAsyncWebServer.h>
|
||||
|
||||
class WebApiClass {
|
||||
public:
|
||||
WebApiClass();
|
||||
void init();
|
||||
|
||||
private:
|
||||
AsyncWebServer _server;
|
||||
AsyncWebSocket _ws;
|
||||
AsyncEventSource _events;
|
||||
|
||||
void onWebsocketEvent(AsyncWebSocket* server, AsyncWebSocketClient* client, AwsEventType type, void* arg, uint8_t* data, size_t len);
|
||||
|
||||
void onNotFound(AsyncWebServerRequest* request);
|
||||
};
|
||||
|
||||
extern WebApiClass WebApi;
|
||||
@ -4,6 +4,8 @@
|
||||
|
||||
#define APP_HOSTNAME "OpenDTU-%06X"
|
||||
|
||||
#define HTTP_PORT 80
|
||||
|
||||
#define ACCESS_POINT_NAME "OpenDTU-"
|
||||
#define ACCESS_POINT_PASSWORD "openDTU"
|
||||
|
||||
|
||||
@ -21,7 +21,7 @@ build_flags =
|
||||
${env.build_flags}
|
||||
-D=${PIOENV}
|
||||
|
||||
#lib_deps = https://github.com/lorol/LITTLEFS.git
|
||||
lib_deps = https://github.com/me-no-dev/ESPAsyncWebServer.git
|
||||
|
||||
board = esp32dev
|
||||
board_build.partitions = partitions_custom.csv
|
||||
|
||||
45
src/WebApi.cpp
Normal file
45
src/WebApi.cpp
Normal file
@ -0,0 +1,45 @@
|
||||
#include "WebApi.h"
|
||||
#include "defaults.h"
|
||||
#include <LittleFS.h>
|
||||
|
||||
WebApiClass::WebApiClass()
|
||||
: _server(HTTP_PORT)
|
||||
, _ws("/ws")
|
||||
, _events("/events")
|
||||
{
|
||||
}
|
||||
|
||||
void WebApiClass::init()
|
||||
{
|
||||
using namespace std::placeholders;
|
||||
|
||||
_server.addHandler(&_ws);
|
||||
_server.addHandler(&_events);
|
||||
|
||||
_ws.onEvent(std::bind(&WebApiClass::onWebsocketEvent, this, _1, _2, _3, _4, _5, _6));
|
||||
|
||||
_server.serveStatic("/", LITTLEFS, "/", "max-age=86400").setDefaultFile("index.htm");
|
||||
_server.onNotFound(std::bind(&WebApiClass::onNotFound, this, _1));
|
||||
_server.begin();
|
||||
}
|
||||
|
||||
void WebApiClass::onNotFound(AsyncWebServerRequest* request)
|
||||
{
|
||||
// Handle Unknown Request
|
||||
request->send(404, "text/plain", "404 Not Found");
|
||||
}
|
||||
|
||||
void WebApiClass::onWebsocketEvent(AsyncWebSocket* server, AsyncWebSocketClient* client, AwsEventType type, void* arg, uint8_t* data, size_t len)
|
||||
{
|
||||
if (type == WS_EVT_CONNECT) {
|
||||
char str[64];
|
||||
sprintf(str, "Websocket: [%s][%u] connect", server->url(), client->id());
|
||||
Serial.println(str);
|
||||
} else if (type == WS_EVT_DISCONNECT) {
|
||||
char str[64];
|
||||
sprintf(str, "Websocket: [%s][%u] disconnect", server->url(), client->id());
|
||||
Serial.println(str);
|
||||
}
|
||||
}
|
||||
|
||||
WebApiClass WebApi;
|
||||
@ -1,4 +1,5 @@
|
||||
#include "Configuration.h"
|
||||
#include "WebApi.h"
|
||||
#include "WiFiSettings.h"
|
||||
#include "defaults.h"
|
||||
#include <Arduino.h>
|
||||
@ -43,6 +44,11 @@ void setup()
|
||||
WiFiSettings.init();
|
||||
Serial.println(F("done"));
|
||||
WiFiSettings.applyConfig();
|
||||
|
||||
// Initialize WebApi
|
||||
Serial.print(F("Initialize WebApi... "));
|
||||
WebApi.init();
|
||||
Serial.println(F("done"));
|
||||
}
|
||||
|
||||
void loop()
|
||||
|
||||
Loading…
Reference in New Issue
Block a user