To control (LED) ON/OFF using (WiFi Module EPS 8266)
Code:
#include <ESP8266WiFi.h>
const char* ssid = “اكتب اسم الواي فاي “; // Replace with your WiFi SSID
const char* password = “كلمة السر “; // Replace with your WiFi password
// GPIO pin for the LED
const int ledPin = 2; // GPIO2 on ESP8266 code in>>>>> osief.com
WiFiServer server(80);
// ▄▄▄▄▄▄▄▄▄ ▄▄▄▄▄▄▄▄▄▄▄ ▄▄▄▄▄▄▄▄▄▄▄ ▄▄▄▄▄▄▄▄▄▄▄ ▄▄▄▄▄▄▄▄▄▄▄ ▄▄▄▄▄▄▄▄▄▄▄ ▄▄▄▄▄▄▄▄▄▄▄ ▄▄ ▄▄ // ▐░░░░░░░░░▌ ▐░░░░░░░░░░░▌▐░░░░░░░░░░░▌▐░░░░░░░░░░░▌▐░░░░░░░░░░░▌▐░░░░░░░░░░░▌▐░░░░░░░░░░░▌▐░░▌ ▐░░▌ //▐░█░█▀▀▀▀▀█░▌▐░█▀▀▀▀▀▀▀▀▀ ▀▀▀▀█░█▀▀▀▀ ▐░█▀▀▀▀▀▀▀▀▀ ▐░█▀▀▀▀▀▀▀▀▀ ▐░█▀▀▀▀▀▀▀▀▀ ▐░█▀▀▀▀▀▀▀█░▌▐░▌░▌ ▐░▐░▌ //▐░▌▐░▌ ▐░▌▐░▌ ▐░▌ ▐░▌ ▐░▌ ▐░▌ ▐░▌ ▐░▌▐░▌▐░▌ ▐░▌▐░▌ //▐░▌ ▐░▌ ▐░▌▐░█▄▄▄▄▄▄▄▄▄ ▐░▌ ▐░█▄▄▄▄▄▄▄▄▄ ▐░█▄▄▄▄▄▄▄▄▄ ▐░▌ ▐░▌ ▐░▌▐░▌ ▐░▐░▌ ▐░▌ //▐░▌ ▐░▌ ▐░▌▐░░░░░░░░░░░▌ ▐░▌ ▐░░░░░░░░░░░▌▐░░░░░░░░░░░▌▐░▌ ▐░▌ ▐░▌▐░▌ ▐░▌ ▐░▌ //▐░▌ ▐░▌ ▐░▌ ▀▀▀▀▀▀▀▀▀█░▌ ▐░▌ ▐░█▀▀▀▀▀▀▀▀▀ ▐░█▀▀▀▀▀▀▀▀▀ ▐░▌ ▐░▌ ▐░▌▐░▌ ▀ ▐░▌ //▐░▌ ▐░▌▐░▌ ▐░▌ ▐░▌ ▐░▌ ▐░▌ ▐░▌ ▐░▌ ▐░▌▐░▌ ▐░▌ //▐░█▄▄▄▄▄█░█░▌ ▄▄▄▄▄▄▄▄▄█░▌ ▄▄▄▄█░█▄▄▄▄ ▐░█▄▄▄▄▄▄▄▄▄ ▐░▌ ▄ ▐░█▄▄▄▄▄▄▄▄▄ ▐░█▄▄▄▄▄▄▄█░▌▐░▌ ▐░▌ // ▐░░░░░░░░░▌ ▐░░░░░░░░░░░▌▐░░░░░░░░░░░▌▐░░░░░░░░░░░▌▐░▌▐░▌ ▐░░░░░░░░░░░▌▐░░░░░░░░░░░▌▐░▌ ▐░▌ // ▀▀▀▀▀▀▀▀▀ ▀▀▀▀▀▀▀▀▀▀▀ ▀▀▀▀▀▀▀▀▀▀▀ ▀▀▀▀▀▀▀▀▀▀▀ ▀ ▀ ▀▀▀▀▀▀▀▀▀▀▀ ▀▀▀▀▀▀▀▀▀▀▀ ▀ ▀
void setup() {
Serial.begin(115200);
// Set the LED pin as an output
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW);
// Connect to Wi-Fi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println(“Connecting to WiFi…”);
}
Serial.println(“Connected to WiFi”);
// osief.com
// osief.com
// osief.com
// osief.com
// osief.com
// osief.com
// osief.com
// osief.com
// Print the ESP8266’s IP address
Serial.print(“Local IP address: “);
Serial.println(WiFi.localIP());
// Initialize the server
server.begin();
}
// ██████ ███████ ██ ███████ ███████ ██████ ██████ ███ ███ //██ ████ ██ ██ ██ ██ ██ ██ ██ ████ ████ //██ ██ ██ ███████ ██ █████ █████ ██ ██ ██ ██ ████ ██ //████ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ // ██████ ███████ ██ ███████ ██ ██ ██████ ██████ ██ ██
void loop() {
WiFiClient client = server.available();
if (!client) {
return;
}
// Wait for the client to send a request
while (!client.available()) {
delay(1);
}
// osief.com
// osief.com
// osief.com
// osief.com
// osief.com
// osief.com
// osief.com
// osief.com
// Read the first line of the request
String request = client.readStringUntil(‘\r’);
client.flush();
// Check for “GET /LED=ON” in the request
if (request.indexOf(“GET /LED=ON”) != -1) {
digitalWrite(ledPin, HIGH); // Turn on the LED
client.println(“HTTP/1.1 200 OK”);
client.println(“Content-Type: text/html”);
client.println();
client.println(“<html><body>”);
client.println(“<h1>LED is ON</h1>”);
client.println(“</body></html>”);
}
// osief.com
// osief.com
// osief.com
// osief.com
// osief.com
// osief.com
// osief.com
// osief.com
// Check for “GET /LED=OFF” in the request
else if (request.indexOf(“GET /LED=OFF”) != -1) {
digitalWrite(ledPin, LOW); // Turn off the LED
client.println(“HTTP/1.1 200 OK”);
client.println(“Content-Type: text/html”);
client.println();
client.println(“<html><body>”);
client.println(“<h1>LED is OFF</h1>”);
client.println(“</body></html>”);
}
// Invalid URL
else {
client.println(“HTTP/1.1 404 Not Found”);
client.println(“Content-Type: text/html”);
client.println();
client.println(“<html><body>”);
client.println(“<h1>Page not found</h1>”);
client.println(“</body></html>”);
}
delay(1);
client.stop();
}
// ██████╗ ███████╗██╗███████╗███████╗ ██████╗ ██████╗ ███╗ ███╗ //██╔═████╗██╔════╝██║██╔════╝██╔════╝ ██╔════╝██╔═══██╗████╗ ████║ //██║██╔██║███████╗██║█████╗ █████╗ ██║ ██║ ██║██╔████╔██║ //████╔╝██║╚════██║██║██╔══╝ ██╔══╝ ██║ ██║ ██║██║╚██╔╝██║ //╚██████╔╝███████║██║███████╗██║ ██╗ ╚██████╗╚██████╔╝██║ ╚═╝ ██║ // ╚═════╝ ╚══════╝╚═╝╚══════╝╚═╝ ╚═╝ ╚═════╝ ╚═════╝ ╚═╝ ╚═╝
The pinout of a common ESP8266 WiFi module, such as the ESP-01, can vary slightly between different versions and manufacturers. Below is a typical pinout for the ESP-01 module:
- VCC (3.3V): This is the power supply pin for the ESP8266 module. It requires a stable 3.3V power source. Do not connect it to 5V as it can damage the module.
- GND (Ground): This is the ground or common reference voltage.
- TX (Transmit): This pin is used for serial data transmission from the ESP8266 to an external device like an Arduino. It sends data from the module to another device.
- RX (Receive): This pin is used for serial data reception by the ESP8266 from an external device. It receives data from another device.
- GPIO0 (General-Purpose Input/Output 0): This pin can be used for general-purpose digital input/output operations. It is often used during the flashing of firmware, so its state during boot-up can affect the module’s behavior.
- GPIO2 (General-Purpose Input/Output 2): Similar to GPIO0, this pin can also be used for general-purpose digital input/output operations. It’s commonly used for connecting external components like LEDs or sensors.
- CH_PD (Chip Enable): This is the chip enable pin, which is used to enable the ESP8266 module.
- RST (Reset): This is the reset pin used to reset the module.
0sief