Home / References / ESP32 Library / WebServer Library
Description
The send_p()
method is a powerful function in the ESP32 WebServer Library that allows you to send HTTP responses containing data stored in PROGMEM (Program Memory). This method is particularly useful for serving static content like HTML, CSS, or JavaScript files efficiently, as it leverages the flash memory of the ESP32 to reduce RAM usage. It’s an essential tool for building lightweight web servers on resource-constrained devices like the ESP32.
Syntax and Usage
The send_p()
method can be used in different ways depending on whether you need to specify a content length. Below are the available syntaxes:
- Basic Response with Status Code, Content Type, and Content
server.
send_p
(statusCode, contentType, content);
This is the most common usage where you specify the HTTP status code, content type, and the actual content to be sent. - Response with Status Code and Content
server.
send_p
(statusCode, content);
When you want to send a response with just a status code and content, assuming a default content type of “text/html”. - Response with Content Only
server.
send_p
(content);
For sending basic content with a default status code of 200 (OK) and content type of “text/html”.
For practical applications and examples of this method, please consult the “Example Code” section on this page. This section provides comprehensive guidance to help you better understand and apply the method effectively.
Argument(s)
The send_p()
method accepts the following arguments:
- statusCode (int): The HTTP status code to send in the response (e.g., 200 for OK, 404 for Not Found).
- contentType (const char*): A string specifying the MIME type of the content (e.g.,
"text/html"
,"application/json"
). - content (const char*): A pointer to the PROGMEM data to be sent. This data must be stored in flash memory using the
PROGMEM
keyword.
Return Value
The send_p()
method does not return a value. Instead, it sends the HTTP response directly to the client connected to the ESP32 web server. If the operation fails (e.g., due to a disconnected client), the method handles the error internally without throwing an exception.
Example Codes
Below are example codes demonstrating the two ways to use the send_p()
method. Each example includes an explanation to guide you through its implementation.
Example 1: Basic Usage (with Content Type)
This code demonstrates how to use the ESP32 WebServer library’s `send_P()` method to serve HTML content directly from flash memory (PROGMEM). This method is used to efficiently serve static web pages, saving valuable RAM on the ESP32 by storing the HTML in flash.
To use this code, replace “your-SSID” and “your-PASSWORD” with your Wi-Fi credentials. After uploading the code to your ESP32, open a web browser and navigate to the ESP32’s IP address, which will be printed in the serial monitor. The web page stored in PROGMEM will be displayed.
/*
* Author: Avant Maker
* Date: March 17, 2025
* Version: 1.0
* License: MIT
*
* Description:
* This example demonstrates how to use ESP32 WebServer Library's
* send_p method to set up a basic web server that serves an
* HTML page when a client connects to the root URL.
*
* Code Source:
* This example code is sourced from the Comprehensive Guide
* to the ESP32 Arduino Core Library, accessible on AvantMaker.com.
* For additional code examples and in-depth documentation related to
* the ESP32 Arduino Core Library, please visit:
*
* https://avantmaker.com/home/all-about-esp32-arduino-core-library/
*
* AvantMaker.com, your premier destination for all things
* DIY, AI, IoT, Smart Home, and STEM projects. We are dedicated
* to empowering makers, learners, and enthusiasts with
* the resources they need to bring their innovative ideas to life.
*/
#include <WiFi.h>
#include <WebServer.h>
const char* ssid = "your-SSID"; // Replace with your Wi-Fi SSID
const char* password = "your-PASSWORD"; // Replace with your Wi-Fi password
WebServer server(80);
// Store HTML content in PROGMEM to save RAM
const char MAIN_page[] PROGMEM = R"rawliteral(
<!DOCTYPE html>
<html>
<head>
<title>ESP32 Web Server</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<h1>AvantMaker ESP32 Web Server using PROGMEM</h1>
<p>This page is stored in flash memory (PROGMEM) to save RAM.</p>
</body>
</html>
)rawliteral";
void setup() {
Serial.begin(115200);
// Connect to Wi-Fi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
Serial.print("IP Address: ");
Serial.println(WiFi.localIP());
// Define server routes
server.on("/", handleRoot);
// Start server
server.begin();
Serial.println("HTTP server started");
}
void loop() {
server.handleClient();
}
void handleRoot() {
// Send HTML content stored in PROGMEM
server.send_P(200, "text/html", MAIN_page);
}
ESP32 Library Index
- ESP32 WiFi Library
- ESP32 WiFiClient Library
- ESP32 HTTPClient Library
- ESP32 WiFiClientSecure Library
- ESP32 AsyncUDP Librarry
- ESP32 WebServer Library
- Server Operation
- Client Hnadling
- Routing and Handlers
- Authentication
- Request Information
- Request Header Management
- Response Information
- Server Configuration
- Which ESP32 Boards are Recommended for Learners
- How to Copy Codes from AvantMaker.com
- What is SPIFFS and how to upload files to it?
- What is LIttleFS and how to upload files to it?
Ready to experiment and explore more about ESP32? Visit our website’s All About ESP32 Resources Hub, packed with tutorials, guides, and tools to inspire your maker journey. Experiment, explore, and elevate your skills with everything you need to master this powerful microcontroller platform!