Categories: ESP32 Firmware Guide

Simple WiFi server Program on ESP32

A Web server is a program that uses HTTP (Hypertext Transfer Protocol) to serve the files that form Web pages to users, in response to their requests, which are forwarded by their computer’s HTTP clients. This tutorial “Simple WiFi server Program on ESP32” shows how to blink an LED via the web. Here we create a web server using ESP32. Then the ESP32 controller will show you an IP address. When you type the IP address in the browser. The browser sends a request to the web server for the connection. After the connection established, the web server will return a web page.

The first thing you need to do is include the wifi.h library

#include"wifi.h"

Set the LED pin as an output.

pinMode(5, OUTPUT);

Connect to the WiFi network by entering your SSID and password. After connecting to the network, get an IP address of your WiFi shield(once connected). You can open the address on a web browser, to turn on and off the LED on pin 5.

 if (currentLine.length() == 0) {  
            client.println("HTTP/1.1 200 OK");
            client.println("Content-type:text/html");
            client.println();

The connection between the client and the web server is established using the HTTP protocol. The HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)and a content-type so the client knows what’s coming.

client.print("Click <a href=\"/H\">here</a> to turn the LED on pin 5 on.<br>");
client.print("Click <a href=\"/L\">here</a> to turn the LED on pin 5 off.<br>");

The content of the HTTP response follows the HTTP header.

if (currentLine.endsWith("GET /H")) {
          digitalWrite(5, HIGH);             
   if (currentLine.endsWith("GET /L")) {
          digitalWrite(5, LOW);                
        }

If the client request was “GET /H”, it turns the LED on the client request was “GET /L”, it turns the LED off.
stop() disconnect from the server.

  client.stop();

The content of the HTTP response displayed on the serial monitor.

Share