ESP8266 LED Control Using WiFi

Introduction

ESP8266 NodeMCU is a powerful WiFi-enabled microcontroller. It allows you to control devices over the internet.

In this tutorial, we will control an LED using a web browser over WiFi.


Components Required

  • ESP8266 NodeMCU
  • LED
  • 220Ω resistor
  • Breadboard
  • Jumper wires

Circuit

  • LED positive → D1 pin
  • LED negative → 220Ω resistor
  • Resistor → GND

ESP8266 Code

#include <ESP8266WiFi.h>const char* ssid = "YOUR_WIFI";
const char* password = "YOUR_PASSWORD";WiFiServer server(80);int ledPin = D1;void setup() {
pinMode(ledPin, OUTPUT);
WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) {
delay(500);
} server.begin();
}void loop() {
WiFiClient client = server.available(); if (client) {
String request = client.readStringUntil('\r'); if (request.indexOf("/LED=ON") != -1)
digitalWrite(ledPin, HIGH); if (request.indexOf("/LED=OFF") != -1)
digitalWrite(ledPin, LOW); client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("");
client.println("<a href=\"/LED=ON\">LED ON</a><br>");
client.println("<a href=\"/LED=OFF\">LED OFF</a>");
}
}

How It Works

  • ESP8266 connects to WiFi
  • A small web server runs on the module
  • When you open the IP address in a browser, buttons appear
  • Clicking buttons sends commands to control the LED

Conclusion

This project introduces IoT basics using ESP8266.

You can extend this project to control:

  • Relays
  • Lights
  • Home appliances

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top