#include <WiFi.h>
#include <ESP32Servo.h>

Servo myservo; 

int pos = 0; //variable to store servo position
static const int servoPin = 18;

const int LED = 12;
String command;

void setup(){  Serial.begin(115200);
  WiFi.mode(WIFI_STA);
        //allow allocation of all timers
  ESP32PWM::allocateTimer(0);       	ESP32PWM::allocateTimer(1);
	ESP32PWM::allocateTimer(2);       	ESP32PWM::allocateTimer(3);
	myservo.setPeriodHertz(50);         //standard 50hz servo
  myservo.attach(servoPin, 10, 2800); // attaches the servo on pin 18 to the servo object
  pinMode(LED, OUTPUT);
  Serial.println("Door is ");
}

void loop(){
while (Serial.available() >0 ){
String command = Serial.readString();

 if (command == "OPEN"){
   Serial.println(command);
    for (pos = 0; pos<=180; pos+=1){
      myservo.write(pos);
      digitalWrite(LED,HIGH);
      delay(15);
    }
  }

  if (command == "CLOSED"){
    Serial.println(command);
    for (pos = 180; pos>=0; pos-=1){
      myservo.write(pos);
      digitalWrite(LED,LOW);
      delay(15);
    }
  }
}
}
//Serial.print(pos);