Tuesday, 7 March 2017

Raspberry Pi Wedding Twitter selfie camera

I wanted to do something for my wedding to my wonderful wife that was different.  We had already planned a non traditional wedding with lots of different things, but I also wanted something that would be seen as trendy amongst the cool kids.

My daughter told me about a photo booth company but with such a small budget we couldnt afford one then I remembered something similar had been done and after a few googles I found tons of similar ideas but all seem to use Twython and from what I read parts had been depreciated yet I couldnt find the right commands to actually make it work.

So with the latest Raspberry PiZeroW and offical case, old pi camera from another project I began my project.  I had to enable ssh and vnc, personally I prefer sshing in to deal with issues but vnc can be better for trouble shooting.

I then had to make a twitter account (@angelapawedding) and set up the api so i could get the access and consumer tokens and set to read write and got to work, even soldering two pins on the back to 3rd and 4th pin in on the top row to attach a two wire pc wire/button reset switch to.  I figured this way if kids dropped or pulled it at least it could be reconnected with ease.


here is the code:
-----------------------------------------------------------------------------------

#mypifi feel free to use
#!/usr/bin/env python2.7
import tweepy
import sys
from picamera import PiCamera
from time import sleep
from datetime import datetime
from gpiozero import Button

consumer_key        = 'goes here'
consumer_secret     = 'goes here'
access_token        = 'access token code goes here'
access_token_secret = 'access secret goes here'
button = Button(14)
camera = PiCamera()
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)

while True:
    button.wait_for_press()
    status = "#LobsterWedding"
    timestamp = datetime.now().isoformat()
    photo_path = '/home/pi/wedding/photo/%s.jpg' % timestamp
    sleep(3)
    camera.capture(photo_path)

    with open(photo_path, 'rb') as photo:
        api.update_with_media(photo_path, status=status)


-------------------------------------------------------------------

The status part of the code is what text you want to display with the picture I used a hashtag so it be easy for people to find pictures or even add theirs for the wedding.  If you are interested, lobsters are supposedly meant to mate and be loyal to the same partner for life.

photo_path is where the picture is stored in my case its stored in folder called wedding in another folder within called photo.  each file saved here is given a unique filename and the program then tweets the latest picture using the twitter api via tweepy.

I saved the file as camera.py and chmod +x the file.  Once in place I vnc'd into it opened up a terminal and ran camera.py and then left it.

So throughout the evening guests (and especially the younger ones) had something to do and even my selfie obsessed daughter had a go as you will see below.  Guests could look up what had been tweeted by the camera at @angelapawedding or by searching the hashtag #lobsterwedding.



Sure the code could be cleaned up a bit more and more could be done with it, but for a quick project between finalising the final parts of the wedding I thought it was pretty good and was also a good talking point to guests.

If you do find a way of tidying up code then feel free to comment below and I will add it to the blog.