Tutorial: Real Time GPS / GSM Tracker in Arduino

Video:



Description:
This is  a tutorial on how to start making your own GPS/GSM/GPRS Realtime tracking device which you can use for your robotics and iot projects.


Materials / Parts List:
1. Arduino Board
2. Any GPS Module
3. Any GSM/GPRS Module
4. 1x Resistor 1M
5. 1x Resistor 100K
6. 3x Resistor 220 ohm
7. 15uf 25v Capacitor
8. Breadboard / With Wires
9. External Power Supply min., 2Amp

Schematics:


Source Code:
#include "TinyGPS++.h"
#include "SoftwareSerial.h"
const byte _lBat = 2;
const int gpsL = 9;
SoftwareSerial gps_SerCon(5, 6); //tx, rx
TinyGPSPlus gps;
const byte rxPin = 10;        // aGPTX
const byte txPin = 11;        // aGPRX
const byte _lSim = 7;
SoftwareSerial _aGPR (rxPin, txPin);
char _regSimNo[] = "ATD09088638616";
char _smsPush[] = "AT+CMGS=\"09088638616\"\r";
float latThresh = 11.582408;//opyinal
float lonThresh = 122.758041;//optinal
float locStart = 0.000000;
float _poslocThresh = 200.000000; //distance thrghh
float _neglocThresh = -0.000060;
float locDiff = 0.000000;
float curLat = 0.000000;
float curLon = 0.000000;
float curLoc = 0.000000;

const int NUM_SAM = 10;
int _sumVCC = 0;
float r_Vol;
unsigned char s_count = 0;
float vol = 0.0;
int l_Pow = 0;
float v_Tresh = 5.60;




void setup()
{
  delay(4000);
  Serial.begin(115200);
  pinMode(gpsL, OUTPUT);
  pinMode(_lSim, OUTPUT);
  pinMode(_lBat, OUTPUT);
  p_Check();
  _parseGPRS();
  GPSCheck();
  //_pushSMS();
}

void loop()
{
  p_Check();
  _parseGDat();
  delay(50);
}


void GPSCheck() {
  delay(3000);

  gps_SerCon.begin(9600);
  Serial.println("GPS Marking...");
  digitalWrite(gpsL, HIGH);
  delay(500);
  while (locStart < 1.000000) {
    digitalWrite( _lBat, HIGH);
    digitalWrite(gpsL, LOW);
    digitalWrite(_lSim, HIGH);
    delay(15);
    for (int x = 0; x < 20; x++) {
      while (gps_SerCon.available())
      {
        gps.encode(gps_SerCon.read());
      }
      if (gps.location.isUpdated())
      {
        delay(50);
        latThresh = gps.location.lat();
        lonThresh = gps.location.lng();
        locStart = latThresh + lonThresh / 2;
        Serial.println(locStart, 6);
      }
      delay(15);
    }
    digitalWrite( _lBat, LOW);
    digitalWrite(gpsL, HIGH);
    digitalWrite(_lSim, LOW);
    delay(50);
  }
  digitalWrite(_lSim, HIGH);
  digitalWrite(gpsL, LOW);
  Serial.println("GPS Start");
}
void _parseGDat() {
  while (gps_SerCon.available())
  {
    digitalWrite(gpsL, LOW);
    gps.encode(gps_SerCon.read());
  }
  if (gps.location.isUpdated())
  {
    digitalWrite(gpsL, HIGH);
    delay(50);
    curLat = gps.location.lat();
    curLon = gps.location.lng();
    curLoc = curLat + curLon / 2;
    locDiff =  abs((locStart - curLoc) * 1000000);
    //Serial.print("Starting Location: ");
    //Serial.println(locStart,6);
    //Serial.print("Current Location: ");
    //Serial.println(curLoc,6);
    Serial.print("Location Difference: ");
    Serial.println(locDiff, 6);
    if (locDiff > _poslocThresh) {
      Serial.println("Sending Text Message");
      delay(100);
      digitalWrite( _lBat, HIGH);
      digitalWrite(gpsL, HIGH);
      digitalWrite(_lSim, HIGH);
      delay(5000);
      digitalWrite( _lBat, LOW);
      digitalWrite(gpsL, LOW);
      //_pushSMS();
      Serial.print(locStart, 6); Serial.print("  =>  ");
      locStart = curLoc;
      Serial.println(locStart, 6);
    }
  }
}
void _parseGPRS() {

  Serial.begin(115200);
  _aGPR.begin(115200);
  delay(5000);
  _aGPR.println("AT");
  digitalWrite(_lSim, HIGH);
  for (int timer = 0; timer < 3; timer++) {
    delay(10);
    String inData = _aGPR.readStringUntil('\n');
    Serial.println("Got response from AT: " + inData);
  }
  delay(100);
  _aGPR.println(_regSimNo);
  for (int timer = 0; timer < 25; timer++) {
    digitalWrite(_lSim, LOW);
    delay(15);
    String inData = _aGPR.readStringUntil('\n');
    Serial.println("Got response from AT: " + inData);
    digitalWrite(_lSim, HIGH);
    delay(15);
  }
  _aGPR.println("ATH");
  digitalWrite(_lSim, LOW);
  delay(3000);
  Serial.println("Done!");
  digitalWrite(_lSim, HIGH);
}
void _pushSMS() {
  _aGPR.println("AT+CMGF=1");
  delay(1000);
  _aGPR.println(_smsPush);
  delay(1000);
  _aGPR.println(gps.location.lat(), 6);
  delay(100);
  _aGPR.println(gps.location.lng(), 6);
  delay(100);
  _aGPR.println((char)26);
  delay(3000);
  _aGPR.println("AT+CMGD=1,4");
  delay(3000);
}


void p_Check() {
  l_Pow = _vccInStat();
  delay(75);
  while (l_Pow > 0) {
    digitalWrite(2, HIGH);
    delay(75);
    digitalWrite(2, LOW);
    delay(75);
    l_Pow = _vccInStat();
  } digitalWrite(2, LOW);
}
int _vccInStat() {
  while (s_count < NUM_SAM) {
    _sumVCC += analogRead(A0);
    s_count++;
    delay(10);
  }
  vol = ((float)_sumVCC / (float)NUM_SAM * 5.015) / 1024.0;
  r_Vol = (vol * 11.132);
  if (r_Vol < v_Tresh) {
    l_Pow = 1;
  } else {
    l_Pow = 0;
  }
  s_count = 0;
  _sumVCC = 0;
  return l_Pow;
}

Comments

  1. gumgawa din ako similar project every time ipush yung button magsesend ng text yung gsm n may link n map ng para sa location , ganito po b yung concept?

    ReplyDelete
    Replies
    1. Hi Nathan, no its not, the design is to automatically update you kapag nagkaroon ng changes sa present position by 200 meters sa next position. you can change the distance settings sa code.

      Delete
    2. but it uses both gsm and gps module? baka kasi magkaron ng problem dahil parehas silang software serial ang gmit,...any suggestion ? pwede ko ng modify yung code imbes n 200 meters push button?
      can you help me? salamat

      Delete
    3. The code works using both serialsoftware, you'll just have to assign pins for the serial library, pero if you dont' want to use gsm ok din, to use a button you have to tweak the code by adding conditional statement like this:

      int button = 13 // attach button pin here
      int buttonState;

      digitalRead (buttonState);

      if(buttonState > 0) {
      _parseGDat();
      }

      Delete
  2. Sir, pareho man amon dira gina himo nga thesis puwede na sa nga ma detect ya ang gps through sms nga ma send sang message sa database then ma appear sa map imo nga device?

    ReplyDelete
  3. pwede ko b gamitin sir yung SIM808 ng may built-in GPS. tnx

    ReplyDelete
  4. Contact Info po please. Thank you!

    ReplyDelete

Post a Comment

Popular Posts