Tag Archives: GSM

Real-time GPS tracking with a Raspberry Pi

In this guide, we will show how to do real time tracking, use a BerryGPS-GSM and initialstate.com 

Initialstate has some great tools to easily stream data from a Raspberry Pi to Initialstate.com and show this data within a dashboard using tiles.  We will send longitude,  latitude and  speed. And use a BerryGPS-GSM to get these values and upload them via 3G.

 

You will need to create an account on Initialstate.com and then grab your access key which can be found under “My Settings”

BerryGPS-GSM setup

If you are using a BerryGPS-GSM, you can follow this guide to get the GPS working and get your Pi to connect to via 3G using PPP.

The above guide also shows how to make your Pi connect to the carrier network automatically when booted. You will need this if you plan to perform remote tracking(E.g. Asset tracking).

Install required libraries

pi@raspberrypi ~ $ sudo apt-get install python-pip
pi@raspberrypi ~ $ sudo pip install pynmea2
pi@raspberrypi ~ $ sudo pip install ISStreamer

 

Main Python Script

Here we will create the main script which will stream the GPS data to Initialstate.com.
The code below creates a separate thread which is used to monitor the serial port. This is needed because we have a pause in the main loop. The pause is there to limit how much data we upload over 3G.
If we did everything in the same thread during the pause, the serial buffer would fill up (it is FIFO) and when we get the next value from the buffer, it will be old by a few seconds. This happens every loop and eventually the data will be minutes or hours behind.

The access key below is not a valid key, it is just an example. You will need to replace it with your own key.

pi@raspberrypi ~ $ nano ~/GPStracker.py

#! /usr/bin/python
from gps import *
from time import *
import threading
import datetime
from ISStreamer.Streamer import Streamer
gpsd = None  #Setup global variable 
#Setup the Initialstate stream, give it a bucket name and the access key
streamer = Streamer(bucket_name="GPS_Tracker20190713", bucket_key="GPS_Tracker20190713", access_key="ist_W4aHj0eCkMjCD8JVpp3AMsKomys8NaD")

class GPSDcollector(threading.Thread):
   def __init__(self, threadID):
      threading.Thread.__init__(self)
      self.threadID = threadID
      global gpsd #bring it in scope
      gpsd = gps(mode=WATCH_ENABLE) #Start GPSD
      self.running = True #Start running this thread
   def run(self):
      global gpsd
      while gpsdThread.running:
        gpsd.next() 
        
if __name__ == '__main__':
  gpsdThread = GPSDcollector(1) # create a thread to collect data
  try:
    gpsdThread.start() # start it up
    while True:
        print 'GPS ' , gpsd.utc,'--> CPU time->',datetime.datetime.now().time() ,
        if (gpsd.fix.longitude<>0) and (gpsd.fix.longitude<>'nan'): #Only upload data if it is valid
          streamer.log("Location", "{lat},{lon}".format(lat=gpsd.fix.latitude,lon=gpsd.fix.longitude))
          streamer.log("speed",gpsd.fix.speed)
        print '  lat    ' , gpsd.fix.latitude,
        print '  lon   ' , gpsd.fix.longitude,
        print '  speed ', gpsd.fix.speed
        sleep(5)
  except (KeyboardInterrupt, SystemExit): #when you press ctrl+c
    print "\nKilling Thread..."
    gpsdThread.running = False
    gpsdThread.join() # wait for the thread to finish what it's doing
  print "Done.\nExiting."
  

 

Start the script automatically on boot

If you are doing remote monitoring, you would want the script to run on boot. To do this, we will create a small script which will start the main python program.

pi@raspberrypi ~ $ nano ~/GPStrackerStart.sh

copy in the below lines;

#!/bin/bash
sleep 15
python /home/pi/GPStracker.py &

The pause above is there to give the Pi time to boot and connect via PPP.

Make the script executable;

pi@raspberrypi ~ $ chmod +x ~/GPStrackerStart.sh

We will use cron to start the script every time the Pi boots;

pi@raspberrypi ~ $ crontab -e

Add the below line to the bottom

@reboot /home/pi/GPStrackerStart.sh &

Other Guides and Tutorials for BerryGPS-GSM

Get a GPS fix in seconds using assisted GPS on a Raspberry Pi with a BerryGPS-GSM

Typically, a GPS module can take a few minutes to get  Time To First Fix(TTFF), or even longer if you are in  built up areas(+20mins).  This is because the Almanac needs to be downloaded from  satellites before a GPS fix can be acquired and only a small portion of the Almanac is sent in each GPS update.

Assisted GPS speeds this up significantly by  downloading  ephemeris, almanac, accurate time and satellite status over the network, resulting in faster TTTF, in a few seconds. This is very similar how to GPS works on a smartphone.

The BerryGPS-GSM supports assisted GPS. The below video shows a comparison between assisted and normal GPS.

  • Assisted GPS takes 19secs to get a fix
  • Normal GPS takes 8min 22Sec to get a fix

 

How does the BerryGPS-GSM do this?

The two main components on the BerryGPS-GSM are;

The SARA-U201 can be configured to download GPS assist data and then pass this over the the GPS module. These two components speak to each other via i2c.

This assist data is downloaded by the SARA-U201 modem (not the Pi), therefore the modem needs to create an internal PDP (Packet Data Protocol) connection.

Once the PDP connection is made, the SARA-U201 will reach out to uBlox AssitNow servers and download the latest assist data. A valid token is needed to perform this, all BerryGPS-GSM have had this token pre-configured.

Continue reading Get a GPS fix in seconds using assisted GPS on a Raspberry Pi with a BerryGPS-GSM