Quantcast
Channel: Blog My Wiki! » RaspberryPi
Viewing all articles
Browse latest Browse all 66

Tweeting photos from PS2 EyeToy on Raspberry Pi

$
0
0


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 kernal used in Raspbian means that, at the time of writing, it doesn’t – you get horrid error messages about ‘bogus Huffman table definitions’.

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. It seemed to break the guvcview webcam viewer, but fswebcam still works, so that’s what I use to capture the image.

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 and wrote some Python code to grab a picture from the EyeToy and tweet it. I couldn’t get the pygame modules 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 them use cron to schedule the running of this script every hour or whatever you liked.


Viewing all articles
Browse latest Browse all 66

Trending Articles