#include "DHT.h"
int moistSensor = A0; //moisture sensor
int pumpPin = 8; //relay pin
#define RELAY_FAN_PIN 2 // Arduino pin connected to relay which connected to fan
#define DHTPIN 12 // Arduino pin connected to relay which connected to DHT sensor
#define DHTTYPE DHT22
int buzzer = 13;
int inputPin = 4;
int ledPin = 6;
int ledPin2 = 5;
int val = 0; // variable for reading the pin status
int pirState = LOW;
const int TEMP_THRESHOLD_UPPER = 25; // upper threshold of temperature, change to your desire value
const int TEMP_THRESHOLD_LOWER = 20; // lower threshold of temperature, change to your desire value
DHT dht(DHTPIN, DHTTYPE);
float temperature; // temperature in Celsius
void setup()
{
Serial.begin(9600); // initialize serial
dht.begin(); // initialize the sensor
pinMode(RELAY_FAN_PIN, OUTPUT); // initialize digital pin as an output
pinMode(buzzer,OUTPUT);
pinMode(inputPin, INPUT);
pinMode(moistSensor, INPUT);
pinMode(pumpPin, OUTPUT);
}
void loop()
{
tempDetect();// wait a few seconds between measurements.
stolenDetect();
watering();
}
void tempDetect(){
delay(2000);
temperature = dht.readTemperature();;
if (isnan(temperature)) {
Serial.println("Failed to read from DHT sensor!");
} else {
if(temperature > TEMP_THRESHOLD_UPPER){
Serial.println("The fan is turned on");
digitalWrite(RELAY_FAN_PIN, HIGH); // turn on
digitalWrite(buzzer,HIGH);
} else if(temperature < TEMP_THRESHOLD_LOWER){
Serial.println("The fan is turned off");
digitalWrite(RELAY_FAN_PIN, LOW); // turn on
digitalWrite(buzzer,LOW);
}
}
}
void stolenDetect(){
val = digitalRead(inputPin); // read input value
if (val == HIGH) { // check if the input is HIGH
digitalWrite(ledPin, HIGH); // turn LED ON
if (pirState == LOW) {
// we have just turned on
Serial.println("Motion detected!");
// We only want to print on the output change, not state
pirState = HIGH;
}
} else {
digitalWrite(ledPin, LOW); // turn LED OFF
if (pirState == HIGH) {
// we have just turned of
Serial.println("Motion ended!");
// We only want to print on the output change, not state
pirState = LOW;
}
}
}
void watering(){
int moistSensor = analogRead(A0); //read analog value of moisture sensor
int moisture = ( 100 - ( (moistSensor / 1023.00) * 100 ) ); //convert analog value to percentage
Serial.println(moisture);
if (moisture < 40) { //change the moisture threshold level based on your calibration values
digitalWrite(pumpPin, HIGH); // turn on the motor
delay(500); // multiply by 1000 to translate seconds to milliseconds
Serial.println("The Water Pump is turned on");
}
else {
digitalWrite(pumpPin, LOW); // turn off the motor
delay(500); // multiply by 60000 to translate minutes to milliseconds
Serial.println("The Water Pump is turned off");
}
}