
#include <OneWire.h>
#include <DS18B20.h>  //temp DS18B20_RT by rob tillaart
#include <DS3231.h>   //RTC DS3231 by andrew wickert
#include <DHT.h>      //Adafruit
#include <Wire.h>

/*****************************************************************
   Déclaration des Capteurs
 *****************************************************************/
//relay Lumiere
const int RougeHaut      = 2;
const int BleuHaut       = 3;
const int RougeBas       = 4;
const int BleuBas        = 5;

//relay Ventilation
const int DehumPin        = 6;
const int Extracteur      = 7;
const int VentilHaut      = 8;
const int VentilBas       = 9;

//relay Arrosage
const int Arro1            = 10;
const int Arro2            = 11;
const int Evacuation      = 12;

//Capteur niveau eau
const int NiveauEauRecup  = 13;
const int NiveauEauAqua   = 14;

//Capteur Temp
#define ONE_WIRE_TEMP 15
OneWire oneWireTemp(ONE_WIRE_TEMP);
DS18B20 Temp(&oneWireTemp);

//capteur Hum air
#define DHTTYPE DHT11
#define DHTPIN 16
DHT dht(DHTPIN, DHTTYPE);

//Capteur hum sol
const int HumidSolHaut = 17;
const int HumidSolBas  = 18;

//interupteur
const int interType  = 22;
const int interLum  = 24;
const int interONExt  = 26;
const int interONVentil  = 28;
const int interOFFExt  = 30;
const int interONAro  = 32;
const int interONVid  = 34;
const int interOFFAro  = 36;
const int inter9  = 38;
const int inter10 = 40;

//RTC
DS3231 Clock;

/*****************************************************************
   Déclaration des Variables globals
 *****************************************************************/

//Variable lumiere
int newDay = 1; // Variable pour savoir si nouveau jour.
int HA;  //heure allumage en minute
int HE;  //heure extinction en minute (Ha +12Hou18H +/-15min)

//Variable Extraction
int tmpExtract = 0, humExtract;
int extract;

//Variabe Arrosage
const int delaySecArro = 20;
const int delaySecEvacuation = 30;
int HeuArrosage;
/***********************************************************************
     Setup
 **********************************************************************/

void setup() {
  Serial.begin(9600);

  /***********************************************************************
    init des pins
  * *********************************************************************/
  //PIN Lumiere
  pinMode(RougeHaut, OUTPUT);
  pinMode(RougeBas, OUTPUT);
  pinMode(BleuHaut, OUTPUT);
  pinMode(BleuBas, OUTPUT);
  digitalWrite(RougeHaut, HIGH);
  digitalWrite(RougeBas, HIGH);
  digitalWrite(BleuHaut, HIGH);
  digitalWrite(BleuBas, HIGH);

  //Pin ventilation
  pinMode(DehumPin, OUTPUT);
  pinMode(Extracteur, OUTPUT);
  pinMode(VentilHaut, OUTPUT);
  pinMode(VentilBas, OUTPUT);
  digitalWrite(DehumPin, HIGH);
  digitalWrite(Extracteur, HIGH);
  digitalWrite(VentilHaut, HIGH);
  digitalWrite(VentilBas, HIGH);

  //Pin Arrosage
  pinMode(Arro1, OUTPUT);
  pinMode(Arro2, OUTPUT);
  pinMode(Evacuation, OUTPUT);
  digitalWrite(Arro1, HIGH);
  digitalWrite(Arro2, HIGH);
  digitalWrite(Evacuation, HIGH);

  //interupteur
  pinMode(interType, INPUT);
  pinMode(interLum, INPUT);
  pinMode(interONExt, INPUT);
  pinMode(interONVentil, INPUT);
  pinMode(interOFFExt, INPUT);
  pinMode(interONAro, INPUT);
  pinMode(interONVid, INPUT);
  pinMode(interOFFAro, INPUT);
  pinMode(inter9, INPUT);
  pinMode(inter10, INPUT);

  /***********************************************************************
    init des capteurs
  * *********************************************************************/

  //Capteur Temp
  Temp.begin();
  Temp.setResolution(12);

  //capteur Hum air
  dht.begin();

  //Capteur hum sol
  pinMode(HumidSolBas, INPUT);
  pinMode(HumidSolHaut, INPUT);

  //RTC
  Wire.begin();
}

void loop() {

  /***********************************************************************
    Gestion des capteurs
  * **********************************************************************/
  //Capteur Temp
  Temp.requestTemperatures();
  while (!Temp.isConversionComplete());
  float Temper = Temp.getTempC();

  //capteur Hum air
  float hum = dht.readHumidity();

  //Capteur hum sol

  //RTC
  bool h24, am;
  int heure = Clock.getHour(h24, am );
  int minu = Clock.getMinute();
  int HMin = heure * 60 + minu; // heure en minute

  Serial.print("H:");
  Serial.print(HMin / 60);
  Serial.print(":");
  Serial.print(HMin);
  Serial.print("  T:");
  Serial.print(Temper);
  Serial.print("  Hum:");
  Serial.println(hum);

  /***********************************************************************
    Gestion de la lumiere
   * *********************************************************************
  */
  if (newDay == 1) {
    newDay = 0;
    if (digitalRead(interType)) { //SI intertype ==1 alors Flo
      Serial.println("Floraison");
      HA = random (585, 615);
      HE = HA + 705 + random(15);
    }
    else {
      Serial.println("Croissance");
      HA = random (285, 315);
      HE = HA + 1065 + random(15);
    }
  }

  if (HMin == 0)
    newDay = 1;

  if (HMin >= HA & HMin < HE & digitalRead(interLum)) {
    digitalWrite(RougeHaut, LOW);
    digitalWrite(RougeBas, LOW);
    if (digitalRead(interType)) { //SI flo
      digitalWrite(BleuBas, LOW);
      digitalWrite(BleuHaut, LOW);
    }
  }
  else {
    digitalWrite(RougeHaut, HIGH);
    digitalWrite(RougeBas, HIGH);
    digitalWrite(BleuBas, HIGH);
    digitalWrite(BleuHaut, HIGH);
  }


  /***********************************************************************
    Gestion de l'extraction
  * *********************************************************************/
  if (Temper >= 26) {
    Serial.println("tmp >= 26 °C");
    tmpExtract = 1 ;
  }

  if (Temper < 25) {
    Serial.println("tmp <25 °C");
    tmpExtract = 0;
  }

  if (digitalRead(interType) == 0 & hum > 70) {
    Serial.println("interType est en croi et plus de 70% d'hum");
    humExtract = 1;
  }
  else if ( digitalRead(interType) == 0 & hum > 60) {
    Serial.println("interType est en flo et plus de 60 % d'hum");
    humExtract = 1;
  }
  else
    humExtract = 0;

  if (minu == 0 | minu == 1 | tmpExtract | humExtract | digitalRead(interONExt)) {
    Serial.println("Extracteur ON");
    digitalWrite(Extracteur, LOW);
  }
  else {
    Serial.println("Extracteur OFF");
    digitalWrite(Extracteur, HIGH);
  }


  /***********************************************************************
    Gestion de la ventilation
  * *********************************************************************/
  if (minu >= 55 | digitalRead(interONVentil) | tmpExtract | humExtract ) {
    Serial.println("Ventilation ON");
    digitalWrite(VentilHaut, LOW);
    digitalWrite(VentilBas, LOW);
  }
  else {
    Serial.println("Ventilation OFF");
    digitalWrite(VentilHaut, HIGH);
    digitalWrite(VentilBas, HIGH);
  }

  /***********************************************************************
    Gestion de l'arrosage
  * *********************************************************************/

  if ((HumidSolBas | HumidSolHaut) & digitalRead(interOFFAro) == 0 & digitalRead(interONAro) == 0) {
    Serial.println("Arrosage");
    digitalWrite(Arro1, LOW);
    digitalWrite(Arro2, LOW);
    delay(delaySecArro * 1000 );
    digitalWrite(Arro1, HIGH);
    digitalWrite(Arro2, HIGH);
    HeuArrosage = HMin;
  }

  if (HeuArrosage + 10 == HMin & digitalRead(interOFFAro) == 0 & digitalRead(interONVid) == 0) {
    Serial.println("Evacuation");
    digitalWrite(Evacuation, LOW);
    delay(delaySecEvacuation * 1000 );
    digitalWrite(Evacuation, HIGH);
  }

  if (digitalRead(interONVid)) {
    Serial.println("Evacuation manuel");
    digitalWrite(Evacuation, LOW);
  }
  else
    digitalWrite(Evacuation, HIGH);


  if (digitalRead(interONAro)) {
    Serial.println("Arrosage manuel");
    digitalWrite(Arro1, LOW);
    digitalWrite(Arro2, LOW);
  }
  else {
    digitalWrite(Arro1, HIGH);
    digitalWrite(Arro2, HIGH);
  }


  delay(500);

}
