

/* Comment this out to disable prints and save space */
//#define BLYNK_PRINT Serial


//#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <TimeLib.h>
#include <WidgetRTC.h>



// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "5I28lN8RWcLQLvaoRyv0RNuiuBVwy99K";

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "Livebox-7C2C";
char pass[] = "566C61F61F9C925C9F3D7D79E6";

WidgetRTC rtc;

BlynkTimer timer;


//************************************************************************
//                Variable Global perso
//************************************************************************
#define POMPE1 0
#define POMPE2 1
#define POMPE3 2
#define POMPE4 3

#define FACTEURML 635

#define INTER24H 1
#define INTER12H 2
#define INTER6H  3

int inter1, inter2, inter3, inter4;

const int PINPOMPE1 = 16;
const int PINPOMPE2 = 5;
const int PINPOMPE3 = 4;
const int PINPOMPE4 = 0;

float ML1 = 0.0;
float ML2 = 0.0;
float ML3 = 0.0;
float ML4 = 0.0;


float nbMLTest = 2.0;

bool AUTO1 = false;
bool AUTO2 = false;
bool AUTO3 = false;
bool AUTO4 = false;

#define H1 0*60
#define H2 6*60
#define H3 12*60
#define H4 18*60

//************************************************************************
//                Fonction
//************************************************************************
void Active1(float ml) {
  if (AUTO1) {
    char message[30];
    digitalWrite(PINPOMPE1, LOW);
    delay(FACTEURML * ml);
    digitalWrite(PINPOMPE1, HIGH);
    Serial.print("Distribue pompe1 : ");
    Serial.print(ml);
    Serial.println("ml");
    sprintf(message, "Distribution %.1fml de calcium", ml);
    //Blynk.notify(message);
  }
}


void Active2(float ml) {
  if (AUTO2) {
    char message[30];
    digitalWrite(PINPOMPE2, LOW);
    delay(FACTEURML * ml);
    digitalWrite(PINPOMPE2, HIGH);
    Serial.print("Distribue pompe2 : ");
    Serial.print(ml);
    Serial.println("ml");
    sprintf(message, "Distribution %.1fml de KH", ml);
    //Blynk.notify(message);
  }
}

void Active3(float ml) {
  if (AUTO3) {
    char message[30];
    digitalWrite(PINPOMPE3, LOW);
    delay(FACTEURML * ml);
    digitalWrite(PINPOMPE3, HIGH);
    Serial.print("Distribue pompe3 : ");
    Serial.print(ml);
    Serial.println("ml");
    sprintf(message, "Distribution %.1fml de Iode", ml);
    //Blynk.notify(message);
  }
}

void Active4(float ml) {
  if (AUTO4) {
    char message[30];
    digitalWrite(PINPOMPE4, LOW);
    delay(FACTEURML * ml);
    digitalWrite(PINPOMPE4, HIGH);
    Serial.print("Distribue pompe4 : ");
    Serial.print(ml);
    Serial.println("ml");
    sprintf(message, "Distribution %.1fml de Pompe4", ml);
    //Blynk.notify(message);
  }
}

void Ft_Timer() {
  int Hmin = hour() * 60 + minute();
  Serial.println(Hmin);
  switch (Hmin) {
    case ( H1):
      Active1(ML1);
      break;
    case ( H1 + 30 ):
      Active2(ML2);
      break;
    case ( H1 + 60 ):
      Active3(ML3);
      break;
    case ( H1 + 90 ):
      Active4(ML4);
      break;
    case H2:
      if (inter1 == INTER6H)
        Active1(ML1);
      break;
    case H2+30:
      if (inter2 == INTER6H)
        Active2(ML2);
      break;
    case H2+60:
      if (inter3 == INTER6H)
        Active3(ML3);
      break;
    case H2+90:
      if (inter4 == INTER6H)
        Active4(ML4);
      break;
    case H3:
      if (inter1 == INTER12H)
        Active1(ML1);
      break;
    case H3+30:
      if (inter2 == INTER12H)
        Active2(ML2);
      break;
    case H3+60:
      if (inter3 == INTER12H)
        Active3(ML3);
      break;
    case H3+90:
      if (inter4 == INTER12H)
        Active4(ML4);
      break;
    case H4:
      if (inter1 == INTER6H)
        Active1(ML1);
      break;
    case H4+30:
      if (inter2 == INTER6H)
        Active2(ML2);
      break;
    case H4+60:
      if (inter3 == INTER6H)
        Active3(ML3);
      break;
    case H4+90:
      if (inter4 == INTER6H)
        Active4(ML4);
      break;
    default:
      break;
  }
}




//************************************************************************
//                Fonction Blynk
//************************************************************************

BLYNK_CONNECTED() {
  Blynk.syncAll();
  rtc.begin();
}


//Quantité
BLYNK_WRITE(V0) {
  float ml = param.asFloat();
  if (ml < 10) {
    inter1 = INTER24H;
    ML1 = ml;
  }
  else if (ml < 30) {
    inter1 = INTER12H;
    ML1 = ml / 2;
  }
  else {
    inter1 = INTER6H;
    ML1 = ml / 4;
  }
}

BLYNK_WRITE(V1) {
  float ml = param.asFloat();
  if (ml < 10) {
    inter2 = INTER24H;
    ML2 = ml;
  }
  else if (ml < 30) {
    inter2 = INTER12H;
    ML2 = ml / 2;
  }
  else {
    inter2 = INTER6H;
    ML2 = ml / 4;
  }
}

BLYNK_WRITE(V2) {
  float ml = param.asFloat();
  if (ml < 10) {
    inter3 = INTER24H;
    ML3 = ml;
  }
  else if (ml < 30) {
    inter3 = INTER12H;
    ML3 = ml / 2;
  }
  else {
    inter3 = INTER6H;
    ML3 = ml / 4;
  }
}

BLYNK_WRITE(V3) {
  float ml = param.asFloat();
  if (ml < 10) {
    inter4 = INTER24H;
    ML4 = ml;
  }
  else if (ml < 30) {
    inter4 = INTER12H;
    ML4 = ml / 2;
  }
  else {
    inter4 = INTER6H;
    ML4 = ml / 4;
  }
}

//ON/OFF Auto
BLYNK_WRITE(V4) {
  if ( param.asInt()) {
    AUTO1 = true;
  }
  else
    AUTO1 = false;

}
BLYNK_WRITE(V5) {
  if ( param.asInt())
    AUTO2 = true;
  else
    AUTO2 = false;

}
BLYNK_WRITE(V6) {
  if ( param.asInt())
    AUTO3 = true;
  else
    AUTO3 = false;
}
BLYNK_WRITE(V7) {
  if ( param.asInt())
    AUTO4 = true;
  else
    AUTO4 = false;

}

//Forcé
BLYNK_WRITE(V12) {
  if ( param.asInt())
    Active1(nbMLTest);
}

BLYNK_WRITE(V13) {
  if ( param.asInt())
    Active2(nbMLTest);
}

BLYNK_WRITE(V14) {
  if ( param.asInt())
    Active3(nbMLTest);
}

BLYNK_WRITE(V15) {
  if ( param.asInt())
    Active4(nbMLTest);
}


void setup()
{
  // Debug console
  Serial.begin(9600);
  Serial.println("Start");

  //Pin Setup
  pinMode(PINPOMPE1, OUTPUT);
  pinMode(PINPOMPE2, OUTPUT);
  pinMode(PINPOMPE3, OUTPUT);
  pinMode(PINPOMPE4, OUTPUT);
  digitalWrite(PINPOMPE1, HIGH);
  digitalWrite(PINPOMPE2, HIGH);
  digitalWrite(PINPOMPE3, HIGH);
  digitalWrite(PINPOMPE4, HIGH);

  //Blynk.begin(auth, ssid, pass);
  // You can also specify server:
  //Blynk.begin(auth, ssid, pass, "blynk-cloud.com", 80);
  Blynk.begin(auth, ssid, pass, IPAddress(192, 168, 1, 18), 8081);

  setSyncInterval(10 * 60); // interval de syncro de la RTC (10 min)

  timer.setInterval(60000L, Ft_Timer);
}

void loop()
{
  Blynk.run();
  timer.run();
}
