#include <SoftwareSerial.h> //Including Sotware Serial lib for second serial port to connect Siemens TC35i
String tel_1 = "+11221234567"; //Telephone number 1
String tel_2 = "+11227654321"; //Telephone number 2
String temp; //Define temp string
int rxpin = 9; //Define rx and tx pin
int txpin = 8;
byte door = 10; //Relay output
SoftwareSerial serial2(rxpin,txpin); //RX,TX
void setup() {
pinMode(door, OUTPUT); //Set relay port to output
digitalWrite(door, HIGH); //Set relay port to high (switch relay default state)
Serial.begin(115200); //Initialize HW serial port for debug
serial2.begin(57600); //Initialize SW serial port for communicat with modem
Serial.println("Starting...."); //Program starting
delay(120000); //Waiting for modem (120sec is enough)
serial2.println("at"); //Send AT Command
Serial.println("at");
temp = serial2.readString();
Serial.print(temp);
delay(1000);
Serial.println("at+clip=1"); //Send AT Command to receiving caller ID
serial2.println("at+clip=1");
temp = serial2.readString();
Serial.println(temp);
delay(1000);
Serial.println("Enter to loop..."); //Loop is stating...
}
void loop() {
digitalWrite(door, HIGH); //Set relay outpout to high (relay normal state)
if (serial2.available()) { //Wait for SW Serial communication is active
String str = serial2.readString(); //Read out full string from SW serial
temp = (str.substring(18,30)); //Parser out from only Caller ID
Serial.println(str.substring(18,30)); //Show Caller ID (only ID, without any text. Example: RING ect..
if (temp == tel_1) //Find caller id in database
{
Serial.println("Telephone number 1"); //Print caller id in debug port
serial2.println("AT H"); //Hang up the call/ring
dooropen(); //Open the door
}
if (temp == tel_2) //Find caller id in database
{
Serial.println("Telephone number 2"); //Print caller id in debug port
serial2.println("AT H"); //Hang up the call/ring
dooropen(); //Open the door
}
else {
Serial.println("Unknow number door sill locked"); //Print to debug port
serial2.println("AT H"); //Hang up the call/ring
temp = serial2.readString(); //Eliminate "OK" feedback to AT H command
}
}
}
void dooropen()
{
Serial.println("Opening the door"); //Print to debug port
digitalWrite(door, LOW); //Set relay output LOW, activate the relay
delay(1000); //Wait 1 sec
digitalWrite(door, HIGH); //Set relay output HIGH, relay normal state
}