Setting Up a Web Server on ESP32

Introduction

Bits&Bots
4 min readMay 19, 2024

Welcome to the next chapter in our journey of exploring the vast possibilities of IoT with ESP32 microcontrollers. In this installment, we delve into a fundamental aspect of IoT development: setting up a web server on ESP32. By establishing a web server, you gain the ability to control and monitor your IoT devices remotely, opening up a world of opportunities for automation, data visualization, and remote management.

In this comprehensive guide, we’ll walk you through the process of setting up a web server on ESP32, from understanding the basics to implementing practical examples. Whether you’re a seasoned IoT enthusiast or just starting your journey into the world of connected devices, this tutorial will equip you with the knowledge and skills to take your projects to the next level.

Please note that setting up a web server on ESP8266 involves using a different library and approach, which will be covered in another blog post. For now, let’s focus on unleashing the full potential of ESP32 microcontrollers with web server capabilities.

So, grab your ESP32 board, fire up the Arduino IDE, and let’s embark on this exciting journey of empowering your IoT projects with the magic of web servers.

Understanding the Basics:

Before we dive into setting up a web server on ESP32, it’s essential to grasp the fundamentals of web servers and their significance in IoT projects.

What is a Web Server? — A web server is a software application or hardware device responsible for serving web pages to clients over the internet or a local network. It acts as a mediator between clients (such as web browsers) and web applications, facilitating the exchange of data and resources.

How Does a Web Server Work? — When a client requests a web page, such as typing a URL into a browser’s address bar, the browser sends a request to the web server hosting that page. The web server processes the request, retrieves the necessary resources (HTML, CSS, JavaScript, images, etc.), and sends them back to the client’s browser, which then renders the page for the user.

Web Servers in IoT: In the context of IoT, web servers play a crucial role in enabling remote control and monitoring of connected devices. By hosting a web server on an IoT device like the ESP32, you can create a user-friendly interface accessible from any web browser. This interface allows users to interact with their IoT devices, such as toggling LEDs, adjusting settings, or viewing sensor data, without the need for specialized software or apps.

Advantages of Using Web Servers in IoT:

  • Accessibility: Web-based interfaces are accessible from any device with a web browser, making them platform-independent and user-friendly.
  • Scalability: Web servers can handle multiple client connections simultaneously, making them suitable for IoT applications with multiple users or devices.
  • Flexibility: Web servers offer flexibility in designing custom interfaces tailored to specific IoT applications or user requirements.
  • Integration: Web servers can easily integrate with other web services, APIs, and cloud platforms, enabling seamless data exchange and interoperability in IoT ecosystems.

By harnessing the power of web servers, IoT developers can create dynamic and interactive experiences for users, unlocking the full potential of their connected devices.

Setting up the web server

We’ll start by including the required libraries and defining some variables:

#include <WiFi.h>
#include <WebServer.h>

// Replace with your network credentials
const char* ssid = "YourNetworkSSID";
const char* password = "YourNetworkPassword";

// Create a WebServer object listening on port 80
WebServer server(80);

// HTML content for the main page
const char* html_page = R"(
<!DOCTYPE html>
<html>
<head>
<title>ESP32 Web Server</title>
</head>
<body>
<h1>Control Your IoT Devices</h1>
<p>Click the button below to toggle the LED:</p>
<button onclick=\"toggleLED()\">Toggle LED</button>
<script>
function toggleLED() {
var xhr = new XMLHttpRequest();
xhr.open("GET", "/toggle", true); // Send GET request to /toggle route
xhr.send(); // Send the request
}
</script>
</body>
</html>
)";

// Handler for the root page ("/")
void handleRoot() {
server.send(200, "text/html", html_page); // Send HTML page to client
}

// Handler for the "/toggle" route
void handleToggle() {
// Toggle the LED (assuming built-in LED is connected to pin LED_BUILTIN)
digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
server.send(200, "text/plain", "OK"); // Send response to client
}

void setup() {
// Initialize built-in LED pin as an output
pinMode(LED_BUILTIN, OUTPUT);

Serial.begin(115200);

// Connect to Wi-Fi network
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");

// Define routes and their corresponding handlers
server.on("/", HTTP_GET, handleRoot); // Handle requests to the root ("/") route
server.on("/toggle", HTTP_GET, handleToggle); // Handle requests to the "/toggle" route

// Start the web server
server.begin();
Serial.println("Web server started");
}

void loop() {
// Handle client requests
server.handleClient();
}

Conclusion

In this tutorial, we’ve explored the process of setting up a web server on the ESP32 microcontroller, empowering you to control and monitor your IoT devices remotely. By leveraging the power of web-based interfaces, you can create dynamic and interactive experiences for users, opening up a world of possibilities for automation, data visualization, and remote management.

We began by understanding the basics of web servers and their role in IoT projects, highlighting their accessibility, scalability, flexibility, and integration capabilities. We then walked through the steps to prepare your development environment, including installing the necessary libraries and configuring your ESP32 board.

With your environment set up, we proceeded to write the code to configure the web server, define routes, and handle client requests. By serving a simple HTML page with a button to toggle an LED, we demonstrated how you can interact with your IoT devices through the web-based interface.

Finally, we concluded with a complete code example and explanations of key concepts, ensuring that you have everything you need to get started with building your own web-based IoT projects on the ESP32 platform. Further, blog for web server on ESP8266 will be published soon…

As you continue your journey into the world of IoT development, remember that the possibilities are endless with ESP32 as your trusty companion. Stay tuned for more tutorials and project ideas in our “Bits & Bots” blog series, where we’ll continue to explore the exciting realm of IoT with practical examples and insights.

--

--

Bits&Bots

Tech-curious maker empowering you to unleash the magic of ESP32/ESP8266! Join me for tutorials, projects, & many more. Let's build the future!