ESP8266 ArduinoCore Tutorial Web Server

A web server is a combination of software and hardware that stores, processes, and delivers the content of websites to users over the internet, typically using the HTTP or HTTPS protocol. When you enter a website’s URL in your browser, your browser sends a request to the web server, which then sends back the requested files, such as HTML pages, images, and videos, for you to view

TCP Server Listener

The below Arduino code will also create a server and Access Point in ESP8266 which will continuously listen for a connection.

Code

//www.ArunEworld.com

#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>

const char *ssid = "ArunEworld";
const char *password = "Arun";

ESP8266WebServer server(80);

void handleRoot() 
{
    server.send(200, "text/html", "<t1>ArunEworld</t1>");
    server.send(200, "text/html", "<h1>ArunEworld : TCP WebServer Listener</h1>");
    server.send(200, "text/html", "<h2>You are connected</h2>");
}

void setup()
{
    delay(1000);
    Serial.begin(115200);
    Serial.println();
    Serial.print("Configuring access point...");
    WiFi.softAP(ssid, password);
    IPAddress myIP = WiFi.softAPIP();
    Serial.print("AP IP address: ");
    Serial.println(myIP);
    server.on("/", handleRoot);
    server.begin();
    Serial.println("HTTP server started");
}

void loop() 
{
    server.handleClient();
}
After uploading this sketch, you can find a new Access Point named “test” from your Laptop or PC.

Code Explanation

1. Libraries

#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
  • ESP8266WiFi.h: Provides WiFi functions (connect, AP mode, etc.).
  • WiFiClient.h: Used for TCP client connections (not directly used here but needed).
  • ESP8266WebServer.h: Simple HTTP web server class for ESP8266.

WiFi SSID & Password

const char *ssid = "ArunEworld";
const char *password = "Arun";
  • Defines Access Point (AP) credentials.
  • Your ESP8266 will create its own WiFi hotspot named “ArunEworld” with password “Arun”.

Web Server Object

ESP8266WebServer server(80);
  • Creates a web server running on port 80 (default HTTP port).

Root Page Handler

void handleRoot() 
{
    server.send(200, "text/html", "<t1>ArunEworld</t1>");
    server.send(200, "text/html", "<h1>ArunEworld : TCP WebServer Listener</h1>");
    server.send(200, "text/html", "<h2>You are connected</h2>");
}
  • When someone visits ESP8266’s IP address / (root page), it sends back HTML response.
    ⚠️ Note: server.send() should be used once per request. Multiple calls overwrite each other. So only the last one (<h2>You are connected</h2>) will be displayed.

Correct way:

server.send(200, "text/html", "<h1>ArunEworld</h1><h2>TCP WebServer Listener</h2><p>You are connected</p>");

Setup Function

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

    Serial.println("Configuring access point...");
    WiFi.softAP(ssid, password);   // Start WiFi in Access Point mode

    IPAddress myIP = WiFi.softAPIP();
    Serial.print("AP IP address: ");
    Serial.println(myIP);

    server.on("/", handleRoot);    // Define what to do when "/" is requested
    server.begin();                // Start the HTTP server
    Serial.println("HTTP server started");
}
  • ESP8266 starts in Access Point mode (Hotspot).
  • Gets its own IP (default: 192.168.4.1).
  • Attaches / request to the function handleRoot().
  • Starts the web server.

Loop Function

void loop() 
{
    server.handleClient();
}
  • Keeps listening for client requests (e.g., when you open ESP8266 IP in a browser).
  • Calls handleRoot() when the root page is accessed.

How it works in real-time

  1. Flash this code to your ESP8266.
  2. It creates a WiFi hotspot → SSID: ArunEworld, Password: Arun.
  3. Connect your laptop/phone to that WiFi.
  4. Open browser → enter IP 192.168.4.1.
  5. You’ll see the webpage served by ESP8266.

Output (webpage) will show:

ArunEworld
TCP WebServer Listener
You are connected

Result

Image


 

Please turn AdBlock off, and continue learning

Notice for AdBlock users

Please turn AdBlock off
Index

Discover more from ArunEworld

Subscribe now to keep reading and get access to the full archive.

Continue reading