Introduction: Twitter Raspberry Pi Photo Live Feed
This script will allow you to use twitter as a live feed or surveillance device that uploads content directly to twitter.
On my Pi I used a Waveshare night vision camera, but you can use any type of camera.
Step 1: Create a Twitter App
Go to https://apps.twitter.com/ and create a new app for your Raspberry Pi script. You will need to already have a Twitter account to do this.
Under the "Keys and Access Tokens" tab is where you will find what you need to implement into the code.
Step 2: Install Tweepy
Download and upgrade tweepy with:
sudo pip install tweepy --upgrade
Step 3: Write the Script
#!/usr/bin/env python2.7
import tweepy import time import random from subprocess import call from datetime import datetime
#These are the phrases variables that will be sent with the tweet tweet = ['A tweet from my pi ', 'Hello! ']
while True:
#time and date for filename i = datetime.now() now = i.strftime('%Y%m%d-%H%M%S') photo_name = now + '.jpg' #creates command and destination for photo cmd = 'raspistill -t 500 -w 1024 -h 768 -o /tmp/' + photo_name #shoot the photo call ([cmd], shell=True)
# Replace each with the keys and tokens from your twitter app consumer_key = 'nzY1xjjTtglUvfEP14XZrpn9A' consumer_secret = 'fn9VTJwZF1kSJKUFKdrxMpiwwWFohvaWlkiPQRyj2oRZ7c9ojV' access_token = '2775601040-LlC1dTEwMCwPgASSXwQdXC2R1KjHYsKjrk3ASnE' access_token_secret = '5irgLOLbIqeYjswY0cDjsunL1feMKW50k9NxFC04kFExD'
# OAuth process, using the keys and tokens auth = tweepy.OAuthHandler(consumer_key, consumer_secret) auth.set_access_token(access_token, access_token_secret)
# Creation of the actual interface, using authentication api = tweepy.API(auth)
# Send photo to destination photo_path = '/tmp/' + photo_name # Tweet text status = (random.choice(tweet)) + i.strftime('%Y/%m/%d %H:%M:%S') # Send the tweet with photo api.update_with_media(photo_path, status=status)
#How many seconds before the script runs again time.sleep(900)
Step 4: Modify the Script
Where it asks for the consumer key, consumer secret, access token, and access token secret, you will want to insert the code generated in your twitter app.
You will probably want to change the duration between photos being sent as well as the information sent with the tweets.
To change how often a tweet is sent, you change the amount of seconds to sleep in time.sleep().
To change what phrases are tweeted with the photo, you edit tweet = [] at the top of the script.
Step 5: Run the Script
After you finish modifying the script, save with scriptname.py
Now you can run the script with python scriptname.py