// Example testing sketch for various DHT humidity/temperature sensors
// Written by ladyada, public domain
// REQUIRES the following Arduino libraries:
// - DHT Sensor Library: https://github.com/adafruit/DHT-sensor-library
// - Adafruit Unified Sensor Lib: https://github.com/adafruit/Adafruit_Sensor
#include "DHT.h"
#include "Servo.h"
#define RELAY1 4
Servo srv1;
#define SRV1_PIN 3
#define SRV1_CLOSE 0.0
#define SRV1_OPEN 90.0
#define DHTPIN 2 // Digital pin connected to the DHT sensor
// Uncomment whatever type you're using!
//#define DHTTYPE DHT11 // DHT 11
#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
//#define DHTTYPE DHT21 // DHT 21 (AM2301)
// Connect pin 1 (on the left) of the sensor to +5V
// NOTE: If using a board with 3.3V logic like an Arduino Due connect pin 1
// to 3.3V instead of 5V!
// Connect pin 2 of the sensor to whatever your DHTPIN is
// Connect pin 4 (on the right) of the sensor to GROUND
// Connect a 10K resistor from pin 2 (data) to pin 1 (power) of the sensor
// Initialize DHT sensor.
// Note that older versions of this library took an optional third parameter to
// tweak the timings for faster processors. This parameter is no longer needed
// as the current DHT reading algorithm adjusts itself to work on faster procs.
DHT dht(DHTPIN, DHTTYPE);
int heater_status = 0;
int door_status = 0;
void setup()
{
Serial.begin(9600);
Serial.println(F("~ Door temp Control ~"));
dht.begin();
srv1.attach(SRV1_PIN);
srv1.write(SRV1_CLOSE);
pinMode(RELAY1, OUTPUT);
digitalWrite(RELAY1, LOW);
}
void loop()
{
// Wait a few seconds between measurements.
delay(2000); //min 750mS
float t = dht.readTemperature();
if (isnan(t))
{
Serial.println(F("Failed to read from DHT sensor!"));
return;
}
if(t <= 16.0)
{
digitalWrite(RELAY1, HIGH);
srv1.write(SRV1_CLOSE);
heater_status = 1;
door_status = 0;
}
else if(t >= 18.0 && t < 22.0)
{
digitalWrite(RELAY1, LOW);
srv1.write(SRV1_CLOSE);
heater_status = 0;
door_status = 0;
}
else if(t >= 22.0)
{
digitalWrite(RELAY1, LOW);
srv1.write(SRV1_OPEN);
heater_status = 0;
door_status = 1;
}
Serial.print(F("Temp: "));
Serial.print(t);
Serial.print(F("°C "));
Serial.print("Door status = ");
Serial.print(door_status ? "open, " : "closed, ");
Serial.print("Heater status = ");
Serial.print(heater_status ? "open, " : "closed, ");
Serial.println();
}