ESP8266 server strona html z obrazkami na karcie CD przykład
ESP8266WiFi
, SD
i ESP8266WebServer
(lub asynchronicznej ESPAsyncWebServer
), zainicjowanie karty SD w setup()
, a następnie przekierowanie wszystkich zapytań HTTP do funkcji, która odczytuje żądany plik z karty i strumieniuje go („streamFile”) do przeglądarki z odpowiednim nagłówkiem MIME.index.html
, grafiki, CSS, JS) w strukturze katalogów oraz implementacja funkcji getContentType()
./
├── index.html
├── style.css
├── images/
│ ├── logo.jpg
│ └── scenic.png
└── js/
└── app.js
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <SD.h>
#include <SPI.h>
const char* ssid = "TwojeSSID";
const char* pass = "TwojeHASLO";
const uint8_t SD_CS = 15; // D8
ESP8266WebServer server(80);
// -------------------------------------------------
String getContentType(const String& name) {
if (name.endsWith(".html")) return "text/html";
if (name.endsWith(".css")) return "text/css";
if (name.endsWith(".js")) return "application/javascript";
if (name.endsWith(".png")) return "image/png";
if (name.endsWith(".jpg") || name.endsWith(".jpeg")) return "image/jpeg";
if (name.endsWith(".gif")) return "image/gif";
if (name.endsWith(".ico")) return "image/x-icon";
return "text/plain";
}
void handleFile() {
String path = server.uri();
if (path.endsWith("/")) path += "index.html";
if (!SD.exists(path)) { server.send(404, "text/plain", "404 - nie znaleziono"); return; }
File f = SD.open(path, "r");
server.streamFile(f, getContentType(path));
f.close();
}
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED) delay(500);
if (!SD.begin(SD_CS)) { Serial.println("SD error"); while (true); }
server.onNotFound(handleFile);
server.begin();
Serial.println(WiFi.localIP());
}
void loop() { server.handleClient(); }
ESPAsyncWebServer
)Bezblokujące przetwarzanie i lepsza wydajność przy wielu jednoczesnych połączeniach.
#include <ESP8266WiFi.h>
#include <ESPAsyncTCP.h>
#include <ESPAsyncWebServer.h>
#include <SD.h>
#include <SPI.h>
AsyncWebServer server(80);
const uint8_t SD_CS = 15;
void setup() {
WiFi.begin("SSID","PASS"); while(WiFi.status()!=WL_CONNECTED) delay(100);
if(!SD.begin(SD_CS)){Serial.println("SD fail"); while(true);}
server.on("^\\/.*$", HTTP_GET, [](AsyncWebServerRequest *req){
String path = req->url(); if(path.endsWith("/")) path += "index.html";
if(!SD.exists(path)){ req->send(404,"text/plain","404"); return; }
File f = SD.open(path, "r");
req->send(f, path, SD_MMC); // automatyczny typ MIME
});
server.begin();
}
void loop(){}
server.streamFile()
/req->send()
przekazuje dane w kawałkach (≈2 kB) – nie zapełnia RAM-u.ESPAsyncWebServer
(nieblokujący, obsługa WebSocket, SSE).Content-Encoding: gzip
) zmniejsza transfer nawet o 70 %.SPI.begin(SCK,MISO,MOSI,SD_CS)
po Wi-Fi.server.sendHeader("Cache-Control", "max-age=86400");
SD.begin(int, SPI_HALF_SPEED)
).Mongoose-OS
lub AsyncElegantOTA
dla OTA firmware + pliki stronie.mDNS
dla wygodnych nazw hosta.Serwer WWW na ESP8266, który udostępnia stronę HTML i grafiki z karty microSD, wymaga:
Dla większej wydajności warto rozważyć bibliotekę asynchroniczną, kompresję GZIP i cache’owanie. Projekt jest skalowalny – od prostej galerii, po pełnoprawny interfejs IoT.