Complete Guide to Building a Web Server on an ESP32 Chip

A Complete Guide to Building a Web Server on an ESP32 Chip

ESP32 is a powerful microcontroller, widely used in IoT projects, and can run a web server allowing electronic devices to be controlled over the web. This guide will walk you through the procedure of creating a simple web server on an ESP32 chip that will enable a user to control an LED remotely.

Table of Contents

Prerequisites

Before we begin, ensure you have the following equipment:

Note: This tutorial assumes that you have a basic understanding of the Arduino IDE and PHP programming.

The Circuit Diagram

The next step is to connect the LED to the ESP32. For this, connect the LED's anode (long leg) to the GPIO 23 of the ESP32 through a 220-ohm resistor. Connect the LED's cathode (short leg) directly to the GND.

Circuit Diagram

The Web Server Code

#include <WiFi.h>

const char* ssid = "your_SSID";
const char* password = "your_PASSWORD";

WiFiServer wifiServer(80);

void setup() {
  Serial.begin(115200);
  delay(1000);

  pinMode(23, OUTPUT);
  digitalWrite(23, LOW);

  Serial.print("Connecting to ");
  Serial.println(ssid);

  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println("...");
  }

  Serial.println(WiFi.localIP());

  wifiServer.begin();
}

void loop() {
  WiFiClient client = wifiServer.available();

  if (client) {
    String request = client.readStringUntil('\r');
    Serial.println(request);
    client.flush();

    if (request.indexOf("/LED=ON") != -1) {
      digitalWrite(23, HIGH);
    }

    if (request.indexOf("/LED=OFF") != -1) {
      digitalWrite(23, LOW);
    }

    client.println("HTTP/1.1 200 OK");
    client.println("Content-Type: text/html");
    client.print("<html><body><h1>Hello from ESP32!</h1></body></html>");

    delay(1);
    client.stop();

    Serial.println("Client disconnected");
    Serial.println("");
  }
}

Uploading the Code

To upload the code:

  1. Open Arduino IDE.
  2. Connect the ESP32 to your PC via a USB cable.
  3. From the Tools menu, select the ESP32 board and the appropriate port.
  4. Click on upload.

Connecting to ESP32 Web Server

On successful upload:

  1. Open the Serial Monitor in the Arduino IDE.
  2. Reset the ESP32. You'll find the ESP IP address in the Serial Monitor.
  3. Open a web browser, and enter the IP address. You'll see "Hello from ESP32!" and can control the LED by accessing /LED=ON or /LED=OFF.

FAQs

  1. What can you do with an ESP32 web server?

    • With an ESP32 web server, you can control almost any appliance or device remotely from any part of the globe.
  2. Can multiple clients connect to the ESP32 web server at the same time?

    • Yes, it's possible for multiple clients to connect at once, but performance may degrade as the number of clients rises.
  3. Can the ESP32 server be accessed outside the local network?

    • Yes, but you'll need to perform port forwarding on your router or use a service like ngrok.

Remember, coding is about exploration and creativity. Once you've got the basics down with this LED example, you can expand on this project by adding more functionalities to your web server.

Conclusion

This guide has demonstrated how to create a basic web server on an ESP32 to control an LED over the internet. The possibilities are endless with this, as the functionality can be adapted and expanded for many other applications, from home automation to industrial machine control.

Enjoy coding!