web username and password

This commit is contained in:
Siwat Sirichai 2024-01-01 20:30:17 +07:00
parent 19cfaf6cba
commit daaabe5f26
4 changed files with 119 additions and 2 deletions

View File

@ -256,6 +256,7 @@ void ESPMegaPRO::enableWebServer(uint16_t port) {
return; return;
} }
webServer = new ESPMegaWebServer(port, this->iot); webServer = new ESPMegaWebServer(port, this->iot);
webServer->bindFRAM(&fram);
webServer->begin(); webServer->begin();
webServerEnabled = true; webServerEnabled = true;
} }

View File

@ -14,6 +14,7 @@ ESPMegaWebServer::~ESPMegaWebServer()
void ESPMegaWebServer::begin() void ESPMegaWebServer::begin()
{ {
this->loadCredentialsFromFRAM();
this->server->begin(); this->server->begin();
auto bindedDashboardHandler = std::bind(&ESPMegaWebServer::dashboardHandler, this, std::placeholders::_1); auto bindedDashboardHandler = std::bind(&ESPMegaWebServer::dashboardHandler, this, std::placeholders::_1);
this->server->on("/", HTTP_GET, bindedDashboardHandler); this->server->on("/", HTTP_GET, bindedDashboardHandler);
@ -26,6 +27,83 @@ void ESPMegaWebServer::loop()
// AsyncWebServer doesn't have a loop function // AsyncWebServer doesn't have a loop function
} }
void ESPMegaWebServer::bindFRAM(FRAM *fram)
{
this->fram = fram;
}
void ESPMegaWebServer::loadCredentialsFromFRAM()
{
this->fram->read(301, (uint8_t*)this->webUsername, 32);
this->fram->read(333, (uint8_t*)this->webPassword, 32);
// Verify if credentials are valid
// A valid username and password is null terminated
// Scan for null terminator
bool validUsername = false;
bool validPassword = false;
for (int i = 0; i < 32; i++)
{
if (this->webUsername[i] == '\0')
{
validUsername = true;
break;
}
}
for (int i = 0; i < 32; i++)
{
if (this->webPassword[i] == '\0')
{
validPassword = true;
break;
}
}
if (!validUsername || !validPassword)
{
this->resetCredentials();
return;
}
// A valid username and password is at least 1 character long
if(strlen(this->webUsername) == 0 || strlen(this->webPassword) == 0)
{
this->resetCredentials();
return;
}
}
void ESPMegaWebServer::saveCredentialsToFRAM()
{
this->fram->write(301, (uint8_t*)this->webUsername, 32);
this->fram->write(333, (uint8_t*)this->webPassword, 32);
}
void ESPMegaWebServer::resetCredentials()
{
// The default username and password is "admin"
strcpy(this->webUsername, "admin");
strcpy(this->webPassword, "admin");
this->saveCredentialsToFRAM();
}
char* ESPMegaWebServer::getWebUsername()
{
return this->webUsername;
}
char* ESPMegaWebServer::getWebPassword()
{
return this->webPassword;
}
void ESPMegaWebServer::setWebUsername(const char* username)
{
strcpy(this->webUsername, username);
}
void ESPMegaWebServer::setWebPassword(const char* password)
{
strcpy(this->webPassword, password);
}
void ESPMegaWebServer::dashboardHandler(AsyncWebServerRequest *request) void ESPMegaWebServer::dashboardHandler(AsyncWebServerRequest *request)
{ {
auto bindedDashboardProcessor = std::bind(&ESPMegaWebServer::dashboardProcessor, this, std::placeholders::_1); auto bindedDashboardProcessor = std::bind(&ESPMegaWebServer::dashboardProcessor, this, std::placeholders::_1);
@ -123,5 +201,20 @@ String ESPMegaWebServer::configProcessor(const String &var)
{ {
return String(mqttConfig->base_topic); return String(mqttConfig->base_topic);
} }
else if (var == "web_username")
{
return String(this->webUsername);
}
else if (var == "web_password")
{
return String(this->webPassword);
}
return ""; return "";
} }
void ESPMegaWebServer::otaHandler(AsyncWebServerRequest *request)
{
// Prepare to receive firmware
}

View File

@ -3,8 +3,15 @@
#include <ESPAsyncWebServer.h> #include <ESPAsyncWebServer.h>
#include <ESPMegaIoT.hpp> #include <ESPMegaIoT.hpp>
#include <Update.h> #include <Update.h>
#include <FRAM.h>
#include <html/all.h> #include <html/all.h>
/**
* @brief Provides a web server for ESPMegaPRO
*
* This class use FRAM address 301-400
*/
class ESPMegaWebServer class ESPMegaWebServer
{ {
public: public:
@ -12,7 +19,20 @@ class ESPMegaWebServer
~ESPMegaWebServer(); ~ESPMegaWebServer();
void begin(); void begin();
void loop(); void loop();
void resetCredentials();
char* getWebUsername();
char* getWebPassword();
void setWebUsername(const char* username);
void setWebPassword(const char* password);
void bindFRAM(FRAM *fram);
void loadCredentialsFromFRAM();
void saveCredentialsToFRAM();
private: private:
// FRAM
FRAM *fram;
// Credentials
char webUsername[32];
char webPassword[32];
// Web Server // Web Server
AsyncWebServer *server; AsyncWebServer *server;
uint16_t port; uint16_t port;

View File

@ -128,7 +128,7 @@ void setup()
ESP_LOGI("Initializer", "Installing climate card"); ESP_LOGI("Initializer", "Installing climate card");
espmega.installCard(2, &climateCard); espmega.installCard(2, &climateCard);
ESP_LOGI("Initializer", "Binding climate card to FRAM"); ESP_LOGI("Initializer", "Binding climate card to FRAM");
climateCard.bindFRAM(&espmega.fram, 301); climateCard.bindFRAM(&espmega.fram, 1001);
ESP_LOGI("Initializer", "Loading climate card state from FRAM"); ESP_LOGI("Initializer", "Loading climate card state from FRAM");
climateCard.loadStateFromFRAM(); climateCard.loadStateFromFRAM();
ESP_LOGI("Initializer", "Enabling climate card FRAM autosave"); ESP_LOGI("Initializer", "Enabling climate card FRAM autosave");
@ -145,6 +145,9 @@ void setup()
#ifdef WEB_SERVER_ENABLE #ifdef WEB_SERVER_ENABLE
ESP_LOGI("Initializer", "Enabling web server"); ESP_LOGI("Initializer", "Enabling web server");
espmega.enableWebServer(80); espmega.enableWebServer(80);
espmega.webServer->setWebUsername("admin");
espmega.webServer->setWebPassword("Passw0rd");
espmega.webServer->saveCredentialsToFRAM();
#endif #endif
} }