Learn how to build a Mood Indicator using Twitter's developer API and a Raspberry Pi.  

There are millions of people using Twitter today to interact with the rest of the world. In this project, we’ll be utilizing the popular social media platform’s developer API along with a Raspberry Pi to build a mood indicator. 

Using Raspberry Pi with Twitter 

To start, we’ll focus on Twitter’s use of hashtags. The goal in this experiment is to enable LED lights to flash when a specific hashtag appears on the Twitter website. Beyond this simple project, Twitter’s network can also be used to conduct sentiment analysis and even create AI-enabled systems. 

Main Components Required

  • A Raspberry Pi
  • An adaptor to power the Raspberry Pi 
  • A few LEDs
  • A Breadboard
  • A couple of jumper wires

Required Software Components 

  • Any Linux version, which will be installed on the Raspberry Pi (Preferably Raspbian). 
  • A USB Wi-Fi dongle (if you're using Raspberry Pi 3 you won't need a Wi-Fi dongle).

Circuit Diagram: Connecting the LEDs to the Raspberry Pi 

First, connect the long leg of one LED to GPIO14, then the short leg of another LED to the Raspberry Pi GND. Use an ohm resistor as shown in the diagram below.

A diagram of how to connect the LEDs to the Raspberry Pi.

Application and Registration Process on Twitter API

As of July of this year, in order to gain access to the Twitter API platform, all users are required to apply for a developer account.

Start the application process on the Twitter API homepage by selecting Apply in the top right corner. 

You’ll land on the main developer application page, where you’ll fill out the specific project details, agree to the Twitter API use terms, and submit your application to access the platform. 

Once you’ve acquired access to the Twitter API platform, you’ll need to navigate to the Keys and Token page and obtain the following for use in this project: 

  • Consumer Key 
  • Consumer Secret 
  • Access Token 
  • Access Token Secret

In the Raspberry Pi terminal, run the following commands:

Sudo apt-get install update
Sudo apt-get install upgrade
Sudo apt-get install python-pip
Sudo pip install twython
Sudo apt-get install python-dev
Sudo pip install Adafruit_BIBO

Open Python IDE and paste the following code:

        import time
import RPi.GPIO as GPIO
from twython import TwythonStreamer
 
# Search terms
TERMS = '#yes'
 
# GPIO pin number of LED
LED = 14
 
APP_KEY = ' abcdefgh'
APP_SECRET = 'abcdefgh'
OAUTH_TOKEN = ' abcdefgh'
OAUTH_TOKEN_SECRET = ' abcdefgh'
 
class BlinkyStreamer(TwythonStreamer):
        def on_success(self, data):
                if 'text' in data:
                        print data['text'].encode('utf-8')
                        print
                        GPIO.output(LED, GPIO.HIGH)
                        time.sleep(0.5)
                        GPIO.output(LED, GPIO.LOW)
 
# Setup GPIO as output
GPIO.setmode(GPIO.BOARD)
GPIO.setup(LED, GPIO.OUT)
GPIO.output(LED, GPIO.LOW)
 
# Create streamer
try:
        stream = BlinkyStreamer(APP_KEY, APP_SECRET, OAUTH_TOKEN, OAUTH_TOKEN_SECRET)
        stream.statuses.filter(track=TERMS)
except KeyboardInterrupt:
        GPIO.cleanup()
    

Go ahead and save the code, then run it. 

You'll know that you've successfully completed the project when the LEDs flash in response to a designated hashtag appearing on-screen. 

Reginald Watson
I love challenging myself by creating new projects using different microcontrollers to see what I can come up with.

Maker Pro Logo
Continue to site
Quote of the day

-