ESP32 - Web Server on SD Card

In this tutorial, we'll find out how to make ESP32 web server hosted on MicroSD Card. The HTML, CSS, JavaScript, image files will be stored on the MicroSD Card.

ESP32 web server on MicroSD Card

Introduction to ESP32 and Web Server

If you're not familiar with ESP32 and Web Server, MicroSD Card (including pinout, how it works, and programming), you can learn about them through the following tutorials:

Wiring Diagram

ESP32 Micro SD Card Module Wiring Diagram

This image is created using Fritzing. Click to enlarge image

If you're unfamiliar with how to supply power to the ESP32 and other components, you can find guidance in the following tutorial: The best way to Power ESP32 and sensors/displays.

Copy HTML contents to MicroSD Card

  • Please make sure that your Micro SD Card is formated on FAT32 format as below (on Windows OS, right mouse click → format):
Micro SD Card format FAT32
  • Create a HTML file index.html and add the following code:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>ESP32 Web Page</title> <link rel="icon" href="favicon.ico" type="image/x-icon"> <link rel="stylesheet" href="styles.css"> </head> <body> <div class="container"> <h1>Web Page hosted on MicroSD Card</h1> <img src="luffy.jpg" alt="A beautiful image"> <p>Sponsored by <a href="https://amazon.com/diyables">DIYables</a></p> </div> </body> </html>
  • Create a CSS file styles.css and add the following code:
body { font-family: Arial, sans-serif; margin: 0; padding: 0; background-color: white; } h1 { font-size: 28px; } .container { max-width: 800px; margin: 20px auto; text-align: center; } img { max-width: 80%; height: auto; display: block; margin: 20px auto; }
  • Prepare an image. This tutorial uses luffy.jpg, which is used in HTML code. You can download this image here:
luffy
  • (Optional) Prepare favicon image. This tutorial uses this favicon.ico
  • Put all files in the root directory of MicroSD Card, like below images:
ESP32 HTML contents on microSD Card

Please note that you can add other files such as JavaScript, txt, csv...

ESP32 Code - Web server on MicroSD Card

/* * This ESP32 code is created by esp32io.com * * This ESP32 code is released in the public domain * * For more detail (instruction and wiring diagram), visit https://esp32io.com/tutorials/esp32-web-server-on-sd-card */ #include <FS.h> #include <SD.h> #define PIN_SPI_CS 5 // The ESP32 pin GPIO5 // Replace with your network credentials const char* ssid = "YOUR_WIFI_SSID"; // CHANGE IT const char* password = "YOUR_WIFI_PASSWORD"; // CHANGE IT // Create AsyncWebServer object on port 80 AsyncWebServer server(80); void setup() { Serial.begin(9600); // Print the ESP32's IP address Serial.print("ESP32 Web Server's IP address: "); Serial.println(WiFi.localIP()); // init MicroSD Card if (!SD.begin(PIN_SPI_CS)) { while (1) { Serial.println(F("SD CARD FAILED, OR NOT PRESENT!")); delay(1000); } } server.on("/", HTTP_GET, [](AsyncWebServerRequest* request) { request->send(SD, "/index.html", "text/html"); }); server.serveStatic("/", SD, "/"); server.begin(); } ================= #include <DIYables_ESP32_WebServer.h> #include "index.h" // WiFi credentials const char WIFI_SSID[] = "YOUR_WIFI_SSID"; const char WIFI_PASSWORD[] = "YOUR_WIFI_PASSWORD"; // Create web server instance DIYables_ESP32_WebServer server; // Page handlers void handleHome(WiFiClient& client, const String& method, const String& request, const QueryParams& params, const String& jsonData) { server.sendResponse(client, HOME_PAGE); } void setup() { Serial.begin(9600); delay(1000); Serial.println("ESP32 Web Server"); // Configure routes server.addRoute("/", handleHome); // Start web server with WiFi connection server.begin(WIFI_SSID, WIFI_PASSWORD); } void loop() { server.handleClient(); }

Quick Instructions

  • If this is the first time you use ESP32, see how to setup environment for ESP32 on Arduino IDE.
  • Connect the ESP32 board to your PC via a micro USB cable
  • Open Arduino IDE on your PC.
  • Select the right ESP32 board (e.g. ESP32 Dev Module) and COM port.
  • Open the Library Manager by clicking on the Library Manager icon on the left navigation bar of Arduino IDE.
  • Search “DIYables ESP32 WebServer”, then find the Web Server library created by DIYables.
  • Click Install button to install the Web Server library.
ESP32 Web Server library
  • Copy the above code and open with Arduino IDE
  • Change the wifi information (SSID and password) in the code to yours
  • Click Upload button on Arduino IDE to upload code to ESP32
  • Open the Serial Monitor
  • Check out the result on Serial Monitor.
Newbiely | Arduino IDE 2.3.8
──
File
Edit
Sketch
Tools
Help
ESP32 Dev Module
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'ESP32 Dev Module' on 'COM15')
New Line
9600 baud
Connecting to WiFi... Connected to WiFi ESP32 Web Server's IP address: 192.168.0.3
Ln 11, Col 1
ESP32 Dev Module on COM15
2
  • You will see an IP address on the Serial Monitor, for example: 192.168.0.3
  • Type the IP address on the address bar of a web browser on your smartphone or PC.
  • Please note that you need to change the 192.168.0.3 to the IP address you got on Serial Monitor.
  • You will see a page loads HTML, CSS, images from SD Card
ESP32 Web page on SD Card
  • As you can see, the HTML, CSS, images stored on SD Card were loaded by web browser.

Now you can modify the HTML, CSS code, add more images, JavaScript to make your own web page.

※ OUR MESSAGES