I've got a doozy for you Python and Django experts out there.
I am trying to read GPS coordinates from the AdaFruit Ultimate GPS hat that is soldered to a Raspberry Pi. Further I need to make an object that when printed, spits out the current Longitude and Latitude. Lastly, I need to present this data in a Django HttpResponse.
Folder Structure:
manage.py
hud/
views/
views.py
gps_reader.py
templates/
hud/
index.html
I have a script that will gather the GPS coords among other things and print them to the console which works great. Here is that script in it's entirety:
gps_reader.py :
import os, time, threading
from gps import *
from time import *
gpsd = None #seting the global variable
os.system('clear') #clear the terminal (optional)
class GpsPoller(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
global gpsd #bring it in scope
gpsd = gps(mode=WATCH_ENABLE) #starting the stream of info
self.current_value = None
self.running = True #setting the thread running to true
def run(self):
global gpsd
while gpsp.running:
gpsd.next() #this will continue to loop and grab EACH set of gpsd info to clear the buffer
if __name__ == '__main__':
gpsp = GpsPoller() # create the thread
try:
gpsp.start() # start it up
while True:
#It may take a second or two to get good data
os.system('clear')
print 'latitude ' , gpsd.fix.latitude
print 'longitude ' , gpsd.fix.longitude
time.sleep(5) #set to whatever
except (KeyboardInterrupt, SystemExit): #when you press ctrl+c
print "\nKilling Thread..."
gpsp.running = False
gpsp.join() # wait for the thread to finish what it's doing
print "Done.\nExiting."
Here is my Django view.py:
from django.shortcuts import render
from django.http import Http404
import gps_reader.GpsPoller as GpsPoller
session = GpsPoller()
session.start()
def sensors(request):
gps_lat = "{}".format(session.fix.latitude)
return render(request, 'hud/index.html', {"gps_lat": gps_lat})
Finally, here is my simplified index.html:
<p>
Latitude: {{ gps_lat }}
</p>
When I run the development server with the above settings, I get the following:
ImportError at /hud/
No module named GpsPoller
Request Method: GET
Request URL: http://ift.tt/1Kf2EqR
Django Version: 1.8
Exception Type: ImportError
Exception Value:
No module named GpsPoller
Exception Location: /home/pi/lab/django/pi/hud/views/views.py in <module>, line 3
Python Executable: /usr/bin/python
Python Version: 2.7.3
Aucun commentaire:
Enregistrer un commentaire