/*
Godzilla monitor v1. Monitor temperature and humidity with 2x DHT22's. Temperature parameters are >75 <90*F.
Humidity parameters are >40 <60%. Humidity is controlled by a submersible waterpump that activates when humidity falls
below 40%, connected to an IoT relay, should only need 1 relay switch. A UVB light is also connected to an IoT and will
cycle between 12hrs on / 12hrs off, should also need 1 relay switch here as well. Temperature and humidity will be
displayed on a 16x2 LCD. A red LED will illuminate when temp or humidity is out of parameters. A blue LED will
illuminate when the water pump is active. A green LED will illuminate when a relay is activated.
-Eventually I want to connect it to the internet, possibly a different arduino board.
https://forum.arduino.cc/t/tortoise-terrarium-controller/1266256
*/
#include <DHT.h>
#include <LiquidCrystal.h>
#include "RTClib.h"
//#include <I2C_RTC.h> - not in sim
#include <time.h>
#define RED1_PIN 32
#define RED2_PIN 33
#define YEL1_PIN 34
#define YEL2_PIN 35
#define UV_PIN 30
#define REL2_PIN 31
#define PUMP1_PIN 36
#define PUMP2_PIN 40
#define DHT1_PIN 2
#define DHT2_PIN 3
#define DHTTYPE DHT22
//static DS3231 RTC;
RTC_DS1307 RTC;
//Type of DHT
DHT dht_1(DHT1_PIN, DHT22);
DHT dht_2(DHT2_PIN, DHT22);
//LED pins
//RED DHT1-32
//RED DHT2-33
//Yellow DHT1-34
//Yellow DHT2-35
//BLUE-36
//GREEN-31
//GREEN-30
//LCD signal pins
// initialize the library by associating any needed LCD interface pin
// with the arduino pin number it is connected to
const byte rs = 13, en = 12, d4 = 5, d5 = 6, d6 = 7, d7 = 8;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
void setup() {
Serial.begin(9600);
//starting various things
//set pin modes..
pinMode(RED1_PIN, OUTPUT);
pinMode(RED2_PIN, OUTPUT);
pinMode(YEL1_PIN, OUTPUT);
pinMode(YEL2_PIN, OUTPUT);
pinMode(UV_PIN, OUTPUT);
pinMode(REL2_PIN, OUTPUT);
pinMode(PUMP1_PIN, OUTPUT);
pinMode(PUMP2_PIN, OUTPUT);
//RTC Starting - MUST change time and day if changing program
RTC.begin();
//RTC.setHourMode(CLOCK_H24);
//RTC.setDate(8,10,24);
//RTC.setTime(19,16,30);
if (! RTC.isrunning()) {
Serial.println("RTC is NOT running, let's set the time!");
// When time needs to be set on a new device, or after a power loss, the
// following line sets the RTC to the date & time this sketch was compiled
RTC.adjust(DateTime(F(__DATE__), F(__TIME__)));
// This line sets the RTC with an explicit date & time, for example to set
// January 21, 2014 at 3am you would call:
// RTC.adjust(DateTime(2014, 1, 21, 3, 0, 0));
}
//DHTs Starting
dht_1.begin();
dht_2.begin();
//LCD Start
lcd.begin(16, 2);
// Print a message to the LCD.
lcd.print("Godzilla's House");
delay(3000);
lcd.clear();
lcd.println("Since Apr30,2024");
delay(3000);
lcd.clear();
}
void loop() {
// set the cursor to column 0, line 1
// (note: line 1 is the second row, since counting begins with 0):
lcd.setCursor(0, 0);
//RTC calling date and time
//switch (RTC.getWeek());
DateTime now = RTC.now();
Serial.print(now.hour());
Serial.print(":");
Serial.print(now.minute());
Serial.print(":");
Serial.print(now.second());
Serial.print(" ");
//RTC.startClock();
Serial.println("Monitoring");
//This is DHT_1
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
float h = dht_1.readHumidity();
// Read temperature as Celsius (the default)
float t = dht_1.readTemperature();
// Read temperature as Fahrenheit (isFahrenheit = true)
float f = dht_1.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_1!"));
lcd.clear();
lcd.println(F("Failed to read from DHT_1!"));
return;
}
Serial.print(F("Hum1: "));
Serial.print(h);
Serial.print(F("% Temp1: "));
Serial.print(f);
Serial.println(F("°F"));
lcd.clear();
lcd.setCursor(0, 0);//1st row
lcd.print(F("Hum : ")); lcd.print(h);
lcd.print(F("%"));
lcd.setCursor(0, 1);//2nd row
lcd.print(F("Temp: ")); lcd.print(f);
lcd.print(F("F"));
delay(600);
//This is DHT_2
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
float h2 = dht_2.readHumidity();
// Read temperature as Celsius (the default)
float t2 = dht_2.readTemperature();
// Read temperature as Fahrenheit (isFahrenheit = true)
float f2 = dht_2.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_2!"));
lcd.clear();
lcd.println(F("Failed to read from DHT_2!"));
return;
}
Serial.print(F("Hum2: "));
Serial.print(h2);
Serial.print(F("% Temp2: "));
Serial.print(f2);
Serial.println(F("°F"));
//If temp above 90 on DHT1/2 turn on Red LEDs
if ( f > 90.00) digitalWrite(RED1_PIN, HIGH);
else digitalWrite(RED1_PIN, LOW);
if ( f2 > 90.00) digitalWrite(RED2_PIN, HIGH);
else digitalWrite(RED2_PIN, LOW);
//added lcd write for temp high
if ( f > 90.00)
{
Serial.println("Temp1 High");
lcd.clear();
lcd.print("Temp1 High");
}
if ( f > 90.00)
{
Serial.println("Temp2 High");
lcd.clear();
lcd.print("Temp2 High");
}
delay(600);
//IF Humidity below 40% turn on Yellow LEDs
if ( h < 40 ) digitalWrite(YEL1_PIN, HIGH);
else if (h > 58) digitalWrite(YEL1_PIN, LOW);
if ( h2 < 40) digitalWrite(YEL2_PIN, HIGH);
else if (h2 > 58) digitalWrite(YEL2_PIN, LOW);
//Added lcd write for humidity high
if ( h > 60) {
Serial.println("Humidity1 High");
lcd.clear();
lcd.print("Humidity1 High");
}
if ( h2 > 60) {
Serial.println("Humidity2 High");
lcd.clear();
lcd.print("Humidity2 High");
}
//Relay 1 for UVB light, 12hrs on, 12hrs off, 2 green LEDs show when active
//12hrs in milliseconds: 43200000 ms
if (now.hour() > 6 && now.hour() < 19)
digitalWrite(UV_PIN, HIGH);
else
digitalWrite(UV_PIN, LOW);
//Relay 2
digitalWrite(REL2_PIN, HIGH);
delay(50);
digitalWrite(REL2_PIN, LOW);
delay(1000);
//Waterpump operation - Change parameters before going LIVE, currently set-up for testing
if (h < 40 && h2 < 40) {
digitalWrite(PUMP2_PIN, HIGH);
digitalWrite(PUMP1_PIN, HIGH);
}
else if (h >= 60 && h2 >= 60) {
(digitalWrite(PUMP2_PIN, LOW));
digitalWrite(PUMP1_PIN, LOW);
}
}