#include "DHT.h"
#include <LiquidCrystal_I2C.h>
#include "RTClib.h"
RTC_DS1307 rtc;
#define I2C_ADDR 0x27
#define LCD_COLUMNS 20
#define LCD_LINES 4
#define DHTTYPE DHT22
#define DHTPIN 2
DHT dht(DHTPIN, DHTTYPE);
LiquidCrystal_I2C lcd(I2C_ADDR, LCD_COLUMNS, LCD_LINES);
#define LDR_PIN 2
const float GAMMA = 0.7;
const float RL10 = 50;
void setup() {
Serial.begin(9600);
pinMode(LDR_PIN, INPUT);
lcd.init();
lcd.backlight();
Serial.println(F("DHTxx test!"));
dht.begin();
if (! rtc.begin()) {
Serial.println("Couldn't find RTC");
while (1);
}
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}
void loop() {
// Wait a few seconds between measurements.
delay(2000);
lcd.clear();
DateTime now = rtc.now();
int analogValue = analogRead(A0);
float voltage = analogValue / 1024. * 5;
float resistance = 2000 * voltage / (1 - voltage / 5);
float lux = pow(RL10 * 1e3 * pow(10, GAMMA) / resistance, (1 / GAMMA));
float h = dht.readHumidity();
float t = dht.readTemperature();
int hour = now.hour();
int minute = now.minute();
int second = now.second();
lcd.setCursor(0, 0);
lcd.print("Humidity: ");
lcd.println(h);
lcd.setCursor(0, 1);
lcd.print("Temperature: ");
lcd.println(t);
lcd.setCursor(0, 2);
lcd.print("Lux: ");
lcd.println(lux);
lcd.setCursor(0, 3);
lcd.print("Time: ");
lcd.print(hour);
lcd.print(':');
lcd.println(minute);
lcd.print(':');
lcd.print(second);
Serial.print(F("Humidity: "));
Serial.print(h);
Serial.print(F("% Temperature: "));
Serial.print(t);
Serial.print(F("°C "));
Serial.print(now.year(), DEC);
Serial.print('/');
Serial.print(now.month(), DEC);
Serial.print('/');
Serial.print(now.day(), DEC);
Serial.print(" (");
Serial.print(now.dayOfTheWeek());
Serial.print(") ");
Serial.print(now.hour(), DEC);
Serial.print(':');
Serial.print(now.minute(), DEC);
Serial.print(':');
Serial.println(now.second(), DEC);
}