loading

The main purpose of this Instructable is to learn how to use Twitter's API to pull tweets into your python code, so that you can use real time data in your projects

After this Tutorial, you will be familiar with:

Raspberry Pi Blink LED

Twitter's Apps and Twitter APIs structure

JSON

oAuth

I hope this will help you improve your existing project or spark a new idea for a future project

Step 1: Carve the Pumpkin.

Get yourself a nice pumpkin a week before Halloween

Find a Halloween pumpkin cool template to carve

I chose the traditional Jack-O-Lantern for this project

http://www.freefunhalloween.com/pumpkin-stencils/j...

Carve the Pumpkin using the template. Be careful not to cut yourself in the process

Keep your pumpkin in a cool place, with no direct sunlight. This will help preserve your pumpkin

Step 2: Set Up the Hardware

For this project, I am using all the components that come in the a Raspberry Pi B+ Ultimate CanaKit . You can purchase it here (http://www.amazon.com/CanaKit-Raspberry-Ultimate-Starter-Components/dp/B00G1PNG54)

In order to have a standalone pumpkin, I am using aUSB wireless adapter for internet connection and portable battery to power the Hardware. I have left the Raspberry Pi running off this 13000mAh power bank and it stayed on for over 16 hours with the LEDs Blinking.

Setup the wireless network in you raspberry pi (https://www.youtube.com/watch?v=_1WWKUM_IUA)

Install TightVNC Server on raspberry pi and VNC Viewer on your desktop for Remote Login (https://www.youtube.com/watch?v=tbbp-vvsKZ4)

IMPORTANT: Do one of those Raspberry Pi Blink LED tutorials. Here is a quick one I found via google http://www.rpiblog.com/2012/09/using-gpio-of-raspberry-pi-to-blink-led.html. This is the "Hello World" of hardware and will give you enough confidence to move on.

If it is the first time your Pi is online, make sure to run update and upgrade on brand new raspberry pi. If you don't know what this means then look for one of those "setting up new raspberry pi" videos on youtube

-=THE HARDWARE LIST=-
RaspberryPy B+

USB Wireless Adapter

Breadboard

LEDs

Some resistors

13000 mAh 5V power bank

Step 3: Set Up the Twitter API

FACT: The Twitter API had a major update and now it requires authentication to use and it has been broken down into a few smaller APIs for different purposes. For this project, we will be using Twitter's REST API

To authenticate to Twitter's API you need a Token and a Key that are generated when you create a Twitter Application

To get a Twitter Application, you need twitter account WITH A VERIFIED PHONE NUMBER.

Create a new Twitter app at https://apps.twitter.com/. The info entered for Name, Description, and Website of this app doesn't really affect our python code because all we need is the Token and Key, so type in anything you want.

Once the app is created, go into it's "Key and Access Token" Tab and click to generate a new Key and Token

Take note of your CUSTOMER_KEY, CUSTOMER_SECRETE, ACCESS_KEY, ACCESS_SECRETE

Step 4: Set Up the Python

In this step, we will retrieve a simple tweet from your twitter timeline via python

Twitter API returns data in JSON format. Python as a Library called simplejson that handles JSON as dictionaries.

Twitter API authenticates via oAuth. Python has a library called httplib2 that has oAuth inside of it.

To get started, Install Python 2.7 if you do not have it already in your rapsberry

Install the simplejson and httplinb packages. Hint: I used PIP to easily install python packages.

Create a new python script and copy the code .Make sure you change the 5 different places in the code that are IN UPPERCASE with your own information

import simplejson as json

import oauth2 as oauth


CONSUMER_KEY = "YOUR CONSUMER_KEY"

CONSUMER_SECRET = "YOURCONSUMER_SECRET"

ACCESS_KEY = "YOURACCESS_KEY"

ACCESS_SECRET = "YOURACCESS_SECRET"


consumer = oauth.Consumer(key=CONSUMER_KEY, secret=CONSUMER_SECRET)

access_token = oauth.Token(key=ACCESS_KEY, secret=ACCESS_SECRET)

client = oauth.Client(consumer, access_token)


timeline_endpoint = "https://api.twitter.com/1.1/statuses/user_timeline.json?sreen_name=YOUR_SCREENAME&count=2"

response, data = client.request(timeline_endpoint)


tweets = json.loads(data)

for tweet in tweets:

print tweet['text']


Step 5: The Final Software

Now that everything is setup properly, here is the final code

import simplejson as json
import oauth2 as oauth
import RPi.GPIO as GPIO
from time import sleep

GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)

CONSUMER_KEY = "YOUR CONSUMER_KEY"
CONSUMER_SECRET = "YOURCONSUMER_SECRET"
ACCESS_KEY = "YOURACCESS_KEY"
ACCESS_SECRET = "YOURACCESS_SECRET"

#Here enter your GPIO number for each color
RED = 18
GREEN = 19
YELLOW = 17
BLUE = 16

def main():
tweet_text = get_new_tweet()
new_color, BLINK_FLAG = color_from_tweet(tweet_text)
if new_color:
if BLINK_FLAG:
blink(new_color, 999)
else:
change_color(new_color)
else:
print "No new color"

def get_new_tweet():
consumer = oauth.Consumer(key=CONSUMER_KEY, secret=CONSUMER_SECRET)
access_token = oauth.Token(key=ACCESS_KEY, secret=ACCESS_SECRET)
client = oauth.Client(consumer, access_token)

timeline_endpoint = "https://api.twitter.com/1.1/statuses/mentions_timeline.json?count=1"
response, data = client.request(timeline_endpoint)

tweets = json.loads(data)
for tweet in tweets:
print tweet['text']
return tweet['text']

def color_from_tweet(tweet):
if "BLINK" in tweet:
BLINK_FLAG = True
else:
BLINK_FLAG = False

if "RED" in tweet:
return RED, BLINK_FLAG
if "GREEN" in tweet:
return GREEN, BLINK_FLAG
if "YELLOW" in tweet:
return YELLOW, BLINK_FLAG
if "BLUE" in tweet:
return BLUE, BLINK_FLAG
return None, None

def change_color(color):
off()
GPIO.setup(color, GPIO.OUT)
GPIO.output(color,True)

def off():
for colors in [RED,GREEN,YELLOW,BLUE]:
GPIO.setup(colors, GPIO.OUT)
GPIO.output(colors,False)

def blink(color, time):
GPIO.setup(color, GPIO.OUT)
GPIO.output(color, False)
for x in range(time):
GPIO.output(color,True)
sleep(.5)
GPIO.output(color,False)
sleep(.5)
print ".",
print ""

<p>Awesome Jack-o-lantern. If you want to make the video easier for people to view, you can embed it in the instructable using the Embed Video tool in the step editor. Then people will be able t view it on the page without having to download it. </p>

About This Instructable

500views

1favorite

License:

More by evandromiami:TRUE Aeroponics - Simple Indoors Low Water Lettuce Growing Fogponics System Lettuce For Life - Arduino 101 Based Automated Controller for Hydroponics, Aeroponics, Aquaponics, Etc.  Intel Curie Tweet Pumpkin - Change  LED color via Twitter with Raspberry Pi - IoT 
Add instructable to: