ESP8266 Web Server

ESP8266 Wi-Fi Module Web Server

Introduction

This experiment demonstrates how to set up a simple web server using the ESP8266 Wi-Fi module. The web server can control outputs such as LEDs or other devices over a web page.

ESP8266 Web Server

Components Needed

Circuit Setup

  1. Connect the VCC pin of the ESP8266 to the 3.3V pin of the Arduino.
  2. Connect the GND pin of the ESP8266 to the GND pin of the Arduino.
  3. Connect the LED to pin 2 of the Arduino with the 220-ohm resistor in series.

Make sure the connections are correct before proceeding.

ESP8266 Circuit Setup

Code for ESP8266 Web Server

Upload the following code to your Arduino to create a web server that controls the LED:


#include 

const char* ssid = "yourSSID";
const char* password = "yourPassword";

WiFiServer server(80);
const int ledPin = 2;

void setup() {
  Serial.begin(115200);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println("Connecting...");
  }
  server.begin();
  pinMode(ledPin, OUTPUT);
}

void loop() {
  WiFiClient client = server.available();
  if (client) {
    String request = client.readStringUntil('\r');
    client.flush();
    if (request.indexOf("/led/on") != -1) {
      digitalWrite(ledPin, HIGH);
    }
    if (request.indexOf("/led/off") != -1) {
      digitalWrite(ledPin, LOW);
    }
    client.print("HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n");
    client.print("

ESP8266 Web Server

Turn On LED
Turn Off LED

"); client.stop(); } }

Explanation

This code sets up a web server on the ESP8266. When you visit the IP address of the ESP8266, you can control the state of the LED by clicking on links.

Troubleshooting