//import librairies
#define FASTLED_INTERNAL
#include <FastLED.h>
#include <DHT.h>
//define pins
#define PHOTO_PIN A7 //analog pin photoresistor
#define WATER_PIN A0 //analog pin water sensor
#define BUZZ_PIN 10 //digital pin buzzer
#define DHTPIN 9 // Digital pin connected to the DHT sensor
#define LED_PIN 11 //digital pin led
#define NUM_LEDS 10 //number of led of the strip
#define DHTTYPE DHT11 // model od dht
#define MOISTURE_PIN A4 //analog soil moisture pin
#define RELAY_PIN A5 // analog IN pin of relay
DHT dht(DHTPIN, DHTTYPE);
CRGB leds[NUM_LEDS];
int water_value; //value of the water sensor
int photo_value; // store the value from photoresistor (0-1023)
int moisture_value; //store the value from the soil moisture
void setup() {
Serial.begin(9600); //sets data rate to 115200 bps
FastLED.addLeds<WS2812, LED_PIN, GRB>(leds, NUM_LEDS);
Serial.println("DHT test");
dht.begin();
FastLED.addLeds<WS2812, LED_PIN, GRB>(leds, NUM_LEDS);
pinMode(PHOTO_PIN,INPUT);
pinMode(WATER_PIN,INPUT);
pinMode(BUZZ_PIN,OUTPUT);
pinMode(RELAY_PIN, OUTPUT);
delay(1000);
}
void loop() {
//read the value of the moisture sensor
moisture_value= analogRead(MOISTURE_PIN);
moisture_value = map(moisture_value,0,1023,0,100);
Serial.print("Moisture : ");
Serial.print(moisture_value);
Serial.println("%");
//If the soil is dry the relay is on
if (moisture_value >= 37){
digitalWrite(RELAY_PIN, LOW); // turn on the pump
Serial.print("Pump On");
Serial.print(RELAY_PIN);
}
else{
digitalWrite(RELAY_PIN, HIGH); // turn off the pump
Serial.print("Pump Off");
Serial.print(RELAY_PIN);
}
//read photoresistor value
int analog_value = analogRead(PHOTO_PIN);
int photo_value = map(analog_value, 0, 1023, 0, 100);// read value’s current range to value’s target range
while (photo_value > 50){ // while is daylight do the project otherwise nothing is activated
Serial.print("photoresistor = ");
Serial.print(photo_value);
Serial.print(" "); Serial.print("\n");
//Read Water Value
water_value = analogRead(WATER_PIN);
if (water_value < 3050 && water_value > 0){ //the buzzer makes sound if the level of water is too low
//read buzz sensor
digitalWrite(BUZZ_PIN,HIGH);
delay(100);
digitalWrite(BUZZ_PIN,LOW);
delay(100);
Serial.print("Water box low");
Serial.print(" | ");
}
else if (water_value == 0){
Serial.print("Water box empty !!!!!");
Serial.print(" | ");
}
else{
Serial.print("Water box level OK");
Serial.print("\n");
Serial.print(" ");
}
//turn on the led strip
for (int i = 0; i < water_value; ++i) {
leds[i] = CRGB(255, 255, 0);
FastLED.show();
//delay(500);
}
//turn off the led strip
for (int i = water_value; i > 0; --i) {
leds[i] = CRGB(0, 0, 0);
FastLED.show();
}
//read temperature + humidity sensor
float h = dht.readHumidity();
float t = dht.readTemperature();//as celsius
if(isnan(h) || isnan(t)){
Serial.println("Failed to read DHT sensor");
Serial.println("");
return;
}
//Display message
Serial.print("Humidity :");
Serial.print(h);
Serial.print("%");
Serial.print(" | ");
Serial.print("Temperature :");
Serial.print(t);
Serial.print("°C");
Serial.print("\n");
Serial.print("\n");
Serial.print("Level Water = ");
Serial.print(water_value);
Serial.print("\n");
Serial.print(" ");
delay(500);
}
}
Comments