#include <TinyGPS.h>
#include <SoftwareSerial.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
SoftwareSerial SIM900A(7, 8 ) ;
char phone_no[] = "+8801845746473"; //replace with phone no. to get sms
TinyGPS gps; //Creates a new instance of the TinyGPS object
int state;
String textMessage;
const int sensor = A1;
const int ldrPin = A0; // Assigning analog pin A5 to variable 'sensor'
float tempc=0; //variable to store temperature in degree Celsius
float vout=0; //temporary variable to hold sensor reading
void setup()
{
Serial.begin(9600);
SIM900A.begin(9600);
delay(1000);
// AT command to set SIM900 to SMS mode
Serial.println("AT+CMGF=1\r");
delay(100);
// Set module to send SMS data to serial out upon receipt
Serial.println("AT+CNMI=2,2,0,0,0\r");
pinMode(6, INPUT);
pinMode(sensor, INPUT);
pinMode(ldrPin, INPUT);
lcd.begin();
lcd.backlight();
lcd.clear();
lcd.print("Searching ");
lcd.setCursor(0, 1);
lcd.print("Network");
}
void loop()
{
bool newData = false;
unsigned long chars=0;
unsigned short sentences=0, failed=0;
// For one second we parse GPS data and report some key values
for (unsigned long start = millis(); millis() - start < 1000;)
{
while (Serial.available())
{
char c = Serial.read();
Serial.print(c);
if (gps.encode(c))
newData = true;
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
vout = analogRead(sensor); //Reading the value from sensor
vout = (vout * 500) / 1023;
tempc = vout;
int ldrStatus = analogRead(ldrPin); // Storing value in Degree Celsius
//lcd.println(tempc);
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
if (SIM900A.available()>0)
{
Serial.println("Available");
textMessage = SIM900A.readString();
textMessage.toUpperCase();
Serial.println(textMessage);
delay(100);
}
if (textMessage.indexOf("LOCAL") >= 0)
{
Serial.println("LOCAL");
lcd.clear();
lcd.print("Message Recevived");
delay(3000);
lcd.clear();
lcd.print("Getting ");
lcd.setCursor(0, 1);
lcd.print("Location");
delay(3000);
float flat, flon;
unsigned long age;
gps.f_get_position(&flat, &flon, &age);
SIM900A.println("AT+CMGF=1");
delay(400);
SIM900A.println("AT+CMGS="+880845746473"\r");
//
lcd.clear();
lcd.print("Sending info");
lcd.setCursor(0, 1);
lcd.print("To Base");
delay(1000);
// lcd.print("T=");
// lcd.print(tempc);
// lcd.print("H=");
// lcd.print(ldrStatus/10);
// delay(1000);
SIM900A.println("Asheesh ,Battalion No. 1233456 ");
SIM900A.println("Temperature is ");
SIM900A.print(tempc);
SIM900A.println("Heartbeat is ");
SIM900A.print(ldrStatus / 10);
SIM900A.println("Google Maps");
// Gsm.print("Latitude = ");
SIM900A.println(flat == TinyGPS::GPS_INVALID_F_ANGLE ? 0.0 : flat, 6);
//Gsm.print(" Longitude = ");
Serial.println(",");
SIM900A.println(flon == TinyGPS::GPS_INVALID_F_ANGLE ? 0.0 : flon, 6);
delay(200);
SIM900A.println((char)26); // End AT command with a ^Z, ASCII code 26
delay(200);
// SIM900A.println();
// delay(10000);
}
state = digitalRead(6);
if (state == HIGH) //If newData is true
{
float flat=0, flon=0;
unsigned long age=0;
gps.f_get_position(&flat, &flon, &age);
SIM900A.println("AT+CMGF=1");
delay(1000);
SIM900A.println("AT+CMGS="+8801845746473"\r");
delay(1000);
// SIM900A.print(phone_no);
// SIM900A.println(""");
lcd.clear();
lcd.print("Sending info");
lcd.setCursor(0, 1);
lcd.print("To Base");
delay(1000);
lcd.print("T=");
lcd.print(tempc);
lcd.print("H=");
lcd.print(ldrStatus/10);
delay(1000);
SIM900A.println("Alert I need help....Asheesh ,Battalion No. 1233456 ");
SIM900A.println("Temperature is");
SIM900A.print(tempc);
SIM900A.println("Heartbeat is");
SIM900A.print(ldrStatus / 10);
SIM900A.println("Google Maps");
SIM900A.print("Latitude = ");
SIM900A.println(flat == TinyGPS::GPS_INVALID_F_ANGLE ? 0.0 : flat, 6);
SIM900A.print(" Longitude = ");
Serial.print(",");
SIM900A.println(flon == TinyGPS::GPS_INVALID_F_ANGLE ? 0.0 : flon, 6);
delay(200);
SIM900A.println((char)26); // End AT command with a ^Z, ASCII code 26
delay(200);
// SIM900A.println();
// delay(10000);
}
else {
lcd.clear();
lcd.print("Soldier Tracking");
lcd.setCursor(0, 1);
lcd.print("System ");
delay(2000);
lcd.clear();
lcd.print("Sending location");
lcd.setCursor(0,1);
lcd.print("To Base");
delay(1000);
lcd.print("T=");
lcd.print(tempc);
lcd.print("H=");
lcd.print(ldrStatus/10);
delay(1000);
}
Serial.println("1");
// if (chars == 0)
// Serial.println("** No characters received from GPS: check wiring **");
}