#include <WiFi.h>
#include <WebServer.h>
#include <ESP32Servo.h>
// === Configuration Access Point ===
const char* ssid = "ESP32-Servo";
const char* password = "12345678";
// === Servo (PWM natif) ===
const int servoPin = 13; // pin PWM
int angle = 90;
int newAngle = 90;
Servo myservo;
// === Serveur web classique ===
WebServer server(80);
// === Convertir angle (0-180) vers signal PWM ===
// === Page HTML ===
String getHTML() {
String html = "<!DOCTYPE html><html><head><meta name='viewport' content='width=device-width, initial-scale=1'>";
html += "<title>ESP32 Servo</title><style>body{text-align:center;font-family:Arial;}input{padding:10px;font-size:18px;}</style></head><body>";
html += "<h2>Controle Servo - ESP32</h2>";
html += "<p>Angle actuel : " + String(angle) + "°</p>";
html += "<form action='/set' method='GET'>";
html += "<input type='number' name='value' min='0' max='180'>";
html += "<input type='submit' value='Envoyer'>";
html += "</form></body></html>";
return html;
}
void handleRoot() {
server.send(200, "text/html", getHTML());
}
void handleSet() {
if (server.hasArg("value")) {
int newAngle = server.arg("value").toInt();
myservo.write(angle);
Serial.println("Angle reçu : " + String(angle));
}
server.send(200, "text/html", getHTML());
}
void setup() {
Serial.begin(115200);
delay(10000);
// Allow allocation of all timers
ESP32PWM::allocateTimer(0);
ESP32PWM::allocateTimer(1);
ESP32PWM::allocateTimer(2);
ESP32PWM::allocateTimer(3);
myservo.setPeriodHertz(50); // standard 50 hz servo
myservo.attach(servoPin, 1000, 2000); // attaches the servo on pin 18 to the servo object
// using default min/max of 1000us and 2000us
// different servos may require different min/max settings
// for an accurate 0 to 180 sweep
Serial.println("Servo config done");
// Démarrage WiFi AP
WiFi.softAP(ssid, password);
Serial.print("IP : ");
Serial.println(WiFi.softAPIP());
// Routes
server.on("/", handleRoot);
server.on("/set", handleSet);
server.begin();
}
void loop() {
server.handleClient(); // gestion des requĂȘtes*
myservo.write(angle);
if (newAngle != angle) {
Serial.println("angle appliqué" + newAngle);
newAngle = angle;
}
delay(50);
}
hi,
this code is to control a servo motor on a web server using an ESP32 the "void loop" does not run and i think its because of the "server.handleClient(); " line but im not sure
Welcome to the forum
Thank you for using code tags in your first post
As your topic does not relate directly to the installation or operation of the IDE it has been moved to the Programming category of the forum
You create a temporary new variable here, assign the value to it. It gets destroyed when the function ends. Your global newAngle never gets the value.
ok thank you ill try and tell you
#include <WiFi.h>
#include <WebServer.h>
#include <ESP32Servo.h>
// === Configuration Access Point ===
const char* ssid = "ESP32-Servo";
const char* password = "12345678";
// === Servo (PWM natif) ===
const int servoPin = 13; // pin PWM
int angle = 90;
int newAngle = 90;
Servo myservo;
// === Serveur web classique ===
WebServer server(80);
// === Convertir angle (0-180) vers signal PWM ===
// === Page HTML ===
String getHTML() {
String html = "<!DOCTYPE html><html><head><meta name='viewport' content='width=device-width, initial-scale=1'>";
html += "<title>ESP32 Servo</title><style>body{text-align:center;font-family:Arial;}input{padding:10px;font-size:18px;}</style></head><body>";
html += "<h2>Controle Servo - ESP32</h2>";
html += "<p>Angle actuel : " + String(angle) + "°</p>";
html += "<form action='/set' method='GET'>";
html += "<input type='number' name='value' min='0' max='180'>";
html += "<input type='submit' value='Envoyer'>";
html += "</form></body></html>";
return html;
}
void handleRoot() {
server.send(200, "text/html", getHTML());
}
void handleSet() {
if (server.hasArg("value")) {
int temp = server.arg("value").toInt();
newAngle = temp;
myservo.write(newAngle);
Serial.print("Angle reçu : ");
Serial.println(angle);
}
server.send(200, "text/html", getHTML());
}
void setup() {
Serial.begin(115200);
delay(10000);
// Allow allocation of all timers
ESP32PWM::allocateTimer(0);
ESP32PWM::allocateTimer(1);
ESP32PWM::allocateTimer(2);
ESP32PWM::allocateTimer(3);
myservo.setPeriodHertz(50); // standard 50 hz servo
myservo.attach(servoPin, 1000, 2000); // attaches the servo on pin 18 to the servo object
// using default min/max of 1000us and 2000us
// different servos may require different min/max settings
// for an accurate 0 to 180 sweep
Serial.println("Servo config done");
// Démarrage WiFi AP
WiFi.softAP(ssid, password);
Serial.print("IP : ");
Serial.println(WiFi.softAPIP());
// Routes
server.on("/", handleRoot);
server.on("/set", handleSet);
server.begin();
}
void loop() {
server.handleClient(); // gestion des requĂȘtes*
myservo.write(angle);
if (newAngle != angle) {
Serial.print("angle appliqué : ");
Serial.println(newAngle);
newAngle = angle;
}
delay(50);
}
alright so now the code doesn't actualize the servo
Why did you add the temp var?
Not all code is posted, and several more errors can be seen.
I added the temp because Camsysca told me the int temp = server.arg("value").toInt(); line was temporary so i added the newAngle = temp; line to reuse the var later
im pretty new in the arduino universe so i grab some part of code i found on some forum and project
For that part, all you needed to do was remove the "int", because you want your existing variable to receive the value:
newAngle = server.arg("value").toInt();
However, I don't have time to delve into the rest of your code, and changes. Must run!
xfpd
April 21, 2025, 5:39pm
12
Can another "ESP servo" pre-written example help you?
Run IoT and embedded projects in your browser: ESP32, STM32, Arduino, Pi Pico, and more. No installation required!
maybe i will try it tomorrow have a nice day yall