Skip to main content

ESP32 Web Server LED Control - ESP32 IOT Projects



 

Components Required

  • ESP32 Development Board
  • 2 × LEDs (any color)
  • 2 × 220Ω resistors
  • Breadboard & jumper wires
  • USB cable for programming ESP32

Circuit Connection :


Connect LED1 anode (long leg) → 220ohm resistor → GPIO16.

Connect LED2 anode (long leg) → 220ohm resistor → GPIO17.

Both LED cathodes (short leg) → GND.



Step 1: Install Arduino IDE & ESP32 Board


Download and install the Arduino IDE from Arduino.cc
.

Add ESP32 board support:

Go to File > Preferences.

In Additional Board Manager URLs, add:

https://dl.espressif.com/dl/package_esp32_index.json

Go to Tools > Board > Board Manager, search ESP32, and install.


Step 2: Connect ESP32 and Select Board


Plug ESP32 into your computer with USB cable.

In Arduino IDE:

Go to Tools > Board → Select ESP32 Dev Module.

Select the correct Port.


Step 3: Full Arduino Code

#include <WiFi.h>

// Replace with your network credentials
const char* ssid = "YOUR_WIFI_SSID";
const char* password = "YOUR_WIFI_PASSWORD";

// GPIO pins
const int led1 = 16;
const int led2 = 17;

// Web server runs on port 80
WiFiServer server(80);

// Store HTTP request
String header;

// Current state of LEDs
String led1State = "off";
String led2State = "off";

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

  // Initialize the output variables as outputs
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);

  // Set both LEDs to off
  digitalWrite(led1, LOW);
  digitalWrite(led2, LOW);

  // Connect to Wi-Fi
  Serial.print("Connecting to ");
  Serial.println(ssid);
  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected.");
  Serial.print("IP Address: ");
  Serial.println(WiFi.localIP());

  // Start the server
  server.begin();
}

void loop() {
  WiFiClient client = server.available();   // Listen for incoming clients

  if (client) {
    Serial.println("New Client Connected.");
    String currentLine = "";
    header = "";

    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        header += c;

        if (c == '\n') {
          if (currentLine.length() == 0) {
            // Send HTTP response
            client.println("HTTP/1.1 200 OK");
            client.println("Content-type:text/html");
            client.println("Connection: close");
            client.println();

            // LED control
            if (header.indexOf("GET /led1/on") >= 0) {
              digitalWrite(led1, HIGH);
              led1State = "on";
            } else if (header.indexOf("GET /led1/off") >= 0) {
              digitalWrite(led1, LOW);
              led1State = "off";
            } else if (header.indexOf("GET /led2/on") >= 0) {
              digitalWrite(led2, HIGH);
              led2State = "on";
            } else if (header.indexOf("GET /led2/off") >= 0) {
              digitalWrite(led2, LOW);
              led2State = "off";
            }

            // HTML Page
            client.println("<!DOCTYPE html><html>");
            client.println("<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">");
            client.println("<style>");
            client.println("body { font-family: Arial; text-align: center; }");
            client.println(".button { padding: 15px 40px; font-size: 20px; margin: 10px; border: none; border-radius: 8px; }");
            client.println(".on { background-color: green; color: white; }");
            client.println(".off { background-color: red; color: white; }");
            client.println("</style></head>");
            client.println("<body><h2>ESP32 Web Server - LED Control</h2>");

            // LED1 Buttons
            client.println("<p>LED1 - State: " + led1State + "</p>");
            if (led1State == "off") {
              client.println("<a href=\"/led1/on\"><button class=\"button on\">ON</button></a>");
            } else {
              client.println("<a href=\"/led1/off\"><button class=\"button off\">OFF</button></a>");
            }

            // LED2 Buttons
            client.println("<p>LED2 - State: " + led2State + "</p>");
            if (led2State == "off") {
              client.println("<a href=\"/led2/on\"><button class=\"button on\">ON</button></a>");
            } else {
              client.println("<a href=\"/led2/off\"><button class=\"button off\">OFF</button></a>");
            }

            client.println("</body></html>");
            client.println();
            break;
          } else {
            currentLine = "";
          }
        } else if (c != '\r') {
          currentLine += c;
        }
      }
    }
    // Close connection
    client.stop();
    Serial.println("Client disconnected.");
    Serial.println("");
  }
}



Step 4: Upload the Code


Replace YOUR_WIFI_SSID and YOUR_WIFI_PASSWORD with your Wi-Fi details.

Click Upload in Arduino IDE.

Open Serial Monitor at 115200 baud.

Copy the ESP32 IP Address displayed.



Step 5: Control LEDs from Browser


Open a browser on your phone/PC.

Enter the IP address of ESP32 (e.g., http://192.168.1.45).

You’ll see a simple web page with ON/OFF buttons for LED1 and LED2.

Tap the buttons → LEDs will toggle ON/OFF instantly.



Comments

Popular posts from this blog

Clap On Off Switch Circuit Using Arduino

  Arduino Clap LED program #define MicAO 8 int ledPin = 13; int clap = 0; long detection_range_start = 0; long detection_range = 0; boolean status_lights = false; void setup() {   pinMode(MicAO, INPUT);   pinMode(ledPin,OUTPUT);   } void loop() { int status_MicAO = digitalRead(MicAO); if (status_MicAO == 0) { if (clap == 0) { detection_range_start = detection_range = millis(); clap++; } else if (clap > 0 && millis()-detection_range >= 50) { detection_range = millis(); clap++; } } if (millis()-detection_range_start >= 400) { if (clap == 2) { if (!status_lights) { status_lights = true; digitalWrite(ledPin, HIGH); } else if (status_lights) { status_lights = false; digitalWrite(ledPin, LOW); } } clap = 0; } }

How To Make Radar With Arduino UNO Using Ultrasonic Sensor

  Creating a radar system with an Arduino UNO and an ultrasonic sensor involves a few straight forward steps. First, you'll need to connect the ultrasonic sensor's trigger pin to one of the Arduino’s digital output pins and the echo pin to a digital input pin. Then, write a simple Arduino sketch to send a pulse from the trigger pin and measure the duration of the pulse received on the echo pin. This duration, proportional to the distance of an object from the sensor, can be calculated using the speed of sound. By continuously taking readings and mapping them to distances, you can plot these measurements to visualize objects in your environment, effectively creating a basic radar system. To display the results, you could use a serial monitor or even integrate an LCD display for real-time feedback. Here is the code below ,- //radar.ino #include <Servo.h>  const int trigPin = 9; const int echoPin = 8; long duration; int distance; Servo myServo;  void setup() {   p...

Obstacle Avoiding Robot Car Using Arduino - How To Make a Smart Car

How To Make Smart Robot Car With Arduino Obstacle avoiding robot car using Arduino, how to make a smart car with ultrasonic sensor, servo motor, and L298N motor driver module. Arduino UNO :- https://amzn.to/3MF9jky SG90 Servo Motor And HC-SR04 Ultrasonic Sensor :- https://amzn.to/3EX03pu LM298N motor driver module :- https://amzn.to/3y2tx3Y BO Motor with Wheel Pair :- https://amzn.to/38zFMu6 arduino smart car kit :- https://amzn.to/3kmtZlk Project Code :-  #include <Servo.h>          //Servo motor library. This is standard library #include <NewPing.h>        //Ultrasonic sensor function library. You must install this library //our L298N control pins const int LeftMotorForward = 7; const int LeftMotorBackward = 6; const int RightMotorForward = 4; const int RightMotorBackward = 5; //sensor pins #define trig_pin A1 //analog input 1 #define echo_pin A2 //analog input 2 #define maximum_distance 200 boolean goesForward = ...