Home Contact us


Mobile GPS Tracking



//-----------------------------------------------------------------------------
// GPS Tracker.vpl, created 2003-04-28 13:02
//
//
// Program that sends a VSMS PDU message each 10 seconds, IF it has a valid gps position
//
// In order to run this example, you need the following:
// 1. Install and configure the RTCU GPRS Gateway product
// 2. Insert a GPRS enabled SIM card in the RTCU unit
// 3. Configure the TCP/IP and Gateway settings in the RTCU, using the RTCU-IDE program
// 4. Modify the variable "DestNodeID" to the correct nodeid for the receiver
// 5. Download this project into the RTCU unit
//
// When running correctly, LED1 will turn on after 30..50 seconds, signalling that 
// the unit is now connected to the RTCU GPRS Gateway. LED2 will blink every time
// the unit gets a correct GPS position (2D or 3D)
// The program will then send a VSMS message every 10 seconds to the "DestNodeID"
// with information about the current GPS time (in UTC) and lattitude/longitude
//
// Protocol:
// 
// where all 3 fields are 4 byte words, little endian format, 12 bytes in total
// 
//-----------------------------------------------------------------------------
INCLUDE rtcu.inc
INCLUDE
 tcpip.inc

VAR_OUTPUT
  gpsOK : BOOL; | Monitors valid GPS fix
  gwOK  : BOOL; | Monitors connection to the RTCU GPRS Gateway
END_VAR;

// These are the global variables of the program
VAR
   DestNodeID : STRING:="@4711"; // The nodeid of the receiver
   TXBuf      : ARRAY[0..139] OF SINT; // Holds the transmit buffer
   tSend      : DINT; // Linsec value of next time to send a VSMS message

   gps        : gpsFix; // GPS position information
   Lat        : DINT; // Lattitude
   Lon        : DINT; // Longitude
   gpsTime    : DINT; // GPS time (in UTC !)
END_VAR;

//----------------------------------------------------------------------------
// Misc. helper functions
// Pack/Unpack INT and DINT's from an array of SINT
//----------------------------------------------------------------------------
function getINT:int;
var_input
   adr : ptr;
end_var
;
memcpy
(dst:=addr(getInt),src:=adr,len:=sizeof(getINT));
end_function
;

function
 getDINT:dint;
var_input
   adr : ptr;
end_var
;
memcpy
(dst:=addr(getDINT),src:=adr,len:=sizeof(getDINT));
end_function
;

function
 setINT;
var_input
   adr : ptr;
   v :
 int;
end_var
;
memcpy
(dst:=adr,src:=addr(v),len:=sizeof(v));
end_function
;

function
 setDINT;
var_input
   adr : ptr;
   v :
 dint;
end_var
;
memcpy
(dst:=adr,src:=addr(v),len:=sizeof(v));
end_function
;

//----------------------------------------------------------------------------


PROGRAM GPS_Tracker;

// Switch power on to the GSM and GPS modules
gpsPower(power:=true);
gsmPower
(power:=true);

// Send next position in 10 seconds
tSend:=clockNow() + 10;

// Activate the GPRS support, and connect to the internet
DebugFmt(message:="gprsOpen=\1", v1:=gprsOpen());

BEGIN
   gps();
   // If time to send a new position...
   IF clockNow() > tSend THEN
      // If we have a 2D or 3D fix, go build buffer, and send as a PDU SMS message
      IF gps.mode > 1 THEN
         // Calculate next time we need to send
         tSend:=clockNow() + 10;

         // Get GPS time as linsec value
         gpsTime:=gps.linsec;

        // Get Lat/Lon as two DINT values
        Lat:=gps.latitude;        //  ddmm.mmmm * 10000
        Lon:=
gps.longitude;       // dddmm.mmmm * 10000

        // Show the calculated values in the debug window
        DebugFmt(message:="gpsTime=\4", v4:=gpsTime);
        DebugFmt
(message:="Lattitude=\4", v4:=Lat);
        DebugFmt
(message:="Longitude=\4", v4:=Lon);

        // Pack lattitude, longitude and GPS time into the TX buffer (as little endians !) 
        setDint(adr:=addr(TXBuf[0]), v:=gpsTime); 
        setDint
(adr:=addr(TXBuf[4]), v:=Lat); 
        setDint
(adr:=addr(TXBuf[8]), v:=Lon);

        // demonstrates how to extract data from a "raw" buffer (not needed in this example)
        //DebugFmt(message:="gpsTime=\4", v4:=getDINT(adr:=addr(TXBuf[0])));
        //DebugFmt(message:="Lattitude=\4", v4:=getDINT(adr:=addr(TXBuf[4])));
        //DebugFmt(message:="Longitude=\4", v4:=getDINT(adr:=addr(TXBuf[8])));

        // Send a binary SMS message with the contents to the destination node (via the GPRS Gateway)
        DebugFmt(message:="gsmSendPDU=\1", v1:=gsmSendPDU(phonenumber:=DestNodeID, message:=ADDR(TXBuf), length:=12));

        // Enable the next line to get the PDU message in the Unit->SMS messages window also
        //gsmSendPDU(phonenumber:="9999", message:=ADDR(TXBuf), length:=12);
      END_IF;
   END_IF
;

   gwOK:=
gwConnected();

   // If valid fix from GPS, change state of LED
   IF gps.mode > 1 THEN
      gpsOK:=NOT gpsOK;
   END_IF
;

END
;
END_PROGRAM
;