Skip navigation

Just how can you use a small computing device to spice up your party?

 

How about using it to play music, capture and display the action, and saving evidence for the days to come?

 

Sound good? Well read on.

 

There are four main parts to this accomplishing this.

1)   Playing music

2)   Capturing the action

3)   Displaying the action

4)   Saving the evidence

 

But first there’s step zero, that is, set up the beast.

 

As mentioned in the attached video, the included XBMC SD card did not play nicely with my network settings (for some reason, things wouldn’t stick).

Tried a few backend tweaks, but I ended up going to a bog standard install of Raspbian. There are plenty of guides available if you’re going to do this yourself so I won’t cover it here.

 

So lets get into it.

After installing Raspbian I enabled ssh using the raspi config editor, expanded the file system to fill the entire SD card, and set it to boot straight to the desktop.

 

For the music player I installed OMXplayer and a freely available front end. It was then simple to play a set of files off the inserted USB flash drive.

 

Capturing the action and saving the evidence.

I was using the PiCam. More specifically, the PiCam that had the IR filter removed. This was enabled in the raspi config. Using a python script I took a photo once per minute, set the filename to the current time/date, and made a copy called latest.jpg

 

cap.py

#!/usr/bin/env python3

import sys

import os

import subprocess

from time import sleep

import pifacecad

import datetime

import shutil

 

dt = str(datetime.datetime.now())

name = '/usr/share/nginx/www/<your-folder>/images/IMG_'+dt+'.jpg'

from subprocess import call

call(["raspistill","-o",name,"-w","1280","-h","960","-rot","90"])

shutil.copy2(name,'/usr/share/nginx/www/<your-folder>/images/latest.jpg')

 

 

Displaying the action.

I originally intended to have a looping slideshow going, however this didn’t eventuate. Instead I installed a lightweight webserver (nginx) to show a simple auto-refreshing page pointed at the latest.jpg file which gave the effect of a slideshow, but only when the image updated.

The other displaying shown in the video was on the LCD display.

This was the PiFace CAD. Although I didn’t use the CA part, the display was perfectly reasonable for my task.

Using a second python script the PiFace CAD display was updated with the current image count, as well as the disk/SD card usage.

The python script was an edited version of the script distrbuted with the piface samples to show system info.

 

photostaken.py

#!/usr/bin/env python3

import sys

import subprocess

from time import sleep

import pifacecad

import datetime

from sys import exit

 

TIME_CMD =" str(datetime.datetime.now())"

UPDATE_INTERVAL = 60 * 5  # 5 mins

GET_IP_CMD = "hostname --all-ip-addresses"

GET_TEMP_CMD = "/opt/vc/bin/vcgencmd measure_temp"

TOTAL_MEM_CMD = "free | grep 'Mem' | awk '{print $2}'"

USED_MEM_CMD = "free | grep '\-\/+' | awk '{print $3}'"

PHOTO_COUNT_CMD = "ls '/usr/share/nginx/www/<your-folder>/images' | grep .*.jpg | wc -l"

 

temperature_symbol = pifacecad.LCDBitmap(

    [0x4, 0x4, 0x4, 0x4, 0xe, 0xe, 0xe, 0x0])

memory_symbol = pifacecad.LCDBitmap(

    [0xe, 0x1f, 0xe, 0x1f, 0xe, 0x1f, 0xe, 0x0])

temp_symbol_index, memory_symbol_index = 0, 1

photo_symbol = pifacecad.LCDBitmap([

0b11111,

0b11011,

0b11011,

0b11111,

0b11000,

0b11000,

0b11000,

0b11000

])

 

time_symbol = pifacecad.LCDBitmap([

0b11111,

0b00100,

0b00100,

0b00100,

0b00000,

0b11111,

0b10101,

0b10101

])

 

photo_symbol_index = 2

time_symbol_index = 3

 

def run_cmd(cmd):

    return subprocess.check_output(cmd, shell=True).decode('utf-8')

 

 

def get_my_ip():

    return run_cmd(GET_IP_CMD)[:-1]

 

 

def get_my_temp():

    return run_cmd(GET_TEMP_CMD)[5:9]

 

 

def get_my_free_mem():

    total_mem = int(run_cmd(TOTAL_MEM_CMD))

    used_mem = int(run_cmd(USED_MEM_CMD))

    mem_perc = used_mem / total_mem

    return "{:.1%}".format(mem_perc)

 

def get_photo_count():

    all_images = int(run_cmd(PHOTO_COUNT_CMD))

    taken_photos = all_images-1

    return taken_photos

 

def wait_for_ip():

    ip = ""

    while len(ip) <= 0:

        sleep(1)

        ip = get_my_ip()

 

def get_time():

            dt=int(run_cmd(TIME_CMD))

            return dt

 

def show_sysinfo():

    while True:

        cad.lcd.clear()

        #cad.lcd.write_custom_bitmap(time_symbol_index)

        #cad.lcd.write(":{}".format(get_time()))

 

        #cad.lcd.write_custom_bitmap(photo_symbol_index)

        cad.lcd.write(":{}images ".format(get_photo_count()))

 

        cad.lcd.write_custom_bitmap(memory_symbol_index)

        cad.lcd.write(":{}".format(get_my_free_mem()))

        #sleep(UPDATE_INTERVAL)

        exit(0)

 

if __name__ == "__main__":

    cad = pifacecad.PiFaceCAD()

    cad.lcd.blink_off()

    cad.lcd.cursor_off()

 

    if "clear" in sys.argv:

        cad.lcd.clear()

        cad.lcd.display_off()

        cad.lcd.backlight_off()

    else:

        cad.lcd.store_custom_bitmap(temp_symbol_index, temperature_symbol)

        cad.lcd.store_custom_bitmap(memory_symbol_index, memory_symbol)

            #cad.lcd.store_custom_bitmap(time_symbol_index, time_symbol)

            #cad.lcd.store_custom_bitmap(photo_symbol_index, photo_symbol)

        cad.lcd.backlight_on()

        cad.lcd.write("Updating")

        show_sysinfo()

There is a lot of unneeded code in the above, but I couldn't be bothered trimming it down.

 

Both of these python scripts were coordinated using the crontab set as follows

use sudo crontab -e to edit this

* *   * * *  python3 /usr/share/nginx/www/<folder-name>/cap.py

*/5 *  * * * python3 /usr/share/nginx/www/<folder-name>/photostaken.py

 

This runs both scripts at startup, takes and stores one image per minute (or rather, on the minute, every minute), and refreshes the LCD display every five minutes (every time a minute occurs which is a multiple of 5).

 

Now for the TV display. This was controlled using the below php file, which was placed in the folder that contained my scripts, which happened to be a folder in the web server directory.

 

index.php

<?php header("refresh: 10;");?>

<html>

<head>

<title>MERRY CHRISTMAS</title>

</head>

<body>

<?php exec("sudo -u root -S python3 cap.py < pass"); ?>

<Center>

<img src="images/latest.jpg" width="1280" height="960">

</center>

</body>

</html>

‘pass’ is the path to a text file containing your sudo password. This means you can run it even though it required sudo access.

 

This particular code runs the python code to capture/name/save an image, and displays an image. If using this code, the line relating to cap.py in the crontab is not really required.

 

 

So what about this video?

Initially you see the physical setup, then the LCD display, a timelapse generated from some images taken using the python code (and turned into a movie using mencoder), and finally a few snippets of NYE. I didn’t have the pi capturing the party though.

 

VIDEO TO COME WHEN I GET ONTO MY OTHER COMPUTER

Filter Blog

By date: By tag: