Hackster is hosting Hackster Holidays, Ep. 6: Livestream & Giveaway Drawing. Watch previous episodes or stream live on Monday!Stream Hackster Holidays, Ep. 6 on Monday!
Hugo Gomes
Published © GPL3+

Track ME

Track ME is a "small" GPS, SD Card, and GSM Shield controlled by an Arduino Mega. Call me and get my location.

IntermediateFull instructions provided74,425
Track ME

Things used in this project

Hardware components

Arduino Mega 2560
Arduino Mega 2560
×1
Adafruit SD Card Shield
×1
NEO 6 GPS
×1
Adafruit SD Card Shield
×1
Seeed Studio Seeedstudio gprs shield V2.0
×1
Seeed Studio GPRS Shield V2.0
×1
Jumper wires (generic)
Jumper wires (generic)
×1
9V battery (generic)
9V battery (generic)
×1
Box
×1
Box
×1

Story

Read more

Schematics

Basic connection circuit

Electronic schematic

Code

Code

Arduino
#include "SIM900.h"
#include <SoftwareSerial.h>
#include "sms.h"
#include "call.h"

#include <TinyGPS++.h>

//To change pins for Software/Hardware Serial, use 27, 28 lines in GSM.cpp.
//To use Arduino Mega please define MEGA in line 5 in GSM.h.

#include <SPI.h>
#include <SD.h>

//Mega CS pin
const int chipSelect = 53;


//We have to create the classes for SMSs and calls.
CallGSM call;
SMSGSM sms;

char number[20];
byte stat = 0;
int value = 0;

char value_str[5];

//GPS Variable

// The TinyGPS++ object
TinyGPSPlus gps;

const char *googlePrefix = "http://maps.google.com/maps?q=";


double Lat;
double Long;
int day, month, year;
int hour, minute, second;

int num_sat;

boolean one_point_true = false;



void setup()
{

  //Serial connection.
  Serial.begin(9600); 
  
  //GPRS connection
  Serial.print(F("Starting GSM..."));
  //Start configuration of shield with baudrate.
  if (gsm.begin(9600))
    Serial.println(F("READY"));
  else Serial.println(F("IDLE"));

  //GPS connection 
  Serial.println(F("Starting GPS"));
  //GPS com port
  Serial2.begin(9600);

  //SD Card start
  Serial.print(F("Initializing SD card..."));
  // make sure that the default chip select pin is set to
  // output, even if you don't use it:
  pinMode(53, OUTPUT);

  // see if the card is present and can be initialized:
  if (!SD.begin(chipSelect)) {
    Serial.println(F("Card failed, or not present"));
    // don't do anything more:
    return;
  }
  Serial.println(F("card initialized."));

}


//******************************************************************************
//Main loop
void loop()
{

  Get_GPS(); // Get the position

  Check_call(); //See if anyone is calling


};

////////////////////////////////////////////////////////////////////////////////


//******************************************************************************
// Get the position from the GPS
void Get_GPS()
{

  while (Serial2.available() > 0)
    if (gps.encode(Serial2.read()))

      num_sat = gps.satellites.value();
  //Serial.println(num_sat);

  if (gps.location.isValid() == 1) {

    Lat = gps.location.lat();
    Long = gps.location.lng();

    if (Lat != 0 && Long != 0) one_point_true = true;

  }




  if (gps.date.isValid())
  {
    day = gps.date.day();
    month = gps.date.month();
    year = gps.date.year();
  }

  if (gps.time.isValid())
  {

    hour = gps.time.hour();
    minute = gps.time.minute();
    second = gps.time.second();
  }

  smartDelay(500);


  if (millis() > 5000 && gps.charsProcessed() < 10)
  {
    Serial.println(F("No GPS detected: check wiring."));
    while (true);
  }


}


//******************************************************************************
// This custom version of delay() ensures that the gps object
// is being "fed".
static void smartDelay(unsigned long ms)
{
  unsigned long start = millis();
  do
  {
    while (Serial2.available())
      gps.encode(Serial2.read());
  }
  while (millis() - start < ms);
}
//////////////////////////////////////////////////////////////////////////////// 

//******************************************************************************
// Check to see if anyone is calling
void Check_call()
{
  //Checks status of call
  stat = call.CallStatusWithAuth(number, 0, 0);

  //If the any incoming call
  if (stat == CALL_INCOM_VOICE_AUTH) {
    delay(100);
    //Hang up the call.
    call.HangUp(); //Reject the call

    Send_SMS();

  }
}
////////////////////////////////////////////////////////////////////////////////


//******************************************************************************
// Send an SMS with the current position or the last known position
void Send_SMS()
{


  int error;

  char Date[13];
  sprintf(Date, "%02d/%02d/%04d ", day, month, year);

  char time1[10];
  sprintf(time1, "%02d:%02d:%02d ", hour, minute, second);

  //Serial print GPS data
  /*
   Serial.print(Date);
  Serial.print("  ");
  Serial.print(time1);
  Serial.print("  ");
  Serial.print("Lat: ");
  Serial.print(Lat, 6);
  Serial.print("  ");
  Serial.print("Long: ");
  Serial.println(Long, 6);
  */

  delay(100);

  char lat_print[10];
  dtostrf(Lat, 5, 6, lat_print);

  char Long_print[10];
  dtostrf(Long, 5, 6, Long_print);


  if (num_sat >= 3 && one_point_true == true)
  {
    char sms_OK[160];
    sprintf(sms_OK, "Hugo location is now: Lat: %s, Log: %s. %s%s,+%s\n", lat_print, Long_print, googlePrefix, lat_print, Long_print);
    Serial.println(sms_OK);

    error = sms.SendSMS(number, sms_OK);
    delay(500);
    if (error == 0) //Check status
    {
      Serial.println(F("SMS ERROR"));
    }
    else
    {
      Serial.println(F("SMS OK"));
    }

  }

  else if (num_sat < 3 && one_point_true == true)
  {

    char sms_NOK[150];
    sprintf(sms_NOK, "I can not seen to find Hugo. Last time that i've saw him, was in: Lat: %s, Log: %s. %s%s,+%s\n", lat_print, Long_print, googlePrefix, lat_print, Long_print);
    Serial.println(sms_NOK);

    int error = sms.SendSMS(number, sms_NOK);
    if (error == 0) //Check status
    {
      Serial.println(F("SMS ERROR"));
    }
    else
    {
      Serial.println(F("SMS OK"));
    }

  }

  else if (one_point_true == false)
  {
    Serial.println(F("No valid GPS point"));

  }

//Save everything in the SD Card
  File dataFile = SD.open("datalog.txt", FILE_WRITE);

  // if the file is available, write to it:
  if (dataFile) {


    dataFile.print(Date);
    dataFile.print(" ");
    dataFile.print(time1);
    dataFile.print(" ");
    dataFile.print(number);
    dataFile.print(" ");
    dataFile.print(lat_print);
    dataFile.print(", ");
    dataFile.print(Long_print);
    dataFile.print(" ");
    dataFile.print(error);
    dataFile.println();

    dataFile.close();

  }
  // if the file isn't open, pop up an error:
  else {
    Serial.println(F("error opening datalog.txt"));
  }

}
////////////////////////////////////////////////////////////////////////////////

Credits

Hugo Gomes

Hugo Gomes

10 projects • 116 followers
http://www.hugogomes.net Youtube Channel - https://goo.gl/fnQLJo

Comments