How to Set up a Local Web Server on ESP8266

Setting up a web server on ESP8266 requires very little code and is surprisingly straightforward. A Web server uses HTTP to serve the files that form Web pages to users, in response to their requests, which are forwarded by their computers’ HTTP clients. The versatile ESP8266WiFi library allows you set up your webserver in minutes!

Setting up ESP8266 Web Server

First, you need to include a few libraries which will be needed to connect the ESP8266 to the local wifi network.

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

Define your SSID and Password of your WiFi router, where the ESP connects

//SSID and Password for ESP8266
const char* ssid = "your_ssid";
const char* password = "password";

There are two ways to make web server one is to connect to the WiFi hotspot or make ESP as a hot spot (Access Point).

This command is used to connect to your WiFi Access point. The term Access Point (AP) is same as WiFi Hot Spot. If the network is open you can remove password field from command.

WiFi.begin(ssid, password);     //Connect to your WiFi router

After connection request, we wait for WiFi to get connected. ESP8266 once connected and disconnected afterward due to signal loss or any reason, there is no need to give this command again, it will try to connect again automatically. This is handled by its OS, you may find some stack errors displayed on the serial monitor, and these errors come from its internal OS.

// Wait for connection

  while (WiFi.status() != WL_CONNECTED) {

    delay(500);

    Serial.print(".");}

To get IP address i.e. assigned to ESP8266 by your WiFi router use this command

This subroutine is called when you enter IP address in the web browser and hit enter. This routine sends the test “Hello World” to the web browser.

To start the server use this command

 Simple Code to set up a web server

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

ESP8266WebServer server;
char* ssid = "YOUR_SSID";
char* password = "YOUR_PASSWORD";

void setup()
{
 WiFi.begin(ssid,password);
 Serial.begin(115200);
 while(WiFi.status()!=WL_CONNECTED)
 {
 Serial.print(".");
 delay(500);
 }
 Serial.println("");
 Serial.print("IP Address: ");
 Serial.println(WiFi.localIP());

server.on("/",[](){server.send(200,"text/plain","Hello World!");});
 server.begin();
}

void loop()
{
 server.handleClient();
}
This should be sufficient for at least testing out the web server side of things!
2

IP address displayed on the serial monitor

Copy the IP address that’s been assigned by your local Wi-Fi router and open a browser window

1

 —

Christine

Christine

Author at onlineshouter
Christine writes for people who seek for knowledge about SEO, blogging, online marketing, gadgets and web apps.
Christine

Leave a Reply

Your email address will not be published.