Categories
Electronics GitHub PiDashCam Raspberry Pi

An Update…

So, after being alerted by the Raspberry Pi Weekly mailing list to a liveblog by Alex Eames detailing his process for building a biking “dash cam” I thought I better start detailing what I’ve done, what I’m currently doing, and what I plan to do with my PiDashCam project which has taken a back seat for a number of months years. I’ve watched the videos Alex has made, but not yet looked at the code. My first post was using an Original Raspberry Pi with no camera attached, hooked up to a GPS module to ensure I got valid GPS data, and a small SPI screen so I could view the data

The Before

Unfortunately work and life took over and apart from spending a few odd minutes here and there over a number of months, no real advances were made apart from this update in which I was now testing with the Raspberry Pi Zero W, and figuring out how to get it talking to the GPS module, with the accelerometer, button and RGB LED all hooked up (but still no camera).

The After

I’m now going to try and update here with that I’ve decided upon, some new design goals and design directions, and the code needed to run my PiDashCam, in addition to stripping back to the bare essentials (you know, like being a camera).

I’ve remained with the Raspberry Pi Zero Wireless and I have an RTC Module hooked up, in addition to the camera, and one button. At present, the Pi boots, and automatically runs two scripts.

The new, updated, lesser qualified PiDashCam

The first, record.py, grabs the date and time, starts the camera, and starts recording, updating the annotated text on the screen. It then outputs a new file named after the current year, month, day, hour, minute and second (YYMMDDHHMMSS) every pre-determined number of seconds (roughly). You will be able to view this source code on GitHub as soon as I get round to uploading it. It also annotates the current date and time, and the current camera_mode.

The second, start.py, simply waits for the button press. At present it only detects an on push, nothing fancy like a multiple second hold. It then gracefully pkills the Python Process that is controlling the camera and after a few seconds delay, initiates a shut down of the Pi.

As its a bit unwieldily at present, I’ve stuck and blu-tacked it to a cheap dash camera I bought from eBay or AliExpress at some point or another. 

The camera works when testing in my house. I’ve still to take it a road test and make sure it works. I have a second, cheap Dash Cam that was another eBay or AliExpress special that does what it needs to do, but the quality is pretty poor. I plan on keeping both cameras connected and comparing the output. If I cant at least match the quality of a sub £20 Dash Cam then there’s no real point in continuing this one.

My Plan is as follows

  1. Test the camera, make sure it works in the car
  2. QUALITY CHECK
  3. Test multiple camera modes with the First Edition Raspberry Pi Camera
  4. Test multiple camera modes with the Second Edition Raspberry Pi Camera
  5. Test multiple camera modes with a cheap FishEye Pi Camera
  6. QUALITY CHECK
  7. Add in GPS Logging
  8. Add second button for marking of emergency record
  9. Add in G-Sensor for additional annotation and Emergency Record Function
  10. QUALITY CHECK
  11. Create a settings style file where all user-selectable settings are set
  12. Put it all in a nice case
  13. Work out a way to convert the raw h264 files to a usable mp4 format
  14. Build a nice web-based interface with an API so that apps can access the files/settings via Wi-fi
  15. Integrate with the CAN-Bus to take car data for better analysis of driving etc.

After each quality check, if the quality is not something that is useful then it could be a cancellation of the project. I’m trying to provide a more organised, and focused attempt this time and having these Design Goals will ensure that I focus on what’s required without jumping ahead. As always, I’m a great started and terrible finisher, but hopefully that will change!

In addition to the PiDashCam, I’m Aldo going to try my had again at small electronics. I’ve had a plate of 100 of these WS8211B RGB LED’s, and due to the way I stored them, they managed to snap. Playing about with the layout meant I could resonable build a Binary Clock, which I’ve wanted to do for quite a while. Given how many I had though, I figured I could extend this out and make it alternate between a Binary and Regular clock, and seeing as I have an ESP8266 spare, this could all be controlled wirelessly, with the time updated from an NTP server. But, there will be a separate blog post about that!


Updated 20/02/2019 with correct links to Alex Eames blog and minor technical details.

Categories
Electronics GitHub PiDashCam Raspberry Pi

Pi Zero W – PiDashCam Part 2

There will be a longer post about the newly released Pi Zero W, however suffice to say as soon as I saw it was available it was ordered, and I now have one!

There has been a few changes to the PiDashCam project, which has included moving to the Pi Zero when v1.3 was released (As it had a camera connector on the board). I missed my opportunity to write about those updates!

The Pi Zero W uses a similar set up to the Pi 3, in that the built-in bluetooth capabilities use the hardware UART, which means that my UART GPS receiver wont work.

As it is so similar to the Pi 3 (excluding the memory and processor) the current Device Tree for disabling bluetooth on the Pi 3 works for the Pi Zero W.

I’ve included some code below. This starts from a fresh install and allows you to use the hardware UART of the Pi Zero W and disables bluetooth on that board.

I hope it helps someone! (Also, feel free to replace vi with nano – I’m trying to teach myself Vim).

sudo vi /boot/cmdline.txt

in this file, remove the following text:

console=serial0,115200

Save and close the file. Next up,

sudo vi /boot/config.txt

and add the following to the end of the file:

enable_uart=1
dtoverlay=pi3-disable-bt

Next, it’s these three quick commands which includes a reboot of the Pi Zero W, which enables hardware UART:

sudo systemctl stop [email protected]
sudo systemctl disable [email protected]
sudo reboot

To complete the GPS side of things however, continue with the following:

sudo apt-get install gpsd gpsd-clients python-gps
sudo systemctl stop gpsd.socket
sudo systemctl disable gpsd.socket

and finally,

sudo killall gpsd
sudo gpsd /dev/ttyAMA0 -F /var/run/gpsd.sock
cgps

which should see you getting data from the GPS Receiver.

Thanks to Adafruit for their existing tutorial, and the Raspberry Pi github/forums and IRC for the Device Tree information.

Categories
Electronics General GitHub PHP

Using only PHP to save Google Starred items to Pocket!

So, based on my last post, I wanted to see if I could do everything with PHP. After a bit of google-fu and using the php.net manual, I’ve managed this beauty. Use at your own risk! This works with my Google Starred items, and you still have to obtain the starred.json file from the Google Take Out service (See my last post for more information).

If you have any tips on how to improve this, drop me a comment!

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Instapaper: Export</title>
</head>
<body>
<h1>Unread</h1>
<ol>
<?php

// First off, we start by opening the file required (starred.json),
// Then we set the $galbool paramater (This is used where sites have
// given a "Gallery" URL (To make it more cosmetic, it appends the
// text [Gallery] to the end of the description.

$file_handle = fopen("starred.json", "r");
while (!feof($file_handle)) {
    $galbool = FALSE;
    $line = fgets($file_handle);

// This is our first check. We run through the json file and look for
// lines that contain the text "href". If it does not have that text,
// we are not interested, and set that line to be blank.

    $preg = "(\"href\")";
    $urlcheck = preg_match($preg, $line);
    if ($urlcheck !== 1) {
    $line = "";
    } else {

// A cheeky little bit of coding. Whislt we are in the loop, and
// I know that this is a URL we are intersted in, I'll have a look
// at the last character. If its a ",", I also want to delete that
// line. Looking at the JSON file, if a line contains a URL and
// ends with a ",", it means its not the *ACTUAL* URL we want, so
// we continue our ruthless streak and set that line to blank!
// (This was included ot deal with hackaday.com URL's, which for
// some reason doubled up, and this was a quick and easy way to
// get rid of them!

        $preg = "(,)";
        $clean = preg_match($preg, $line);
        if ($clean !== 0) {
            $line = "";
        }
        }

// Now we trim the whitespace and other non-needed characters, and
// we remove the first bit from the string thats not needed. This
// takes us right to the http:// part of the link, which is what we
// need! We also remove the trailing slash from the link as well.

        trim($line);
        $line = substr($line, 16);
        $line = substr_replace($line, "", -2);
        $string = $line;

        $check = $string[strlen($string)-2];
        if ( $check == "/"){
            $string = rtrim($string);
            $string = rtrim($string, "/");
            $desc = $string;
        }
        $check = $string[strlen($string)-1];
        if ( $check == "/"){
            $string = rtrim($string);
            $string = rtrim($string, "/");
            $desc = $string;
        }

// This is just a quick check to see if the URL passed is a gallery
// URL. If so, we set the $galbool value to true, and then do our
// usual URL cleanup. I have removed the /gallery part from the URL
// This is personal preferance, and I've not had any adverse effects
// from either taking it in, or removing it. It has to be removed
// just now to make figuring out the link text easier though.
// We can add it back in later if required.

        $gallerycheck = str_replace("/gallery", "", $string, $count);
        if ($count == 1){
            $galbool = TRUE;
            $string = rtrim($string);
            $string = rtrim($string, "/gallery");
            $desc = $string;
        }

// And now for the (almost) finale! We take everything after the
// forward slash in the URL, remove that forward slash, then we
// run through and replace every "-" with a space. This makes the
// end HTML page look nice, and it keeps with Instapapers Export
// option. If the $galbool value is true, we create a [Gallery]
// tag.

        $desc = strrchr($string, "/");
        $desc = str_replace("/", "", $desc);
        $desc = str_replace("-", " ", $desc);
        $desc = ucwords($desc);
        if ($string != "" ){
        if ($galbool == TRUE){

// you can add back in the /gallery link here again if you need it!
// Just uncomment the relevant line and comment out the other!
//        -------------------------------------------------------------------------------------------------

//        $formatted = '            <li><a href="' . $string . '/gallery">' . $desc . '[Gallery]</a></li>';

//        ----------------------------------***OR THIS LINE***---------------------------------------------

        $formatted = '            <li><a href="' . $string . '">' . $desc . '[Gallery]</a></li>';

//        -------------------------------------------------------------------------------------------------

        } else {
        $formatted = '            <li><a href="' . $string . '">' . $desc . '</a></li>';
        }
        echo $formatted;
        }
    }

// We now close the file (good housekeeping), and finish up
// the script.

fclose($file_handle);
?>
</ol>
</body>
</html>

 

Categories
Electronics GitHub PHP

Using the terminal & PHP to save Google Reader Starred items!

*MAJOR UPDATE* – USING PHP ONLY WITH NEW CODE! Please view this next post for more information!

So. Google Reader is closing down. I’m not going to get all high and mighty – It’s Google’s product. They do with it as they wish! There are several places that let you save your feeds from Google Reader, but I wanted to add all my Starred Items from Reader into Pocket. It turns out it wasn’t the easiest thing to do! After a few terminal commands and some PHP, this is what I came up with!

Firstly, head to Google Reader, and use the Data Takeout feature that Google provides to save your Reader Data only. The outputted ZIP file should contain a folder entitled “Reader”. Within that, there should be a file named “starred.json”.

*UPDATE* – I have saved these 2 files to GitHub! Go, Grab!
https://github.com/nickwebcouk/pocketimport

Now the fun begins! To make it easy (and quick) I used the Terminal on Mac and ran these commands within the above folder. I used two files (new.txt and newnew.txt) just to keep track of what was happening. There are easier ways of doing this! I ran the following commands from terminal:

grep -a1 "canonical" starred.json > new.txt
grep -v "^\--" new.txt > newnew.txt
grep -v "} ]," newnew.txt > new.txt
grep -v "\"canonical\" : \[ {" new.txt > newnew.txt
grep -v "\"updated" newnew.txt > new.txt
grep -v "} \]," new.txt > newnew.txt
cat newnew.txt | rev | cut -c 2- | rev > new.txt
cut -c 17- new.txt > newnew.txt
rm -r new.txt
mv newnew.txt url.txt

The “grep” command looks through text files for specific expressions. “cat” outputs a full file, “rev” reverses items, “cut” cuts text, “rm” removes files and “mv” moves files.

To make this easier, I created a Shell Script (Tested on OSX 10.8.2 only)

#!/bin/sh

clear
grep -a1 "canonical" starred.json > new.txt
grep -v "^\--" new.txt > newnew.txt
grep -v "} ]," newnew.txt > new.txt
grep -v "\"canonical\" : \[ {" new.txt > newnew.txt
grep -v "\"updated" newnew.txt > new.txt
grep -v "} \]," new.txt > newnew.txt
cat newnew.txt | rev | cut -c 2- | rev > new.txt
cut -c 17- new.txt > newnew.txt
rm -r new.txt
mv newnew.txt url.txt

You can run that file by saving it to the same location as the Google Reader folder, and running the following in terminal first:

chmod 755 script.sh
./script.sh

the first line tells the computer to allow script.sh to be executed, and the second line executes the script.

I then moved the url.txt file that had just been created to my www root folder (for me its under /users/~name/sites/), and created the following PHP/HTML code:

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Instapaper: Export</title>
</head>
<body>
<h1>Unread</h1>
<ol>
<?php
    $file_handle = fopen("url.txt", "r");
    while (!feof($file_handle)) {
        $galbool = FALSE;
        $line = fgets($file_handle);
        $string = $line;
        $check = $string[strlen($string)-2];
        if ( $check == "/"){
            $string = rtrim($string);
            $string = rtrim($string, "/");
            $desc = $string;
        }
        $gallerycheck = str_replace("/gallery", "", $string, $count);
        if ($count == 1){
            $galbool = TRUE;
            $string = rtrim($string);
            $string = rtrim($string, "/gallery");
            $desc = $string;
        }
        $desc = strrchr($string, "/");
        $desc = str_replace("/", "", $desc);
        $desc = str_replace("-", " ", $desc);
        $desc = ucwords($desc);
        if ($galbool == TRUE){
        $formatted = '			<li><a href="' . $string . '">' . $desc . '[Gallery]</a></li>';
        } else {
        $formatted = '			<li><a href="' . $string . '">' . $desc . '</a></li>';
        }
        echo $formatted;
    }
    fclose($file_handle);
?>
</ol>
</body>
</html>

This provided me with a HTML page, which if saved as instapaper-export.html allowed me to head to getpocket.com and use the Instapaper import option.

584 starred articles and 2 seconds later, I received this wonderful little message!

getpocket import