SMS - Sensor Jarak - RTC - Arduino

#include
"Wire_h"
"LiquidCrystal_I2C_h"
"RTClib_h"
"SoftwareSerial_h"

LiquidCrystal_I2C lcd(0x3F, 16, 2);
RTC_DS1307 rtc;

int lastminute=0;
char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};

#define trigPin 2
#define echoPin 3
const int buzzer = 7; //buzzer to arduino pin 7
long duration, distance, distanceOld, TinggiAir;
long distanceA, distanceB,distanceC,distanceD,distanceE,distanceF;

int  dataMenitOld = 0;
int  MarkdataMenit = 0;
int  MarkdataJam = 0;
int errorloop = 0;

SoftwareSerial SIM800L(8, 9); // RX | TX
// Connect the SIM800L TX to Arduino pin 8 RX.
// Connect the SIM800L RX to Arduino pin 9 TX.

void setup() {
  // put your setup code here, to run once:
  pinMode(buzzer, OUTPUT); // Set buzzer - pin 7 as an output
 
  tone(buzzer, 2000); // Send 1KHz sound signal...
  delay(50);        // ...for 1 sec
  noTone(buzzer);     // Stop sound...
  delay(1000);
    lcd.begin();
    Serial.begin(57600);

  if (! rtc.begin()) {
    lcd.print("Couldn't find RTC");
    while (1);
  }

  if (! rtc.isrunning()) {
    lcd.print("RTC is NOT running!");
    // following line sets the RTC to the date & time this sketch was compiled
    // rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
    // This line sets the RTC with an explicit date & time, for example to set
    // January 21, 2014 at 3am you would call:
    // rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
  }

 delay(1000);
    SIM800L.begin(57600); 

    lcd.print("SIM800L started at 57600");
    delay(1000);
    lcd.print("Setup Complete!");
     lcd.setCursor(0,1); 
    lcd.print("SIM800L is Ready!");
 delay(1000);

lcd.clear();
lcd.print("www.wahyucf.com");
lcd.setCursor(0,1); 
lcd.print(" 087770-187770 ");
 delay(1000);

  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);

  
 //   lcd.print(distance);
 //   lcd.print(" cm");
 
 
 
 delay(5000);

     DateTime now = rtc.now();

    String thn=String(now.year(), DEC);
    Serial.print(thn);
    Serial.print('/');
    String bln=String(now.month(), DEC);
    Serial.print(bln);
    Serial.print('/');
    String hr=String(now.day(), DEC);
    Serial.print(hr);
    Serial.print(" (");
    Serial.print(daysOfTheWeek[now.dayOfTheWeek()]);
    Serial.print(") ");
    String dataJam=String(now.hour(), DEC);
    Serial.print(':');
    String dataMenit=String(now.minute(), DEC);
    Serial.print(':');
    String dataDetik=String(now.second(), DEC);
    Serial.println();

  digitalWrite(trigPin, LOW);  // Added this line
  delayMicroseconds(2); // Added this line
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10); // Added this line
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration/2) / 29.1; 
 
 distance = ukur(); 
  TinggiAir =100 - distance;

//String Tanggal = "\n"+ thn + '/'+ bln + '/'+ hr + " - "+ dataJam + ":" + dataMenit + ":" + dataDetik + "\n"+  "Level air: "  + String(TinggiAir) + " cm"+ "\n";
String Tanggal = "\n"+ thn + '/'+ bln + '/'+ hr + " - "+ dataJam + ":" + dataMenit + "\n"+  "Level air: "  + String(TinggiAir) + " cm"+ "\n";

    Serial.print(Tanggal);

 // String message = Tanggal + " - " + dataJam + ":" + dataMenit + ":" + dataDetik + " -" + " Jarak saat ini: ";  + String(distance) + " cm"+ "\n"; 

delay(5000);

 lcd.clear(); 
   lcd.print("Setup Complete!");
 lcd.setCursor(0,1);  
  lcd.print("Sending SMS...");
   
  //Set SMS format to ASCII
  SIM800L.write("AT+CSCS?\r\n");
  delay(1000);

  SIM800L.write("AT+CSCS=\"GSM\"\r\n");
  delay(1000);
  SIM800L.write((char)26);
  delay(1000);  
  SIM800L.write("AT+CMGF=1\r\n");
  delay(1000);
  //Send new SMS command and message number
  SIM800L.write("AT+CMGS=\"+6287770187770\"\r\n");
  delay(1000);   
  //Send SMS content
  SIM800L.write("Piranti Start: ");
  delay(1000);
  SIM800L.write(Tanggal.c_str());
  //Send Ctrl+Z / ESC to denote SMS message is complete
  SIM800L.write((char)26);
  delay(20000);
 
  SIM800L.write("AT+CSCS=\"GSM\"\r\n");
  delay(1000);
  SIM800L.write((char)26);
  delay(1000);  
  SIM800L.write("AT+CMGF=1\r\n");
  delay(1000);
  //Send new SMS command and message number
  SIM800L.write("AT+CMGS=\"+628977143153\"\r\n");
  delay(1000);   
  //Send SMS content
  SIM800L.write("Piranti Start: ");
  delay(1000);
  SIM800L.write(Tanggal.c_str());
  //Send Ctrl+Z / ESC to denote SMS message is complete
  SIM800L.write((char)26);
  delay(1000);
 
 
 
 
 
  
  lcd.clear();   
  lcd.print("Piranti Start");
 
    tone(buzzer, 2000); // Send 1KHz sound signal...
  delay(50);        // ...for 1 sec
  noTone(buzzer);     // Stop sound...
  delay(100);
    tone(buzzer, 2000); // Send 1KHz sound signal...
  delay(50);        // ...for 1 sec
  noTone(buzzer);     // Stop sound...
  delay(1000);
   
 delay(5000);
 
 
 
}

void loop() {
  // put your main code here, to run repeatedly:
    DateTime now = rtc.now();
    

    String thn=String(now.year(), DEC);
    Serial.print(thn);
    Serial.print('/');
    String bln=String(now.month(), DEC);
    Serial.print(bln);
    Serial.print('/');
    String hr=String(now.day(), DEC);
    Serial.print(hr);
    Serial.print(" (");
    Serial.print(daysOfTheWeek[now.dayOfTheWeek()]);
    Serial.print(") ");
    String dataJam=String(now.hour(), DEC);
    Serial.print(dataJam);
    int dataJamInt=dataJam.toInt();
    Serial.print(':');
    String dataMenit=String(now.minute(), DEC);
    Serial.print(dataMenit);
    int dataMenitInt=dataMenit.toInt();
   Serial.print(':');
   String dataDetik=String(now.second(), DEC);
    Serial.print(dataDetik);
    Serial.println();
 
 
    Serial.println(dataMenitInt);
    Serial.println(dataMenitOld);   
    

 distance = ukur(); 
  TinggiAir =100 - distance;

if ((distance >25)&&(errorloop!=0))
{
  errorloop=0;
}

if ((distance <=25)&&(errorloop<5)){

    lcd.setCursor(0,1); 
    lcd.print("Object To Close");
    errorloop=errorloop+1;

if ((distance ==25)){    
String Tanggal = thn + '/'+ bln + '/'+ hr ;     
     kirimsms("CekSensor","Jarak Obj", Tanggal, dataJam, dataMenit, dataDetik, distance, "- www.wahyucf.com -"); 
}

  } 

if (((dataJamInt == 17)||(dataJamInt==7))&&(dataMenitOld==0)){

  if(  MarkdataJam != dataJamInt)
{
  
String Tanggal = thn + '/'+ bln + '/'+ hr ;     
     kirimsms("Data", "Level air", Tanggal, dataJam, dataMenit, dataDetik, TinggiAir, "================"); 
    dataMenitOld = dataMenitInt;
    MarkdataJam = dataJamInt;

}

  } 
  
if ((dataMenitInt == (dataMenitOld + 30)) || (dataMenitInt == 0) || (dataMenitOld == 0)){
    dataMenitOld = dataMenitInt;

    

  if(  MarkdataMenit != dataMenitInt)
{
String Tanggal = thn + '/'+ bln + '/'+ hr ;     
     kirimsms("CekSMS", "Level air", Tanggal, dataJam, dataMenit, dataDetik, TinggiAir, "- www.wahyucf.com -"); 
    MarkdataMenit= dataMenitInt;
}

  } 

    
 if ((distanceOld!=distance) || (lastminute!=now.minute()))
 // if (now.second()!= lastminute) 
 {    
    lcd.clear();
    lcd.setCursor(0,0); 
    lcd.print(now.year(), DEC);
    lcd.print('/');
    lcd.print(now.month(), DEC);
    lcd.print('/');
    lcd.print(now.day(), DEC);
    

//    lcd.print(" (");
//    lcd.print(daysOfTheWeek[now.dayOfTheWeek()]);
//    lcd.print(") ");
    
    lcd.setCursor(11,0);
    lcd.print(now.hour(), DEC);
    lcd.print(':');
    lcd.print(now.minute(), DEC);
//    lcd.print(':');
//    lcd.print(now.second(), DEC);
//    lcd.setCursor(8,1);

    lcd.setCursor(0,1); 
    lcd.print("Level Air:");
    lcd.print(" ");
    lcd.print(TinggiAir);
    lcd.print("cm");

//    lcd.println();
    lastminute=now.minute();

    distanceOld=distance;

  }
}

void kirimsms(String dataTipe,String dataNote,String Tanggal, String dataJam, String dataMenit, String dataDetik, long distance,String Closer) 
{

    tone(buzzer, 2000); // Send 1KHz sound signal...
  delay(50);        // ...for 1 sec
  noTone(buzzer);     // Stop sound...
  delay(100);
    tone(buzzer, 2000); // Send 1KHz sound signal...
  delay(50);        // ...for 1 sec
  noTone(buzzer);     // Stop sound...

  
  
//String message = "\n"+dataTipe + "\n"+Tanggal + " - "+ dataJam + ":" + dataMenit + ":" + dataDetik + "\n"+ dataNote + ": "  + distance + " cm"+ "\n"+"\n"+Closer;

String message = "\n"+dataTipe + "\n"+Tanggal + " - "+ dataJam + ":" + dataMenit + "\n"+ dataNote + ": "  + String(TinggiAir) + " cm"+ "\n"+"\n"+Closer;
    Serial.print(message);

  lcd.clear(); 
  lcd.print("Tinggi air:");
  lcd.print(TinggiAir);
  lcd.print("cm");
  lcd.setCursor(0,1);  
  lcd.print("Sending SMS...");
   
  //Set SMS format to ASCII
  SIM800L.write("AT+CSCS?\r\n");
  delay(1000);
  SIM800L.write("AT+CSCS=\"GSM\"\r\n");
  delay(1000);
  SIM800L.write((char)26);
  delay(1000);  
  SIM800L.write("AT+CMGF=1\r\n");
  delay(1000);
  //Send new SMS command and message number
  SIM800L.write("AT+CMGS=\"+6287770187770\"\r\n");
  delay(1000);
  //Send SMS content
  SIM800L.write("Irigasi-ID ");
  delay(1000);
  SIM800L.write(message.c_str());
  //Send Ctrl+Z / ESC to denote SMS message is complete
  SIM800L.write((char)26);
  delay(20000);

  //Set SMS format to ASCII
  SIM800L.write("AT+CSCS?\r\n");
  delay(1000);
  SIM800L.write("AT+CSCS=\"GSM\"\r\n");
  delay(1000);
  SIM800L.write((char)26);
  delay(1000);  
  SIM800L.write("AT+CMGF=1\r\n");
  delay(1000);
  //Send new SMS command and message number
  SIM800L.write("AT+CMGS=\"+628977143153\"\r\n");
  delay(1000);
  //Send SMS content
  SIM800L.write("Irigasi-ID ");
  delay(1000);
  SIM800L.write(message.c_str());
  //Send Ctrl+Z / ESC to denote SMS message is complete
  SIM800L.write((char)26);
 delay(2000);

if(dataTipe == "Data") {

  delay(18000);
 
   //Set SMS format to ASCII
  SIM800L.write("AT+CSCS?\r\n");
  delay(1000);
  SIM800L.write("AT+CSCS=\"GSM\"\r\n");
  delay(1000);
  SIM800L.write((char)26);
  delay(1000);  
  SIM800L.write("AT+CMGF=1\r\n");
  delay(1000);
  //Send new SMS command and message number
  SIM800L.write("AT+CMGS=\"+628977143153\"\r\n");
  delay(1000);
  //Send SMS content
  SIM800L.write("Irigasi-ID ");
  delay(1000);
  SIM800L.write(message.c_str());
  //Send Ctrl+Z / ESC to denote SMS message is complete
  SIM800L.write((char)26);
  delay(2000);
 
  
}

}
long ukur () {
  

  distanceA = dist ();    
  delay(100);
  distanceB = dist ();  
  delay(100);
  distanceC = dist ();  
  delay(100);  
  distanceD = dist ();  
  delay(100); 
  distanceE = dist ();  
  delay(100); 
  distanceF = dist ();  
 
 
 if (distanceA == distanceF) {
   return distanceA;
 }  
    
 if (distanceA == distanceE) {
   return distanceA;
 }  
  
 if (distanceA == distanceD) {
   return distanceA;
 }

 if (distanceA == distanceC) {
   return distanceA;
 } 

 if (distanceA == distanceB) {
   return distanceA;
 } 

 if (distanceB == distanceF) {
   return distanceB;
 } 

 if (distanceB == distanceE) {
   return distanceB;
 } 
 
 if (distanceB == distanceD) {
   return distanceB;
 } 

 if (distanceB == distanceC) {
   return distanceB;
 } 

 if (distanceC == distanceF) {
   return distanceC;
 } 

 if (distanceC == distanceE) {
   return distanceC;
 }  

 if (distanceC == distanceD) {
   return distanceC;
 } 

 if (distanceD == distanceE) {
   return distanceD;
 }  

 if (distanceD == distanceE) {
   return distanceD;
 } 

 if (distanceE == distanceF) {
   return distanceE;
 } 

}

long dist () {
  digitalWrite(trigPin, LOW);  // Added this line
  delayMicroseconds(2); // Added this line
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10); // Added this line
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = ((duration) / 58.138)+4;  
  return distance;
}