//when temperature is 25 to 26 degree and humidity is 60 to 70% print irrigation off led no light up buzzer no sound servo motor no move
//else led light up buzzer sound servo motor move
//oled will print the temperature is () and humidity is ()
#include "DHT.h"
//https://github.com/adafruit/DHT-sensor-library
#include <Servo.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define DHTPIN 2 // Digital pin connected to the DHT sensor
#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
DHT dht(DHTPIN, DHTTYPE);
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
int buzzerPin = 12; // Buzzer pin connected to 12
float hum; // Read humidity
float temp; // Read temperature
int pos=0; // variable to store the servo position
int angle=0; //angle = 0
Servo myservo; // create servo object to control a servo
// declare an SSD1306 display object connected to I2C
Adafruit_SSD1306 oled(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
void setup()
{
Serial.begin(9600); //begin
Serial.println("DHT22!"); //print DHT22
dht.begin(); //dht start
pinMode(buzzerPin, OUTPUT); //Code declares pin 12 as output pin
myservo.attach(9); // attaches the servo on pin 9 to the servo object
pinMode(LED_BUILTIN, OUTPUT); //Code declares pin 13 as output pin
// initialize OLED display with address 0x3C for 128x64
if (!oled.begin(SSD1306_SWITCHCAPVCC, 0x3C))
{
Serial.println(F("SSD1306 allocation failed"));
while (true);
}
delay(2000); // wait for initializing
oled.clearDisplay(); // clear display
oled.setTextSize(1); // text size
oled.setTextColor(WHITE); // text color
oled.setCursor(0, 1); // position to display
oled.println("Temperature is "); //print temperature is
oled.print(temp); //print temperature value
oled.print(" Celsius, Humidity is "); //print celsius, humidity:
oled.print(hum); //print humidity value
oled.println(" %"); //print %
}
void loop()
{
delay(2000); //wait for initializing
hum = dht.readHumidity(); //read humidity value
temp= dht.readTemperature(); //read temperature value
if (temp >= 25 && temp <=26 && (hum <= 70 && hum >=60))
{
Serial.print("Temperature is "); //print temperature is
Serial.print(temp); //print temperature value
Serial.print(" Celsius, Humidity is "); //print celsius, humidity:
Serial.print(hum); //print humidity value
Serial.println(" %"); //print %
Serial.println (" Irrigation off"); //print irrigation off
noTone(buzzerPin); //buzzer no sound
for(angle = 180; angle>=1; angle-=90) //servo motor turn 90 degree
myservo.write(angle); //servo motor show angle
digitalWrite(LED_BUILTIN, LOW); //led remains no light
delay (500);
}
else
{
Serial.print("Temperature is "); //print temperature is
Serial.print(temp); //print temperature value
Serial.print(" Celsius, Humidity is "); //print celsius, humidity:
Serial.print(hum); //print humidity value
Serial.println(" %"); //print %
Serial.println (" Irrigation on"); //print irrigation on
tone(buzzerPin, 100,1000); //buzzer sound
digitalWrite(LED_BUILTIN, HIGH); //led light up
myservo.write(pos); //servo motor moves
delay (500);
}
oled.clearDisplay();
oled.setTextSize(1);
oled.setTextColor(WHITE);
oled.setCursor(0, 1);
oled.println("Temperature is"); // text to display
oled.print(temp); //print temperature value
oled.print(" Celsius, Humidity is "); //print celsius, humidity:
oled.print(hum); //print humidity value
oled.println(" %"); //print %
oled.display();
}