Prepare web server with embedded files

This commit is contained in:
2025-11-29 20:09:34 +01:00
parent 99a82917a7
commit 830d1b53c1
12 changed files with 466 additions and 56 deletions

View File

@@ -10,25 +10,47 @@
#include <N2kMessages.h>
#include "main.h"
#include "Nmea2kTwai.h"
#include <map>
Preferences preferences; // persistent storage for configuration
class EmbeddedFile;
static std::map<String, EmbeddedFile*> embeddedFiles;
class EmbeddedFile {
public:
const uint8_t *start;
int len;
String contentType;
EmbeddedFile(String name, String contentType, const uint8_t *start, int len) {
this->start = start;
this->len = len;
this->contentType = contentType;
embeddedFiles[name] = this;
}
} ;
#define EMBED_GZ_FILE(fileName, binName, contentType) \
extern const uint8_t binName##_File[] asm("_binary_" #binName "_start"); \
extern const uint8_t binName##_FileLen[] asm("_binary_" #binName "_size"); \
const EmbeddedFile binName##_Config(fileName,contentType,(const uint8_t*)binName##_File,(int)binName##_FileLen);
#include "embeddedfiles.h"
void send_embedded_file(String name, AsyncWebServerRequest *request)
{
std::map<String, EmbeddedFile*>::iterator it = embeddedFiles.find(name);
if (it != embeddedFiles.end()) {
EmbeddedFile* found = it->second;
AsyncWebServerResponse *response = request->beginResponse(200, found->contentType, found->start, found->len);
response->addHeader(F("Content-Encoding"), F("gzip"));
request->send(response);
} else {
request->send(404, "text/plain", "Not found");
}
}
const char* wifi_ssid = "OBPKP61";
const char* wifi_pass = "keypad61";
AsyncWebServer server(80);
const char index_html[] PROGMEM = R"rawliteral(
<!DOCTYPE HTML><html>
<head>
<title>ESP Web Server</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="icon" href="data:,">
</head>
<body>
<h2>ESP Web Server</h2>
<p>Work in progress</p>
</body>
</html>
)rawliteral";
unsigned long lastPrint = 0;
unsigned long counter = 0;
@@ -110,20 +132,29 @@ void setup() {
WiFi.persistent(false);
WiFi.mode(WIFI_MODE_AP);
IPAddress ap_addr(192, 168, 15, 1);
IPAddress ap_subnet(255, 255, 255, 0);
IPAddress ap_gateway(ap_addr);
int channel = WIFI_CHANNEL;
bool hidden = false;
WiFi.softAP(wifi_ssid, wifi_pass, channel, hidden, WIFI_MAX_STA);
IPAddress ap_addr(192, 168, 15, 1);
IPAddress ap_subnet(255, 255, 255, 0);
IPAddress ap_gateway(ap_addr);
WiFi.softAPConfig(ap_addr, ap_gateway, ap_subnet);
// Route for root / web page
server.on("/", HTTP_GET, [](AsyncWebServerRequest *request) {
request->send(200, "text/html", index_html, processor);
send_embedded_file("index.html", request);
});
// Route for all other defined pages
for (auto it = embeddedFiles.begin(); it != embeddedFiles.end(); it++) {
String uri = String("/") + it->first;
server.on(uri.c_str(), HTTP_GET, [it](AsyncWebServerRequest *request) {
send_embedded_file(it->first, request);
});
}
server.begin();
NMEA2000.SetProductInformation("00000001", // Manufacturer's Model serial code
74, // Manufacturer's product code
"OBPkeypad6/1", // Manufacturer's Model ID
@@ -284,7 +315,6 @@ void loop() {
delay(200);
}
// ---- PRINT NUMBER EVERY SECOND ----
if (millis() - lastPrint >= 1000) {
lastPrint = millis();