#include <SoftwareSerial.h>
#include <TinyGPS++.h>
#include <SD.h>
#include <SPI.h>
//setting software serial pins
SoftwareSerial SIM900(10,11);
/////////////declaring content////
TinyGPSPlus gps;
File myFile;
const int chipSelect=53;
double Latitude, Longitude;
String gmap = "Google Maps";
const int pushb=22;
const int vsen=23;
const int ksen=24;
void setup() {
// put your setup code here, to run once:
Serial.begin(19200);
SIM900.begin(19200);
Serial1.begin(9600);
Serial2.begin(9600);
pinMode(pushb, INPUT);
pinMode(vsen, INPUT);
pinMode(ksen, INPUT);
delay(1000);
Serial.print("INITIALIZING....");
SIM900.println("AT");//handshake withSIM900
upd_serial_SIM900();
/* SIM900.println("AT+GMGF=1");//CONGIGURE IN TEXT MODE
upd_serial_SIM900();
SIM900.println("AT+CMGS= "+23059293785"\r");// PHONE NUMBER TO BE TEXT
upd_serial_SIM900();
SIM900.print("Hey SUFYAAN HERE");
delay(100);
upd_serial_SIM900();
SIM900.println((char)26);
delay(100);
*/
}
void loop()
{
// put your main code here, to run repeatedly:
sen_con();
}
//updte the serial port for transmission or receiving data
void upd_serial_SIM900()
{
delay(500);
while(Serial.available())
{
SIM900.write(Serial.read());
}
while(SIM900.available())
{
Serial.write(SIM900.read());
}
}
// GPS informations
void gpsdata()
{
while(Serial1.available())
{
if(gps.encode(Serial1.read()))
{
Serial.println(gps.location.lat(),6);
Serial.println(gps.location.lng(),6);
delay(2000);
Latitude = gps.location.lat(),6;
Longitude = gps.location.lng(),6;
}
}
}
//////SD card
void sd()
{
while (!Serial2)
{
}
Serial.print("Initializing SD card..!");
if (!SD.begin())
{
Serial.println("Initializing failed..");
return;
}
Serial.println("Initializing Done..!");
myFile = SD.open("file.txt", FILE_READ);
if (myFile)
{
Serial.println("file.txt: ");
while (myFile.available())
{
Serial2.write(myFile.read());
myFile.println();
}
myFile.close();
}
else
{
Serial.println("Error file ccannot open");
}
}
/////
void Family_Message()
{
gpsdata();
SIM900.println("AT");
upd_serial_SIM900();
SIM900.println("AT+CMGF=1");
delay(1000);
upd_serial_SIM900();
SIM900.println("AT+CMGS= "+23059293785"\r");
delay(100);
upd_serial_SIM900();
SIM900.println("hey, I met an accident");
delay(100);
upd_serial_SIM900();
SIM900.println("\nHelp me out, follow the locations");
delay(100);
SIM900.print(gmap);
delay(100);
SIM900.print(Latitude, 6);
delay(100);
SIM900.print(",");
delay(100);
SIM900.println(Longitude, 6);
delay(100);
upd_serial_SIM900();
SIM900.println((char)26);
delay(1000);
}
void Emergency_Message()
{
gpsdata();
sd();
SIM900.println("AT");
upd_serial_SIM900();
SIM900.println("AT+CMGF=1");
delay(1000);
upd_serial_SIM900();
SIM900.println("AT+CMGS= "+23059293785"\r");
delay(100);
upd_serial_SIM900();
SIM900.println("Hello there, I met an accident");
delay(100);
SIM900.println("\n Help me out, follow the locations");
delay(100);
SIM900.print(gmap);
delay(100);
SIM900.print(Latitude, 6);
delay(100);
SIM900.print(",");
delay(100);
SIM900.println(Longitude, 6);
delay(100);
upd_serial_SIM900();
SIM900.println((char)26);
delay(1000);
}
void sen_con()
{
int val1=LOW;
int val2=LOW;
int val3=LOW;
val1=digitalRead(pushb);
val2=digitalRead(vsen);
val3=digitalRead(ksen);
if (val1==HIGH)
{
Family_Message();
delay(1000);
}
else
if ((val2== HIGH)|| (val3== HIGH))
{
Emergency_Message();
delay(1000);
}
}