Bus Tracking Using GPS, GSM and Arduino NANO

by sanjusree in Circuits > Arduino

3805 Views, 1 Favorites, 0 Comments

Bus Tracking Using GPS, GSM and Arduino NANO

WhatsApp Image 2022-03-22 at 20.14.14.jpeg

A vehicle tracking system is an electronic device installed in a vehicle to enable the owner or a third party to track the vehicle's location. To design a vehicle tracking system that works using GPS and GSM technology, which would be the cheapest source of vehicle tracking and it would work as anti-theft system. It is an embedded system which is used for tracking and positioning of any vehicle by using Global Positioning System (GPS) and Global system for mobile communication (GSM). This design will continuously monitor a moving Vehicle and report the status of the Vehicle on demand. For doing so an ARDUINO NANO microcontroller is interfaced serially to a GSM Modem and GPS Receiver. A GSM modem is used to send the position (Latitude and Longitude) of the vehicle from a remote place. The GPS modem will continuously give the data i.e. the latitude and longitude indicating the position of the vehicle. The same data is sent to the mobile at the other end from where the position of the vehicle is demanded. When the request by user is sent to the number at the GSM modem, the system automatically sends a return reply to that mobile indicating the position of the vehicle in terms of latitude and longitude in real time.


Supplies

arduino nano.jpg
GSM module.jpg
jumper wires.jpg
GPS.jpg

Arduino NANO: Microcontroller: ATmega328, Architecture: AVR, Operating Voltage: 5 V, analog I/O Pins: 8, DC Current per I/O Pins 40 mA (I/O Pins) Input Voltage 7-12 V Digital I/O Pins 22 PWM Output 6 Power Consumption: 19 mA; The Arduino Nano is a small, complete, and breadboard-friendly board based on the ATmega328 (Arduino Nano 3.x). It has more or less the same functionality of the Arduino Duemilanove, but in a different package. It lacks only a DC power jack, and works with a Mini-B USB cable instead of a standard one.

GSM module: A GSM modem or GSM module is a hardware device that uses GSM mobile telephone technology to provide a data link to a remote network. From the view of the mobile phone network, they are essentially identical to an ordinary mobile phone, including the need for a SIM to identify themselves to the network.

Jumper wires: A jump wire is an electrical wire, or group of them in a cable, with a connector or pin at each end which is normally used to interconnect the components of a breadboard or other prototype or test circuit, internally or with other equipment or components, without soldering.

GPS tracker : GPS is a system of 30+ navigation satellites circling Earth. We know where they are because they constantly send out signals. A GPS receiver in your phone listens for these signals. Once the receiver calculates its distance from four or more GPS satellites, it can figure out where you are.

CODE

#include <SoftwareSerial.h>

#include <TinyGPS++.h>


boolean GetGPS_flag = false;

boolean Location_isValid_flag = false;

boolean Password_flag = false;



String value;

String phone_number;

String message_text;

String password = "HELLOGPS";

String Message;



#define PIN_TX  10

#define PIN_RX  11

SoftwareSerial mySerial(PIN_TX, PIN_RX);




// The TinyGPS++ object

TinyGPSPlus gps;


String SIM808(String value) {

 String Out;


 mySerial.println(value);

 delay(10);

 while (mySerial.available()) {

  Out = (mySerial.readString());

 }


 Out.trim();

 Out.remove(0, value.length() + 3);

 return Out;

}

String SIM808(String value, int count) {

 String Out;


 mySerial.println(value);

 delay(10);

 while (mySerial.available()) {

  Out = (mySerial.readString());

 }


 Out.trim();

 Out.remove(0, value.length() + 3 + count);

 return Out;

}


void setup() {

 mySerial.begin(9600);

 Serial.begin(9600);

}


void loop() {

 //******* RECEIVING SMS *******

 Serial.println("Start\n");


 Serial.println("Power Down The GPS");

 SIM808("AT+CGPSPWR=0");


 Serial.println("Check AT Connection");

 value = SIM808("AT");

 Serial.println(value);

 Serial.println("");


 Serial.println("Put The Module in SMS Text Mode");

 SIM808("AT+CMGF=1");

 delay(1000);



 Serial.println("Delete All Previous Messages");

 SIM808("AT+CMGD=1,4");

 delay(2000);


 value = SIM808("AT+CMGL=\"REC UNREAD\"");

 value = SIM808("AT+CMGL=\"REC UNREAD\"");

 value = SIM808("AT+CMGL=\"REC UNREAD\"");


 do {

  delay(2000);

  Serial.println("No new Messages Received");

  Serial.println("Check for New Messages!");

  value = SIM808("AT+CMGL=\"REC UNREAD\"");

  //Serial.println("end");

 } while (value == "OK");


 SIM808("AT+CMGL=\"REC UNREAD\"");

 Serial.println("Message Recieved");


 value = SIM808("AT+CMGL=\"ALL\"");

 Serial.println(value);

 phone_number = value;

 phone_number.remove(0, 20);

 phone_number.remove(15, phone_number.length());

 message_text = value;

 message_text.remove(0, 63);

 message_text.remove(message_text.length() - 6, message_text.length());


 Serial.println("Phone Number:");

 Serial.println(phone_number);


 Serial.println("Message Text:");

 Serial.println(message_text);



 if (message_text == password) {

  GetGPS_flag = true;

  Password_flag = true;

  Serial.println("password is correct!");

 }

 //******* RECEIVING SMS END *******




 //******* GET GPS *******

 if (GetGPS_flag) {

  do {

   Serial.println("Power Down The GPS");

   SIM808("AT+CGPSPWR=0");

   //Serial.println("end");


   Serial.println("Check The GPS Power");

   value = SIM808("AT+CGPSPWR?");

   value.remove(11, 17);

   Serial.println(value);

   //Serial.println("end");


   Serial.println("Power Up The GPS");

   SIM808("AT+CGPSPWR=1");

   //Serial.println("end");


   Serial.println("Disable The GPS Output");

   SIM808("AT+CGPSOUT=0");

   //Serial.println("end\n");


   Serial.println("Wait For The GPS To Find Location");

   while (!SIM808("AT+CGPSSTATUS?" , 13).startsWith("Location 3D Fix")) {

    Serial.println("Location Not Fixed Yet, Please Wait!");

    delay(2000);

   }

   Serial.println("Location Found!");

   Serial.println("end\n");




   //Get and Analyse The GPS Output

   Serial.println("Get and Analyse The GPS Output");

   String Out = "";

   mySerial.println("AT+CGPSOUT=2");


   delay(10);

   while (mySerial.available()) {

    mySerial.readStringUntil('\n');

    mySerial.readStringUntil('\n');

    Out = (mySerial.readStringUntil('\n')); Out += "\r\n";

   }

   mySerial.println("AT+CGPSOUT=0");

   delay(100);

   mySerial.println("AT+CGPSOUT=32");

   delay(10);

   while (mySerial.available()) {

    mySerial.readStringUntil('\n');

    mySerial.readStringUntil('\n');

    Out += (mySerial.readStringUntil('\n')); Out += "\r\n";

   }

   mySerial.println("AT+CGPSOUT=0");

   Out.trim();

   Serial.println(Out);

   Serial.println("");

   //GPS Output Analized



   int buffint = Out.length();

   char buff[buffint];

   Out.toCharArray(buff, buffint);

   const char *gpsStream = buff;


   while (*gpsStream)

    if (gps.encode(*gpsStream++))

     displayInfo();


   Serial.println("");

   Serial.println("");


   delay(100);

   if (gps.location.isValid())

   {

    Location_isValid_flag = true;

    Message = String(gps.location.lat(), 6);

    Message += ",";

    Message += String(gps.location.lng(), 6);

    Message += " ";

    Serial.println(Message);

   }

  } while (!Location_isValid_flag);


 }

 //******* GET GPS END *******





 //******* SENDING SMS *******

 Serial.println("Start Sending The SMS\n");



 Serial.println("Sending The SMS to");

 Serial.println(phone_number);


 SIM808("AT+CMGS=" + phone_number );

 delay(200);


if (Password_flag == true) {

mySerial.println("Open This Link:");

mySerial.print("https://www.google.com/maps/place/");

mySerial.println(Message);

mySerial.println((char)26);

SIM808(Message);

}

 else {

  mySerial.println("Password Incorrect! ");

 }

 Serial.println("Message Sent!");


 Serial.println("Delete All Previous Messages");

 SIM808("AT+CMGD=1,4");

 delay(2000);

 //******* SENDING SMS END*******


 //reinitializing flags

 GetGPS_flag = false;

 Location_isValid_flag = false;

 Password_flag = false;

}







void displayInfo()

{

 Serial.print(F("Location: "));

 if (gps.location.isValid())

 {

  Serial.print(gps.location.lat(), 6);

  Serial.print(F(","));

  Serial.print(gps.location.lng(), 6);

 }

 else

 {

  Serial.print(F("INVALID"));

 }


 Serial.println();

}


char * floatToString(char * outstr, double val, byte precision, byte widthp) {

 char temp[16];

 byte i;


 // compute the rounding factor and fractional multiplier

 double roundingFactor = 0.5;

 unsigned long mult = 1;

 for (i = 0; i < precision; i++)

 {

  roundingFactor /= 10.0;

  mult *= 10;

 }


 temp[0] = '\0';

 outstr[0] = '\0';


 if (val < 0.0) {

  strcpy(outstr, "-\0");

  val = -val;

 }


 val += roundingFactor;


 strcat(outstr, itoa(int(val), temp, 10)); //prints the int part

 if ( precision > 0) {

  strcat(outstr, ".\0"); // print the decimal point

  unsigned long frac;

  unsigned long mult = 1;

  byte padding = precision - 1;

  while (precision--)

   mult *= 10;


  if (val >= 0)

   frac = (val - int(val)) * mult;

  else

   frac = (int(val) - val ) * mult;

  unsigned long frac1 = frac;


  while (frac1 /= 10)

   padding--;


  while (padding--)

   strcat(outstr, "0\0");


  strcat(outstr, itoa(frac, temp, 10));

 }


 // generate space padding

 if ((widthp != 0) && (widthp >= strlen(outstr))) {

  byte J = 0;

  J = widthp - strlen(outstr);


  for (i = 0; i < J; i++) {

   temp[i] = ' ';

  }


  temp[i++] = '\0';

  strcat(temp, outstr);

  strcpy(outstr, temp);

 }

 return outstr;

}

Working Procedure

connections in bus.jpeg
Screenshot (96).png
connection1.jpg
connections2.jpg

Give the connections as per the circuit shown in the diagram. connect the Arduino NANO pin D10 to TXD of GSM. Connect Arduino NANO pin D11 to RXD of GSM . connect GND of Arduino NANO pin to GND of GSM module. Now upload the code into the Arduino NANO. Install Tinygps library in the Arduino. Connect Antenna pin to Antenna.Connect the Arduino NANO to the laptop and do check the port that is displayed in the screen. Next, open the serial monitor in the Arduino. According to the code we have to send the message as 'HELLOGPS' to the number where the SIM is fixed in the GSM Module .Now we will get the reply as the URL link to us by opening it we can know the exact location of the bus.

Vedio

https://electropeak.com/learn/gps-car-tracker-using-arduino-and-sim808/

https://youtu.be/B3Z76pyyA6s

Conclusion

picture.jpeg

The project titled “Bus Tracking Using GPS, GSM and Arduino NANO” is a model for vehicle tracking unit with the help of GPS receivers and GSM modem. Vehicle Tracking System resulted in improving overall productivity with better fleet management that in turn offers better return on your investments. Better scheduling or route planning can enable you handle larger jobs loads within a particular time. When we send the code as 'HELLOGPS' ( we have to fix it in the code)to the particular sim in GSM module then we will receive the response by using GPS system that is URL link. This URL link shows directly the google maps and location of that particular bus. In this way the driver do not need to attend every call and can reduce the accidents . So in the coming year, it is going to play a major role in our day-to-day living. We have completed the project as per the requirements of our project. Finally the aim of the project i.e. to trace the vehicle is successfully achieved.