Created
January 28, 2020 09:41
-
-
Save dextersiah/68949d78fff5e6a03c6be575f9882e2a to your computer and use it in GitHub Desktop.
An updated version of GPS Tracking for both python 2 and 3.7 that was created by shiyazt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This code is only workable on python 2 | |
import httplib, urllib | |
import time | |
from GPS_API import * | |
import serial | |
ser = serial.Serial("/dev/serial0") # Select your Serial Port | |
ser.baudrate = 9600 # Baud rate | |
ser.timeout = 0.5 | |
sleep = 2 # how many seconds to sleep between posts to the channel | |
key = '<YOUR WRITE API KEY>' # Thingspeak Write API Key | |
msgdata = Message() # Creates a Message Instance | |
# This Function will upload Latitude and Longitude values to the Thingspeak channel | |
def upload_cloud(): | |
temp = get_latitude(msgdata) | |
temp1 = get_longitude(msgdata) | |
params = urllib.urlencode({'field1': temp,'field2': temp1, 'key':key }) | |
headers = {"Content-typZZe": "application/x-www-form-urlencoded","Accept" : "text/plain"} | |
conn = httplib.HTTPConnection("api.thingspeak.com:80") | |
try: | |
conn.request("POST", "/update", params, headers) | |
response = conn.getresponse() | |
print("Lat:",temp) | |
print("Long:",temp1) | |
print(response.status, response.reason) | |
data = response.read() | |
conn.close() | |
except KeyboardInterrupt: | |
print("Connection Failed") | |
if __name__ == "__main__": | |
start_gps_receiver(ser, msgdata) | |
time.sleep(2) | |
ready_gps_receiver(msgdata) | |
while True: | |
upload_cloud() | |
time.sleep(sleep) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This code is for python 3 and above | |
# You would need to install http.client with pip3 install http.client | |
import http.client, urllib.request, urllib.parse, urllib.error | |
import time | |
from GPS_API import * | |
import serial | |
ser = serial.Serial("/dev/serial0") # Select your Serial Port | |
ser.baudrate = 9600 # Baud rate | |
ser.timeout = 0.5 | |
sleep = 2 # how many seconds to sleep between posts to the channel | |
key = '<YOUR WRITE API KEY>' # Thingspeak Write API Key | |
msgdata = Message() # Creates a Message Instance | |
def upload_cloud(): | |
temp = get_latitude(msgdata) | |
temp1 = get_longitude(msgdata) | |
params = urllib.parse.urlencode({'field1': temp,'field2': temp1, 'key':key }) | |
headers = {"Content-typZZe": "application/x-www-form-urlencoded","Accept" : "text/plain"} | |
conn = http.client.HTTPConnection("api.thingspeak.com:80") | |
try: | |
conn.request("POST", "/update", params, headers) | |
response = conn.getresponse() | |
print(("Lat:",temp)) | |
print(("Long:",temp1)) | |
print((response.status, response.reason)) | |
conn.close() | |
except KeyboardInterrupt: | |
print("Connection Failed") | |
if __name__ == "__main__": | |
start_gps_receiver(ser, msgdata) | |
time.sleep(2) | |
ready_gps_receiver(msgdata) | |
while True: | |
upload_cloud() | |
time.sleep(sleep) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This File consists of API calls for the GPS Parsing Functionality.This section parse the GPS values from the GPS Module. | |
# pynmea2 is Python library for parsing the NMEA 0183 protocol (GPS). To get that thing : pip install pynmea2 | |
import threading | |
import pynmea2 | |
import sys | |
class Message: | |
def __init__(self): | |
self.msg ='' | |
# Gps Receiver thread funcion, check gps value for infinte times | |
def get_gps_data(serial, dmesg): | |
print("Initializing GPS\n") | |
while True: | |
strRead = serial.readline() | |
if sys.version_info[0] == 3: | |
strRead = strRead.decode("utf-8","ignore") | |
if strRead[0:6] == '$GPGGA': | |
dmesg.msg = pynmea2.parse(strRead) | |
else: | |
if strRead.find('GGA') > 0: | |
dmesg.msg = pynmea2.parse(strRead) | |
# API to call start the GPS Receiver | |
def start_gps_receiver(serial, dmesg): | |
t2 = threading.Thread(target=get_gps_data, args=(serial, dmesg)) | |
t2.start() | |
print("GPS Receiver started") | |
# API to fix the GPS Revceiver | |
def ready_gps_receiver(msg): | |
print("Please wait fixing GPS .....") | |
dmesg = msg.msg | |
while(dmesg.gps_qual != 1): | |
pass | |
print("GPS Fix available") | |
# API to get latitude from the GPS Receiver | |
def get_latitude(msg): | |
dmesg = msg.msg | |
return dmesg.latitude | |
# API to get longitude from the GPS Receiver | |
def get_longitude(msg): | |
dmesg = msg.msg | |
return dmesg.longitude |
Hello @kazim425 I have same error with you, did you already fix your error?
Hi @danii998 sorry this is quite a legacy code that I had used back in my school. I took a quick look at the code and google and can u try this piece of code of it does solve the issue?
def ready_gps_receiver(msg):
print("Please wait fixing GPS .....")
dmesg = msg.msg
while not hasattr(dmesg, 'gps_qual') or dmesg.gps_qual != 1:
dmesg = msg.msg
print("GPS Fix available")
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Dear author
I am also getting the following error after running your script
nitializing GPS
GPS Receiver started
Please wait fixing GPS .....
Traceback (most recent call last):
File "ConnectionV3.py", line 36, in
ready_gps_receiver(msgdata)
File "/home/pi/Documents/gps/GPS_API.py", line 35, in ready_gps_receiver
while(dmesg.gps_qual != 1):
AttributeError: 'str' object has no attribute 'gps_qual
Any hints please.