//Libraries
#include "DHT.h"
#include <U8g2lib.h>
/* Change these values based on your calibration values */
//Water Sensor CAL
int Waterhreshold = 420;
int WaterDelay = 2000; // Delay time to shut off pump:
//int upperThreshold = 520;
//DHT 22 Sensor CAL
int HumidityThreshold = 90;
int HumidityDelay = 4000; // Delay time between the mister and fan:
//int upperHumThreshold = 95;
// Sensor pins
#define WATER_SENSOR A0
#define DHTPIN 8
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE); // Initialize DHT sensor.
U8X8_SSD1306_128X64_NONAME_HW_I2C display(U8X8_PIN_NONE); //Screen Setup
int WaterVal = 0; // Value for storing water level
// Declare pins to which Relays are connected
const int RELAY_PIN_PUMP = 2; // the Arduino pin, which connects to the IN pin of Pump relay
const int RELAY_PIN_FAN = 3 ; // the Arduino pin, which connects to the IN pin of Fan relay
const int RELAY_PIN_MIST = 4; // the Arduino pin, which connects to the IN pin of Mist relay
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
dht.begin();
//Power Pin Settings:
//pinMode(WATER_POWER, OUTPUT);
//digitalWrite(WATER_POWER, LOW);
//Setting Up Input Pins:
pinMode(WATER_SENSOR,INPUT);
//Setting Up Output Pins:
pinMode(RELAY_PIN_PUMP,OUTPUT);
pinMode(RELAY_PIN_FAN,OUTPUT);
pinMode(RELAY_PIN_MIST,OUTPUT);
//Setting All Output Pins Off:
pinMode(RELAY_PIN_PUMP,HIGH);
pinMode(RELAY_PIN_FAN,HIGH);
pinMode(RELAY_PIN_MIST,HIGH);
//Display Stuff
display.begin();
display.setPowerSave(0);
display.setFont(u8x8_font_pxplusibmcgathin_f);
}
void loop() {
// put your main code here, to run repeatedly:
WaterVal = analogRead(0); // Read water sensor of fill bucket
float h = dht.readHumidity(); // Read humidity (the default)
float t = dht.readTemperature(); // Read temperature as Celsius (the default)
// Check if any reads failed and exit early (to try again).
if (isnan(h) || isnan(t)) {
Serial.println(F("Failed to read from DHT sensor!"));
return;
}
float hic = dht.computeHeatIndex(t, h, false); // Compute heat index in Celsius (isFahreheit = false)
// Can delete once set up is done
delay(1000);
display.setCursor(0,7);
display.print(F("Humidity: "));
display.print(h);
display.print(F("% Temperature: "));
display.print(t);
display.print(F("°C "));
display.print(hic);
display.print(F("°C "));
// Controls the water pump to fill up main tank:
if (WaterVal == 0) {
display.print("Water Level: Empty");
digitalWrite(RELAY_PIN_PUMP, LOW);
}
else if (WaterVal > 0 && WaterVal <= Waterhreshold) {
display.print("Water Level: Low");
digitalWrite(RELAY_PIN_PUMP, HIGH);
digitalWrite(RELAY_PIN_MIST,LOW);
digitalWrite(RELAY_PIN_FAN,LOW);
}
else if (WaterVal > Waterhreshold) {
display.print("Water Level: Good");
delay(WaterDelay);
digitalWrite(RELAY_PIN_PUMP, LOW);
if (h > 0 && h <= HumidityThreshold) {
display.print("Humidity Level: Low");
digitalWrite(RELAY_PIN_MIST, HIGH);
delay(HumidityDelay);
digitalWrite(RELAY_PIN_FAN, HIGH);
}
else if (h > HumidityThreshold){
display.print("Humidity Level: Good");
delay(HumidityDelay);
digitalWrite(RELAY_PIN_MIST, LOW);
delay(HumidityDelay);
digitalWrite(RELAY_PIN_FAN, LOW);
}
}
}