#include<SoftwareSerial.h>
SoftwareSerial mySerial(10,11); // (Rx,Tx > Tx,Rx)
char incomingByte;
String inputString;
int IN1 = 2; // Output for Motor Driver IN1
int IN2 = 3; // Output for Motor Driver IN2
void setup()
{
pinMode(IN1, OUTPUT);
pinMode(IN2, OUTPUT);
digitalWrite(IN1, LOW); // Initial state of the LOW
digitalWrite(IN2, LOW); // Initial state of the LOW
Serial.begin(9600);
mySerial.begin(9600);
while(!mySerial.available()){
mySerial.println("AT");
delay(1000);
Serial.println("Connecting...");
}
Serial.println("Connected!");
mySerial.println("AT+CMGF=1"); //Set SMS to Text Mode
delay(1000);
mySerial.println("AT+CNMI=1,2,0,0,0"); //Procedure to handle newly arrived messages(command name in text: new message indications to TE)
delay(1000);
mySerial.println("AT+CMGL=\"REC UNREAD\""); // Read Unread Messages
}
void loop()
{
if(mySerial.available()){
delay(100);
// Serial Buffer
while(mySerial.available()){
incomingByte = mySerial.read();
inputString += incomingByte;
}
delay(10);
Serial.println(inputString);
inputString.toUpperCase(); // Uppercase the Received Message
//turn RELAY ON or OFF
if (inputString.indexOf("DOOR LOCK") > -1){ // Send sms for door lock
digitalWrite(IN1, HIGH);
delay(500);
digitalWrite(IN1, LOW);
}
if (inputString.indexOf("DOOR UNLOCK") > -1){ //send sms for door unlock
digitalWrite(IN2, HIGH);
delay(500);
digitalWrite(IN2, LOW);
}
delay(50);
//Delete Messages & Save Memory
if (inputString.indexOf("OK") == -1){
mySerial.println("AT+CMGDA=\"DEL ALL\"");
delay(1000);}
inputString = "";
}
}
Comments