#include <WiFi.h>
#include <ESPAsyncWebServer.h>
#include <Servo.h>
const char* ssid = "YOUR_SSID";
const char* password = "YOUR_PASSWORD";
AsyncWebServer server(80);
Servo myServo;
const int servoPin = 15;
void setup() {
Serial.begin(115200);
// 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");
Serial.println(WiFi.localIP());
// Attach the servo to the specified pin
myServo.attach(servoPin);
// Define the endpoint and handle requests
server.on("/rotationD3", HTTP_GET, [](AsyncWebServerRequest *request){
if (request->hasParam("percent")) {
String percentParam = request->getParam("percent")->value();
int percent = percentParam.toInt();
if (percent >= 0 && percent <= 100) {
int angle = map(percent, 0, 100, 0, 180); // Map percent to servo angle
myServo.write(angle);
request->send(200, "text/plain", "Servo rotated to " + String(angle) + " degrees");
} else {
request->send(400, "text/plain", "Invalid percentage");
}
} else {
request->send(400, "text/plain", "Parameter 'percent' not found");
}
});
// Start the server
server.begin();
}
void loop() {
// Nothing to do here
}