#include <ESP32Servo.h>
#include <DHTesp.h>
// #include <ESP32Servo.h>

DHTesp dht;
#define dhtpin 13
#define dir 2
#define step 0
#define enable 4

//ServoESP32 s ;
Servo s ;
const int rev = 2000;
bool flag = false;
void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  Serial.println("Hello, ESP32!");
  dht.setup(dhtpin,DHTesp::DHT22);
  pinMode(dir, OUTPUT);
  pinMode(step, OUTPUT);
  pinMode(enable, OUTPUT);
  digitalWrite(enable, HIGH);
   s.attach(18);
  s.write(0);
  
  delay(1000);
}

void loop() {
 
  delay(10); // this speeds up the simulation
  TempAndHumidity data = dht.getTempAndHumidity();
  String temp = String(data.temperature,1);
  String humid = String(data.humidity,1);// 1 Refers for number of decimal points
  Serial.println("temp : "+temp);
  Serial.println("humid : "+humid);
  if(temp.toInt()<=24){
    if(flag == false){ // the flag is to stop the stepper motor after the condition is met
      flag = true;
      serv();
      delay(5000);
      ClockWise();
    }
    
  }else{
    if(flag == false){
    CounterClockWise();
    flag = true;
    }
    
  }
 
  delay(2000);
}
void ClockWise(){
digitalWrite(enable, LOW);
digitalWrite(dir, HIGH);
 
for(int i = 0 ; i<rev; i++){
  digitalWrite(step, HIGH);
  delayMicroseconds(1000);
   digitalWrite(step, LOW);
  delayMicroseconds(1000);
}
}
void CounterClockWise(){
digitalWrite(enable, LOW);
digitalWrite(dir, LOW);
for(int i = 0 ; i<rev; i++){
  digitalWrite(step, HIGH);
  delayMicroseconds(1000);
   digitalWrite(step, LOW);
  delayMicroseconds(1000);
}
}
void serv(){
   s.write(90);
}
A4988