#include "DHT.h"
#include "RTClib.h"
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#define DHTPIN 27
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
RTC_DS1307 rtc;
char daysOfTheWeek[7][12] = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" };
LiquidCrystal_I2C lcd(0x27, 20, 4);
int vr = 32;
int relay = 13;
String relaystate;
void setup() {
pinMode(relay, OUTPUT);
Serial.begin(9600);
Serial.println(F("DHTxx test!"));
dht.begin();
lcd.init();
lcd.backlight();
#ifndef ESP8266
while (!Serial)
;
#endif
if (!rtc.begin()) {
Serial.println("Couldn't find RTC");
Serial.flush();
while (1) delay(10);
}
if (!rtc.isrunning()) {
Serial.println("RTC is NOT running, let's set the time!");
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}
}
void loop() {
DateTime now = rtc.now();
float h = dht.readHumidity();
// Read temperature as Celsius (the default)
float t = dht.readTemperature();
// Read temperature as Fahrenheit (isFahrenheit = true)
float f = dht.readTemperature(true);
// Check if any reads failed and exit early (to try again).
if (isnan(h) || isnan(t) || isnan(f)) {
Serial.println(F("Failed to read from DHT sensor!"));
return;
}
int val = analogRead(vr);
int percent = map(val,0,4095,0,100);
if(h>50 && percent>50){
if (now.second() % 15 == 0) {
digitalWrite(relay, HIGH);
relaystate = "On";
} else if (now.second() % 5 == 0) {
digitalWrite(relay, LOW);
relaystate = "Off";
}
}
else{
digitalWrite(relay, LOW);
relaystate = "Off";
}
Serial.print("Humidity:");
Serial.print(h);
Serial.print(" ");
Serial.print("Percen:");
Serial.print(percent);
Serial.print(" ");
Serial.print("Time:");
Serial.print(now.hour(), DEC);
Serial.print(':');
Serial.print(now.minute(), DEC);
Serial.print(':');
Serial.print(now.second(), DEC);
Serial.print(" ");
Serial.println(relaystate);
lcd.setCursor(0, 0);
lcd.print("H:");
lcd.print(h);
lcd.setCursor(9, 0);
lcd.print("VR:");
lcd.print(percent);
lcd.setCursor(0, 1);
lcd.print(now.hour(), DEC);
lcd.print(':');
lcd.print(now.minute(), DEC);
lcd.print(':');
lcd.print(now.second(), DEC);
lcd.setCursor(9, 1);
lcd.print(relaystate);
delay(1000);
lcd.clear();
}