- LED Blink with Button
- Motion Detection Alarm System
- Simple RGB LED Controller
- Blinking LED with WebSocket Control
- Control LED Brightness using PWM
- Web Page with HTML and CSS on ESP32
- Control Multiple LEDs
- ESP32 with Bluetooth Serial Communication
- EEPROM with ESP32
- ESP32 Push Button Input: Reading Digital States
- Interfacing DHT11 Sensor
- Interfacing Ultrasonic Sensor
- Interfacing Flame Sensor
- Interfacing Sound Sensor
- Interfacing Potentiometer
- Interfacing IR Sensor
- Interfacing Servo Motor
- Interfacing Cam Wireless
- Interfacing DC Motor
- Interfacing Shock Sensor
- Interfacing Color Recognition Sensor
- Interfacing RFID Module
- TTGO LoRa32 SX1276 OLED
- Interfacing Keypad
- Interfacing Solenoid Lock
- Interfacing 16x2 LCD
- Interfacing Soil Moisture
- Interfacing MQ-7 Gas Sensor
- Interfacing Light Sleep Mode
- Interfacing Smart Light Control
- Interfacing (IoT) Weather Station
- Interfacing Web Server for Temperature Data Display
- Interfacing Home Automation System with Relay Control
- Interfacing IoT Smart Garden
- Face Recognition-Based Door Unlock System
- Interfacing Wi-Fi Jammer Detector
- Interfacing Health Band with Pulse
- Interfacing Sound Level Logger for Classrooms
- Night Vision Surveillance Camera
- Solar Panel Monitoring System
- Smart Farming Robot for Crop Surveillance
- Smart Water Quality Monitoring System
- Industrial IoT Gateway for Real-Time Monitoring
- Agriculture System with Automated Drone Control
ESP32 Blinking LED with WebSocket Control
Introduction to ESP32 Blinking LED with WebSocket Control
This project demonstrates how to control an LED connected to an ESP32 using WebSocket communication. WebSocket provides full-duplex communication channels over a single TCP connection, making it ideal for real-time applications like controlling hardware devices remotely. In this case, the ESP32 will receive commands from a WebSocket client to toggle the LED's state.
Components Required
To build this project, you will need the following components: ESP32 development board, an LED, a 220Ω resistor, jumper wires, and a breadboard.
- Set up a WebSocket server on the ESP32 to listen for incoming connections from a WebSocket client.
- Connect the LED to a GPIO pin on the ESP32 through a 220Ω resistor.
- Write a simple WebSocket client (in HTML/JavaScript) to send messages (ON/OFF) to the ESP32.
- Control the LED's state on the ESP32 by toggling it based on the received WebSocket messages.
- Test the system by toggling the LED state using the WebSocket client.
Formula: LED State = (WebSocket Command) → ON/OFF
Components List
- 1 × ESP32 Development Board
- 1 × LED
- 1 × 220Ω Resistor
- Jumper wires
- Breadboard
Pin Configuration
- GPIO 5: The LED is connected to GPIO 5 of the ESP32 through a 220Ω resistor.
Ensure that the LED is connected to the correct GPIO pin and that the resistor is used to prevent overloading the ESP32 pin.
Wiring and Connections
- -> GPIO 5
Code for ESP32 Blinking LED with WebSocket Control
1#include <WiFi.h>
2#include <WebSocketsServer.h>
3
4const char* ssid = "Your_SSID";
5const char* password = "Your_PASSWORD";
6
7WebSocketsServer webSocket = WebSocketsServer(81);
8
9#define LED_PIN 5
10
11void setup() {
12 Serial.begin(115200);
13 pinMode(LED_PIN, OUTPUT);
14 WiFi.begin(ssid, password);
15 while (WiFi.status() != WL_CONNECTED) {
16 delay(1000);
17 Serial.println("Connecting to WiFi...");
18 }
19 Serial.println("Connected to WiFi");
20 webSocket.begin();
21 webSocket.onEvent(webSocketEvent);
22}
23
24void loop() {
25 webSocket.loop();
26}
27
28void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t length) {
29 if (type == WStype_TEXT) {
30 String message = (char*) payload;
31 if (message == "ON") {
32 digitalWrite(LED_PIN, HIGH);
33 } else if (message == "OFF") {
34 digitalWrite(LED_PIN, LOW);
35 }
36 }
37}
Code Explanation
- #include <WiFi.h>: This includes the necessary WiFi library to connect the ESP32 to a Wi-Fi network.
- #include <WebSocketsServer.h>: This includes the WebSockets library, which allows the ESP32 to act as a WebSocket server.
- const char* ssid = "Your_SSID";: This defines the Wi-Fi SSID for the ESP32 to connect to.
- const char* password = "Your_PASSWORD";: This defines the Wi-Fi password for the ESP32 to connect to.
- WebSocketsServer webSocket = WebSocketsServer(81);: This initializes the WebSocket server on port 81.
- pinMode(LED_PIN, OUTPUT);: This sets GPIO 5 (connected to the LED) as an output.
- WiFi.begin(ssid, password);: This starts the Wi-Fi connection process on the ESP32.
- webSocket.begin();: This begins the WebSocket server, allowing it to accept incoming WebSocket connections.
- webSocket.onEvent(webSocketEvent);: This registers the WebSocket event handler function to manage incoming messages.
- webSocket.loop();: This keeps the WebSocket server active, processing messages from clients.
- String message = (char*) payload;: This converts the incoming WebSocket payload into a string.
- if (message == "ON") { digitalWrite(LED_PIN, HIGH); }: If the WebSocket message is 'ON', the LED is turned on (HIGH).
- else if (message == "OFF") { digitalWrite(LED_PIN, LOW); }: If the WebSocket message is 'OFF', the LED is turned off (LOW).
Applications
- Real-time control systems.
- Home automation and IoT devices.
- Remote monitoring and control of physical devices.
Conclusion
The ESP32 Blinking LED with WebSocket Control project demonstrates how to use WebSocket communication to control an LED in real-time. This is a foundational project that can be extended to control various devices, sensors, and actuators remotely through WebSocket.