Skip to content

Commit

Permalink
First version of WebApi
Browse files Browse the repository at this point in the history
  • Loading branch information
tbnobody committed Apr 11, 2022
1 parent 153f289 commit 82c3a91
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 1 deletion.
20 changes: 20 additions & 0 deletions include/WebApi.h
Original file line number Diff line number Diff line change
@@ -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;
2 changes: 2 additions & 0 deletions include/defaults.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

#define APP_HOSTNAME "OpenDTU-%06X"

#define HTTP_PORT 80

#define ACCESS_POINT_NAME "OpenDTU-"
#define ACCESS_POINT_PASSWORD "openDTU"

Expand Down
2 changes: 1 addition & 1 deletion platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
45 changes: 45 additions & 0 deletions src/WebApi.cpp
Original file line number Diff line number Diff line change
@@ -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;
6 changes: 6 additions & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "Configuration.h"
#include "WebApi.h"
#include "WiFiSettings.h"
#include "defaults.h"
#include <Arduino.h>
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit 82c3a91

Please sign in to comment.