Initial workingHelloWorld Web Server

This commit is contained in:
Siwat Sirichai 2024-01-01 13:28:15 +07:00
parent f8661dd4a1
commit 8c43d260d0
5 changed files with 66 additions and 3 deletions

View File

@ -61,6 +61,9 @@ void ESPMegaPRO::loop() {
if(internalDisplayEnabled) { if(internalDisplayEnabled) {
display->loop(); 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++) { for (int i = 0; i < 500; i++) {
Serial.printf("%d: %c\n", i,this->fram.read8(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;
} }

View File

@ -45,6 +45,7 @@ class ESPMegaPRO {
bool updateTimeFromNTP(); bool updateTimeFromNTP();
void enableIotModule(); void enableIotModule();
void enableInternalDisplay(HardwareSerial *serial); void enableInternalDisplay(HardwareSerial *serial);
void enableWebServer(uint16_t port);
rtctime_t getTime(); rtctime_t getTime();
void dumpFRAMtoSerial(uint16_t start, uint16_t end); void dumpFRAMtoSerial(uint16_t start, uint16_t end);
void dumpFRAMtoSerialASCII(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. * @note You must call enableIotModule() before using this component.
*/ */
ESPMegaIoT *iot; 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: private:
bool iotEnabled = false; bool iotEnabled = false;
bool internalDisplayEnabled = false; bool internalDisplayEnabled = false;
bool webServerEnabled = false;
ExpansionCard* cards[255]; ExpansionCard* cards[255];
bool cardInstalled[255]; bool cardInstalled[255];
uint8_t cardCount = 0; uint8_t cardCount = 0;

View File

@ -0,0 +1,25 @@
#include <ESPMegaWebServer.hpp>
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!");
}

View File

@ -7,13 +7,13 @@
class ESPMegaWebServer class ESPMegaWebServer
{ {
public: public:
ESPMegaWebServer(uint16_t port); ESPMegaWebServer(uint16_t port, ESPMegaIoT *iot);
~ESPMegaWebServer(); ~ESPMegaWebServer();
void begin(ESPMegaIoT *iot); void begin();
void loop(); void loop();
private: private:
// Web Server // Web Server
AsyncWebServer server; AsyncWebServer *server;
uint16_t port; uint16_t port;
// ESPMegaIoT // ESPMegaIoT
ESPMegaIoT *iot; ESPMegaIoT *iot;

View File

@ -9,6 +9,7 @@
#define CLIMATE_CARD_ENABLE #define CLIMATE_CARD_ENABLE
#define MQTT_CARD_REGISTER #define MQTT_CARD_REGISTER
#define DISPLAY_ENABLE #define DISPLAY_ENABLE
#define WEB_SERVER_ENABLE
// Demo PLC firmware using the ESPMegaPRO OOP library // Demo PLC firmware using the ESPMegaPRO OOP library
@ -141,6 +142,10 @@ void setup()
ESP_LOGI("Initializer", "Binding climate card to internal display"); ESP_LOGI("Initializer", "Binding climate card to internal display");
espmega.display->bindClimateCard(&climateCard); espmega.display->bindClimateCard(&climateCard);
#endif #endif
#ifdef WEB_SERVER_ENABLE
ESP_LOGI("Initializer", "Enabling web server");
espmega.enableWebServer(80);
#endif
} }
void loop() void loop()