//Limitations:
//1. This code only works for Software Serial and not Hardware Serial
//2. Message length should not be greater than 70 letters
//3. Message should be encapsulated in "___"
#include <SoftwareSerial.h>
SoftwareSerial SIM900(10,11);
unsigned char buffer[200]; //to store the received data from gsm
int counter[20]; // to store the address of " in string
int count=0; // used to point buffer array
int pnt=0; // used to count counter array
int pnt1=0; // to count : in string
int pnt2=0; // flag to avoide multiple phone & msg print
String phone_num; // string to store phone number
String message_gsm; // string to store message
int Gpsdata; // for incoming serial data
unsigned int finish =0; // indicate end of message
unsigned int pos_cnt=0; // position counter
unsigned int lat_cnt=0; // latitude data counter
unsigned int log_cnt=0; // longitude data counter
unsigned int flg =0; // GPS flag
unsigned int com_cnt=0; // comma counter
char lat[20]; // latitude array
char lg[20]; // longitude array
void Receive_GPS_Data();
int value1=0;
int value2=0;
int digit1=0;
int digit2=0;
float latt=0;
int value3=0;
int value4=0;
int digit3=0;
int digit4=0;
float longi=0;
int beforedecimal=0;
float afterdecimal=0;
int beforedecimal2=0;
float afterdecimal2=0;
float combined2=0;
char latitude;
char longitude;
String message="";
String message2="";
String message3="";
String mahi="";
char charVal[10]; //temporarily holds data from vals
String stringVal = ""; //data on buff is copied to this string
char charVal2[10]; //temporarily holds data from vals
String stringVal2 = ""; //data on buff is copied to this string
float latt_min =19.0110 ;
float latt_max= 19.0120;
float longi_min= 72.8300 ;
float longi_max=72.8330 ;
void setup()
{
pinMode(4,OUTPUT); // to fingerprint keypad
pinMode(5,INPUT); // password match conformation
pinMode(3,OUTPUT); // to relay
pinMode(2,INPUT); //vibration sensor
Serial.begin(9600); // setting baud rate to serial monitor
SIM900.begin(19200); // setting baud rate to software serial
delay(200); // give time to log on to network.
SIM900.print("AT+CMGF=1\r"); // set SMS mode to text
delay(100);
SIM900.print("AT+CNMI=2,2,0,0,0\r"); // blurt out contents of new SMS upon receipt to the GSM shield's serial out
delay(100);
}
void loop()
{
gps();
// Now we simply display any text that the GSM shield sends out on the serial monitor
if (latt >= latt_min && latt <= latt_max && longi >= longi_min && longi <= longi_max)
{
digitalWrite(4,HIGH); // destination reached indication
}
else
{
digitalWrite(4,LOW);
}
if(SIM900.available()) // checks for any data from gsm
{
while(SIM900.available()) //
{
phone_num=""; //Initial clearing
message_gsm="";
buffer[count++]=SIM900.read(); // Reading and storing in buffer
if(count==200)break; // buffer overflow
}
for(int a=0;a<201;a++)
{
if(buffer[a]=='"') // checks for character "
{
counter[pnt++]=a; // storing address of character "
}
}
for(int a=0;a<201;a++)
{
if(buffer[a]==':') // checks for character :
{
pnt1++; // counting no. of :
}
if(pnt1==1 && pnt2==0) // storing the data only once
{
for(int b=counter[0]+1;b<counter[0]+14;b++)
{
phone_num +=(char(buffer[b]));
}
for(int b=counter[6]+1;b<counter[6]+100;b++)
{
if(buffer[b]=='"')break;
message_gsm +=(char(buffer[b]));
}
pnt2=1;
}
}
clearBufferArray(); // clearing all variables & buffer
count=0;
pnt=0;
pnt1=0;
pnt2=0;
}
Serial.print("Phone No.:- ");
Serial.println(phone_num);
Serial.print("Message:- ");
Serial.println(message_gsm);
if (message_gsm=="FETCH")
{
send_lat();
send_lon();
message_gsm="";
phone_num="";
}
if (digitalRead(5)==1) // open lock command
{digitalWrite(3,HIGH); // to relay
}
/*if (digitalRead(2)==1) //from vibration sensor
{
send_vibration();
}
*/
}
void clearBufferArray() // Clearing buffer
{
for (int i=0;i<count;i++)
{buffer[i]=NULL;}
}
void gps()
{ message="";
Receive_GPS_Data();
lat_conversion();
lon_conversion();
/*Serial.print("Latitude : ");
Serial.println(latt,6);
Serial.print("Longitude : ");
Serial.println(longi,6);*/
message = "latitude=";
message.concat(stringVal);
//message = message + stringVal;
message2 = "longitude=";
//message.concat("longitude=");
message2.concat(stringVal2);
//Serial.print(message);
//Serial.println(message2);
//message2 = message2 + stringVal2;
//message.concat(message2);
//Serial.println(message);
//Serial.println(message2);
finish = 0;pos_cnt = 0;
stringVal="";
stringVal2="";
}
/************************************************************************************
Function : Receive_GPS_Data()
Description : finding Latitudse and longitude from GPRMC message
************************************************************************************/
void Receive_GPS_Data()
{
while(finish==0){
while(Serial.available()>0){ // Check GPS data
Gpsdata = Serial.read();
flg = 1;
if( Gpsdata=='$' && pos_cnt == 0) // finding GPRMC header
pos_cnt=1;
if( Gpsdata=='G' && pos_cnt == 1)
pos_cnt=2;
if( Gpsdata=='P' && pos_cnt == 2)
pos_cnt=3;
if( Gpsdata=='R' && pos_cnt == 3)
pos_cnt=4;
if( Gpsdata=='M' && pos_cnt == 4)
pos_cnt=5;
if( Gpsdata=='C' && pos_cnt==5 )
pos_cnt=6;
if(pos_cnt==6 && Gpsdata ==','){ // count commas in message
com_cnt++;
flg=0;
}
if(com_cnt==3 && flg==1){
lat[lat_cnt++] = Gpsdata; // latitude
flg=0;
}
if(com_cnt==5 && flg==1){
lg[log_cnt++] = Gpsdata; // Longitude
flg=0;
}
if( Gpsdata == '*' && com_cnt >= 5){
com_cnt = 0; // end of GPRMC message
lat_cnt = 0;
log_cnt = 0;
flg = 0;
finish = 1;
}
}
}
}
void lat_conversion()
{ float combined;
float x = atof(lat);
//Serial.println(x,4);
int beforedecimal = x;
//Serial.println(beforedecimal);
float afterdecimal=(x-beforedecimal);
//Serial.println(afterdecimal,4);
while(value1<beforedecimal)
{
value1++;
digit2++;
if(digit2==100)
{
digit2=0;
digit1++;
}
}
combined = afterdecimal+digit2;
//Serial.println(combined,4);
float sum0= combined/60;
latt=digit1+sum0;
dtostrf(latt, 2, 6, charVal);//4 is mininum width, 3 is precision; float value is copied onto buff
//convert chararray to string
for(int i=0;i<sizeof(charVal);i++)
{
stringVal+=charVal[i];
}
// Serial.print(stringVal);
//Serial.println(digit1);
// Serial.println(digit2);
}
void lon_conversion()
{ float combined2;
float y = atof(lg);
// Serial.println(y,4);
int beforedecimal2 = y;
//Serial.println(beforedecimal2);
float afterdecimal2=(y-beforedecimal2);
//Serial.println(afterdecimal2,4);
while(value3<beforedecimal2)
{
value3++;
digit4++;
if(digit4==100)
{
digit4=0;
digit3++;
}
}
combined2 = afterdecimal2+digit4;
//Serial.println(combined2,4);
float sum=combined2/60;
//Serial.println(sum,6);
longi=digit3+sum;
dtostrf(longi, 2, 6, charVal2);//4 is mininum width, 3 is precision; float value is copied onto buff
//convert chararray to string
for(int i=0;i<sizeof(charVal2);i++)
{
stringVal2+=charVal2[i];
}
// Serial.println(stringVal2);
//Serial.println(digit4);
}
void send_lat()
{
SIM900.print("AT+CMGF=1\r"); // AT command to send SMS message
delay(100);
SIM900.print("AT + CMGS = \""); // recipient's mobile number, in international format
SIM900.print(phone_num);
SIM900.println("\"");
delay(100);
SIM900.println(message); // message to send
delay(100);
SIM900.println((char)26); // End AT command with a ^Z, ASCII code 26
delay(100);
SIM900.println();
digitalWrite(13,HIGH);
delay(5000); // give module time to send SMS
SIM900.print("AT+CMGF=1\r"); // set SMS mode to text
delay(100);
SIM900.print("AT+CNMI=2,2,0,0,0\r"); // blurt out contents of new SMS upon receipt to the GSM shield's serial out
delay(100);
}
void send_lon()
{
SIM900.print("AT+CMGF=1\r"); // AT command to send SMS message
delay(100);
SIM900.print("AT + CMGS = \""); // recipient's mobile number, in international format
SIM900.print(phone_num);
SIM900.println("\"");
delay(100);
SIM900.println(message2); // message to send
delay(100);
SIM900.println((char)26); // End AT command with a ^Z, ASCII code 26
delay(100);
SIM900.println();
digitalWrite(13,HIGH);
delay(5000); // give module time to send SMS
phone_num=""; // string to store phone number
message_gsm=""; // string to store message
SIM900.print("AT+CMGF=1\r"); // set SMS mode to text
delay(100);
SIM900.print("AT+CNMI=2,2,0,0,0\r"); // blurt out contents of new SMS upon receipt to the GSM shield's serial out
delay(100);
}
void send_vibration()
{
SIM900.print("AT+CMGF=1\r"); // AT command to send SMS message
delay(100);
SIM900.print("AT + CMGS = \""); // recipient's mobile number, in international format
SIM900.print(phone_num);
SIM900.println("\"");
delay(100);
SIM900.println("vibration detected"); // message to send
delay(100);
SIM900.println((char)26); // End AT command with a ^Z, ASCII code 26
delay(100);
SIM900.println();
digitalWrite(13,HIGH);
delay(5000); // give module time to send SMS
phone_num=""; // string to store phone number
message_gsm=""; // string to store message
SIM900.print("AT+CMGF=1\r"); // set SMS mode to text
delay(100);
SIM900.print("AT+CNMI=2,2,0,0,0\r"); // blurt out contents of new SMS upon receipt to the GSM shield's serial out
delay(100);
}
Comments
Please log in or sign up to comment.