Introduction :-
ESP32 ko ab aap wifi se bhi connect kar sakte hai. Bas aapko simple sa code ko apne ARDUINO IDE me likhe uske baad aap apne ESP32 ko connect karke control kar sakte hai. Iske pahle maine Bluetooth se control karna bataya tha waise hi isko bhi control kar sakte hai.
How Does It Work :-
Sabse pahle aap apne ESP32 ko apne laptop se connect kare. Connect hone ke baad aap aap ARDUINO IDE ko open karenge uske baad aap naya sketch banayenge uske baad ish code ko aap waha pe paste karenge……
include const char* ssid = “HUAWEI”; const char* password = “12345678”; WiFiServer server(80); void setup() { Serial.begin(115200); delay(1000); pinMode(2, OUTPUT); digitalWrite(2, LOW); Serial.print(“Connecting to “); Serial.println(ssid); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print(“.”); } Serial.println(“”); Serial.println(“WiFi connected.”); Serial.println(“IP address: “); Serial.println(WiFi.localIP()); server.begin(); } void loop() { WiFiClient client = server.available(); if (client) { String currentLine = “”; while (client.connected()) { if (client.available()) { char c = client.read(); Serial.write(c); if (c == '\n') { if (currentLine.length() == 0) { client.println("HTTP/1.1 200 OK"); client.println("Content-type:text/html"); client.println("Connection: close"); client.println(); // Web Page Content client.println("<!DOCTYPE html><html>"); client.println("<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">"); client.println("<link rel=\"icon\" href=\"data:,\">"); client.println("<style>body { font-family: Arial, Helvetica, sans-serif; text-align: center; margin: 0;}"); client.println(".container { display: flex; flex-direction: column; justify-content: center; align-items: center; height: 100vh;}"); client.println(".button { background-color: #FF0000; border: none; color: white; padding: 16px 40px;"); client.println("text-decoration: none; font-size: 30px; margin: 2px; cursor: pointer; width: 120px;}"); client.println(".button2 { background-color: #00FF00;}"); client.println(".heading { font-size: 24px; margin-bottom: 20px; }"); client.println(".subscribe-btn { background-color: #FF0000; border: none; color: white; padding: 12px 24px;"); client.println("text-decoration: none; font-size: 18px; margin-top: 20px; } </style></head>"); client.println("<body><div class=\"container\">"); client.println("<h1 class=\"heading\">Created by Yarana IoT Guru</h1>"); client.println("<p>GPIO 2 - State " + String((digitalRead(2) ? "ON" : "OFF")) + "</p>"); client.println("<a href=\"/on\"><button class=\"button\">ON</button></a>"); client.println("<a href=\"/off\"><button class=\"button button2\">OFF</button></a>"); client.println("<a href=\"https://www.youtube.com/@YaranaIotGuru\"><button class=\"subscribe-btn\">Subscribe</button></a>"); client.println("</div></body></html>"); client.println(); break; } else { currentLine = ""; } } else if (c != '\r') { currentLine += c; } if (currentLine.endsWith("GET /on")) { digitalWrite(2, HIGH); } if (currentLine.endsWith("GET /off")) { digitalWrite(2, LOW); } } } delay(10); client.stop(); Serial.println("Client disconnected."); Serial.println(""); } } |
Code ko paste karne ke baad isko upload kar do upload ho jane ke baad aapko “serial monitor” me ek IP Address show ho raha hoga us IP Address ko aap copy kar le uske baad chrome kp open kar lo open karne ke baad us IP Address ko waha pe paste kar do uske baad ENTER karne ke baad aapko ek screen dikh jayegi …….
Uske baad aap ON pe click karenge to LED ON ho jayegi aur OFF pe click karenge to LED OFF ho jayegi.
Bluetooth Code :-
Agar aap bluetooth se bhi connect karke control karna chahte hai to aap iss code ka use karke apne ESP32 ko control kar skte hai.
#include <BluetoothSerial.h> BluetoothSerial SerialBT; const int ledPin = 2; void setup() { pinMode(ledPin, OUTPUT); digitalWrite(ledPin, LOW); SerialBT.begin(“ESP32_LED_Control”); // Bluetooth device name Serial.println(“Bluetooth Device is ready to pair.”); } void loop() { if (SerialBT.available()) { char receivedChar = SerialBT.read(); if (receivedChar == ‘0’) { digitalWrite(ledPin, LOW); SerialBT.println(“LED OFF”); } else if (receivedChar == ‘1’) { digitalWrite(ledPin, HIGH); SerialBT.println(“LED ON”); } } } |
Ye code ka aap use kar sakte hai