7.9 KiB
Read GPS data - Raspberry Pi
For dis part of di lesson, you go add GPS sensor to your Raspberry Pi, and read di values wey e dey give.
Hardware
Di Raspberry Pi need GPS sensor.
Di sensor wey you go use na Grove GPS Air530 sensor. Dis sensor fit connect to plenty GPS systems to get fast and correct location. Di sensor get two parts - di main electronics of di sensor, and one external antenna wey connect with thin wire to collect radio waves from di satellites.
Dis na UART sensor, so e dey send GPS data through UART.
Connect di GPS sensor
Di Grove GPS sensor fit connect to di Raspberry Pi.
Task - connect di GPS sensor
Connect di GPS sensor.
-
Put one end of Grove cable inside di socket for di GPS sensor. E go only enter one way.
-
When di Raspberry Pi dey off, connect di other end of di Grove cable to di UART socket wey dem mark UART for di Grove Base hat wey dey attach to di Pi. Dis socket dey middle row, for di side wey near di SD Card slot, opposite di USB ports and ethernet socket.
-
Arrange di GPS sensor so di antenna wey dey attach go fit see di sky - e go better if e dey near open window or outside. E go easy to get better signal if nothing dey block di antenna.
Program di GPS sensor
Di Raspberry Pi fit now dey programmed to use di GPS sensor wey dey attach.
Task - program di GPS sensor
Program di device.
-
On di Pi and wait make e boot.
-
Di GPS sensor get 2 LEDs - one blue LED wey dey flash when e dey transmit data, and one green LED wey dey flash every second when e dey receive data from satellites. Make sure say di blue LED dey flash when you power di Pi. After some minutes, di green LED go dey flash - if e no flash, you fit need to reposition di antenna.
-
Open VS Code, either directly for di Pi, or connect am through di Remote SSH extension.
⚠️ You fit check di instructions for how to set up and open VS Code for lesson 1 if you need am.
-
For di newer Raspberry Pi wey support Bluetooth, e get wahala between di serial port wey Bluetooth dey use and di one wey di Grove UART port dey use. To fix am, do dis:
-
From di VS Code terminal, edit di
/boot/config.txtfile usingnano, one terminal text editor wey dey built-in, with dis command:sudo nano /boot/config.txtYou no fit edit dis file with VS Code because you need
sudopermissions, wey be elevated permission. VS Code no dey run dis permission. -
Use your cursor keys to go di end of di file, then copy di code below and paste am for di end of di file:
dtoverlay=pi3-miniuart-bt dtoverlay=pi3-disable-bt enable_uart=1You fit paste am using di normal keyboard shortcuts for your device (
Ctrl+vfor Windows, Linux or Raspberry Pi OS,Cmd+vfor macOS). -
Save di file and comot nano by pressing
Ctrl+x. Pressywhen dem ask if you wan save di modified buffer, then pressenterto confirm say you wan overwrite/boot/config.txt.If you make mistake, you fit comot without saving, then repeat di steps.
-
Edit di
/boot/cmdline.txtfile for nano with dis command:sudo nano /boot/cmdline.txt -
Dis file get plenty key/value pairs wey space dey separate. Remove any key/value pairs wey get di key
console. Dem go look like dis:console=serial0,115200 console=tty1You fit use di cursor keys go di entries, then delete am with di normal
delorbackspacekeys.For example, if your original file look like dis:
console=serial0,115200 console=tty1 root=PARTUUID=058e2867-02 rootfstype=ext4 elevator=deadline fsck.repair=yes rootwaitDi new version go be:
root=PARTUUID=058e2867-02 rootfstype=ext4 elevator=deadline fsck.repair=yes rootwait -
Follow di steps wey dey above to save di file and comot nano.
-
Reboot your Pi, then reconnect for VS Code after di Pi don reboot.
-
-
From di terminal, create new folder for di
piuser home directory wey dem callgps-sensor. Create file for dis folder wey dem callapp.py. -
Open dis folder for VS Code.
-
Di GPS module dey send UART data through serial port. Install di
pyserialPip package to fit communicate with di serial port from your Python code:pip3 install pyserial -
Add di code below to your
app.pyfile:import time import serial serial = serial.Serial('/dev/ttyAMA0', 9600, timeout=1) serial.reset_input_buffer() serial.flush() def print_gps_data(line): print(line.rstrip()) while True: line = serial.readline().decode('utf-8') while len(line) > 0: print_gps_data(line) line = serial.readline().decode('utf-8') time.sleep(1)Dis code dey import di
serialmodule from dipyserialPip package. E then connect to di/dev/ttyAMA0serial port - dis na di address of di serial port wey di Grove Pi Base Hat dey use for di UART port. E go clear any data wey don already dey di serial connection.Next, e define one function wey dem call
print_gps_datawey dey print di line wey e pass to am for di console.After dat, di code go dey loop forever, dey read as many lines of text as e fit from di serial port for each loop. E go dey call di
print_gps_datafunction for each line.After e don read all di data, di loop go sleep for 1 second, then e go try again.
-
Run dis code. You go see di raw output from di GPS sensor, something like dis:
$GNGGA,020604.001,4738.538654,N,12208.341758,W,1,3,,164.7,M,-17.1,M,,*67 $GPGSA,A,1,,,,,,,,,,,,,,,*1E $BDGSA,A,1,,,,,,,,,,,,,,,*0F $GPGSV,1,1,00*79 $BDGSV,1,1,00*68If you see one of di errors below when you stop and restart your code, add
try - exceptblock to your while loop.UnicodeDecodeError: 'utf-8' codec can't decode byte 0x93 in position 0: invalid start byte UnicodeDecodeError: 'utf-8' codec can't decode byte 0xf1 in position 0: invalid continuation bytewhile True: try: line = serial.readline().decode('utf-8') while len(line) > 0: print_gps_data() line = serial.readline().decode('utf-8') # There's a random chance the first byte being read is part way through a character. # Read another full line and continue. except UnicodeDecodeError: line = serial.readline().decode('utf-8') time.sleep(1)
💁 You fit find dis code for di code-gps/pi folder.
😀 Your GPS sensor program don work well!
Disclaimer:
Dis dokyument don use AI transleshion service Co-op Translator do di transleshion. Even as we dey try make am accurate, abeg make you sabi say automatik transleshion fit get mistake or no dey correct well. Di original dokyument wey dey for im native language na di one wey you go take as di main source. For important informashun, e good make you use professional human transleshion. We no go fit take blame for any misunderstanding or wrong interpretation wey fit happen because you use dis transleshion.

