Home / References / ESP32 Library / WebServer Library
Description
The send()
method is a key function in the ESP32 WebServer Library, used to transmit HTTP responses to connected clients. It allows you to send a status code, content type, and response body in a single call, making it an efficient way to deliver web content such as HTML pages, JSON data, or plain text from your ESP32-based server.
Syntax and Usage
The send()
method can be used in multiple ways depending on the content you want to send and the level of customization required. Here are the different ways to use this method:
- Basic Response with Status Code, Content Type, and Content
server.send(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(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(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()
method accepts the following arguments:
- statusCode: An integer representing the HTTP status code of the response (e.g., 200 for OK, 404 for Not Found).
- contentType: A string indicating the MIME type of the content (e.g., “text/html”, “application/json”, “text/plain”).
- content: The actual content to be sent to the client. This can be a string, HTML, JSON, or other text-based content.
Return Value
The send()
method does not return any value. It is a void function that sends the HTTP response to the connected client.
Example Codes
Example 1: Basic Response with Status Code, Content Type, and Content
This code demonstrates how to use the ESP32 WebServer Library’s send()
method to set up a basic web server. When a client connects to the root URL, the server sends an HTML page using the send()
method. This example shows the fundamental way to serve web content from an ESP32.
To use this code, replace “your-SSID” and “your-PASSWORD” with your Wi-Fi credentials. After uploading the code to your ESP32, open the Serial Monitor to get the assigned IP address. Open a web browser and enter the ESP32’s IP address to view the simple HTML page served by the ESP32.
/*
* Author: Avant Maker
* Date: March 17, 2025
* Version: 1.0
* License: MIT
*
* Description:
* This example demonstrates how to use ESP32 WebServer Library's
* send 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>
// Wi-Fi credentials
const char* ssid = "your-SSID"; // Replace with your Wi-Fi SSID
const char* password = "your-PASSWORD"; // Replace with your Wi-Fi password
// Create WebServer object on port 80
WebServer server(80);
// Handler for the root path
void handleRoot() {
String html = "<!DOCTYPE html><html><body>";
html += "<h1>ESP32 Web Server</h1>";
html += "<p>Welcome to AvantMaker.com's ESP32 Web Server!</p>";
html += "</body></html>";
server.send(200, "text/html", html);
}
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 route for root
server.on("/", handleRoot);
// Start the server
server.begin();
Serial.println("Web server started");
}
void loop() {
server.handleClient();
}
Example 2: Sending Different Content Types (respond with HTML, plain text, and JSON data)
This ESP32 Web Server code demonstrates how to create a HTTP server. This code establishes three different endpoints that respond with HTML, plain text, and JSON data.
To use this ESP32 Web Server code, first fill in WiFi credentials with your network’s SSID and password. Upload the code to your ESP32 using the Arduino IDE. Once connected to WiFI, the ESP32 will display its IP address in the serial monitor. Access this IP address in a web browser to view the root page. Add “/text” or “/json” to the URL to test the different response types.
/*
* Author: Avant Maker
* Date: March 17, 2025
* Version: 1.0
* License: MIT
*
* Description:
* This example demonstrates how to use ESP32 WebServer Library's
* send method to create a HTTP server. This code establishes
* three different endpoints that respond with HTML,
* plain text, and JSON data.
*
* 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>
#include <ArduinoJson.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);
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);
server.on("/text", handleText);
server.on("/json", handleJson);
// Start server
server.begin();
Serial.println("HTTP server started");
}
void loop() {
server.handleClient();
}
// Handler for the root path
void handleRoot() {
String html = "<!DOCTYPE html><html><body>";
html += "<h1>ESP32 Web Server</h1>";
html += "<p>Welcome to AvantMaker.com's ESP32 Web Server!</p>";
html += "</body></html>";
server.send(200, "text/html", html);
}
void handleText() {
// Send plain text content
server.send(200, "text/plain", "This is a plain text response from the ESP32 web server.");
}
void handleJson() {
// Create a JSON object
StaticJsonDocument<200> doc;
doc["sensor"] = "ESP32";
doc["time"] = millis();
doc["data"] = random(0, 100);
// Serialize JSON to string
String jsonString;
serializeJson(doc, jsonString);
// Send JSON content
server.send(200, "application/json", jsonString);
}
ESP32 Library Index
- ESP32 WiFi Library
- ESP32 WiFiClient Library
- ESP32 HTTPClient Library
- ESP32 WiFiClientSecure Library
- ESP32 WebServer Library
- Server Operation
- Client Hnadling
- Routing and Handlers
- Authentication
- Request Information
- 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!