GPS logger & time lapse image device: Difference between revisions

From Exceed Industries Wiki
Jump to navigation Jump to search
(Created page with "(aka Pictures & Places) This is a project that is designed to be installed into a vehicle and do two things: It tracks your GPS position and records it to a database, and it a...")
 
 
(7 intermediate revisions by the same user not shown)
Line 12: Line 12:
* apt-get install gpsd gpsd-clients python-gps sqlite3 fswebcam
* apt-get install gpsd gpsd-clients python-gps sqlite3 fswebcam
* pip install RPi.GPIO
* pip install RPi.GPIO
* My scripts on GitHub
* [https://github.com/karlexceed/PicturesAndPlace My scripts on GitHub]


== More Information ==
== More Information ==
Depending on your comfort level with Python, Linux, and/or working with the Raspberry Pi, you may have several questions about what exactly is going on with these scripts. So here I'll break down a couple of the more interesting parts. First let me say that this is by no means the only (or even the best) way to accomplish these goals. This is simply how I got it to work for me.
Depending on your comfort level with Python, Linux, and/or working with the Raspberry Pi, you may have several questions about what exactly is going on with these scripts. So here I'll break down a couple of the more interesting parts. First let me say that this is by no means the only (or even the best) way to accomplish these goals. This is simply how I got it to work for me.


Auto-start the script
== Auto-start the script ==
I took a slightly different approach here than what most people would expect. I didn't know if my project would be using a display or not, and it logs some interesting/useful data to the console as it runs, so I wanted to make sure that it would run AFTER the GUI started and would show up on screen. To this end, I accomplished the auto-start by doing two things; first, I created a shell script in the pi user's home directory that just calls my Python scripts (saved as launcher.sh). Of course, you must "chmod +x" this file to be able to execute it.
I took a slightly different approach here than what most people would expect. I didn't know if my project would be using a display or not, and it logs some interesting/useful data to the console as it runs, so I wanted to make sure that it would run AFTER the GUI started and would show up on screen. To this end, I accomplished the auto-start by doing two things; first, I created a shell script in the pi user's home directory that just calls my Python scripts (saved as launcher.sh). Of course, you must "chmod +x" this file to be able to execute it.


The file looks like this:
The file looks like this:
<source lang="python">
<syntaxhighlight lang="bash">
#!/bin/sh
#!/bin/sh
# launcher.sh
# launcher.sh
Line 28: Line 28:
cd /home/pi/logger
cd /home/pi/logger
sudo python your_script.py
sudo python your_script.py
</source>
</syntaxhighlight>


And then I edited /etc/xdg/lxsession/LXDE-pi/autostart to add this line at the bottom:
And then I edited /etc/xdg/lxsession/LXDE-pi/autostart to add this line at the bottom:
<syntaxhighlight lang="bash">
@lxterminal -e /home/pi/launcher.sh
@lxterminal -e /home/pi/launcher.sh
</syntaxhighlight>


Bingo!
Bingo!
Line 41: Line 43:


So, what i did instead was modify some code that I found via Google, and I have my shell script call that script before it runs my main project script. The trick here is that my system time was still not reporting correctly, so if you look in the file "gpstime.py" you'll see this entry:
So, what i did instead was modify some code that I found via Google, and I have my shell script call that script before it runs my main project script. The trick here is that my system time was still not reporting correctly, so if you look in the file "gpstime.py" you'll see this entry:
<source lang="python">
<syntaxhighlight lang="python">
# Hack to address a bug
# Hack to address a bug
os.system('sudo killall gpsd')
os.system('sudo killall gpsd')
Line 48: Line 50:
print 'Sleep quick!'
print 'Sleep quick!'
time.sleep(5)
time.sleep(5)
</source>
</syntaxhighlight>


What this is doing is:
What this is doing is:
Line 56: Line 58:


Then, shortly after you'll see this code:
Then, shortly after you'll see this code:
<source lang="python">
<syntaxhighlight lang="python">
gpstime = gpsd.utc[0:4] + gpsd.utc[5:7] + gpsd.utc[8:10] + ' ' + gpsd.utc[11:19]
gpstime = gpsd.utc[0:4] + gpsd.utc[5:7] + gpsd.utc[8:10] + ' ' + gpsd.utc[11:19]
print 'Setting system time to GPS time...'
print 'Setting system time to GPS time...'
Line 62: Line 64:
os.system('sudo date --set="%s" -u' % gpstime)
os.system('sudo date --set="%s" -u' % gpstime)
print 'System time set.'
print 'System time set.'
</source>
</syntaxhighlight>


This is assembling a string from the GPS output that will match what the date command is looking for.
This is assembling a string from the GPS output that will match what the date command is looking for.

Latest revision as of 14:29, 7 April 2018

(aka Pictures & Places) This is a project that is designed to be installed into a vehicle and do two things: It tracks your GPS position and records it to a database, and it also takes periodic pictures from a webcam mounted on the dash. This is accomplished through the use of some fairly basic Python, and using SQLite for the database. The images are saved as JPEG.

The brains of the operation is a Raspberry Pi Model B running the latest Raspbian image. Thanks to this and the low power requirements for the peripherals, the entire project can be run from the power of a single USB port. All needed storage is provided by the SD card that the RPi boots from.

Hardware

  • Raspberry Pi Model B
  • BU-353 S4 USB GPS Reciever
  • Logitech C270 HD webcam
  • A nice universal car charger with a USB port

Software

  • apt-get install gpsd gpsd-clients python-gps sqlite3 fswebcam
  • pip install RPi.GPIO
  • My scripts on GitHub

More Information

Depending on your comfort level with Python, Linux, and/or working with the Raspberry Pi, you may have several questions about what exactly is going on with these scripts. So here I'll break down a couple of the more interesting parts. First let me say that this is by no means the only (or even the best) way to accomplish these goals. This is simply how I got it to work for me.

Auto-start the script

I took a slightly different approach here than what most people would expect. I didn't know if my project would be using a display or not, and it logs some interesting/useful data to the console as it runs, so I wanted to make sure that it would run AFTER the GUI started and would show up on screen. To this end, I accomplished the auto-start by doing two things; first, I created a shell script in the pi user's home directory that just calls my Python scripts (saved as launcher.sh). Of course, you must "chmod +x" this file to be able to execute it.

The file looks like this:

#!/bin/sh
# launcher.sh
# You can cd or skip the next line and add the full path to the last line
# but be careful if your script relies on relative paths?
cd /home/pi/logger
sudo python your_script.py

And then I edited /etc/xdg/lxsession/LXDE-pi/autostart to add this line at the bottom:

@lxterminal -e /home/pi/launcher.sh

Bingo!

Setting date and time

Being a small, cheap computer, the Raspberry Pi does not include a real-time clock (RTC). This can be a headache when working on a project that is both not connected to the internet, and relies on having a fairly accurate reading of the time. The good news in my case is that I have a GPS receiver plugged into my RPi, and it just so happens that the GPS signals I am receiving from the satellites contain information such as the time, kept accurate by the atomic clocks in each satellite. So let's put this data to good use.

Now, I should point out that there are articles and information about how you can add your GPS receiver to your NTP sources list to have it automatically polled by NTP and sync as needed. I decided that I didn't want to take this approach, both because I don't need HIGHLY accurate time, and because that seemed like an invitation to more headaches. Keep it simple.

So, what i did instead was modify some code that I found via Google, and I have my shell script call that script before it runs my main project script. The trick here is that my system time was still not reporting correctly, so if you look in the file "gpstime.py" you'll see this entry:

# Hack to address a bug
os.system('sudo killall gpsd')
os.system('sudo fake-hwclock load force')
os.system('sudo /etc/init.d/gpsd start')
print 'Sleep quick!'
time.sleep(5)

What this is doing is: First, kill any running gpsd instances because they were started with incorrect time information. This will cause the GPS to report incorrect time back to us later on. Then use the RPi's fake-hwclock to force a load of the last saved time back into the running system. This gets us close enough to get good time data. Then, restart gpsd and wait while it initializes.

Then, shortly after you'll see this code:

gpstime = gpsd.utc[0:4] + gpsd.utc[5:7] + gpsd.utc[8:10] + ' ' + gpsd.utc[11:19]
print 'Setting system time to GPS time...'
print gpstime
os.system('sudo date --set="%s" -u' % gpstime)
print 'System time set.'

This is assembling a string from the GPS output that will match what the date command is looking for. Then, it uses the date command to update the system clock. Since the GPS reports in UTC, we set the -u flag to preserve our local timezone.

Now, every time we ask for current date and time information from the system, it's actually telling the truth! Yay!