#include <Servo.h>
#include "DHT.h"
#define DHTPIN 2
#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
Servo myservo; // create servo object to control a servo
DHT dht(DHTPIN, DHTTYPE);
void setup() {
Serial.begin(115200);
Serial.println(F("DHT22 example!"));
dht.begin();
myservo.attach(9); // attaches the servo on pin 9 to the servo object
}
void loop() {
// scale it to use it with the servo (value between 0 and 180)
float temperature = dht.readTemperature();
float humidity = dht.readHumidity();
// Check if any reads failed and exit early (to try again).
if (isnan(temperature) || isnan(humidity))
{
Serial.println(F("Failed to read from DHT sensor!"));
return;
}
if (humidity >40 && temperature >30)
{
myservo.write(180);
}else
{
myservo.write(0);
}
Serial.print(F("Humidity: "));
Serial.print(humidity);
Serial.print(F("% Temperature: "));
Serial.print(temperature);
Serial.println(F("°C "));
// Wait a few seconds between measurements.
delay(2000);
// sets the servo position according to the scaled value
// waits for the servo to get there
}