Loading presentation...

Present Remotely

Send the link below via email or IM


Present to your audience

Start remote presentation

  • Invited audience members will follow you as you navigate and present
  • People invited to a presentation do not need a Prezi account
  • This link expires 10 minutes after you close the presentation
  • A maximum of 30 users can follow your presentation
  • Learn more about this feature in our knowledge base article

Do you really want to delete this prezi?

Neither you, nor the coeditors you shared it with will be able to recover it again.


Lecture 7&8 - Interfacing Pi and Twitter

No description

iCode Inc.

on 26 September 2018

Comments (0)

Please log in to add your comment.

Report abuse

Transcript of Lecture 7&8 - Interfacing Pi and Twitter

Interfacing Pi and Twitter
What this Project is all about?
Raspberry Pi is programmed using Python to take a picture using a Pi camera and share the photo via Twitter when a button is pressed.
Set up the Twitter account
Set up the Twitter app
Program Code
Circuit Diagram
What you'll need
power supply
SD card
HDMI cable
220 ohm resistor
Button Switch
Twython python library
What is a Switch?
A switch is a component which controls the open-ness or closed-ness of an electric circuit. They allow control over current flow in a circuit.

A switch can only exist in one of two states: open or closed.

In the off state, a switch looks like an open gap in the circuit (called open circuit, preventing current from flowing)

In the on state, a switch acts just like a piece of perfectly-conducting wire. A short. This closes the circuit, turning the system “on” and allowing current to flow.
off state
on state
There are tons and tons of switches out there: toggle, rotary, DIP, push-button, rocker, membrane, … the list just goes on and on.
All switches fall into one of two distinct categories:
momentary or maintained.

Push-button switches are the classic momentary switch.(
Momentary switches are switches which only remain in their on state as long as they’re being actuated (pressed, held etc)
)Typically these switches have a really nice, tactile, “clicky” feedback when you press them.
Project Set up Steps
Create a Twitter Account and add phone number
Setup Twitter App
Install Pi Camera
Install Python Libraries
Program Code
Wire Raspberry Pi
Run the Code / Press the Button

To interface with the Twitter API, you must setup a Twitter app that has both read and write access to your timeline. In order to do this, you must have a Twitter account with a verified mobile phone number associated with it. After you create the app, you can remove your mobile number.
1. Login to your Twitter account.
2. Click on your Twitter icon and select Settings.
4. Add your mobile phone number and click Continue.
5. A verification code will be send to you. Enter that code on the confirmation screen.
3. After you are logged in, click on the Mobile link on the left.
1. go to https://apps.twitter.com/
2. Click on Create New App.
3. Fill in Name, Description, and Website. Agree to T&Cs. Click Create your Twitter application button. Do not worry if you don’t have a website for the project, just fill this in with any valid web address. Be sure to include the “http://”.
4. Click on Permissions Tab. Select Read and Write. Press Update settings.
5. Click on the Keys and Access Tokens tab, then click on the Create my access token button.
6. After you press the Create my access token button, you will see the Access Token and Access Token Secret fields. Keep all of these field values handy. You will need them for the Python code.

Consumer Key (API Key)
Consumer Secret (API Secret)
Access Token
Access Token Secret
Install Pi Camera
If you do not already have a Pi camera connected to your Raspberry Pi board and enabled, please follow the instructions here to get that done.


Use the below commands to install the required Python libraries. The only one required for this project is twython.

sudo apt-get update
sudo apt-get install python-pip
sudo pip install twython
Install Python Libraries
import time
import subprocess
import RPi.GPIO as GPIO
from twython import Twython
#setup GPIO using Broadcom SOC channel numbering/physical numbering
GPIO.setup(BUTTON, GPIO.IN, pull_up_down=GPIO.PUD_UP)

IMG_WIDTH = "1280"
IMG_HEIGHT = "720"
IMG_NAME = "tweet-pic.jpg"
# your twitter app keys goes here
apiKey = ' '
apiSecret = ' '
accessToken = ' '
accessTokenSecret = ' '

snapCommand = "raspistill -w " + IMG_WIDTH + " -h " + IMG_HEIGHT + " -o " + IMG_NAME

api = Twython(apiKey,apiSecret,accessToken,accessTokenSecret)

# set 'ready LED(red)' on
# set 'working LED(green)' off
#print a statement to show that system is ready
while True:

print "Program running...\n"
GPIO.output(SYSTEM_READY, False)
print "Capturing photo...\n"
ret = subprocess.call(snapCommand, shell=True)
photo = open(IMG_NAME, 'rb')

print "Uploading photo to twitter...\n"
media_status = api.upload_media(media=photo)
time_now = time.strftime("%H:%M:%S")
date_now = time.strftime("%d/%m/%Y")

tweet_txt = "Photo captured by @twybot at " + time_now + " on " + date_now
print "Posting tweet with picture...\n"
api.update_status(media_ids=[media_status['media_id']], status=tweet_txt)
#Make the running signal Low
#Signal program is ready again
#Keyboard Interrupt
#Final GPIO cleanup
Use any GPIO pins of your choice.This is just for reference
Press the button and this is what it looks on Twitter
Full transcript