the Arduino framework for ESP32 development.
Please note that this is a simplified example, and you might need to adapt it based on your specific requirements.
Hardware Setup:
Connect the servo motor to the appropriate pin on the ESP32. For example, connect it to GPIO 4.
Make sure your ESP32 is connected to your Wi-Fi network.
#include <WiFi.h>
#include <ESPAsyncWebServer.h>
#include <Servo.h>
const char *ssid = "your-ssid";
const char *password = "your-password";
Servo lockServo; // Create a servo object
const int servoPin = 4; // Pin to which the servo is connected
bool isLocked = false;
AsyncWebServer server(80);
void setup() {
Serial.begin(115200);
// Attach the servo to the pin
lockServo.attach(servoPin);
// 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");
// Define web server routes
server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){
request->send(SPIFFS, "/index.html", String(), false);
});
server.on("/lock", HTTP_GET, [](AsyncWebServerRequest *request){
isLocked = !isLocked;
controlLock();
request->send(200, "text/plain", isLocked ? "Locked" : "Unlocked");
});
// Serve static files from SPIFFS
server.serveStatic("/", SPIFFS, "/");
server.begin();
}
void loop() {
// Do nothing here for now
}
void controlLock() {
if (isLocked) {
lockServo.write(0); // Rotate servo to the locked position
} else {
lockServo.write(90); // Rotate servo to the unlocked position
}
delay(1000); // Allow time for the servo to move
}
Comments
Post a Comment