#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <Servo.h>
const char* ssid = "Som";
const char* password = "1234567890";
#define LED D8 // LED at GPIO4 D2
#define SERVO_PIN D2
#define RTS_PIN D0 // RTS pin connected to GPIO0
Servo myservo;
WiFiServer server(80);
void setup() {
pinMode(RTS_PIN, OUTPUT);
// Trigger reset by setting RTS pin low
digitalWrite(RTS_PIN, LOW);
delay(100); // Hold low for a short duration
digitalWrite(RTS_PIN, HIGH);
Serial.begin(115200);
Serial.println("Reset triggered via RTS pin");
pinMode(LED, OUTPUT);
myservo.attach(SERVO_PIN);
myservo.write(45); // Initial position of servo
digitalWrite(LED, LOW); // Initial state of LED
Serial.println();
Serial.println();
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("........");
server.begin();
Serial.println("Server started at...");
Serial.println(WiFi.localIP());
}
void loop() {
// Check if a client has connected
WiFiClient client = server.available();
if (!client) {
return;
}
// Wait until the client sends some data
Serial.println("New client");
while (!client.available()) {
delay(1);
}
// Read the first line of the request
String req = client.readStringUntil('\r');
Serial.println(req);
client.flush();
// Match the request
if (req.indexOf("/F") != -1) {
digitalWrite(LED, LOW); // Turn off LED
myservo.write(45); // Move servo to locked position
} else if (req.indexOf("/B") != -1) {
digitalWrite(LED, HIGH); // Turn on LED
myservo.write(180); // Move servo to unlocked position
}
// Return the response
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Connection: close");
client.println();
client.println("<!DOCTYPE HTML>");
client.println("<HTML>");
client.println("<H1>Automatic Door Lock</H1>");
client.println("<br />");
client.println("<a href=\"/F\"><button style='font-size: 50px; height: 200px; width: 300px;'>LOCK</button></a>");
client.println("<a href=\"/B\"><button style='font-size: 50px; height: 200px; width: 300px;'>UNLOCK</button></a>");
client.println("</html>");
delay(1);
Serial.println("Client disconnected");
Serial.println("");
}