Adding SPIFFS to this project to store webpages

This commit is contained in:
Sébastien Vallée 2017-12-25 09:12:06 +01:00
parent e97d9b98b5
commit fe49e16934

View File

@ -14,6 +14,7 @@
#include <ArduinoOTA.h> #include <ArduinoOTA.h>
#include "OTAsetup.h" #include "OTAsetup.h"
#include "d_helper.h" #include "d_helper.h"
#include "FS.h"
//################# DISPLAY CONNECTIONS ################ //################# DISPLAY CONNECTIONS ################
@ -55,6 +56,7 @@ void setup() {
matrix.fillScreen(LOW); //clr screen matrix.fillScreen(LOW); //clr screen
initOTA(); initOTA();
SPIFFS.begin();
for (int i=0; i<numberOfHorizontalDisplays; i++){ for (int i=0; i<numberOfHorizontalDisplays; i++){
DEBUG_PRINT(F("Changing orientation of display #")); DEBUG_PRINT(F("Changing orientation of display #"));
@ -189,32 +191,43 @@ void GetMessage() {
if (Argument_Name == "brightness") brightness = client_response.toInt(); if (Argument_Name == "brightness") brightness = client_response.toInt();
} }
} }
String webpage = fileRead("/index.html");
String ipaddress = WiFi.localIP().toString();
webpage.replace("ipaddress", ipaddress);
server.send(200, "text/html", webpage); // Send a response to the client to enter their inputs, if needed, Enter=defaults server.send(200, "text/html", webpage); // Send a response to the client to enter their inputs, if needed, Enter=defaults
} }
void append_page_header() { String fileRead(String name){
webpage = F("<!DOCTYPE HTML><html lang='fr'><head>"); //read file from SPIFFS and store it as a String variable
webpage += "<META HTTP-EQUIV='Content-Type' CONTENT='text/html; charset=utf-8'>"; String contents;
webpage += F("<title>Envoi de messages - LibreMetric</title><style>"); File file = SPIFFS.open(name.c_str(), "r");
webpage += F("body {width:"); if (!file) {
webpage += SITE_WIDTH; String errorMessage = "Can't open '" + name + "' !\r\n";
webpage += F("px;margin:0 auto;font-family:arial;font-size:14px;text-align:center;color:#776688;background-color:#F7F2Fd;}"); DEBUG_PRINTLN(errorMessage);
webpage += F("ul{list-style-type:circle; margin:0;padding:0;overflow:hidden;background-color:#d8d8d8;font-size:14px;}"); return "FILE ERROR";
webpage += F("li{float:left;border-right:1px solid #bbb;}last-child {border-right:none;}"); }
webpage += F("li a{display: block;padding:2px 12px;text-decoration:none;}"); else {
webpage += F("li a:hover{background-color:#FFFFFF;}");
webpage += F("section {font-size:16px;}"); // this is going to get the number of bytes in the file and give us the value in an integer
webpage += F("p {background-color:#D1DBE3;font-size:16px;}"); int fileSize = file.size();
webpage += F("div.header,div.footer{padding:0.5em;color:white;background-color:gray;clear:left;}"); int chunkSize=1024;
webpage += F("h1{background-color:#d8d8d8;font-size:26px;}"); //This is a character array to store a chunk of the file.
webpage += F("h2{color:#9370DB;font-size:22px;line-height:65%;}"); //We'll store 1024 characters at a time
webpage += F("h3{color:#9370DB;font-size:16px;line-height:55%;}"); char buf[chunkSize];
webpage += F("</style></head><body><h1>LibreMetric "); int numberOfChunks=(fileSize/chunkSize)+1;
webpage += version+"</h1>";
} int count=0;
int remainingChunks=fileSize;
void append_page_footer(){ // Saves repeating many lines of code for HTML page footers for (int i=1; i <= numberOfChunks; i++){
webpage += F("<ul><li><a href='/'>Recharger la page</a></li></ul>"); if (remainingChunks-chunkSize < 0){
webpage += F("</body></html>"); chunkSize=remainingChunks;
}
file.read((uint8_t *)buf, chunkSize-1);
remainingChunks=remainingChunks-chunkSize;
contents+=String(buf);
}
file.close();
return contents;
}
} }