In this tutorial we are going to control "Servo Motor using Wifi microcontroller ESP01"
Controlling servo motors with different controllers and circuits is quite an easier thing which we have discussed in our various articles. However, controlling the same Servos using a web server might be a new thing for you. Thus, trying new things is always fun. So, here we are with the project ESP01 Servo Motor Control using a Web Server. Hence for making this project we are using the esp01 wifi module.
PCBWay commits to meeting the needs of its customers from different industries in terms of quality, delivery, cost-effectiveness, and any other demanding requests. As one of the most experienced PCB manufacturers in China. They pride themselves to be your best business partners as well as good friends in every aspect of your PCB needs.
#include
#include
#include
#include "index.h";
#define LED 2
#define ServoPin 1
//WiFi Connection configuration
const char *ssid = "Circuits DIY";
const char *password = "03433212601";
Servo myservo; // create servo object to control a servo
// twelve servo objects can be created on most boards
ESP8266WebServer server(80);
void handleServo(){
String POS = server.arg("servoPOS");
int pos = POS.toInt();
myservo.write(pos); // tell servo to go to position
delay(15);
Serial.print("Servo Angle:");
Serial.println(pos);
digitalWrite(LED,!(digitalRead(LED))); //Toggle LED
server.send(200, "text/plane","");
}
void handleRoot() {
String s = MAIN_page; //Read HTML contents
server.send(200, "text/html", s); //Send web page
}
void setup() {
delay(1000);
Serial.begin(115200);
Serial.println();
pinMode(LED,OUTPUT);
myservo.attach(ServoPin); // attaches the servo on GIO2 to the servo object
//Connect to wifi Network
WiFi.begin(ssid, password); //Connect to your WiFi router
Serial.println("");
// Wait for connection
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
//If connection successful show IP address in serial monitor
Serial.println("");
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP()); //IP address assigned to your ESP
//Initialize Webserver
server.on("/",handleRoot);
server.on("/setPOS",handleServo); //Sets servo position from Web request
server.begin();
}
void loop() {
server.handleClient();
}