From 8c43d260d0a3b41255077c1a8e79e5bed12ffef4 Mon Sep 17 00:00:00 2001 From: Siwat Sirichai Date: Mon, 1 Jan 2024 13:28:15 +0700 Subject: [PATCH] Initial workingHelloWorld Web Server --- .../lib/ESPMegaPRO/ESPMegaPRO_OOP.cpp | 25 +++++++++++++++++++ .../lib/ESPMegaPRO/ESPMegaPRO_OOP.hpp | 8 ++++++ .../lib/ESPMegaPRO/ESPMegaWebServer.cpp | 25 +++++++++++++++++++ .../lib/ESPMegaPRO/ESPMegaWebServer.hpp | 6 ++--- ESPMegaPRO-firmware/src/main.cpp | 5 ++++ 5 files changed, 66 insertions(+), 3 deletions(-) create mode 100644 ESPMegaPRO-firmware/lib/ESPMegaPRO/ESPMegaWebServer.cpp diff --git a/ESPMegaPRO-firmware/lib/ESPMegaPRO/ESPMegaPRO_OOP.cpp b/ESPMegaPRO-firmware/lib/ESPMegaPRO/ESPMegaPRO_OOP.cpp index 6af235e..7930d98 100644 --- a/ESPMegaPRO-firmware/lib/ESPMegaPRO/ESPMegaPRO_OOP.cpp +++ b/ESPMegaPRO-firmware/lib/ESPMegaPRO/ESPMegaPRO_OOP.cpp @@ -61,6 +61,9 @@ void ESPMegaPRO::loop() { if(internalDisplayEnabled) { display->loop(); } + if(webServerEnabled) { + webServer->loop(); + } } /** @@ -233,4 +236,26 @@ void ESPMegaPRO::dumpFRAMtoSerialASCII(uint16_t start, uint16_t end) { for (int i = 0; i < 500; i++) { Serial.printf("%d: %c\n", i,this->fram.read8(i)); } +} + +/** + * @brief Enables the internal web server. + * + * @note This function can only be called if the ESPMegaIoT module is enabled. + * @note This function can only be called once. + * + * @param port The port to use for the web server. + */ +void ESPMegaPRO::enableWebServer(uint16_t port) { + if (!iotEnabled) { + ESP_LOGE("ESPMegaPRO", "Cannot enable web server without IoT module enabled"); + return; + } + if (webServerEnabled) { + ESP_LOGE("ESPMegaPRO", "Web server already enabled"); + return; + } + webServer = new ESPMegaWebServer(port, this->iot); + webServer->begin(); + webServerEnabled = true; } \ No newline at end of file diff --git a/ESPMegaPRO-firmware/lib/ESPMegaPRO/ESPMegaPRO_OOP.hpp b/ESPMegaPRO-firmware/lib/ESPMegaPRO/ESPMegaPRO_OOP.hpp index 54925c7..b20f1ce 100644 --- a/ESPMegaPRO-firmware/lib/ESPMegaPRO/ESPMegaPRO_OOP.hpp +++ b/ESPMegaPRO-firmware/lib/ESPMegaPRO/ESPMegaPRO_OOP.hpp @@ -45,6 +45,7 @@ class ESPMegaPRO { bool updateTimeFromNTP(); void enableIotModule(); void enableInternalDisplay(HardwareSerial *serial); + void enableWebServer(uint16_t port); rtctime_t getTime(); void dumpFRAMtoSerial(uint16_t start, uint16_t end); void dumpFRAMtoSerialASCII(uint16_t start, uint16_t end); @@ -75,9 +76,16 @@ class ESPMegaPRO { * @note You must call enableIotModule() before using this component. */ ESPMegaIoT *iot; + /** + * @brief This component is used to create a web server on the ESPMegaPRO board. + * @typedef ESPMegaWebServer + * @note You must call enableWebServer() before using this component. + */ + ESPMegaWebServer *webServer; private: bool iotEnabled = false; bool internalDisplayEnabled = false; + bool webServerEnabled = false; ExpansionCard* cards[255]; bool cardInstalled[255]; uint8_t cardCount = 0; diff --git a/ESPMegaPRO-firmware/lib/ESPMegaPRO/ESPMegaWebServer.cpp b/ESPMegaPRO-firmware/lib/ESPMegaPRO/ESPMegaWebServer.cpp new file mode 100644 index 0000000..644ccd8 --- /dev/null +++ b/ESPMegaPRO-firmware/lib/ESPMegaPRO/ESPMegaWebServer.cpp @@ -0,0 +1,25 @@ +#include + +ESPMegaWebServer::ESPMegaWebServer(uint16_t port, ESPMegaIoT *iot) { + this->port = port; + this->iot = iot; + this->server = new AsyncWebServer(port); +} + +ESPMegaWebServer::~ESPMegaWebServer() { + delete this->server; +} + +void ESPMegaWebServer::begin() { + this->server->begin(); + auto bindedDashboardHandler = std::bind(&ESPMegaWebServer::dashboardHandler, this, std::placeholders::_1); + this->server->on("/", HTTP_GET, bindedDashboardHandler); +} + +void ESPMegaWebServer::loop() { + // AsyncWebServer doesn't have a loop function +} + +void ESPMegaWebServer::dashboardHandler(AsyncWebServerRequest *request) { + request->send(200, "text/plain", "Hello World!"); +} \ No newline at end of file diff --git a/ESPMegaPRO-firmware/lib/ESPMegaPRO/ESPMegaWebServer.hpp b/ESPMegaPRO-firmware/lib/ESPMegaPRO/ESPMegaWebServer.hpp index 639085f..779b8be 100644 --- a/ESPMegaPRO-firmware/lib/ESPMegaPRO/ESPMegaWebServer.hpp +++ b/ESPMegaPRO-firmware/lib/ESPMegaPRO/ESPMegaWebServer.hpp @@ -7,13 +7,13 @@ class ESPMegaWebServer { public: - ESPMegaWebServer(uint16_t port); + ESPMegaWebServer(uint16_t port, ESPMegaIoT *iot); ~ESPMegaWebServer(); - void begin(ESPMegaIoT *iot); + void begin(); void loop(); private: // Web Server - AsyncWebServer server; + AsyncWebServer *server; uint16_t port; // ESPMegaIoT ESPMegaIoT *iot; diff --git a/ESPMegaPRO-firmware/src/main.cpp b/ESPMegaPRO-firmware/src/main.cpp index 3ca2d47..c141733 100644 --- a/ESPMegaPRO-firmware/src/main.cpp +++ b/ESPMegaPRO-firmware/src/main.cpp @@ -9,6 +9,7 @@ #define CLIMATE_CARD_ENABLE #define MQTT_CARD_REGISTER #define DISPLAY_ENABLE +#define WEB_SERVER_ENABLE // Demo PLC firmware using the ESPMegaPRO OOP library @@ -141,6 +142,10 @@ void setup() ESP_LOGI("Initializer", "Binding climate card to internal display"); espmega.display->bindClimateCard(&climateCard); #endif + #ifdef WEB_SERVER_ENABLE + ESP_LOGI("Initializer", "Enabling web server"); + espmega.enableWebServer(80); + #endif } void loop()