Last mod: 2024.10.25
Raspberry Pi - GPS receiver
Hardware
We need two devices:
- Raspberry Pi Zero (or other version)
- GPS USB receiver
Raspberry Pi Zero OS
Select Raspberry Pi OS Lite (32-bit) on microSD card:
and write:
When we can connect over SSH to Raspberry we can go to next step.
GPS Receiver
Connect GPS receiver to Raspberry and list USB devices:
lsusb
and result:
On line:
Bus 001 Device 002: ID 1546:01a8 U-Blox AG [u-blox 8]
we can see our GPS signal receiver device.
Upgrade and install software
We need libraries to read and decode GPS signals, NMEA codes.
Verify Python version:
python --version
Upgrade and install software:
sudo apt update && sudo apt upgrade -y
sudo apt install software-properties-common
sudo add-apt-repository universe
sudo apt update
sudo apt install python3-full python3-nmea2 python3-serial -y
Receive GPS commmands
Our GPS receiver must be placed outside the building, possibly in a window, but so that a minimum of 4 GPS satellites can be seen.
From console list devices:
ls /dev/tty*
The list displayed should include the device:
/dev/ttyACM0
If the device is available, we can display NMEA messages received from GPS satellites on the console:
sudo cat /dev/ttyACM0
It has to be said that these messages are not very clear to humans.
Using the installed libraries, let's write a simple script to decode position and height. Create file:
vi nmea-receiver.py
with body:
import io, serial, pynmea2
ser = serial.Serial('/dev/ttyACM0', 9600, timeout=10.0)
sio = io.TextIOWrapper(io.BufferedRWPair(ser, ser))
while True:
try:
line = sio.readline()
msg = pynmea2.parse(line)
if isinstance(msg, pynmea2.types.talker.GGA):
print('lat={}, lng={}, alt={}'.format(msg.latitude, msg.longitude, msg.altitude))
except serial.SerialException as e:
print('Exception: {}'.format(e))
break
and run:
python3 nmea-receiver.py
After a few seconds, we should see latitude, longitude and altitude data: