Working with GPS on Raspberry Pi 3 Python

It is a memo dealing with GPS module in Python of Raspberry Pi 3. OS version of Raspberry Pi 3

~~Raspbian GNU/Linux 8.0 (jessie)~~ Raspbian GNU / Linux 9.4 (stretch) (Fixed October 5, 2018)

is. Raspberry Pi3 + GPS

GPS module

For the GPS module, I used Akizuki Denshi's "GPS receiver kit 1 compatible with" Michibiki "with PPS output".

This GPS module sends out the received GPS data serially. There are two ways to serially communicate with the Raspberry Pi 3: using the USB port or using the GPIO pin. When using a USB port, a USB serial conversion module is required, so I used GPIO pins this time. Connect the Raspberry Pi 3 and the GPS module as follows.

Raspberry Pi3 GPS module
04(5v) 5V
06(Ground) GND
08(TXD0) RXD
10(RXD0) TXD

I don't use the GPS 1PPS pin, so I don't connect anything.

Serial communication with Python on Raspberry Pi 3

For serial communication with Raspberry Pi3, enable serial in raspi-config and set not to use serial as a console.

Serial enable with raspi-config

Launch raspi-config and enable serial as follows:

pi$ sudo raspi-config

Select 5 Interfacing Options and then P6 Serial. Select Yes for "Would you like a login shell to be accessible over serial?", Finish, and reboot.

pi$ sudo reboot

A device called / dev / serial0 will be created.

pi$ ls /dev/se*
/dev/serial0  /dev/serial1

Modify /boot/cmdline.txt

Modify /boot/cmdline.txt to set serial0 not to be used as a console.

pi$ cat /boot/cmdline.txt 
dwc_otg.lpm_enable=0 console=tty1 console=serial0,115200 root=/dev/mmcblk0p2 rootfstype=ext4 elevator=deadline fsck.repair=yes rootwait quiet splash plymouth.ignore-serial-consoles

Delete "console = serial0,115200" in the editor and reboot.

pi$ sudo reboot

Install the Python serial module

Install the Python serial module. (October 5, 2018: Changed from python-serial to pyserial)

pi$ pip install pyserial

If you are not using pyenv, install it with pip3.

Now you can use Python on Raspberry Pi 3 for serial communication. If you connect the GPS module to the Raspberry Pi3, launch Python as follows, and check it, you can see that the GPS data can be read.

pi$ python3
>>> import serial
>>> s = serial.Serial('/dev/serial0', 9600, timeout=10)
>>> print(s.readline())
b'\x00\x00\xc0\x00\xfe\x00pK&&KLN\xcb14,302,,06,08,254,27,30,07,230,29*70\r\n'
>>> print(s.readline())
b'$GPGSV,4,4,14,23,05,150,19,14,04,054,*74\r\n'
>>>

The first readline () is half-finished data, but the second and subsequent readline () seems to be able to read GPS data.

Python GPS library

The data sent from the GPS module is a character string in the format NMEA-0183. For the data format of NMEA-0183, the site "NMEA 0183 sentences data analysis" is easy to understand.

The GPS module receives signals from GPS satellites and displays information such as time, latitude, longitude, altitude above sea level, number and ID of satellites used for positioning, and position (azimuth and elevation) of each satellite in NMEA-0183 format. It is sent as a character string of.

There is a Python library that converts this string data into programmatically easy-to-use data. A web search found micropyGPS and pynmea2.

micropyGPS is a GPS data analysis library that runs on Python 3.x and MicroPython. When you enter GPS data, it analyzes it and adds and updates the data to the GPS object. Information such as time, latitude, longitude, number of satellites used for positioning and ID can be obtained as GPS object data. The documentation is also solid.

pynmea2 just seems to parse GPS data line by line, and the documentation looks poor.

So I decided to use micropyGPS as a library. To use it, just download micropyGPS.py from Github and place it in the directory where you start Python.

Python program

The Python program that reads GPS data looks like this: Read the data from the GPS module The process of adding and updating data to the GPS object is made into a thread, and the data is output every 3 seconds.

The time zone time difference (+9 hours in Japan) and the latitude / longitude output format are specified as arguments when creating a MicroGPS object. It seems that the following format can be specified as the output format.

'ddm'Decimal degrees, minutes(40° 26.767′ N)
'dms'Decimal degrees, minutes, seconds(40° 26′ 46″ N)
'dd'Decimal degree(40.446° N)
import serial
import micropyGPS
import threading
import time

gps = micropyGPS.MicropyGPS(9, 'dd') #Create a MicroGPS object.
                                     #Arguments are time zone time difference and output format

def rungps(): #Read GPS module and update GPS object
    s = serial.Serial('/dev/serial0', 9600, timeout=10)
    s.readline() #Discard the first line as it may read half-finished data
    while True:
        sentence = s.readline().decode('utf-8') #Read GPS data and convert it to a character string
        if sentence[0] != '$': #The beginning is'$'If not, throw it away
            continue
        for x in sentence: #Analyze the read character string and add or update data to the GPS object
            gps.update(x)

gpsthread = threading.Thread(target=rungps, args=()) #Create a thread to execute the above function
gpsthread.daemon = True
gpsthread.start() #Start a thread

while True:
    if gps.clean_sentences > 20: #Output when proper data is accumulated to some extent
        h = gps.timestamp[0] if gps.timestamp[0] < 24 else gps.timestamp[0] - 24
        print('%2d:%02d:%04.1f' % (h, gps.timestamp[1], gps.timestamp[2]))
        print('longitude latitude: %2.8f, %2.8f' % (gps.latitude[0], gps.longitude[0]))
        print('Above sea level: %f' % gps.altitude)
        print(gps.satellites_used)
        print('Satellite number: (Elevation angle,Azimuth,SN ratio)')
        for k, v in gps.satellite_data.items():
            print('%d: %s' % (k, v))
        print('')
    time.sleep(3.0)

When I run the program, the following result is output. Latitude and longitude after the decimal point are hidden.

14:02:19.0
longitude latitude: 35.********, 139.********
Above sea level: 51.900000
Positioning satellite: [17, 28, 6, 3, 193, 1, 22, 8]
Satellite number: (Elevation angle,Azimuth,SN ratio)
193: (59, 173, 40)
3: (71, 124, 24)
6: (13, 259, 27)
1: (49, 42, 16)
8: (15, 115, 21)
42: (48, 170, 34)
11: (40, 69, None)
14: (4, 47, None)
17: (42, 317, 15)
19: (20, 307, None)
22: (56, 66, 21)
23: (10, 145, None)
28: (63, 248, 38)
30: (3, 224, 16)

The next line after "Satellite number" is the satellite number, elevation angle, azimuth angle, and signal-to-noise ratio of the captured satellite. Satellite number 193 is "Quasi-Zenith Satellite No. 1 Michibiki".

Recommended Posts

Working with GPS on Raspberry Pi 3 Python
Working with sensors on Mathematica on Raspberry Pi
Connect to MySQL with Python on Raspberry Pi
GPS tracking with Raspberry Pi 4B + BU-353S4 (Python)
Try debugging Python on Raspberry Pi with Visual Studio.
Ubuntu 20.04 on raspberry pi 4 with OpenCV and use with python
Adafruit Python BluefruitLE works on Raspberry Pi.
Programming normally with Node-RED programming on Raspberry Pi 3
Run servomotor on Raspberry Pi 3 using python
Detect temperature using python on Raspberry Pi 3!
Control the motor with a motor driver using python on Raspberry Pi 3!
GPGPU with Raspberry Pi
pigpio on Raspberry pi
DigitalSignage with Raspberry Pi
Cython on Raspberry Pi
Discord bot with python raspberry pi zero with [Notes]
Detect slide switches using python on Raspberry Pi 3!
I tried L-Chika with Raspberry Pi 4 (Python edition)
Detect magnet switches using python on Raspberry Pi 3!
Enjoy electronic work with GPIO on Raspberry Pi
Power on / off your PC with raspberry pi
Try working with Mongo in Python on Mac
Get CPU information of Raspberry Pi with Python
Make DHT11 available on Raspberry Pi + python (memo)
Sound the buzzer using python on Raspberry Pi 3!
Play with your Ubuntu desktop on your Raspberry Pi 4
Build a Python development environment on Raspberry Pi
Measure CPU temperature of Raspberry Pi with Python
Detect analog signals with A / D converter using python on Raspberry Pi 3!
Use python on Raspberry Pi 3 to light the LED with switch control!
Try tweeting arXiv's RSS feed on twitter from Raspberry Pi with python
Record temperature and humidity with systemd on Raspberry Pi
Mutter plants with Raspberry Pi
Run LEDmatrix interactively with Raspberry Pi 3B + on Slackbot
Working with LibreOffice in Python
Python: Working with Firefox with selenium
Working with sounds in Python
Control brushless motors with GPIOs on Raspberry Pi Zero
Raspberry Pi + Python + OpenGL memo
raspberry pi 1 model b, python
Install pyenv on Raspberry Pi and version control Python
Output to "7-segment LED" using python on Raspberry Pi 3!
Introduced pyenv on Raspberry Pi
Troubleshoot with installing OpenCV on Raspberry Pi and capturing
Use NeoPixel on Raspberry Pi
Display USB camera video with Python OpenCV with Raspberry Pi
Let's operate GPIO of Raspberry Pi with Python CGI
Install OpenCV4 on Raspberry Pi 3
Install TensorFlow 1.15.0 on Raspberry Pi
Raspberry Pi with Elixir, which is cooler than Python
VScode intellisense doesn't work on Raspberry Pi OS 64bit! (Python)
Update Python for Raspberry Pi to 3.7 or later with pyenv
Run AWS IoT Device SDK for Python on Raspberry Pi
Python beginner opens and closes interlocking camera with Raspberry Pi
I tried running Movidius NCS with python of Raspberry Pi3
Create an LCD (16x2) game with Raspberry Pi and Python
Access google spreadsheet using python on raspberry pi (for myself)
Run BNO055 python sample code with I2C (Raspberry Pi 3B)
SSD 1306 OLED can be used with Raspberry Pi + python (Note)
getrpimodel: Recognize Raspberry Pi model (A, B, B +, B2, B3, etc) with python
Connect Raspberry Pi to Alibaba Cloud IoT Platform with Python