#include <SoftwareSerial.h> //Software Serial Port
#define RxD 7
#define TxD 8

#define pinOUT1 2



SoftwareSerial ericssonSerial(RxD,TxD);

String phonenum="XYXYXYXYXY";
String phonePDU="YXYXYXYXYX";

String OUT1ON="0C31D98C067A56A931D0D309";
String OUT1OFF="0D31D98C067A56A931D0D36804";
String OUT1FAIL="09CF2A35063206934C";
String OUT1ONOK="0ACF2A35067A3A41CF25";
String OUT1OFFOK="0BCF2A35067A1A8DA0E712";

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

 
 pinMode(RxD, INPUT);
 pinMode(TxD, OUTPUT);
 pinMode(pinOUT1, OUTPUT);

 ericssonSerial.begin(9600);
 
 delay(500);
 
 Init();
 
}


void Init(){
  
   while (send_command("AT&F",500) != "&F\r\r\nOK\r\n") {
     delay(1000);
   }
   Serial.println("Modem reset...");
   
   while (send_command("ATE=0",500) != "E=0\r\r\nOK\r\n") {
     delay(1000);
   }
   Serial.println("Echo Off...");
   
   while (send_command("AT+CLIP=1",500) != "\r\nOK\r\n") {
     delay(1000);
   }
   Serial.println("Caller ID enabled...");
   
   while (send_command("AT+CPMS=\"ME\"",500).indexOf("CPMS")==0) {
     delay(1000);
   }
   Serial.println("Phone Memory Selected for SMS...");
   
   while (send_command("AT+CNMI=,1",500) != "\r\nOK\r\n") {
     delay(1000);
   }
   Serial.println("New SMS Indication Activated...");
   
   Serial.println("Ready...");
}


String send_command(String at_cmd, int dly){

  String msg;
  char incomingByte;

  ericssonSerial.println(at_cmd);
  delay(dly);
  while (ericssonSerial.available() > 0) 
  {
    incomingByte = (ericssonSerial.read());
    msg = msg + incomingByte;
  }  
  //delay(1000);

  return msg;
}

void sendSMS(String num,String txt){
  String pduTxt;
  int pduLength;
  
  
   pduTxt="0011000C9103"+num+"0000AA"+txt;  
   pduLength=(pduTxt.length()-2)/2;
   //Serial.println(pduTxt);
   //Serial.println(pduLength);
   ericssonSerial.print("AT+CMGS="+String(pduLength)+"\r");
   delay(5000);
   ericssonSerial.print(pduTxt);
   delay(5000);
   ericssonSerial.write(26);
   delay(1000);
 
}

void loop()
{
 char recvChar;
 String recvStr;
 int ringCount=0;
 String incSMS=0;
 
 while(1){
   recvStr=0;

   while (ericssonSerial.available()>0){
     recvChar = ericssonSerial.read();
     recvStr=recvStr+recvChar;
     //Serial.print(recvChar);
   }


   if (recvStr.indexOf("CLIP")>0){

     if (recvStr.indexOf(phonenum)>0) {
    
       Serial.println(phonenum+" is calling...");
       ringCount=ringCount+1;
       recvStr=0;
       //Serial.println(ringCount);
     }else{
        while (send_command("AT+CHUP",700) != "\r\nOK\r\n"){
        delay(1000);
        }
        Serial.println("Unauthorized caller..");
     } 
   }
   
   if (ringCount==2){
       while (send_command("AT+CHUP",700) != "\r\nOK\r\n"){
         delay(1000);
       }
       digitalWrite(pinOUT1,HIGH);
       Serial.println("OUT1 ON...");
       //delay(5000);
       //digitalWrite(pinOUT1,LOW);
       //Serial.println("LED OFF...");
       ringCount=0;
       Serial.println("Sending SMS <OUT1 ON OK>.....");
       sendSMS(phonePDU, OUT1ONOK);
   }  
   
   if (recvStr.indexOf("CMTI")>0){
     Serial.println("SMS Received...");
     ericssonSerial.println("AT+CMGR=1");
     //delay(500);
   }
   
   if (recvStr.indexOf(phonePDU)>0){
     //Serial.print(recvStr);
     if (recvStr.indexOf(OUT1ON)>0){
       digitalWrite(pinOUT1,HIGH);
       Serial.println("OUT1 ON...");
       delay(1000);
       ericssonSerial.println("AT+CMGD=1");
       delay(1000);
       Serial.println("Sending SMS <OUT1 ON OK>.....");
       sendSMS(phonePDU,OUT1ONOK);
     } else if (recvStr.indexOf(OUT1OFF)>0){
       digitalWrite(pinOUT1,LOW);
       Serial.println("OUT1 OFF...");
       delay(1000);
       ericssonSerial.println("AT+CMGD=1");
       delay(1000);
       Serial.println("Sending SMS <OUT1 OFF OK>.....");
       sendSMS(phonePDU,OUT1OFFOK);
     } else {
         Serial.println("Invalid SMS...");
         delay(1000);
         ericssonSerial.println("AT+CMGD=1");
         delay(1000);
     }
   }
   
   if (recvStr.indexOf("CMGS")>0){
     Serial.println("Message Sent...");
   }
     
   while (Serial.available()){
     recvChar = Serial.read();
     ericssonSerial.print(recvChar);
   }
 }
}