GPS Python notes

Viewed 16 times

I’m stuck with my waveshare hat and it would be cool if you could help me. I would like to obtain the gps location.

I’m a python beginner and tried to use some already written code from this websites:

https://www.waveshare.com/w/upload/4/4a/GSM_GPRS_GNSS_HAT_User_Manual_EN.pdf Throws an error for the sleep function and when I outcommand it, it gives an error

https://www.rhydolabz.com/wiki/?p=18639 Throws an error for the if loop.

https://www.youtube.com/watch?v=A1zmhxcUOxw Works maybe but doesn’t receive any signal from gps hat.

Unfortunately, I assume that this code is written in Python2 and it will throw an error while executing it. All my attempts to change that code in a python3 code failed.

Do you have any recommendations for me?

Thank you very much.

 New contributor
  • Ah, let me see. So you have two problems: (1) How to get GPS data using Python 3, (2) How to convert python 2 code to python 3. For the first problem, you can read my anser to the following question: “Rpi UART to GPS Module Connection Problem”: raspberrypi.stackexchange.com/questions/98840/…. But if your WaveShare GPS module is different from mine, you need a lot to time to learn how to do GPSD. (2) If the WaveShare code is in python 2, It is not that difficult to do the conversion. You can find semiautomatic tools to help you. / 歉欲阱划岐, … – tlfong01 2 days ago
  • Obvious and simple conversions include [print “abc”] becomes [print(“abc”)]. In case the translation is not python generic, but Rpi and GPS specific, such as cannot find modules to import, then you can show us that particular code snippet and see if anybody can help you. – tlfong01 2 days ago
  • 1
    Thanks so far, first I’ll try to convert my code to python3, if I still have problems afterwards, I’ll come back to you – Philipp 2 days ago
  • I skimmed through the WaveShare GPS module user manual and find a section for Rpi (Page 29). For newbies, you need to use Rpi MiniCom (I prefer Rpi CuteCom) to give AT commands by hand. If you find manual mode OK. Then you can write a simple pyhon program to do the AT instruction/status communication (let me know if you cannot find examples). You can find SIM800 modules in this forum, which is similar to this part of manual. – tlfong01 2 days ago
  • Just now I skimmed the tutorial which you mention “Throws an error for the if loop …” is indeed the python program doing serial communication with AT commands. This is the most basic program ALL newbies need to learn. Search for “Rpi python serial “loop back” test programs” in this forum to learn more.” Let me know if you get stuck in loopback testing. Good luck and cheers. – tlfong01 2 days ago
  • I forgot to mention that (1) Tutorials using python 2, especially running in terminal text mode is almost always very OUT OF DATE. (2) You can run them in the Rpi buster GUI Thonny Python IDE. Thonny is very newbie friendly I have been using IDLE python in my Rpi3B+ for a couple of years and found it OK. But now I switched over to Thonny and find it even more newbie friendly. For example: (a) if your old programs use, TAB instead of FOUR SPACES, Thonny will do the conversion for you. If you use [print ‘abc’], Thonny hints to use [print(‘abc’). It has other nice debugging tools as well. – tlfong01 yesterday
  • You are a great help. I begin to understand the code which is written there 😀 – Philipp 16 hours ago
  • I’m going to work on it today and give you feedback afterwards. Thanks! – Philipp 16 hours ago
  • Hi @Philipp, How nice to hear that you are getting started. I read the tutorial using Python 2.7 and found it well written, only problem is python 2.7. I think it is not that difficult to translate it to Python 3.7. I still highly recommend buster Thonny python 3. I would suggest you to get to know Thonny by trying some basic examples which might be useful to your project later, such as (1) ask user to input some integers, (2) find average, and print out floating pointing values etc etc. Take it easy and go slowly. I would suggest spend a couple of days playing around. Happy learning! 🙂 – tlfong01 12 hours ago

Interfacing Gsm/Gprs/Gnss/Bluetooth Hat With Raspberry Pi 3

As in our previous posts, we make a clear picture about interfacing the GPS, GSM/GPRS modules with Raspberry pi individually.

So now its time to integrate these clustered functions in to a single device suited for Raspberry pi. A new module has been introduced with these appended functionalities and it is named as per its properties,  Gsm/Gprs/Gnss/Bluetooth Hat.

Communication system has been attained at its prime level by introducing this module as it prompts the IoT applications along with the location access. All the upcoming electronic systems will be accomplished with this communication schemes. The module will be highlighted when it is being interfaced with a Raspberry pi, as it can easily derive its functionalities. Eventually we can build our own mobile or tablet devices by the integration of these modules or can even up to more.

Here we brought the knowledge of basic interfacing between a Raspberry Pi3 and the HAT module. As in the HAT module, there are two ways for making the connections.

  • USB interfacing
  • GPIO interfacing

Here we make use of a micro USB cable for connecting Raspberry Pi with the HAT module that in-turn provides the suitable power and establish a serial communication.

SIM 868 module is being used in this device which is a complete Quad-Band GSM/GPRS module which combines GNSS technology for satellite navigation. In the left side of the module you can see a yellow jumper connected to the suitable pins. There are four pairs of pins in which the jumpers must be shorted to make three terminal pairs (A,B,C).

A: control the SIM868 through USB TO UART

B: control the SIM868 through Raspberry Pi

C: access Raspberry Pi through USB TO UART

Here we make use of A terminal, ie the jumpers must be connected vertically in the first two pins as shown in the figure given below. 


  • Power on the raspberry pi module with raspbian OS installed on it. Connect the hat module to the raspberry pi through USB as shown in the first figure.
  • Insert a sim card to the HAT module to perform GSM operations.
  • After connecting the USB, the pwr led will be in an on state. Press the PWRKEY for 3 seconds and remove it. The STA LED (status) will also be ina an on state.
  • The NET led will blink in a continuous manner and the after obtaining the range for sim card, the blinking rate will be reduced.


Here we are giving two programs in python for sending messages and for getting the GPS data on Raspberry Pi.

Code for sending messages using Gsm/Gprs/Gnss/Bluetooth Hat


Code for getting GPS data using Gsm/Gprs/Gnss/Bluetooth Hat

Before running the code for GPS a GNSS antenna connector must be connected to the Hat module. Connect a GPS antenna to that connector and place the antenna projected to clear sky for GPS initialization.

As shown in the figure above make the connections, and run the program given below in python.

Python Shell Results

Note: To obtain an internet connection through this hat module, Please refer the link given below;

http://www.rhydolabz.com/wiki/?p=16325.(Use /dev/ttyUSB0 for serial communication).

Shop With US:

  • Click here to buy Gsm/Gprs/Gnss/Bluetooth Hat For Raspberry Pi from rhydoLABZ.
  • Click here to buy Raspberry Pi 3 Starter Kit from rhydoLABZ.


Categories: Uncategorized

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: