Ali
Published © CC BY-SA

DIY Real-time GPS Tracking Via SMS

This can take you a head toward the wireless communication and the automatic control techniques that may help you start your great projects

AdvancedFull instructions providedOver 4 days5,760
DIY Real-time GPS Tracking Via SMS

Story

Read more

Code

SMS_GPS_Tracker.ino

C#
#include <GSM.h>
#include <TinyGPS++.h>
#include <AltSoftSerial.h> //Include all relevant libraries - see above
#define PINNUMBER "" // PIN Number for the SIM - leave blank unless your SIM has a pin, this is inserted between ""
static const uint32_t GPSBaud = 9600; //Baud rate for communication with the GPS, Adafruit GPS = 9600, your GPS may well be 4800, check the spec
TinyGPSPlus gps; // The TinyGPS++ object for interfacing with the GPS
AltSoftSerial ss; // The serial connection object to the GPS device
String yourPassword = "1234"; // Put the password here between the ""
String password; // Temporary variable used for comparison of passwords
GSM gsmAccess; // Initialise the library instances
GSM_SMS sms;
char senderNumber[20]; // Array to hold the number a SMS is retreived from

void setup()
{
  ss.begin(GPSBaud); // begin the GPS serial connection
  Serial.begin(9600); // begin Serial communication with the computer at 9600 baud rate
  Serial.println("Where is my Car?"); // Print to the computer
  Serial.println("Initialising...");
  boolean notConnected = true; // connection state

  while (notConnected) // until it connects
  {
    if (gsmAccess.begin(PINNUMBER) == GSM_READY) // if it succeeds connecting
      notConnected = false; // connected
    else
    {
      Serial.println("Not connected"); // print to the computer
      delay(1000); //delay
    }
  }
  Serial.println("GSM initialized");      // print to the computer
  Serial.println("Waiting for messages");
  
}


void loop()
{

  while (ss.available() > 0) //while there is stuff in the buffer
    if (gps.encode(ss.read())) //if it can successfully decode it, do it. Else try again when more charachters are in the buffer

      if (sms.available()) // if a text has been recieved
      {
        Serial.println("Message received from:"); // print to the computer

        sms.remoteNumber(senderNumber, 20); // assign the sender number to the "senderNumber" variable
        Serial.println(senderNumber); // print the sender number to the computer
        
        password = ""; // flush the temporary variable
        char c;
        while (c = sms.read())
        {
          password += c; // append the sms to the "password" variable
        }

        Serial.println(password); // print the contents of the sms
        Serial.println("\nEND OF MESSAGE"); // print to the computer
        sms.flush(); // delete message from modem buffer
        Serial.println("MESSAGE DELETED"); // print to the computer

        if (password == yourPassword) // if the sms contains the correct password
        {
          Serial.println("\nPASSWORD VALID"); // print to the computer
          sms.beginSMS(senderNumber); // begin an sms to the sender number
          sms.print("https://www.google.com/maps/place/");
          sms.print(gps.location.lat(), 6); // append the lat to the sms
          sms.print(","); // append a comma
          sms.print(gps.location.lng(), 6); // append the lon to the sms
          sms.endSMS(); //send the sms
        }
        else {
          Serial.println("\nPASSWORD NOT VALID"); // print to the computer
      }
    }
  delay(1000); // delay
}

SMS_GPS_Using_AT_Command_First_Draft.ino

C#
/********************************************************************
 *  AltSoftSerial always uses these pins for Serial communication:  *
 *                                                                  *
* Board          Transmit  Receive   PWM Unusable                   *
* -----          --------  -------   ------------                   *
* Teensy 3.0 & 3.1  21        20         22                         *
* Teensy 2.0         9        10       (none)                       *
* Teensy++ 2.0      25         4       26, 27                       *
* Arduino Uno        9         8         10                         *
* Arduino Leonardo   5        13       (none)                       *
* Arduino Mega      46        48       44, 45                       *
* Wiring-S           5         6          4                         *
* Sanguino          13        14         12                         *
*********************************************************************/
#include <SoftwareSerial.h>
#include <TinyGPS++.h>
#include <AltSoftSerial.h>
SoftwareSerial gsm(2, 3); // RX, TX
TinyGPSPlus gps; // The TinyGPS++ object for interfacing with the GPS
AltSoftSerial ss;
char c;

void setup() {
  pinMode(12,OUTPUT);
  pinMode(7, OUTPUT);
  Power_gsm();
  ss.begin(9600); // begin the GPS serial connection
  gsm.begin(9600);  // Start software serial
  delay(2000);
  gsm.println("AT+CMGF=1"); // AT+CMGF Select SMS Massage Format
  delay(100);
  gsm.println("AT+CNMI=1,2,0,0,0"); // AT+CNMI New SMS Massage INdication
  delay(100);
  gsm.println("AT+CMGD=1,4"); // AT+CMGD=1,4 Range of SMS on SIM card Can be deleted
  delay(1000);
  //SMSsend("SYSTEM IS ON:)");//2
  gsm.flush(); // deleting all massages at the first boot
  delay(100);
}


void loop() {

  while (ss.available() > 0) 
    if (gps.encode(ss.read())) 
      while (gsm.available()) {
        delay(3);
        c = gsm.read();
        if (c == 'D') { 
          if (gsm.available()) {
            delay(3);
            c = gsm.read();
            if (c == 'o') {
            digitalWrite(12,HIGH);
            Senddata();
            }
            else if (c == 'c') {
            digitalWrite(12, LOW); 
           }
         }
       }
    }
   gsm.flush();
 }
 
void Power_gsm() {
  digitalWrite(7, HIGH);
  delay(3000);
  digitalWrite(7, LOW);
}

void Senddata() {
  gsm.print("AT+CMGF=1\r");  //  Carriage return
  delay(100);
  gsm.println("AT+CMGS=\"+966566996070\""); // replace x by your number ("AT+CMGS=\"+966xxxxxxxxx\""); to yours
  delay(100);                            // (+966) is the country code replace it with yours
  gsm.print("https://www.google.com/maps/place/");
  gsm.print(gps.location.lat(), 6); 
  gsm.print(","); 
  gsm.print(gps.location.lng(), 6);
  gsm.println((char)26);
  gsm.println();
  delay(100);
  gsm.println("AT+CMGD=1,4");  // some AT Commands
  delay(100);
  gsm.println("AT+CMGF=1");
  delay(100);
  gsm.println("AT+CNMI=1,2,0,0,0");
  delay(200);
}

Credits

Ali

Ali

4 projects • 3 followers
Electrical Engineer

Comments