Test everything now

How it's done

Now it is the best time to test all of the sensors.

Connect Arduino with USB cable to computer and upload code using Arduino IDE.

How to test sensors?

This code reads sensors every minute.

Be sure to avoid night shift. If you test this in late hours change value at line 84.

Test soil sensor triggering water pump. Pump will stop when you change position on shielded float switch.

Of course be sure that other two float switches are sending 1, and play with them after.

If light intensity is under 6000 lux, turn on relay channel 1. If your room light is under 6000 lux, turn on mobile phone flash light ant after next reading you will see that relay channel will be turned off and lux value will be more then 20000 lux.

Extra info:

#include <OneWire.h>
#define DS3231_I2C_ADDRESS 0x68

#include "DHT.h"
#define DHTPIN 8     // what digital pin we're connected to
#define DHTTYPE DHT22   // DHT 22  (AM2302), AM2321

#include <BH1750.h>

DHT dht(DHTPIN, DHTTYPE);
OneWire  ds(12);  // on pin 10 (a 4.7K resistor is necessary)
BH1750 lightMeter;

int relayIn1 = 2;
int relayIn2 = 3;
int relayIn3 = 4;
int relayIn4 = 5;

int flowSwitch1 = 9; 
int flowSwitch2 = 10; 
int flowSwitch3 = 11; 

int soil = A3;

//Data variables

int flowSwitch1Read = -1; //0 need water, 1 enough water;
int flowSwitch2Read = -2; //0 need water, 1 enough water;
int flowSwitch3Read = -3; //0 need water, 1 enough water;

int lastMinute = -1;
int nowMinute = -2;

int nowHour = -1;

float temperature = -1;
float humidity = -1;

int soilHumidity = -1;
int soilHumidityLevel = 950;

uint16_t lux = 1;


void setup(){
int status;
Serial.begin(9600);
pinMode(relayIn1, OUTPUT);
pinMode(relayIn2, OUTPUT);
pinMode(relayIn3, OUTPUT);
pinMode(relayIn4, OUTPUT);

pinMode(flowSwitch1, INPUT_PULLUP);
pinMode(flowSwitch2, INPUT_PULLUP);
pinMode(flowSwitch3, INPUT_PULLUP);

digitalWrite(relayIn1, HIGH);
digitalWrite(relayIn2, HIGH);
digitalWrite(relayIn3, HIGH);
digitalWrite(relayIn4, HIGH);

pinMode(soil, INPUT);

Wire.begin();

dht.begin();

lightMeter.begin();

}



void loop(){
displayTime();  

delay(2000);
  
if(lastMinute!=nowMinute){
  readSensorStatus();
  delay(2000);

  if (nowHour<7){
    //Night shift
      Serial.println("Night!");
      goto Night;
    } else {
      //Day shift
      Serial.println("Day!");
      if(lux<6000){
        turnOnLamp();
      } else {
        turnOffLamp();
      }
      //Water level underwork 
      if(flowSwitch3Read==0){
          Serial.println("Pump is off, no water in tank!");
        } else {
          // Insufficiently Water 
          if(flowSwitch2Read==0){
              Serial.println("Send Notification to add water");
            } 
          
          if (soilHumidity>soilHumidityLevel){
            if(flowSwitch1Read==0){
              turnOnPump();
            }
          }
       
          lastMinute=nowMinute;
      
        }
      delay(1000);  
      }
    }
    Night:;
}

void turnOnPump(){
  while(flowSwitch1Read==0){
    Serial.println("Relay In 1 on");
    digitalWrite(relayIn1, LOW); 
    readSensorStatus();
  }; 
  Serial.println("Relay In 1 off");
  turnOffPump();
}

void turnOffPump(){
    digitalWrite(relayIn1, HIGH);   
}

void turnOnLamp(){
    digitalWrite(relayIn4, LOW);   
}

void turnOffLamp(){
    digitalWrite(relayIn4, HIGH);   
}

void readSensorStatus(){
  lux = lightMeter.readLightLevel();
  Serial.print("Light: ");
  Serial.print(lux);
  Serial.println(" lx");  
  flowSwitch1Read = digitalRead(flowSwitch1);
  Serial.print("Water level in upper pipes: ");
  Serial.println(flowSwitch1Read);
  flowSwitch2Read = digitalRead(flowSwitch2);
  Serial.print("Water in tank: ");
  Serial.println(flowSwitch2Read);
  flowSwitch3Read = digitalRead(flowSwitch3);
  Serial.print("Minimum water in tank: ");
  Serial.println(flowSwitch3Read);
  //Serial.print("Float Switch is ");
  readDHT();
  readWaterTemperature();
  soilHumidity = analogRead(soil);
  Serial.print("Soil Humidity sensor value: ");
  Serial.println(soilHumidity);  
  
}

// Convert normal decimal numbers to binary coded decimal
byte decToBcd(byte val)
{
  return( (val/10*16) + (val%10) );
}
// Convert binary coded decimal to normal decimal numbers
byte bcdToDec(byte val)
{
  return( (val/16*10) + (val%16) );
}


void readDS3231time(byte *second,
byte *minute,
byte *hour,
byte *dayOfWeek,
byte *dayOfMonth,
byte *month,
byte *year)
{
  Wire.beginTransmission(DS3231_I2C_ADDRESS);
  Wire.write(0); // set DS3231 register pointer to 00h
  Wire.endTransmission();
  Wire.requestFrom(DS3231_I2C_ADDRESS, 7);
  // request seven bytes of data from DS3231 starting from register 00h
  *second = bcdToDec(Wire.read() & 0x7f);
  *minute = bcdToDec(Wire.read());
  *hour = bcdToDec(Wire.read() & 0x3f);
  *dayOfWeek = bcdToDec(Wire.read());
  *dayOfMonth = bcdToDec(Wire.read());
  *month = bcdToDec(Wire.read());
  *year = bcdToDec(Wire.read());
}

void displayTime()
{
  byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
  // retrieve data from DS3231
  readDS3231time(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month,
  &year);
  
  nowMinute = minute;
  nowHour = hour;
  
  // send it to the serial monitor
  Serial.print(hour, DEC);
  // convert the byte variable to a decimal number when displayed
  Serial.print(":");
  if (minute<10)
  {
    Serial.print("0");
  }
  Serial.print(minute, DEC);
  Serial.print(":");
  if (second<10)
  {
    Serial.print("0");
  }
  Serial.print(second, DEC);
  Serial.print(" ");
  Serial.print(dayOfMonth, DEC);
  Serial.print("/");
  Serial.print(month, DEC);
  Serial.print("/");
  Serial.print(year, DEC);
  Serial.print(" Day of week: ");
  switch(dayOfWeek){
  case 1:
    Serial.println("Sunday");
    break;
  case 2:
    Serial.println("Monday");
    break;
  case 3:
    Serial.println("Tuesday");
    break;
  case 4:
    Serial.println("Wednesday");
    break;
  case 5:
    Serial.println("Thursday");
    break;
  case 6:
    Serial.println("Friday");
    break;
  case 7:
    Serial.println("Saturday");
    break;
  }
}

void readDHT(){
  humidity = dht.readHumidity();
  temperature = dht.readTemperature();
  
  if (isnan(temperature) || isnan(humidity)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  } else {
    Serial.print("Temperature: ");
    Serial.print(temperature);
    Serial.print(" *C, Humidity: ");
    Serial.println(humidity);
  }
  
  
  
}

void readWaterTemperature(){
  byte i;
  byte present = 0;
  byte type_s;
  byte data[12];
  byte addr[8];
  float celsius, fahrenheit;
  
  if ( !ds.search(addr)) {
    Serial.println("No more addresses.");
    Serial.println();
    ds.reset_search();
    delay(500);
    return;
  }
  
  Serial.print("ROM =");
  for( i = 0; i < 8; i++) {
    Serial.write(' ');
    Serial.print(addr[i], HEX);
  }

  if (OneWire::crc8(addr, 7) != addr[7]) {
      Serial.println("CRC is not valid!");
      return;
  }
  Serial.println();
 
  // the first ROM byte indicates which chip
  switch (addr[0]) {
    case 0x10:
      Serial.println("  Chip = DS18S20");  // or old DS1820
      type_s = 1;
      break;
    case 0x28:
      Serial.println("  Chip = DS18B20");
      type_s = 0;
      break;
    case 0x22:
      Serial.println("  Chip = DS1822");
      type_s = 0;
      break;
    default:
      Serial.println("Device is not a DS18x20 family device.");
      return;
  } 

  ds.reset();
  ds.select(addr);
  ds.write(0x44, 1);        // start conversion, with parasite power on at the end
  
  delay(1000);     // maybe 750ms is enough, maybe not
  // we might do a ds.depower() here, but the reset will take care of it.
  
  present = ds.reset();
  ds.select(addr);    
  ds.write(0xBE);         // Read Scratchpad

  Serial.print("  Data = ");
  Serial.print(present, HEX);
  Serial.print(" ");
  for ( i = 0; i < 9; i++) {           // we need 9 bytes
    data[i] = ds.read();
    Serial.print(data[i], HEX);
    Serial.print(" ");
  }
  Serial.print(" CRC=");
  Serial.print(OneWire::crc8(data, 8), HEX);
  Serial.println();

  // Convert the data to actual temperature
  // because the result is a 16 bit signed integer, it should
  // be stored to an "int16_t" type, which is always 16 bits
  // even when compiled on a 32 bit processor.
  int16_t raw = (data[1] << 8) | data[0];
  if (type_s) {
    raw = raw << 3; // 9 bit resolution default
    if (data[7] == 0x10) {
      // "count remain" gives full 12 bit resolution
      raw = (raw & 0xFFF0) + 12 - data[6];
    }
  } else {
    byte cfg = (data[4] & 0x60);
    if (cfg == 0x00) raw = raw & ~7;  // 9 bit resolution, 93.75 ms
    else if (cfg == 0x20) raw = raw & ~3; // 10 bit res, 187.5 ms
    else if (cfg == 0x40) raw = raw & ~1; // 11 bit res, 375 ms
    //// default is 12 bit resolution, 750 ms conversion time
  }
  celsius = (float)raw / 16.0;
  fahrenheit = celsius * 1.8 + 32.0;
  Serial.print("  Temperature = ");
  Serial.print(celsius);
  Serial.print(" Celsius, ");
  Serial.print(fahrenheit);
  Serial.println(" Fahrenheit");
}

Activity Q&A

Need help with this activity?Add the first post