#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <DHT.h>
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1
#define SDA_PIN 21 // Connect OLED SDA pin to GPIO21 (or any other pin that supports I2C)
#define SCL_PIN 22 // Connect OLED SCL pin to GPIO22 (or any other pin that supports I2C)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
#define DHT_PIN 2
#define DHT_TYPE DHT11
DHT dht(DHT_PIN, DHT_TYPE);
#define TDS_SENSOR_PIN 34 // Connect TDS sensor to GPIO34 (Analog pin)
#define WATER_LEVEL_SENSOR_PIN 35 // Connect water level sensor to GPIO35 (Analog pin)
#define LIGHT_SENSOR_PIN 33 // Connect LDR sensor to GPIO33 (Analog pin)
#define WATER_PUMP_PIN 14 // Connect water pump to GPIO14
#define WATER_VALVE_PIN 15 // Connect water valve to GPIO15
#define COOLING_FAN_PIN 16 // Connect cooling fan to GPIO16
#define LIGHT_BULB_PIN 17 // Connect light bulb to GPIO17
unsigned long pumpStartTime = 0;
const unsigned long maxPumpRuntime = 60000; // Maximum runtime for the pump (milliseconds)
const float COOLING_FAN_THRESHOLD = 30.0; // Temperature threshold for cooling fan control
void setup() {
Serial.begin(9600);
pinMode(WATER_PUMP_PIN, OUTPUT);
pinMode(WATER_VALVE_PIN, OUTPUT);
pinMode(COOLING_FAN_PIN, OUTPUT);
pinMode(LIGHT_BULB_PIN, OUTPUT);
dht.begin();
Wire.begin(SDA_PIN, SCL_PIN); // Initialize I2C communication for OLED display
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display.display();
delay(2000);
display.clearDisplay();
}
void loop() {
float temperature = dht.readTemperature();
float humidity = dht.readHumidity();
display.clearDisplay();
display.setTextSize(1); // Default font size
// Display temperature with measuring unit in red color
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 0);
display.print("Temperature: ");
display.print(temperature);
display.println(" C"); // Celsius
// Display humidity with measuring unit in green color
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 10);
display.print("Humidity: ");
display.print(humidity);
display.println(" %"); // Percentage
// Display TDS value with measuring unit in blue color
float tdsValue = readTDS();
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 20);
display.print("TDS: ");
display.print(tdsValue);
display.println(" ppm"); // Parts per million
// Display water level with measuring unit in blue color
int waterLevel = readWaterLevel();
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 30);
display.print("Water Level: ");
display.print(waterLevel);
display.println(" %"); // Percentage
// Read light intensity and control light bulb
int lightIntensity = analogRead(LIGHT_SENSOR_PIN);
display.setTextColor(SSD1306_WHITE); // Yellow color for light intensity
display.setCursor(0, 40);
display.print("Light Intensity: ");
display.print(lightIntensity);
display.println(" lux"); // Lux - Measuring unit for light intensity
display.display();
// Check if the temperature reading is valid
if (isnan(temperature)) {
// If the temperature reading is not a number (NaN), handle the error
Serial.println("Error: Temperature sensor reading is invalid.");
// You may choose to take specific actions such as logging the error, performing diagnostics, or implementing a backup cooling strategy.
} else {
// The temperature reading is valid, proceed with fan control
if (temperature > COOLING_FAN_THRESHOLD) {
// If the temperature exceeds the threshold, turn on the cooling fan
digitalWrite(COOLING_FAN_PIN, HIGH); // Turn on cooling fan
} else {
// If the temperature is below or equal to the threshold, turn off the cooling fan
digitalWrite(COOLING_FAN_PIN, LOW); // Turn off cooling fan
}
}
// Control Water Pump based on humidity and water level
if ((humidity < 80 && waterLevel >= 20) || (humidity >= 80 && waterLevel < 20)) {
if (millis() - pumpStartTime < maxPumpRuntime) {
digitalWrite(WATER_PUMP_PIN, HIGH); // Turn on water pump
} else {
digitalWrite(WATER_PUMP_PIN, LOW); // Turn off water pump if max runtime exceeded
}
} else {
digitalWrite(WATER_PUMP_PIN, LOW); // Turn off water pump
pumpStartTime = millis(); // Reset pump start time when not in operation
}
// Control Water Valve based on humidity and TDS value
if (tdsValue > 80) {
digitalWrite(WATER_VALVE_PIN, HIGH); // Open water valve
} else {
digitalWrite(WATER_VALVE_PIN, LOW); // Close water valve
}
// Control Light Bulb based on light intensity
if (lightIntensity < 300) { // Adjust the range according to your requirements
digitalWrite(LIGHT_BULB_PIN, HIGH); // Turn on light bulb
} else {
digitalWrite(LIGHT_BULB_PIN, LOW); // Turn off light bulb
}
delay(1000); // Update every second
}
float readTDS() {
// Read TDS sensor and calculate TDS value
int rawValue = analogRead(TDS_SENSOR_PIN);
float tdsValue = map(rawValue, 0, 4095, 0, 1000); // Adjust calibration if needed
return tdsValue;
}
int readWaterLevel() {
// Read water level sensor and calculate water level
int rawValue = analogRead(WATER_LEVEL_SENSOR_PIN);
int waterLevel = map(rawValue, 0, 4095, 0, 100); // Adjust calibration if needed
return waterLevel;
}