#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 =   10000;
const unsigned long interval2 =   5000;

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(500);
  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");
    }
  }
  /////////////////////////////////////////////////
  
}  // 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.");
}












