Servo Control

Hello, i'm working on a wifi garage door opener, that I would like to use servo, and mount a magnetic sensor on it to demonstrate a garage door as it open and closes. I want the servo to move 60 degrees when I hit the "open door" and move back when I hit "close Door" Can you please help me with the code ?

#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>

const char* ssid = "Your SSID";
const char* password = " Your password ";

ESP8266WebServer server(80);

int switchPin = 0;
int switchStateCur;
int relayPin = 2;

int WiFiCon() {
// Check if we have a WiFi connection, if we don't, connect.
int xCnt = 0;

if (WiFi.status() != WL_CONNECTED){

Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);

WiFi.mode(WIFI_STA);

WiFi.begin(ssid, password);

while (WiFi.status() != WL_CONNECTED && xCnt < 50) {
delay(500);
Serial.print(".");
xCnt ++;
}

if (WiFi.status() != WL_CONNECTED){
Serial.println("WiFiCon=0");
return 0; //never connected
} else {
Serial.println("WiFiCon=1");
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
return 1; //1 is initial connection
}

} else {
Serial.println("WiFiCon=2");
return 2; //2 is already connected

}
}

String htmlServe(int doorAction) {
String htmStr;

if (switchStateCur==1 && doorAction ==1){
digitalWrite(relayPin, 0);
delay(400);
digitalWrite(relayPin, 1);
} else if (switchStateCur==0 && doorAction ==0){
digitalWrite(relayPin, 0);
delay(400);
digitalWrite(relayPin, 1);
}

htmStr += "\n<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0,">\n";
htmStr += "<meta http-equiv="refresh" content="5; URL=/sdoor"> \n";
htmStr += "\n\n

\n";

if (switchStateCur==1){
htmStr += "Your Door is Open";
} else {
htmStr += "The Door is Closed";
}

htmStr +="

\n
<a href="/";

if (switchStateCur==0){
htmStr += "odoor";
} else {
htmStr += "cdoor";
}

htmStr += "">\n\n";

if (switchStateCur==0){
htmStr += "Open Door";
} else {
htmStr += "Close Door";
}
htmStr += "\n";

htmStr += "

<a href="/sdoor">Recheck";

htmStr += "\n\n\n";

return htmStr;

}

void setup(){

pinMode(switchPin, INPUT);
pinMode(relayPin, OUTPUT);
digitalWrite(relayPin, HIGH);
Serial.begin(115200);

WiFiCon();

server.on("/odoor", {
server.send(200, "text/html", htmlServe(0));
});

server.on("/cdoor", {
server.send(200, "text/html", htmlServe(1));
});

server.on("/sdoor", {
server.send(200, "text/html", htmlServe(2));
});

server.begin();

}

void loop(){

switchStateCur = digitalRead(switchPin);

server.handleClient();

delay(1000);
}

firascomp:
Hello, i'm working on a wifi garage door opener, that I would like to use servo, and mount a magnetic sensor on it to demonstrate a garage door as it open and closes. I want the servo to move 60 degrees when I hit the "open door" and move back when I hit "close Door" Can you please help me with the code ?

You have a goal and you have a sketch. Is there anything specific about the sketch that is not working as you want?

Hello,

@firascomp please explain further what your difficulty is.

I'm using Magnetic Reed Switch (common door sensor) that I want use the servo to move it, so I can demonstrate a garage door moving.i have relay connected and when I click open or close door it does work The code I posted is working fine, but I need to add the code to use the servo.

Thank you

but I need to add the code to use the servo.

"to use the servo" is NOT a requirement. What, EXACTLY, should the servo do? When, EXACTLY, should it do that? Under what circumstances, EXACTLY? Those are requirements.

You MUST have requirements, not hand-waving.

You would be better off just using a geared motor with limit switches at each end of its travel to stop at open or closed.
A door load is considerable , and a “servo” to do this is hard to image.

To move a servo slowly, look at File->Examples->Servo->Sweep.

To simulate a garage door:
When the door is to open, sweep the servo up.
When the door is to close, sweep the servo down.