#include <SoftwareSerial.h>
#include <avr/wdt.h>
#include <Wire.h>
#include "RTClib.h"
RTC_DS1307 RTC;
SoftwareSerial gprsSerial(7, 8);


const byte sump = 2;
const byte biofilter = 3;
const byte POWER = 4;
const byte IBC_TOP = 10;
const byte IBC_BOTTOM = 11;
unsigned long CURRENT_A;
unsigned long PREVIOUS_A;
unsigned long CURRENT_B;
unsigned long PREVIOUS_B;
unsigned long CURRENT_C;
unsigned long PREVIOUS_C;
unsigned long CURRENT_D;
unsigned long PREVIOUS_D;

const unsigned long interval =   1800000;
const unsigned long interval2 =   15000;

byte POWER_STATE = 0;
byte IBC_TOP_STATE = 0;
byte IBC_BOTTOM_STATE = 0;
byte SUMP_STATE = 0;
byte BIOFILTER_STATE = 0;

const char start[] = "Hi Chris, system is starting";
const char bio_high[] = "BIOFILTER HIGH";
const char bio_ok[] = "BIOFILTER OK";
const char sump_high[] = "SUMP LEVEL HIGH";
const char sump_ok[] = "SUMP LEVEL OK";
const char ibc_high[] = "3A LEVEL HIGH";
const char ibc_ok[] = "3A LEVEL OK";
const char ibc_low[] = "3A LEVEL LOW";
const char power_on[] = "POWER ON";
const char power_off[] = "POWER OFF";

void setup ()
{
  Serial.begin(19200);
  Wire.begin();
  RTC.begin();
  if (! RTC.isrunning()) {
    Serial.println("RTC is NOT running!");
    // following line sets the RTC to the date & time this sketch was compiled
    RTC.adjust(DateTime(__DATE__, __TIME__));
  }
  wdt_enable(WDTO_8S);
  pinMode(2, INPUT_PULLUP);
  pinMode(3, INPUT_PULLUP);
  pinMode(4, INPUT_PULLUP);
  pinMode(10, INPUT_PULLUP);
  pinMode(11, INPUT_PULLUP);
  gprsSerial.begin(19200); // GPRS shield baud rate
  delay(5000);
  wdt_reset();
  gprsSerial.print("AT+CMGF=1\r"); // Set the shield to SMS mode
  delay(5000);
  wdt_reset();
  delay(5000);
  wdt_reset();
  SMS(start);
  wdt_reset();
}
void loop ()
{
  wdt_reset();
  delay(2000);
  DateTime now = RTC.now();
  Serial.print(now.year(), DEC);
  Serial.print('/');
  Serial.print(now.month(), DEC);
  Serial.print('/');
  Serial.print(now.day(), DEC);
  Serial.print(' ');
  if (now.hour() < 10) {
    Serial.print("0");
  }
  Serial.print(now.hour(), DEC);
  Serial.print(':');
  if (now.minute() < 10) {
    Serial.print("0");
  }
  Serial.print(now.minute(), DEC);
  Serial.print(':');
  if (now.second() < 10) {
    Serial.print("0");
  }
  Serial.print(now.second(), DEC);
  Serial.println();
  ////////////////////////////////////////////////

  if (digitalRead (sump) == LOW)
  {


    if (SUMP_STATE == 1)
    {
      Serial.println ("Sump OK, SUMPSTATE = 1");
      PREVIOUS_A = 0;
      SUMP_STATE = 1;
      delay (100);
    }

    if  (SUMP_STATE == 0)
    {
      CURRENT_A = millis();
      Serial.println ("Sump OK, SUMPSTATE = 0 ");
      if (CURRENT_A-PREVIOUS_A>interval2){
      SMS(sump_ok);
      SUMP_STATE = 1;
      delay (100);
      }
    }
  }

  if (digitalRead (sump) == HIGH)
  {
    Serial.println ("SUMP LEVEL HIGH.");
    CURRENT_A = millis();
    if (CURRENT_A - PREVIOUS_A > interval) {
      SMS(sump_high);
      SUMP_STATE = 0;
      PREVIOUS_A = millis();
      delay (100);
    } else {
      Serial.println("INTERVAL TOO SHORT TO SEND SMS");
    }
  }
  /////////////////////////////////////////////////
  if (digitalRead (biofilter) == LOW)
  {
    if (BIOFILTER_STATE == 1)
    {
      Serial.println ("BIO OK, BIOSTATE = 1");
      PREVIOUS_B = 0;
      BIOFILTER_STATE = 1;
      delay (100);
    }

    if  (BIOFILTER_STATE == 0)
    {
      CURRENT_B = millis();
      Serial.println ("BIO OK, BIOSTATE = 0");
      if (CURRENT_B - PREVIOUS_B > interval2) {
      Serial.println ("BIO OK, BIOSTATE = 0");
      SMS(bio_ok);
      BIOFILTER_STATE = 1;
      delay (100);
       }
    }
  }

  if (digitalRead (biofilter) == HIGH)
  {
    Serial.println ("BIOFILTER LEVEL HIGH");
    CURRENT_B = millis();
    if (CURRENT_B - PREVIOUS_B > interval) {
      SMS(bio_high);
      BIOFILTER_STATE = 0;
      PREVIOUS_B = millis();
      delay (100);
    } else {
      Serial.println("INTERVAL TOO SHORT TO SEND SMS");
    }
  }
  /////////////////////////////////////////////

  

  if (digitalRead (IBC_TOP) == LOW)
  {
    if (IBC_TOP_STATE == 1)
    {
      Serial.println ("ibc top OK, IBC_TOP_STATE  = 1");
      PREVIOUS_C = 0;
      IBC_TOP_STATE = 1;
      delay (100);
    }

    if  (IBC_TOP_STATE == 0)
    {
      CURRENT_C = millis();
      Serial.println ("IBC TOP OK, IBC_TOP_STATE  = 0");
       if (CURRENT_C - PREVIOUS_C > interval2) {
      SMS(ibc_ok);
      IBC_TOP_STATE = 1;
      delay (100);
       }
    }
  }

  if (digitalRead (IBC_TOP) == HIGH)
  {
    Serial.println ("IBC LEVEL HIGH");
    CURRENT_C = millis();
    if (CURRENT_C - PREVIOUS_C > interval) {
      SMS(ibc_high);
      IBC_TOP_STATE = 0;
      PREVIOUS_C = millis();
      delay (100);
    } else {
      Serial.println("INTERVAL TOO SHORT TO SEND SMS");
    }
  }
  //////////////////////////////////////
  if (digitalRead (IBC_BOTTOM) == HIGH)
  {
    if (IBC_BOTTOM_STATE == 1)
    {
      Serial.println ("IBC_BOTTOM OK, IBC_BOTTOM_STATE  = 1");
      PREVIOUS_D = 0;
      IBC_BOTTOM_STATE = 1;
      delay (100);
    }

    if  (IBC_BOTTOM_STATE == 0)
    {
      CURRENT_D = millis();
      Serial.println ("IBC_BOTTOM OK, IBC_BOTTOM_STATE  =0");
      if (CURRENT_D - PREVIOUS_D > interval2) {
      SMS(ibc_ok);
      IBC_BOTTOM_STATE = 1;
      delay (100);
      }
    }
  }

  if (digitalRead (IBC_BOTTOM) == LOW)
  {
    Serial.println ("IBC LEVEL LOW");
    CURRENT_D = millis();
    if (CURRENT_D - PREVIOUS_D > interval) {
      SMS(ibc_low);
      IBC_BOTTOM_STATE = 0;
      PREVIOUS_D = millis();
      delay (100);
    } else {
      Serial.println("INTERVAL TOO SHORT TO SEND SMS");
    }
  }
////////////////////////////////////////////////////////

if (digitalRead(POWER) == HIGH)
  {
    if (POWER_STATE == 1)
    {
      Serial.println ("POWER OK, POWER STATE 1");
      PREVIOUS_C = 0;
      POWER_STATE = 1;
      delay (100);
    }

    if  (POWER_STATE == 0)
    {

      Serial.println ("POWER OK,POWER STATE 0 ");
      SMS(power_on);
      POWER_STATE = 1;
      delay (100);

    }
  }

  if (digitalRead (POWER) == LOW)
  {
    Serial.println ("POWER OFF");
    POWER_STATE = 0;
    SMS(power_off);
    delay (100);
    } 
  
  ///////////////////////////////////////////

}  // end of loop



void SMS(String message)
{
  //Serial.println("Sending Text...");
  gprsSerial.println("AT+CMGF=1\r"); // Set the shield to SMS mode
  delay(100);
  // send sms message, the phone number needs to include the country code e.g. if a U.S. phone number such as (540) 898-5543 then the string must be:
  // +15408985543
  gprsSerial.println("AT+CMGS = \"+27829729702\"");
  delay(100);
  gprsSerial.println(message); //the content of the message
  delay(100);
  DateTime now = RTC.now();
  gprsSerial.print(now.year(), DEC);
  gprsSerial.print('/');
  gprsSerial.print(now.month(), DEC);
  gprsSerial.print('/');
  gprsSerial.print(now.day(), DEC);
  gprsSerial.print(' ');
  gprsSerial.print(now.hour(), DEC);
  gprsSerial.print(':');
  gprsSerial.print(now.minute(), DEC);
  gprsSerial.print(':');
  gprsSerial.print(now.second(), DEC);
  gprsSerial.println();
  delay(200);
  gprsSerial.print((char)26);//the ASCII code of the ctrl+z is 26 (required according to the datasheet)
  delay(500);
  //gprsSerial.println();
  Serial.println("Text Sent.");
}












