Rpi Thonny python IDE has a problem running Pynmea2 GPS program. How to fix it?
Ask QuestionAsked todayActive todayViewed 20 times2
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)
pi-4terminalgpsShareEditFollowClose 4Flagedited 32 mins agoasked 12 hours agoIlian2344 bronze badges New contributorAdd a comment
1 Answer
Question
Rpi Thonny python IDE has a problem running a pynmea2 GPS program. How to fix it?
Answer
Part A – Troubleshooting notes
- 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.
- 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.
- 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.
- 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.
- 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
- The OP follows the video by the Sparklers which uses CLI (Common Line Interface), which is a bit tedious and easy to go wrong for newbies not familar with linux.
- 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
(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
ShareEditDeleteFlagedited 6 mins agoanswered 1 hour agotlfong013,74233 gold badges88 silver badges2222 bronze badges
- 1Sure! 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 45 mins ago - 1Please read Ref 1 for my Rpi Thonny python IDE GPS results. Cheers. – tlfong01 42 mins ago
- 1I see what you mean now. For some reason, the part with printf(gps) was left out when I copied it to my computer. – Ilian 29 mins ago
- 1How 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 3 mins ago Edit
- 1Really appreciate the long detailed answer – Ilian 1 min ago
Categories: Uncategorized