Tweeting photos from PS2 EyeToy on Raspberry Pi


It took some fairly heavy footling and jiggery-pokery, but I finally managed to get my Raspberry Pi to run a Python script which takes a photo on a webcam and tweets the picture – meaning I could set up a tweeting web cam.

I made life hard for myself because the only webcam I had to hand was an old Sony Playstation 2 EyeToy. This used to work with the Raspberry Pi, but recent changes to the Linux kernel used in Raspbian means that, at the time of writing, it doesn’t – you get horrid error messages about ‘bogus Huffman table definitions’. The easy thing to do would be to use a supported webcam, like the Playstation 3 Eyecam, or one favourably listed here: http://elinux.org/RPi_USB_Webcams

I got the PS2 EyeToy working by loading an experimental version of the kernel as outlined here: http://www.raspberrypi.org/forums/viewtopic.php?f=28&t=70437

I backed up all the important files from my Raspberry Pi before doing this. Updating the kernel seemed to break the guvcview webcam viewer, but fswebcam still works, so that’s what I use to capture the image. This means I don’t currently have a way of viewing ‘live’ pictures from the webcam, so focusing it was a tedious process of trial and error improvement. Ideas welcome!

I needed to plug the EyeToy into a powered hub – it didn’t work plugged into my old iMac keyboard.

To get Twitter working on the Raspberry Pi, I more-or-less followed the instructions here: http://www.makeuseof.com/tag/how-to-build-a-raspberry-pi-twitter-bot/ with a few changes. The Twitter web pages they describe have changed slightly, but it’s reasonably easy to follow them.

In short, I installed Twython, registered a Twitter app to get the keys you need, installed fswebcam (sudo apt-get install fswebcam) and wrote some Python code to grab a picture from the EyeToy and tweet it. I couldn’t get the pygame camera modules that they used in the Make Use Of tutorial to work, so I used fswebcam instead to grab a JPEG.

The line
sudo fswebcam -r 320x240 -S 20 webcam.jpg
captures the image. 320×240 is the (tiny!) resolution of the PS2 EyeToy, and the -S 20 bit waits 20 frames before grabbing the image to allow the camera to stablise. Without this I was getting weird frame-sync problems.

I saved a Python script called cam.py (code below) which I made executable by typing
chmod +x cam.py
and I ran it by typing
python cam.py

And here’s the tweet: the photo was taken and tweeted all by the Python script below.

I guess I need to get a better web cam now, and train it on the London skyline. Or endlessly tweet the Pi’s CPU temperature. THAT would be good…

My Python script looked like this:

#!/usr/bin/env python
import sys
import os
from twython import Twython
CONSUMER_KEY = '**YOUR KEY HERE****'
CONSUMER_SECRET = '***YOUR SECRET HERE**'
ACCESS_KEY = '**ACCESS KEY HERE******'
ACCESS_SECRET = '***ACCESS SECRET HERE*****'

os.system('sudo fswebcam -r 320x240 -S 20 webcam.jpg')

api = Twython(CONSUMER_KEY,CONSUMER_SECRET,ACCESS_KEY,ACCESS_SECRET)
photo = open('webcam.jpg','rb')
api.update_status_with_media(media=photo, status='Photo taken & tweeted by PS2 EyeToy on RaspberryPi ')

You could then use cron to schedule the running of this script every hour or whatever you liked. Get it triggered by a button or MakeyMakey. Turn it into a tweeting photo booth for a party. Ideas, ideas…


Update

I’ve tweaked the script a bit now. I added a title to the picture like this:

os.system('sudo fswebcam -r 320x240 -S 20 --title "@blogmywiki gardencam" webcam.jpg')

I also set it to automatically tweet a picture every hour on the hour by typing
sudo crontab -e
and adding this line
0 * * * * python /home/pi/tweet/cam.py

(If you try this you’d need to include the full path to where your script lives – mine is in a folder called ‘tweet’).

This entry was posted in Raspberry Pi and tagged , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>