#include "DHTesp.h"
const int DHT_PIN = 15;
const int buz_PIN = 12;
DHTesp dhtSensor;
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,16,2);
#define relay 2
const int pirPin = 13; // PIR sensor digital output pin
void setup()
{
// Set PIR sensor pin as input
pinMode(pirPin, INPUT);
pinMode(relay, OUTPUT);
pinMode(buz_PIN, OUTPUT);
Wire.begin(23, 22);
Serial.begin(9600);
lcd.init();
lcd.backlight();
dhtSensor.setup(DHT_PIN, DHTesp::DHT22);
}
void DHT_sensor()
{
TempAndHumidity data = dhtSensor.getTempAndHumidity();
Serial.println("Temp: " + String(data.temperature, 2) + "°C");
Serial.println("Humidity: " + String(data.humidity, 1) + "%");
Serial.println("---");
// Blynk.virtualWrite(V1,String(data.humidity, 1));
// Blynk.virtualWrite(V3,String(data.temperature, 2));
lcd.setCursor ( 0, 0 );
lcd.print ( "Temp: " );
lcd.print ( String(data.temperature, 2)+ "°C");
lcd.setCursor ( 0, 1 );
lcd.print ( "Humidity:" );
lcd.print ( String(data.humidity, 1) + "%");
if(data.temperature>30)
{
digitalWrite(relay, HIGH);
digitalWrite(buz_PIN, HIGH);
}
else
{
digitalWrite(relay, LOW);
digitalWrite(buz_PIN, LOW);
}
delay(1000); // Wait for a new reading from the sensor (DHT22 has ~0.5Hz sample rate)
}
void loop()
{
int16_t i = analogRead(34);
String msg = i < 2165 ? "WET" : i > 3135 ? "DRY" : "OK";
lcd.clear();
lcd.print("Soil: ");
lcd.print(msg);
delay(500);
lcd.clear();
DHT_sensor();
motionSensor();
}
int pirState = LOW;
void motionSensor()
{
int val = digitalRead(pirPin); // read input value
if (val == HIGH) { // check if the input is HIGH
digitalWrite(relay, HIGH); // turn LED ON
digitalWrite(buz_PIN, HIGH);
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(relay, LOW); // turn LED OFF
digitalWrite(buz_PIN, LOW);
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;
}
}
}