Logging motion with the Raspberry Pi

Since purchasing the Pi Cobbler Breakout Kit I wanted to digitally plan out the project before connecting a single wire. There seems to be a lot of choice for circuit design software (open source even), but the one I chose is called Fritzing.

Fritzing on OS X

After I began to download it, I found out that Adafruit has a library of parts that they update for this very software. I realize it isn’t an enormous leap from lighting an LED with a Raspberry Pi to controlling a PIR sensor, but the amount of fun I’ve been having with the ecosystem surrounding this device (and devices like it, whether single-board microcontrollers or single-board computers) compels me to announce my findings. Even the more minutely incremental.

A Raspberry Pi that logs motion it detects:

While viewing using Fritzing, you can see where positive and negative flow throughout the circuit, move wires around, add more parts, etc. I didn’t connect the breakout kit to the Raspberry Pi in software as I was unsure of how to represent a twenty-six pin ribbon cable, but the rest is there.

Currently, the button turning on the motion detector, and the motion detector doing the logging runs the following scripts below. To install, test and build on, copy/paste them into two files placed in the same directory, then run button.sh. From there, motion.sh is called when pressing the button and stopped when pressing it again. As you can see, I tried to model the scripts similar to the way the Arduino programming language has a setup and loop function.

There’s obvious room for improvement. Play a sound when detecting motion. Log to a Google spreadsheet. You get the idea.

source for button.sh

#!/bin/bash

function setup {
  button=0 #sda0 pin where the button is connected
  state=0 #the initialized state value of the pir sensor
  oldstate=0 #the initialized old state value of the pir sensor
  val=0 #the initialized pin value
  oldval=0 #the initialized old pin value
}

function loop {
  while true
  do
    val=`gpio -g read $button`
    if [[ $val -eq 1 &&; $oldval -eq 0 ]]; then
      state=$((1-$state))
      sleep .1
    fi

    oldval=$val

    if [ $state -eq 1 ]; then
      if [ $oldstate -lt $state ]; then
        #echo disabled
        if [ "$motionPID" != "" ]; then
          clear
          echo attempting to kill motion.sh and disable the PIR sensor
          kill $motionPID
          gpio -g write 17 0
        fi
        clear
        echo Press the button to enable the PIR sensor and begin 
          logging. Press the button again to disable. CTRL-C exits 
          button.sh.
      fi
    else
      if [ $oldstate -gt $state ]; then
        #echo enabled
        `dirname $0`/motion.sh &;
        motionPID=`echo $!`
      fi
    fi

    oldstate=$state
  done
}

setup; loop


source for motion.sh

#!/bin/bash
function setup {
  gpio export 17 out
  gpio -g write 17 1
  gpio export 18 in
  motion=0
  alert=0
  init=0
}

function loop {
  while true
  do
    if [ `gpio -g read 18` -eq 0 ]; then #no motion, no alert
      motion=0
      alert=0
      clear
    else #motion, no alert
      if [ $init -eq 0 ]; then
        echo The PIR sensor is initializing and calibration has 
          begun... Sleeping for forty seconds. Please be as still as 
          possible.
        init=1
        i=1; while [ $i -lt 41 ]; do echo $i; sleep 1; i=$(($i+1)); done
      fi
      if [ $alert -eq 0 ] && [ $init -eq 1 ]; then #motion, alert
        #aplay `dirname $0`/motion.wav > /dev/null 2>&1
        echo 'Motion has been detected: ' `date +"%Y-%m-%d_%H-%M-%S"`
        | tee -a `dirname $0`/motion.log
        alert=1
      fi
    fi
  done
}

setup; loop

UPDATE (2012-12-03): A better script for logging motion (and visualizing it) has been published at Visualizing Motion

Lighting an LED on the Raspberry Pi

Since getting a Raspberry Pi I’ve wanted to use the GPIO pins, but have tried to heed the various warnings regarding static and other issues one might face like using 3.3v vs 5v devices or how much power peripherals draw. I could have started clipping onto the pins directly, but decided to progress cautiously and get them in a more test friendly environment. Not that I wont fry anything now, I’m just trying to be methodical about it.

So I went out and purchased a breakout kit for it and followed the guide on soldering it together. Since, I’ve completed a similar experiment on the Arduino, I thought my first one on the Pi should be something along the lines of lighting an LED. It sort of seems like the “Hello, World!” of software development. First I needed a refresher on resistors (especially attempting to use the right ones) and how the breadboard works. Then I needed to find a way to get data down the pipes. I chose WiringPI.

WiringPi is a Wiring library written in C and should be usable from C++ and many other languages with suitable wrappers

I also found a tutorial on the WiringPi Author’s site that explained exactly what I wanted to do: GPIO Examples 1 – A single LED.

Arduino Motion Detector

As I have been traveling through Getting Started with Arduino I’ve been trying to take on the “tinkering” mentality described in chapter two titled, “The Arduino Way“. And as the text in the book states, “You should now experiment by looking at all the possible devices that have two contacts that close.” So I made a purposeful visit to Radio Shack to look for parts to hook up to it. What I came up with was a Parallax PIR Sensor.

The package didn’t include documentation for the sensor, but I was able to locate it on their website. This gave me enough confidence to try and hook it up… particularly after deciding to modify the “Using a Pushbutton to Control the LED” example and corresponding code from the Getting Started with Arduino book.

The result was a little motion detector, that turned the LED on while it detected motion, and then turned off shortly after the motion stopped. The sky is the limit I suppose (or time and money, right?). The next stop for me seems to be to hook up the Raspberry Pi to the Arduino in some manner.

Arrival of the Raspberry Pi

I got the Raspberry Pi in yesterday and wanted to show what came with it as well as my first steps. Pictured below is the packaging, top, and bottom view of the board:

Getting Started
Raspberry Pi - Top View
Raspberry Pi - Underside
I didn’t receive much in the way of what to do with it once I got it, so naturally I went to the website listed on the getting started guide: http://raspberrypi.org/downloads.

Ubuntu 12.04 with Gimp 2.8

After upgrading to Ubuntu 12.04 from 10.04, I realized Gimp was at an older version (2.6) than 2.8.

Presently, here’s how 12.04 can run 2.8 without much hassle:

  • sudo add-apt-repository ppa:otto-kesselgulasch/gimp
  • sudo apt-get update
  • sudo apt-get install gimp gimp-plugin-registry

Gimp 2.8 in Single-Window Mode

Drupal Modules

I recently completed a project for a customer that afforded me the opportunity to dive into Drupal on better terms. On my first introduction to the software several years back (and Joomla for that matter) I determined I would be able to use my cross-browser theming skills more effectively if I chose WordPress as a base CMS.

This time around I was able to spend a considerable chunk of time reading the documentation (especially around the Theming Guide) and examples on other’s sites. Some topics online seemed incomplete so I did end up reviewing some chapters in a few books on Drupal 7 in particular. In my mind it seems like the developers of WordPress and Drupal are migrating toward each other in feature sets, but delivering solutions from different perspectives.

WordPress appears to be geared more toward out of the box social publishing, with an easy installer, and an easy updater. It can be pushed in almost any direction, but doesn’t seem to be the goal of the software creators. Drupal on the other hand feels more like a collection of building blocks for a web database. While Drupal has fields, blocks, regions, views, and fine grained permissions with roles… URL aliases require a module to automate their selection. There’s even a module to hook up Filemaker Pro and Drupal.

Back to the modules:

I utilized the following:

For spam free contact forms (almost):

For easy page editing:

For dynamic content:

For development and style:

Microsoft Visio in LibreOffice 3.5

I like that LibreOffice seems to be experiencing rapid innovation and bloat removal since The Document Foundation was formed, and the software was forked from OpenOffice.

While I won’t write at length about all the changes I’ve noticed, I did want to show one of the new features coming to a stable release due soon.

The ability to open and display Visio files… more reading about it:

Thunderbird Attachments Missing

I ran into an interesting issue today after a co-worker reported that he had been receiving emails from someone who claimed there were attachments, however they were not being displayed within the email.

After viewing the message source and seeing a base64 encoded block within the email, I wondered if it was Thunderbird doing something I didn’t expect. I checked it in Roundcube and other locations… worked fine elsewhere…

And then I downloaded: Show All Body Parts. Problem worked around for now. 🙂

Continue reading →

Android on VirtualBox

This was fun. A bit of a test drive of Android-x86 3.2 using VirtualBox. I had a hard time getting the internet to work on it, just like I would have if I was at the store playing with it. 🙂

There are how-tos at: http://www.android-x86.org/