
#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 FloBas     = 2;
const int FullBas    = 3;
const int CroBas     = 4;

const int FloHaut     = 5;
const int FullHaut    = 6;
const int CroHaut     = 7;

int PuissanceMax = 230;


//relay Ventilation
const int DehumPin        = 44;
const int Extracteur      = 46;
const int VentilHaut      = 48;
const int VentilBas       = 50;

//relay Arrosage
const int Arro1            = 10; // Bas
const int Arro2            = 11; //Haut
const int Evacuation      = 12;
const int Melange         = 13;

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

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

//capteur Hum air
#define DHTTYPE DHT22
#define DHTPIN 17
DHT dht(DHTPIN, DHTTYPE);

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

//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
int SolBas, SolHaut;
int HeuArrosageMatin = 30;// 30 min apres l'allumage
int HeuArrosagesoir = 690; // 30 min avant la fi de l'allumage

int recup;
int timerecup;
/***********************************************************************
     Setup
 **********************************************************************/

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

  /***********************************************************************
    init des pins
  * *********************************************************************/
  //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(Melange, OUTPUT);
  pinMode(Evacuation, OUTPUT);
  digitalWrite(Arro1, HIGH);
  digitalWrite(Arro2, HIGH);
  digitalWrite(Melange, 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);

  pinMode(NiveauEauRecup, INPUT);
  pinMode(NiveauEauAqua, 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
  SolBas = !digitalRead(HumidSolBas);
  SolHaut = !digitalRead(HumidSolHaut);

  
    /*Serial.println(digitalRead(interType));
    Serial.println(digitalRead(interLum));
    Serial.println(digitalRead(interONExt));
    Serial.println(digitalRead(interONVentil));
    Serial.println(digitalRead(interOFFExt));
    Serial.println(digitalRead(interONAro));
    Serial.println(digitalRead(interONVid));
    Serial.println(digitalRead(interOFFAro));
    Serial.println(digitalRead(inter9));
    Serial.println(digitalRead(inter10));*/

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

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

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

  if (HMin == 0) {
    newDay = 1;
    recup = 0;
    digitalWrite(Evacuation, HIGH);
  }



  if ((HMin >= HA & HMin < HE)  & digitalRead(interLum)) {
    Serial.print("Allumage ");
    analogWrite(FullBas, PuissanceMax);
    analogWrite(FullHaut, PuissanceMax);
    if (1) { //SI flo
      Serial.println("Flo");
      analogWrite(FloHaut, PuissanceMax);
      analogWrite(FloBas, PuissanceMax);
      analogWrite(CroHaut, 0);
      analogWrite(CroBas, 0);
    }
    else {
      Serial.println("Cro");
      analogWrite(CroHaut, PuissanceMax);
      analogWrite(CroBas, PuissanceMax);
      analogWrite(FloBas, 0);
      analogWrite(FloHaut, 0);
    }

  }
  else {
    Serial.println("Extinxtion");
    analogWrite(FullBas, 0);
    analogWrite(FloBas, 0);
    analogWrite(CroBas, 0);
    analogWrite(FullHaut, 0);
    analogWrite(FloHaut, 0);
    analogWrite(CroHaut, 0);
  }


  /***********************************************************************
    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 > 75) {
    Serial.println("interType est en croi et plus de 75% d'hum");
    humExtract = 1;
    }
    else if ( digitalRead(interType) == 1 & hum > 60) {
    Serial.println("interType est en flo et plus de 60 % d'hum");
    humExtract = 1;
    }
    else if ( digitalRead(interType) == 0 & hum < 73) {
    humExtract = 0;
    }
    else if ( hum < 58) {
    humExtract = 0;
    }*/


  if ((minu == 0 | minu == 1 | tmpExtract | humExtract | digitalRead(interONExt)) & digitalRead(interOFFExt)) {
    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 (digitalRead(inter9) || (HMin == HeuArrosageMatin || HMin == HeuArrosagesoir)) { //On arrose 1min en bas
    Serial.println("Arrosage1");
    digitalWrite(Arro1, LOW);
  }
  else {
    digitalWrite(Arro1, HIGH);
  }

  if (digitalRead(inter10) || (HMin == HeuArrosageMatin + 1 || HMin == HeuArrosagesoir + 1)) { // puis 1 min en haut
    Serial.println("Arrosage2");
    digitalWrite(Arro2, LOW);
  }
  else {
    digitalWrite(Arro2, HIGH);
  }

  //Melange                   // On mélange 5min avant les arrosage
  if ((HMin >= HeuArrosageMatin - 5 )&& ( HMin < HeuArrosageMatin )) {
    Serial.println("Melange");
    digitalWrite(Melange, LOW);
  }
  else {
    digitalWrite(Melange, HIGH);
  }

  /***********************************************************************
    Gestion de l'extraction
  * *********************************************************************/
  if (digitalRead(NiveauEauRecup) & recup == 0) {
    Serial.println("HAUT");
    recup = 1;
    timerecup = Hsec;
  }

  if (digitalRead(NiveauEauRecup) == 0 & recup == 1) {
    recup = 0;
    timerecup = Hsec;
  }

  if (recup & Hsec >= timerecup + 40) {
    Serial.println("Evacuation");
    digitalWrite(Evacuation, LOW);
  }
  if ((recup == 0 &  Hsec >= timerecup + 5 ) & digitalRead(interONVid) == 0) {
    Serial.println("Evacuation off");
    digitalWrite(Evacuation, HIGH);
  }

  if (digitalRead(interONVid)) {
    Serial.println("Evacuation manu");
    digitalWrite(Evacuation, LOW);
    timerecup = Hsec;
  }

  /***********************************************************************
    FIN
  * *********************************************************************/
  Serial.println("");
  Serial.println("");

  delay(500);

}
