//version gsm envoi sms avec bluetooth 07/02/2019
/* 
 * au demmarage les zones 1-2-3-4 sont masquees
 * les alarmes sont toutes a securite positive elles doivent etre bouclees a gnd, une rupture declenche l'envoi
 * au declenchement alarme depart sirene pour 180s 
 * 10s apres le depart sirene la lumiere s'allume
 * un voyant rouge fixe signale qu'il y a eu intrusion
 * 
 * touche # enregistre les 10 ch du telephone1 a appeler confirmation du chiffre, a chaque manip
 * touche * enregistre les 10 ch du telephone2 a appeler confirmation du chiffre, a chaque manip
 * touche A retour sans modif si selection par erreur
 * touche D arrete l'alarme si entree sans telephone (plus de batterie ou oubli)
 * touche 1 permet d'inclure exclure zone1
 * touche 2 permet d'Exclure exclure zone2
 * touche 3 permet d'Exclure exclure zone3
 * touche 4 permet d'inclure exclure zone4
 * touche 0 allume eteind le lcd
 * ATTENTION POUR TELECHARGER UN PROGRAMME DEBRANCHER LE HC-05
 * a la mise en marche alarme led rouge 5s puis s'eteind
 * a la mise arret led rouge clignte 2 fois led verte 2s
 * systeme a l'arret, si led rouge clignote c'est une zone ouverte (verifier avant de mettre en service alarme
 */
#include <EEPROM.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <SoftwareSerial.h>
SoftwareSerial sim800(7, 8);
SoftwareSerial blue(14, 15);
LiquidCrystal_I2C lcd(0x27, 16, 2); // set the LCD address to 0x27 for a 16 chars and 2 line display
//affectation des entrees/sorties

int sirene = 2;
int lumiere = 3;

int contact_alm = 11;//a la masse declenche si rupture

int input1 = 4;
int input2 = 5;
int input3 = 6;
int input4 = 10;
int coupsect = 17; //a3 coupure secteur
int efface = 9;   // raz eeprom

int led_rouge = 16; //a1
int led_verte = 12; 


byte izone = 0;
byte izone1 = 0;
byte ialm = 0;
byte mes = 0;
byte tempalm = 0;
byte imarche = 1; //0
int  imarche1 = 0;
byte imarche2 = 0;
byte itrans = 0;


byte almon=0;
int c=0;
byte urgence=0;
byte reserve=0;
byte selzonex = 0;//exclure les zones
byte selzone = 0;//inclure les zones
byte z1 = 0;
byte z2 = 0;
byte z3 = 0;
byte z4 = 0;
byte rz1 = 0;
byte rz2 = 0;
byte rz3 = 0;
byte rz4 = 0;

byte E_tel1 = 1;
byte E_tel2 = 1;

byte E_raz = 1;
byte ALM   = 0;
byte ALM1  = 0;
byte mes1  = 0;
byte SECT  = 0;
byte REL   = 0;
boolean result;
byte led     = 0;
byte pan     = 0;
//------------------------------REGLAGE DES TEMPOS Mini Maxi--------------------
byte etatRelaiSiren;
#define Temp_sirene 180

int interval = 0;
unsigned long previousMillis;


//-----------------keypad---------------
#include <Keypad_I2C.h>
#include <Keypad.h>
#define I2CADDR 0x3e //adresse du pcf8574
int v1 = 0;
const byte ROWS = 4; //4 rangees
const byte COLS = 4; //4 colonnes
char keys[ROWS][COLS] = {
  {'1', '2', '3', 'A'},
  {'4', '5', '6', 'B'},
  {'7', '8', '9', 'C'},
  {'*', '0', '#', 'D'}
};
// Digitran keypad, bit numbers of PCF8574 i/o port
byte rowPins[ROWS] = {7, 6, 5, 4}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {3, 2, 1, 0}; //connect to the column pinouts of the keypad

int num = 0;
Keypad_I2C kpd( makeKeymap(keys), rowPins, colPins, ROWS, COLS, I2CADDR, PCF8574 );
int i = 0;
int j = 0; //valide apres chaque chiffre
int k1 = 0;//compte 10 chiffres au numero de tel1
int k2 = 11; //compte 10 chiffres au numero de tel2
String numero1 ="";
String numero2 ="";
char numero[10];

String msg = String("");
int SmsContentFlag = 0;
String SIM_PIN_CODE = String( "XXXX" );

String transVERS("ALM V9bBLU");// version 

byte selcod  = 0;
byte selcod1 = 0;

char val; 

//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
void setup() {

  Wire.begin();
  lcd.begin();                      // initialize the lcd
    
  kpd.begin( makeKeymap(keys) );
  // Print a message to the LCD.
  lcd.backlight(); 
  lcd.setCursor(0, 0);
  lcd.print(transVERS);
  sim800.begin(19200);  // vitesse serial GPRS
  blue.begin(38400);  // vitesse serial bluetooth  
  Serial.begin(9600);  // vitesse serial moniteur
  pinMode(contact_alm, INPUT);
  digitalWrite(contact_alm, HIGH);//resistance au +
  pinMode(coupsect, INPUT); //coupure secteur
  digitalWrite(coupsect, HIGH);//resistance au +
  pinMode(efface, INPUT); //modif numero tel
  digitalWrite(efface, HIGH);//resistance au +
  pinMode(input1, INPUT); //modif numero tel
  digitalWrite(input1, HIGH);//resistance au +
  pinMode(input2, INPUT); //modif numero tel
  digitalWrite(input2, HIGH);//resistance au +
  pinMode(input3, INPUT); //modif numero tel
  digitalWrite(input3, HIGH);//resistance au +
  pinMode(input4, INPUT);
  digitalWrite(input4, HIGH);//resistance au +
  
  pinMode(lumiere, OUTPUT); 
  pinMode(sirene, OUTPUT); 
  digitalWrite(lumiere, HIGH);
  digitalWrite(sirene, HIGH);
  pinMode(led_rouge, OUTPUT);
  pinMode(led_verte, OUTPUT);

  //--------------MISE A ZERO DES RELAIS---------------
  etatRelaiSiren = HIGH; //low
  digitalWrite( sirene, etatRelaiSiren );
  digitalWrite( lumiere, etatRelaiSiren );
  //--------------lecture num telephone---------------

    for (int ii = 0; ii < 10; ii++) {
    numero[ii] = ( EEPROM.read(ii) + '0' );
    numero1 = numero1 +numero[ii];
    delay(20);
   }
   delay(100); 
   sim800.println("AT+CMGD=1,4"); // efface les sms
   delay(200);
   lcd.setCursor(6, 1);
   lcd.print(numero1);
   delay(2000);

    effL2();
    int ii = 0;
    for (int ii = 11; ii < 21; ii++) {
    numero[ii] = ( EEPROM.read(ii) + '0' );
    numero2 = numero2 +numero[ii];
   }
  delay(100); 
  sim800.println("AT+CMGD=1,4"); // efface les sms
  delay(200);
  lcd.setCursor(6, 1);
  lcd.print(numero2);
  delay(2000);
  selcod  = 0;

  effL2();
}

//?????????????????????????????????????????????????????????
void loop() {
  selec_clavier();
  teste_zone();  //*****avertissement alarme avant mise en service

  if ((selzone == 0)&&(selzonex == 0)) {
  fonction_secteur();
  }
  
 //lit bluetooth -------------------------------
   if( blue.available() >0 ) {
   val = blue.read();
//   Serial.println(val); 
   } 

if( val == '3' ) {
digitalWrite(lumiere,LOW); } 
else 
if( val == 'C' ) {digitalWrite(lumiere,HIGH);} 
 //--------------------------------------------  
   if(almon==0){    //si pas d'alarmes detectees alors marche ok 
    if (val=='1')  {

    result = true;urgence = 1;
      lcd.setCursor(6, 1);
      lcd.print("Transm");
      alarmeTransmit();
    
    delay(1000);
    val=0; 
    }
   }
        if (val=='2')  {

        result = false;reserve = 1;
        delay(1000);
        
      lcd.setCursor(6, 1);
      lcd.print("Transm");
      alarmeTransmit();
      delay(1000);
        
        val=0; 
    }
  
  //-------------------------------------------
  if (E_tel1 == LOW) {
    effL1();
    lcd.setCursor(0, 0);
    lcd.print("Tel 1 10Ch *Eff");
    delay(1000);
    enr_tel();
  }

  //-------------------------------------------
  if (E_tel2 == LOW) {
    effL1();     
    lcd.setCursor(0, 0);
    lcd.print("Tel 2 10Ch *Eff");
    delay(1000);
    enr_tel();
  }

  //--------------------------------------------
  E_raz = digitalRead(efface);
  if (E_raz == LOW) {
    effL1();
    lcd.setCursor(0, 0);
    lcd.print("RAZ EEPROM");
    delay(2000);
    for (int i = 0 ; i < EEPROM.length() ; i++) {
      EEPROM.write(i, 0);
    }

    lcd.setCursor(0, 0);
    lcd.print("Eeprom Vierge");
    effL1();    
    delay(2000);
  }
        

  //---------------fonction mise en service Alarme-----si arrete passe en marche------------------------------
  //if ((mes == LOW) && (imarche1 == LOW) && (selzone == 0)&& (selzonex == 0)) {
      if ((mes == LOW) && (imarche1 == LOW)) {

      //en cas de coupure alimentation reprend la derniere position
      c = EEPROM.read(23);//lecture de la memoire
      rz1 = EEPROM.read(60);
      rz2 = EEPROM.read(61);
      rz3 = EEPROM.read(62);
      rz4 = EEPROM.read(73);
      
      if (c == 1) {
        EEPROM.write(23, imarche1); //adresse en decimal,donnee
        result= true;urgence=1 ;
        delay(100);
       }
      lcd.backlight();
      lcd.setCursor(0, 0);
      lcd.print("ALM Arret   ");        digitalWrite(led_verte, HIGH);  
      effL2();
      }
      if ((result == true)&&(urgence==1)) { 
      mes=HIGH;digitalWrite(led_verte, LOW);
      imarche1 = HIGH;
      imarche2 = HIGH;
      digitalWrite(led_rouge, HIGH);delay(5000);digitalWrite(led_rouge, LOW);  lcd.backlight();
      lcd.setCursor(0, 0);
      lcd.print("ALM Marche ");
      effL2();
      EEPROM.write(23, imarche1); //adresse en decimal,donnee
      //digitalWrite(led_rouge, HIGH);delay(5000);digitalWrite(led_rouge, LOW);      
      delay(100);
      itrans == LOW;
      ialm = LOW;delay(100);  lcd.noBacklight();
      effL2();urgence=0;
      }
  
  // MHS Alarme--------------met hord service --------------------------------
  if (imarche1 == HIGH) {

    if ((result == false)&&(reserve==1)) { // if the arrays are equal, do something.
      digitalWrite(led_rouge, LOW);
      delay(100);
      digitalWrite(led_verte, HIGH);
      etatRelaiSiren = HIGH;//low
      digitalWrite( sirene, etatRelaiSiren );
      digitalWrite( lumiere, etatRelaiSiren );
      ialm = LOW;
      imarche1 = LOW;
      imarche2 = LOW;
      EEPROM.write(23, imarche1); //adresse en decimal,donnee
      mes = LOW;
      tempalm = LOW;
      itrans == LOW;    
      reserve = 0;delay(100);
      }
  }

  // alarme en cours ++++++++++++++  declenchement  +++++++++++++++++++++
  izone = digitalRead(contact_alm);
  z1 = digitalRead(input1);
  z2 = digitalRead(input2);
  z3 = digitalRead(input3);
  z4 = digitalRead(input4);  
  unsigned long currentMillis = millis() / 1000;
  if (((izone == HIGH) || (z1 == HIGH && rz1 == HIGH) || (z2 == HIGH && rz2 == HIGH) || (z3 == HIGH && rz3 == HIGH) || (z4 == HIGH && rz4 == HIGH)) && ((imarche1 == HIGH)  && (ialm == LOW) )) {
    itrans = LOW;
    unsigned long currentMillis = millis() / 1000;
    ialm = HIGH;
    previousMillis = currentMillis;
    delay(200);
  }
 if (ialm == HIGH) {
    affichetest(); //affiche temps ecoule sirene
    if (itrans == LOW) {
      lcd.setCursor(6, 1);
      lcd.print("Transm");
      alarmeTransmit();
      delay(1000);

      effL2();
      delay(200);
      
      if(SECT==LOW){       //+++++++++++++++++++++++++++++++++++++

      lcd.setCursor(6, 1);
      lcd.print("ALARM");
      
      etatRelaiSiren = LOW;//high      
      clignote();    
      digitalWrite( sirene, etatRelaiSiren );
      delay(10000);//allume la lumiere 10s apres la sirene
      digitalWrite( lumiere, etatRelaiSiren );
      }                   //+++++++++++++++++++++++++++++++++++++

      itrans = HIGH;
      delay(200);
      digitalWrite(led_rouge, HIGH); //au moins 1 declenchement alarme

    }

    if (itrans == HIGH) {

      if (currentMillis - previousMillis > Temp_sirene) {
        etatRelaiSiren = HIGH;//low
        digitalWrite( sirene, etatRelaiSiren );
        digitalWrite( lumiere, etatRelaiSiren );

        effL2();

        digitalWrite(led_rouge, HIGH); //au moins 1 declenchement alarme

        ialm = LOW;
        itrans = LOW;

        lcd.setCursor(13, 1);
        lcd.print("   ");
        delay(30000);//tempo si zone toujours en alarme 30 secondes
      }
    }

  }

}     //fin de loop---------------------------

//************************************************
void selec_clavier(){
    //Serial.print(E_tel1);Serial.print(E_tel2);Serial.print(selzone);Serial.print(selzonex);Serial.print(selbut);Serial.println(selcod);
      if (((E_tel1 == HIGH)||(E_tel2 == HIGH)) && (selzone == LOW) && (selzonex == LOW)&& (selcod == LOW)) {
      char key = kpd.getKey();

      if (key != NO_KEY) {
            lcd.backlight(); 
      if ((key == '0')&&(led==1)){
      lcd.backlight();  
      delay(500);    
      led=0;
      delay(500);
      key="";
      }
      if ((key == '0')&& (led==0)) {
      lcd.noBacklight();  
      delay(500);    
      led=1;
      delay(500);
      key="";     
      }
      if ((key == 'D')) { //arret alarme manuel si pas telephone
      lcd.backlight();  
      delay(500);    
       result = false;reserve = 1;     
      delay(500);
      key="";     
      }
      
//-------------m/a zone 1----------------
      if ((key == '1')&&(led==1)){//
      effL1();
      lcd.setCursor(0, 0);
      lcd.print("Z1 ACTIVE");
      rz1 = 1;delay(10);
      EEPROM.write(60, rz1); //adresse en decimal,donnee
        
      led=0;
      delay(500);
      key="";
      }
     if ((key == '1')&& (led==0)) {
      effL1();
      lcd.setCursor(0, 0);
      lcd.print("Z1 STOP");
      rz1 = 0;delay(10);
      EEPROM.write(60, rz1); //adresse en decimal,donnee

     led=1;
     delay(500);
     key="";     
     }
//-------------m/a zone 2----------------
      if ((key == '2')&&(led==1)){//
      effL1();
      lcd.setCursor(0, 0);
      lcd.print("Z2 ACTIVE");
      rz2 = 1;delay(10);
      EEPROM.write(61, rz2); //adresse en decimal,donnee
        
      led=0;
      delay(500);
      key="";
      }
     if ((key == '2')&& (led==0)) {
      effL1();
      lcd.setCursor(0, 0);
      lcd.print("Z2 STOP");
      rz2 = 0;delay(10);
      EEPROM.write(61, rz2); //adresse en decimal,donnee

     led=1;
     delay(500);
     key="";     
     }
//-------------m/a zone 3----------------
      if ((key == '3')&&(led==1)){//
      effL1();
      lcd.setCursor(0, 0);
      lcd.print("Z3 ACTIVE");
      rz3 = 1;delay(10);
      EEPROM.write(62, rz3); //adresse en decimal,donnee
        
      led=0;
      delay(500);
      key="";
      }
     if ((key == '3')&& (led==0)) {
      effL1();      
      lcd.setCursor(0, 0);
      lcd.print("Z3 STOP");
      rz3 = 0;delay(10);
      EEPROM.write(62, rz3); //adresse en decimal,donnee

     led=1;
     delay(500);
     key="";     
     }
//-------------m/a zone 4----------------
     if ((key == '4')&&(pan==1)){
      rz4=0;  
      EEPROM.write(73, rz4); //adresse en decimal,donnee
      effL1();
      lcd.setCursor(0, 0);
      lcd.print("Z4 STOP");
      
      delay(500);    pan=0;delay(500);key="";
      }
     if ((key == '4')&& (pan==0)) {
     rz4=1;
     EEPROM.write(73, rz4);   
      effL1();
      lcd.setCursor(0, 0);
      lcd.print("Z4 ACTIVE");
     
     delay(500);    pan=1;delay(500);key="";
     }
     
//---------------
      // enregistre telephone1
      if (key == '*') {
        E_tel1 = LOW;E_tel2 = HIGH;

      }
      // enregistre telephone2
      if (key == '#') {
      E_tel2 = LOW;E_tel1 = HIGH;

      }
    }
  }
}
//************************************************
void teste_zone(){
  z1 = digitalRead(input1);
  z2 = digitalRead(input2);
  z3 = digitalRead(input3);
  z4 = digitalRead(input4);
  lcd.setCursor(0, 1);
  lcd.print("A");//autoprotection
  delay(10);selec_clavier(); delay(10);
  almon=0;    
  if (rz1 == HIGH) {
    lcd.setCursor(1, 1);
    lcd.print("1");
  }
    delay(10);selec_clavier(); delay(10);
  if (rz2 == HIGH) {
    lcd.setCursor(2, 1);
    lcd.print("2");
  }
    delay(10);selec_clavier(); delay(10);
  if (rz3 == HIGH) {
    lcd.setCursor(3, 1);
    lcd.print("3");
  }
    delay(10);selec_clavier(); delay(10);  
  if (rz4 == HIGH) {
    lcd.setCursor(4, 1);
    lcd.print("4");
  }
  
    delay(50);selec_clavier(); delay(50);  
    
  if (rz1 == LOW) {
    lcd.setCursor(1, 1);
    lcd.print(" ");
  }
  if (rz2 == LOW) {
    lcd.setCursor(2, 1);
    lcd.print(" ");
  }
  if (rz3 == LOW) {
    lcd.setCursor(3, 1);
    lcd.print(" ");
  }
  if (rz4 == LOW) {
    lcd.setCursor(4, 1);
    lcd.print(" ");
  }

  //---------------------------------
//  if ((selzone == 0)&&(selzonex == 0)) {
    izone = digitalRead(contact_alm);
 //     lcd.setCursor(7, 1);
  //    lcd.print("D");

    if ((izone == HIGH) && (imarche2 == LOW)) { // clignote attention une alm prete
      lcd.setCursor(7, 1);
      lcd.print("D");
      lcd.setCursor(8, 1);
      lcd.print("A");almon=1;
      clignote();
    }
    delay(50);selec_clavier(); delay(50);
    if ((z1 == 1) && (rz1 == HIGH) && (imarche2 == LOW)) {
      lcd.setCursor(7, 1);
     lcd.print("D");
      lcd.setCursor(9, 1);
      lcd.print("1");almon=1;
      clignote();
    }
    delay(50);selec_clavier(); delay(50);
    if ((z2 == 1) && (rz2 == HIGH) && (imarche2 == LOW)) {
      lcd.setCursor(7, 1);
      lcd.print("D");
      lcd.setCursor(10, 1);
      lcd.print("2");almon=1;
      clignote();
    }
    delay(50);selec_clavier(); delay(50);  
    if ((z3 == 1) && (rz3 == HIGH) && (imarche2 == LOW)) {
      lcd.setCursor(7, 1);
      lcd.print("D");
      lcd.setCursor(11, 1);
      lcd.print("3");almon=1;
      clignote();
    }
     delay(50);selec_clavier(); delay(50);  
    if ((z4 == 1) && (rz4 == HIGH) && (imarche2 == LOW)) {
      lcd.setCursor(7, 1);
      lcd.print("D");
      lcd.setCursor(12, 1);
      lcd.print("4");almon=1;
      clignote();
    }
   
//  }
 }
 //************************************************
 void   fonction_secteur(){
    SECT = digitalRead(coupsect); //coupure secteur
  if ((SECT == HIGH) && (REL == LOW)) {
    REL = HIGH;
    alarmeTransmit(); 
    delay(2000);
  }
  if ((SECT == LOW) && (REL == HIGH)) {
    REL = LOW;
    alarmeTransmit();
    delay(2000);
  }
  }
  
//------------------ROUTINES******************

void affichetest() {

  unsigned long currentMillis = millis() / 1000;
  lcd.setCursor(13, 1);
  lcd.print(currentMillis - previousMillis);
}

//-------------enregistrement numero telephone--
void enr_tel() {
      if ((E_tel1 == LOW)||(E_tel2 == LOW)) {
  v1 = GetNumber();
  }
  }

int GetNumber(){
  int num;
  char key1 = kpd.getKey();

  while (key1 != '#')
  {

    switch (key1)
    {
      case NO_KEY:
        break;

      case '0': case '1': case '2': case '3': case '4':
      case '5': case '6': case '7': case '8': case '9':
    //    num = (num * 10) + (key1 - '0');
        num = (key1 - '0');
        j++;
        break;


      case '*': //efface
        num = 0;
        k1 = 0;
        k2 = 11;
        effL1();
        lcd.setCursor(0, 0);
        lcd.print("Recommencez Tout");
        break;
        
      case 'A': //pas de modif
      if(E_tel1==LOW){
lecture_numero1(); 
      }    
      if(E_tel2==LOW){
lecture_numero2(); 
      }    
        return;
       
      }


    key1 = kpd.getKey();
    if (j == 1) {
      key1 = '#';
      j = 0;
    }

  }
  effL2();
  lcd.setCursor(15, 0);
  lcd.print(num);
  delay(200);
 
if(E_tel1==LOW){
  //Serial.print(k1);Serial.print(" ");Serial.println(num);
  EEPROM.write(k1, num); //adresse en decimal,donnee
  delay(200);
  k1++;

  if (k1 == 10) {
    lecture_numero1();
  }
}
if(E_tel2==LOW){
 // Serial.print(k2);Serial.print(" ");Serial.println(num);
  EEPROM.write(k2, num); //adresse en decimal,donnee
  delay(200);
  k2++;

  if (k2 == 21) {
    lecture_numero2();
  }
}

  return num;
}

//**************************************
void lecture_numero1() {
  effL1();
  effL2();
  numero1 ="";
  numero2 ="";
    for (int ii = 0; ii < 10; ii++) {
    numero[ii] = ( EEPROM.read(ii) + '0' );
    numero1 = numero1 +numero[ii];
    delay(20);
   }  
  delay(200); 
  sim800.println("AT+CMGD=1,4"); // efface les sms
  delay(200);
  lcd.setCursor(0, 0);
  lcd.print("Initialise 1..");
  delay(2000);
  lcd.setCursor(6, 1);
  lcd.print(numero1);//Serial.println(numero1);
  delay(2000);
  sim800.println("AT+CMGD=1,4");
    effL1();      effL2();  
    lcd.setCursor(0, 0);
    lcd.print(transVERS);
    k1=0;
    E_tel1 = HIGH;   
    char key="";
    delay(1000);      lcd.noBacklight(); 
    selec_clavier();
}

//**************************************
void lecture_numero2() {
  effL1();
  effL2();
     numero1 ="";
     numero2 ="";
     for (int ii = 11; ii < 21; ii++) {
     numero[ii] = ( EEPROM.read(ii) + '0' );
     numero2 = numero2 +numero[ii];
   }  
  delay(200); 
  sim800.println("AT+CMGD=1,4"); // efface les sms
  delay(200);
  lcd.setCursor(0, 0);
  lcd.print("Initialise 2..");
  delay(2000);
  
  lcd.setCursor(6, 1);
  lcd.print(numero2);//Serial.println(numero2);
  delay(2000);
  sim800.println("AT+CMGD=1,4");
    effL1();      effL2();  
    k2=11;
    E_tel2 = HIGH;
    char key="";
    lcd.setCursor(0, 0);
    lcd.print(transVERS);
    selcod  = 0;
    selzone  = 0;
    selzonex  = 0;
    delay(1000);lcd.noBacklight(); 
}
//**************************************
void effL1() {
  lcd.setCursor(0, 0);
  lcd.print("                ");
          
}
void effL2() {
  lcd.setCursor(5, 1);
  lcd.print("            ");

}
//**************************************
void clignote() {
  digitalWrite(led_rouge, HIGH);
  delay(1000);
  digitalWrite(led_rouge, LOW);
  delay(1000);
}


//*****************TRANSMISSION******************
  void  alarmeTransmit() {
  effL1();   
  lcd.setCursor(0, 0);      
  lcd.print(numero1);
  sim800.println("AT+CMGF=1"); // SMS en mode texte
  delay(50);      
  sim800.print("AT+CMGS=\""); sim800.print(numero1); sim800.println("\"");
  delay(200);    // Il est indispensable de faire une pause entre le numero et le corps du message
  messageSMS();
  sim800.write(26); // Caractere de fin 26 <Ctrl-Z>
  sim800.println();
  delay(3000);
  effL1();   
  lcd.setCursor(0, 0);      
  lcd.print(numero2);
  sim800.println("AT+CMGF=1"); // SMS en mode texte
  delay(50);  
  sim800.print("AT+CMGS=\""); sim800.print(numero2); sim800.println("\"");
  delay(200);    // Il est indispensable de faire une pause entre le numero et le corps du message
  messageSMS();
  sim800.write(26); // Caractere de fin 26 <Ctrl-Z>
  sim800.println();
  delay(100);
  effL1();
  effL2();
  }

void messageSMS(){
  if(izone==HIGH){
  sim800.print("ALM AP\r");
  }  
  if(z1 == HIGH && rz1 == HIGH){
  sim800.print("PORTE\r");
  }
  if(z2 == HIGH && rz2 == HIGH){
  sim800.print("CHAMBRES\r");
  }
  if(z3 == HIGH && rz3 == HIGH){
  sim800.print("SEJOUR\r");
  }
  if(z4 == HIGH && rz4 == HIGH){
  sim800.print("CAVE\r");
  }
  if(REL == HIGH){
  sim800.print("COUPURE SECT\r");
  lcd.setCursor(6, 1);
  lcd.print("SECT OFF ");
  }
  if(REL == LOW){
  sim800.print("SECTEUR OK\r");
  lcd.setCursor(6, 1);
  lcd.print("SECT ON ");
  }
  
  if(result == false&&reserve == 1){
  sim800.print("ARRET ALM\r");
 }
  if(result == true&&urgence == 1){
  sim800.print("MARCHE ALM\r");
 }

    
}
