Browse Source

Experimental option to embed the web interface in the firmware image

fastled
Xose Pérez 7 years ago
parent
commit
69984e02fc
3 changed files with 48 additions and 9 deletions
  1. +18
    -0
      code/build-data_h
  2. +2
    -0
      code/espurna/config/general.h
  3. +28
    -9
      code/espurna/web.ino

+ 18
- 0
code/build-data_h View File

@ -0,0 +1,18 @@
#!/bin/python
import binascii
import string
source = 'espurna/data/index.html.gz'
destination = 'espurna/config/data.h'
with open(source, 'rb') as f:
content = f.read()
array = map(lambda x: '0x%02x' % ord(x), content)
with open(destination, 'w') as f:
f.write("#define index_html_gz_len %d\n" % len(array))
f.write("const uint8_t index_html_gz[] PROGMEM = {")
f.write(string.join(array, ','))
f.write("};\n")

+ 2
- 0
code/espurna/config/general.h View File

@ -101,6 +101,8 @@
#define AP_MODE_GW "192.168.4.1"
#define AP_MODE_MASK "255.255.255.0"
#define EMBED_WEB_IN_FIRMWARE 0
// -----------------------------------------------------------------------------
// OTA & NOFUSS
// -----------------------------------------------------------------------------


+ 28
- 9
code/espurna/web.ino View File

@ -16,6 +16,10 @@ Copyright (C) 2016-2017 by Xose Pérez <xose dot perez at gmail dot com>
#include <Ticker.h>
#include <vector>
#if EMBED_WEB_IN_FIRMWARE == 1
#include "config/data.h"
#endif
AsyncWebServer * _server;
AsyncWebSocket ws("/ws");
Ticker deferred;
@ -702,6 +706,14 @@ void _onAuth(AsyncWebServerRequest *request) {
}
#if EMBED_WEB_IN_FIRMWARE == 1
void _onHome(AsyncWebServerRequest *request) {
AsyncWebServerResponse *response = request->beginResponse_P(200, "text/html", index_html_gz, index_html_gz_len);
response->addHeader("Content-Encoding", "gzip");
request->send(response);
}
#endif
void webSetup() {
// Create server
@ -714,21 +726,28 @@ void webSetup() {
// Setup webserver
_server->addHandler(&ws);
// Rewrites
_server->rewrite("/", "/index.html");
// Serve home (basic authentication protection)
#if EMBED_WEB_IN_FIRMWARE == 1
_server->on("/index.html", HTTP_GET, _onHome);
#endif
_server->on("/auth", HTTP_GET, _onAuth);
_server->on("/apis", HTTP_GET, _onAPIs);
_server->on("/rpc", HTTP_GET, _onRPC);
// Serve static files
char lastModified[50];
sprintf(lastModified, "%s %s GMT", __DATE__, __TIME__);
_server->rewrite("/", "/index.html");
_server->serveStatic("/", SPIFFS, "/")
.setLastModified(lastModified)
.setFilter([](AsyncWebServerRequest *request) -> bool {
webLogRequest(request);
return true;
});
#if EMBED_WEB_IN_FIRMWARE == 0
char lastModified[50];
sprintf(lastModified, "%s %s GMT", __DATE__, __TIME__);
_server->serveStatic("/", SPIFFS, "/")
.setLastModified(lastModified)
.setFilter([](AsyncWebServerRequest *request) -> bool {
webLogRequest(request);
return true;
});
#endif
// 404
_server->onNotFound([](AsyncWebServerRequest *request){


Loading…
Cancel
Save