Gps

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

#include <TinyGPS++.

h>
#include <SoftwareSerial.h>
#include <LiquidCrystal.h>

LiquidCrystal lcd(13, 12, 11, 10, 9, 8);

SoftwareSerial gsm(4, 5);


SoftwareSerial gps(2, 3);

TinyGPSPlus gps;

bool trackingMode = false;

void setup() {
Serial.begin(9600);
gsm.begin(9600);
gps.begin(9600);
lcd.begin(16, 2);

pinMode(13, OUTPUT);
digitalWrite(13, LOW);

lcd.print("Vehicle Tracking");
lcd.setCursor(0, 1);
lcd.print(" System ");
delay(2000);

gsm_init();
lcd.clear();
lcd.print("GPS Initializing");
lcd.setCursor(0, 1);
lcd.print(" No GPS Range ");
delay(2000);

lcd.clear();
lcd.print("GPS Range Found");
lcd.setCursor(0, 1);
lcd.print("GPS is Ready");
delay(2000);

lcd.clear();
lcd.print("System Ready");
}

void loop() {
if (Serial.available() > 0) {
String input = Serial.readStringUntil('\n');
if (input == "Track Vehicle") {
trackingMode = true;
}
}

if (trackingMode) {
digitalWrite(13, HIGH);
tracking();
digitalWrite(13, LOW);
trackingMode = false;
}

while (gsm.available() > 0) {


gps.encode(gsm.read());
if (gps.location.isUpdated()) {
digitalWrite(13, HIGH);
tracking();
digitalWrite(13, LOW);
break;
}
}
}

void gsm_init() {
lcd.clear();
lcd.print("Finding Module..");

boolean at_flag = true;


while (at_flag) {
gsm.println("AT");
delay(1);
while (gsm.available() > 0) {
if (gsm.find("OK")) {
at_flag = false;
break;
}
}
delay(1000);
}
lcd.clear();
lcd.print("Module Connected..");
delay(1000);
lcd.clear();
lcd.print("Disabling ECHO");

boolean echo_flag = true;


while (echo_flag) {
gsm.println("ATE0");
while (gsm.available() > 0) {
if (gsm.find("OK")) {
echo_flag = false;
break;
}
}
delay(1000);
}

lcd.clear();
lcd.print("Echo OFF");
delay(1000);
lcd.clear();
lcd.print("Finding Network..");

boolean net_flag = true;


while (net_flag) {
gsm.println("AT+CPIN?");
while (gsm.available() > 0) {
if (gsm.find("+CPIN: READY")) {
net_flag = false;
break;
}
}
delay(1000);
}

lcd.clear();
lcd.print("Network Found..");
delay(1000);
lcd.clear();
}

void tracking() {
lcd.clear();
lcd.print("Tracking Vehicle");

gsm.println("AT+CMGF=1");
delay(400);
gsm.println("AT+CMGS=\"+252906034727\""); // Change with desired recipient's phone
number
delay(400);

String message = "Vehicle Tracking Alert:\n";


message += "Your Vehicle Current Location is:\n";
message += "Latitude: ";
message += String(gps.location.lat(), 6);
message += "\nLongitude: ";
message += String(gps.location.lng(), 6);
message += "\n\nhttps://www.google.com/maps/@";
message += String(gps.location.lat(), 6);
message += ",";
message += String(gps.location.lng(), 6);
message += ",14z";

gsm.print(message);
gsm.write(26); // End of message character

lcd.clear();
lcd.print("Message Sent");
delay(2000);
lcd.clear();
lcd.print("System Ready");
}

You might also like