The goal of this tutorial is to create a headless and portable GPS tracker that can be activated by an SMS. Once activated, the Raspberry Pi GPS tracker sends an SMS every 5 minutes with the geolocation of the device to a predefined phone number. The Tracker doesn’t need an internet connnection and is solely controlled by SMS messages.
Personally I was aiming for a small footprint, so I could hide this device inside my bike. If the bike ever gets stolen, I’d just have to activate the tracker and would know the current location.
1. Hardware that is needed
Before setting up your Raspberry Pi, make sure the Waveshare HAT is connected to the Pi’s GPIO Headers. Connect the antennas and insert the SIM-Card into the GSM HAT. Make sure the SIM-Card doesn’t require a PIN-Code to unlock! If it does, disable it in your phone.
2. Headless Setup of the Raspberry Pi
We are going to setup a headless version of Raspberry Pi OS lite. So, head to the Raspberry Pi website and download the Raspberry Pi Imager. When runing the tool, make sure you select Raspberry Pi OS lite as the desired Operating System and chose your empty SD-Card to flash the image.
Once finished flashing the image, eject the SD-Card from your computer and plug it back in. A boot folder should appear. Inside the boot folder we have to add two files. For headless setup, SSH can be enabled by placing a file named
ssh, without any extension, onto the boot partition of the SD-Card. When the Pi boots, it looks for the
ssh file. If it is found, SSH is enabled and the file is deleted. The content of the file does not matter; it could contain text, or nothing at all.
To make the Pi accessible via SSH from another PC, it has to connect to your Wifi. You will need to create a
wpa_supplicant.conf file for your particular wireless network and place it into the boot folder. Inside the file you’ll need to put the following code:
country=<Insert 2 letter ISO 3166-1 country code here>
ssid="<Name of your wireless LAN>"
psk="<Password for your wireless LAN>"
Please adjust the code for your country code, ssid and wifi password (psk). Once you are done and the
wpa_supplicant.conf files are placed inside the boot folder, you can boot up the Raspberry Pi with your prepared SD-Card.
3. Accessing the Raspberry Pi via SSH
To access your Raspberry Pi you can use any SSH client. I’m using PuTTY. Look up the ip address of your Raspberry Pi in your router settings and connect to your Pi using your SSH client and the standard login credentials pi/raspberry. It is recommended to change your password on your first login.
4. Installing necessary software
Run the following commands inside a new terminal session:
sudo apt-get update && sudo apt-get upgradesudo apt install git python-setuptools python-pip wiringpi screengit clone https://github.com/adammck/pygsm.gitgit clone https://github.com/olaf-hoops/pitrackercd pygsmsudo python setup.py installsudo pip install crcmod
Inside your Raspberry Pi’s home fodler navigate to the pitracker folder
cd pitracker and edit the the tracker.py file:
Find the line
#Set mobile number here and change it to the number on which you want to receive the location updates via SMS. If you are using another device than the Raspberry Pi Zero W, you might have to change the line where it says
GsmModem(port="/dev/serial0"). The Pi Zero is using the port serial0 to communicate with the GSM hat. This might be different on other Raspberry devices. Exit the editor with ctrl+x and save your changes.
Inside the pitracker folder run the following command:
sudo chmod a+x tracker.py
We now want to have the Raspberry Pi listen for incoming SMS that contain the trigger word Start. So when the Raspberry Pi gets triggered by receiving an SMS with the Start keyword, it will start sending the geolocation using the GPS module of the Waveshare GSM/GPRS HAT. We can achieve this by manually running the
tracker.py file inside the pitracker folder. But it would be much more comfortable if the Pi would run the script automatically every time on boot up. For that reason we are setting up a crontab:
sudo crontab -e
Select nano if you are prompted to choose an editor. At the end of the file add the following line:
Exit the editor with ctrl+x and save your changes.
5. Test your GPS Tracking Device
Everything is set up and should be good to go now. It’s time to test your device. Reboot your Raspberry Pi and wait a few minutes to make sure the GPS module is able to detect a signal. Your device could have trouble picking up a GPS signal indoors, so you might want to try this somewhere outside. Just hook up your Raspberry Pi to the power bank and move it out of your house. It doesn’t require Wifi anymore at this point.
Send an SMS with the word Start to the phone number of the SIM-Card inside your Raspberry Pi. You should get back an SMS with the current location of the device and a link to Google Maps.
When booting up the Respbarry Pi, it should automatically start the tracker.py script to listen for incoming SMS. You can check if this is working by establishing an SSH connection and reattaching to the screen session of the the tracker.py script.
sudo screen -r pitracker
You should see the modem details in the screen session and the Pi waiting for messages.
If you can’t see this screen, check if you setup the crontab correctly. Also try to run the tracker.py script inside the pitracker folder manually.
sudo python tracker.py
You should be able to see the script reacting to incoming SMS. The keyword Start should trigger the sending of the current location. If you want to stop the script, just send another SMS with the keyword Stop to the Raspberry Pi.
Please comment below for any further questions.
I want to give credit to David Akerman for most of the code used here.