Skip to content
Khomi-auto-3.ino 5.76 KiB
Newer Older
Mamadou diallo's avatar
Mamadou diallo committed
// Date and time functions using a DS1307 RTC connected via I2C and Wire lib
#include <Wire.h>
#include "RTClib.h"

#include <LCD.h>
#include <LiquidCrystal_I2C.h>

// Definition des pins des capteurs et actionneurs
Mamadou diallo's avatar
Mamadou diallo committed
#define capteur_hum_sol A1    //capteur humidite sol a connecter sur le A0
Mamadou diallo's avatar
Mamadou diallo committed
#define pin_hum_sol 2         //connecter le + du capteur humidite sol 
#define pompe 3               //connecter le S du relais sur la pin 3, il controle la pompe
#define electrovanne 4        //connecter le S du relais sur la pin 4, il controle l'electrovanne
#define capteur_reservoir 5   //connecter le capteur de niveau d'eau 

//Déclaration des variables du programme
int valeur_hum_sol = 0;  // on stocke les valeurs lu par le capteur d'humidité sol
Mamadou diallo's avatar
Mamadou diallo committed
int heure = 19;
int minut = 9;

int Switch = 10; //switch boutton poussoir encoder
byte portA = 11; //encoder     invertion fritzing 6
byte portB = 9; // encoder
byte etat = 0;
bool etat_switch = false;

Mamadou diallo's avatar
Mamadou diallo committed

RTC_DS1307 rtc;
char daysOfTheWeek[7][12] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};

LiquidCrystal_I2C  lcd(0x3F, 2, 1, 0, 4, 5, 6, 7); // 0x27 is the I2C bus address for an unmodified backpack


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

  // activate LCD module
  lcd.begin (16, 2); // for 16 x 2 LCD module
  lcd.setBacklightPin(3, POSITIVE);
  lcd.setBacklight(HIGH);
  lcd.clear();
  lcd.setCursor(3, 0);
  lcd.print("KHomiAuto");
  delay(1000);
  lcd.clear();
Mamadou diallo's avatar
Mamadou diallo committed

  pinMode(capteur_hum_sol, INPUT);
  pinMode(capteur_reservoir, INPUT);
  pinMode(portA, INPUT);
  pinMode(portB, INPUT);
  pinMode(Switch, INPUT_PULLUP);
Mamadou diallo's avatar
Mamadou diallo committed
  pinMode(pin_hum_sol, OUTPUT);
  pinMode(pompe, OUTPUT);
  pinMode(electrovanne, OUTPUT);
  attachInterrupt(0, control, CHANGE);
  attachInterrupt(1, control, CHANGE);
Mamadou diallo's avatar
Mamadou diallo committed

  ////////////////////////////////////// configuration de l'horloge //////////////////////////////////////
  if (! rtc.begin())
  {
    Serial.print("Couldn't find RTC");
    while (1);
  }

  if (! rtc.isrunning())
  {
    Serial.print("RTC is NOT running!");
  }

  rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));//auto update from computer time
  //rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));// configurer manuellement l'heure

  ////////////////////////////////////////////////////////////////////////////////////////////////////////
}

void loop ()
{
  // on lit l'heure sur le rtc
  DateTime now = rtc.now();
  //on lance le debug pour verifier si il ya pas de probleme
Mamadou diallo's avatar
Mamadou diallo committed
 // Debug();
Mamadou diallo's avatar
Mamadou diallo committed
  affiche_lcd();

  //on test si il est heure d'arroser
Mamadou diallo's avatar
Mamadou diallo committed
  if ((now.hour() == heure ) && (now.minute() == minut))
Mamadou diallo's avatar
Mamadou diallo committed
  {
    if ( digitalRead(capteur_reservoir) == 1) // on test si le reservoir d'eau est remplit
    {
      arroser();
Mamadou diallo's avatar
Mamadou diallo committed
      lcd.setCursor(0, 1);
      lcd.print("fin arrosage");
      
Mamadou diallo's avatar
Mamadou diallo committed
    }
    else // sinon on envoie un message alert que le reservoir est vide
    {
Mamadou diallo's avatar
Mamadou diallo committed

      lcd.setCursor(0, 1);
      lcd.print("reservoir vide");
Mamadou diallo's avatar
Mamadou diallo committed
    }
  }
}

////////////////////////////////////// fonction arroser  //////////////////////////////////////

void arroser () {

  digitalWrite(pin_hum_sol, HIGH);  // on allume le capteur

  do {
    //on lit 10 fois le capteur
    for (int i = 0; i < 10; i++)
    {
      valeur_hum_sol = analogRead(capteur_hum_sol); // on lit la valeur analogique du capteur

    }
    ///////////////////// on test le sol si il est mouille ou pas et on action ou pas les moteurs  /////////////////////
    if (valeur_hum_sol > 500 )
    {
      digitalWrite(electrovanne, HIGH); // on allume electrovanne
      delay(500);                       // on attend 500ms
      digitalWrite(pompe, HIGH);        // on allume la pompe
    }

  }
  while (valeur_hum_sol > 500);         // tant que le sol est sec on arrose


  if (valeur_hum_sol < 500 )            // si le sol est mouille
  {
    digitalWrite(electrovanne, LOW);    // on eteind l'electrovanne
    digitalWrite(pompe, LOW);           // on eteind la pompe
    digitalWrite(pin_hum_sol, LOW);     // on eteind le capteur

  }
}

void control() {
  etat = etat << 1 | digitalRead(portA);
  etat = etat << 1 | digitalRead(portB);

  byte etat_test = (etat | B11110000) - B11110000;//test des 4 bits de poids faible
  //Serial.println(etat_test,BIN);//débug : visualisation de la séquence


  //bouton poussoir
  if (digitalRead(Switch) == false)
  {
    etat_switch = !etat_switch;
  }

Mamadou diallo's avatar
Mamadou diallo committed
  // heure configuration
  if (( etat_test == B0111 ) && (etat_switch == 0))
Mamadou diallo's avatar
Mamadou diallo committed
  else if (( etat_test == B1011 ) && (etat_switch == 0))
  {
    heure--;

    if ( heure < 0)
    {
      heure = 00;
    }
  }
Mamadou diallo's avatar
Mamadou diallo committed
  // minute configurration
  if (( etat_test == B0111 ) && (etat_switch == 1))
  {
    minut++;
  }
  else if (( etat_test == B1011 ) && (etat_switch == 1))
  {
    minut--;

    if ( minut < 0)
    {
      minut = 00;
    }
  }

  else if (heure == 25)
  {
    heure = 00;
  }
  else if (minut == 61)
  {
Mamadou diallo's avatar
Mamadou diallo committed
    minut = 00;
  }

}

////////////////////////////////////// fonction affichage  //////////////////////////////////////
Mamadou diallo's avatar
Mamadou diallo committed

void affiche_lcd()
{
  DateTime now = rtc.now();

  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("H=");
  lcd.print(heure);
  lcd.setCursor(7, 0);
  lcd.print("M=");
  lcd.print(minut);

Mamadou diallo's avatar
Mamadou diallo committed
  /* lcd.setCursor(2, 1);
    lcd.println(etat_switch);*/
////////////////////////////////////// Débug //////////////////////////////////////

Mamadou diallo's avatar
Mamadou diallo committed
void Debug()
{
  DateTime now = rtc.now();

  Serial.print(now.hour());
  Serial.print(':');
  Serial.print(now.minute());
  Serial.print(':');
  Serial.print(now.second());
  Serial.print("   ");

  Serial.print(daysOfTheWeek[now.dayOfTheWeek()]);
  Serial.print(" ,");
  Serial.print(now.day());
  Serial.print('/');
  Serial.print(now.month());
  Serial.print('/');
  Serial.print(now.year());

  Serial.print("       valeur humidite sol     ");
  Serial.print(valeur_hum_sol);

  Serial.print("      heure          ");
  Serial.println(heure);