Examples -  Mobile tracking (simple)

Top  Previous  Next

//-----------------------------------------------------------------------------
// 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 Gateway product
// 2. Insert a GPRS enabled SIM card in the RTCU device
// 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 device
//
// When running correctly, LED1 will turn on after 30..50 seconds, signalling that
// the device is now connected to the RTCU Gateway. LED2 will blink every time
// the device 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:
//   <gpstime> <lattitude> <longitude>
// where all 3 fields are 4 byte words, little endian format, 12 bytes in total
//
//-----------------------------------------------------------------------------
INCLUDE rtcu.inc
 
VAR_OUTPUT
  gpsOK : BOOL; | Monitors valid GPS fix
  gwOK : BOOL; | Monitors connection to the RTCU 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 RTCU 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 Device->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;