0

I can't run this gps program in Thonny IDE. How come I can only run this gps program when I run it in the terminal without sudo? Is there a way to fix this? EDIT: The part without the print(gps) and the missing colon is a typo

import serial
import time 
import string
import pynmea2
while True:
    port=”/dev/ttyAMA0”
    ser=serial.Serial(port, baudrate=9600, timeout=0.5)
    dataout = pynmea2.NMEAStreamReader()
    newdata=ser.readline()
if newdata[0:6] == “$GPRMC”
    newmsg = pynmea2.prase(newdata)
    lat = newmsg.latitude
    lng = newmsg.longitude
    gps = “Latitude= ” + str(lat) + “ and Longitude= ” + str(lng)

When I run it in Thonny IDE, it only says Run gps.py with nothing below.

EDIT: This code from @tlfong01 in link #1 seemed to do the trick.

import serial
import time
import string
import pynmea2

port="/dev/ttyAMA0" ser=serial.Serial(port, baudrate=9600 , parity=serial.PARITY_NONE,

stopbits=serial.STOPBITS_ONE,

bytesize=serial.EIGHTBITS,)

while True: dataout = pynmea2.NMEAStreamReader() newdata=ser.readline() if (newdata[0:6] == b"$GPRMC"): newmsg=pynmea2.parse(newdata.decode("utf-8"))

            lat=newmsg.latitude
            lng=newmsg.longitude

            gps = "Latitude=" + str(lat) + "and Longitude=" + str(lng)

            print(gps)

Ilian
  • 11
  • 4

1 Answers1

1

Question

Rpi Thonny python IDE has a problem running a pynmea2 GPS program. How to fix it?


Answer

Part A - Troubleshooting notes

  1. My Thonny is python 3, but your program is python2, so I need to format a bit:

    (a) double quote to single quote,

    (b) add colon after if statement.


  1. Thonny then gave "import pynmea2" error, so I used Thonny package management tools to install pynmea2, then I can run the program without any error message.

  1. Your program seems to read the pynmea2's reader output OK, but then you check the new data without any decision, such as:

    (a) If cannot read GPS data, then print error message "Cannot find GPS data",

    (b) If GPS data correctly read, then print out GPS data.


  1. I suggest you to set up the GPS thing and try again. I am using Neo-6/7/8M. If you are using the same thing, I might try to compare and contrast your results.

  1. Of course you might not have setup your GPS hardware/software correctly, and have a problem getting fix, so you program would not print any GPS data. You might like to read my test results Ref 1 on how to use python to print out Neo-8M GPS results. Actually the GPS NMEA data file is very simple. So it should be easy to extract the data, not necessary from Neo-6/7/8M.

Part B - Tips for GPS newbies

  1. The OP follows the video by the Sparklers which uses CLI (Command Line Interface), which is a bit tedious and easy to go wrong for newbies.

  2. I would suggest newbies to start with Windows ublox u-Centre which is GUI (Graphics User Interface) and therefore very user friendly. Actually you don't any Rpi, just put it away and learn the basics with Windows ublox U-Centre.


References

(1) Rpi Neo-6M / Neo-8M GPS Module Setup/Fix/Update Problems (Using Thonny python to print out GPS fixing data) - RpiSE, Asked 2020jun18, Viewed 1k times

(2) GPS taking long time to fix out in the open - EESE, Asked 2020sep03, Viewed 2k times

(3) How can Rpi listen to a GPS module? - RpiSE, 2020jun02, Viewed 1k times

(4) YouTube Video on How to use Neo 6M GPS module with Raspberry Pi and Python (11:19 The OP's program) - Sparklers 2019jul23


Seamus
  • 21,900
  • 3
  • 33
  • 70
tlfong01
  • 4,665
  • 3
  • 10
  • 24
  • 1
    Sure! I would appreciate it if we could compare our GPS results. 2. where can I find the Thonny package management tools? I only used pip. 3. Do you mean I need an else statement? I'm not sure if it's a hardware problem. It works fine when i run it in the terminal by typing python gps.py – Ilian May 19 '21 at 09:27
  • 1
    Please read Ref 1 for my Rpi Thonny python IDE GPS results. Cheers. – tlfong01 May 19 '21 at 09:29
  • 1
    I see what you mean now. For some reason, the part with printf(gps) was left out when I copied it to my computer. – Ilian May 19 '21 at 09:43
  • 1
    How nice to hear that you are also using Neo GPS sensors. As suggested in my answer, Neo ulbox u-centre is really newbie friendly. Happy GPS playing. Cheers. – tlfong01 May 19 '21 at 10:08
  • 1
    Really appreciate the long detailed answer – Ilian May 19 '21 at 10:11