3 Simple Ways to
Share What You Make

With Instructables you can share what you make with the world — and tap into an ever-growing community of creative experts.

PhotosPhotos

Share one or more photos of a project, recipe, or whatever you've made, quickly and easily.

Step by StepStep-By-Step

Share your step-by-step photos with text instructions of what you made so others can do it too!

VideoVideo

Share your how-to video. You'll need your embed code from a video site such as YouTube.

Twitter Mention Mood Light

Step 4Python

Python
If you don't know How to install Python Packages on Windows 7 then you *really * need to follow that link. It's my instructable. It's a set of directions, not explanations. You'll need to do that before you can use this code here. 

You've got Python installed (w/ help from the link above) and now you need to get your packages in order.
Python - get the Python 2.7.2 Windows Installer from http://www.python.org/download/
pySerial - http://pyserial.sourceforge.net/
simplejson - http://pypi.python.org/pypi/simplejson
httplib2 - http://code.google.com/p/httplib2/
python-oauth2 - https://github.com/simplegeo/python-oauth2
python-twitter - http://code.google.com/p/python-twitter/

Refer to my How to install Python Packages on Windows 7 instructable if you need help installing these.

To run a python script, open it in IDLE and hit "Run > Run Module".
To learn about YOUR TWITTER KEY, goto the next step. 

#######################################################################
#######################################################################
# twitterMentionMoodLight_python.py
# for use with twitterMentionMoodLight_arduino
# visit my instructables for more information
# http://www.instructables.com/member/pdxnat/
# http://python-twitter.googlecode.com/hg/doc/twitter.html
# http://dev.twitter.com/pages/rate_limiting_faq

print '<twitterMentionMoodLight>'

# import libraries
import twitter
import serial
import time

# connect to arduino via serial port
arduino = serial.Serial('COM4', 9600, timeout=1)

# establish OAuth id with twitter
api = twitter.Api(consumer_key='YOUR_CONSUMER_KEY',
                  consumer_secret='YOUR_CONSUMER_SECRET',
                  access_token_key='YOUR_ACCESS_TOKEN_KEY',
                  access_token_secret='YOUR_ACCESS_TOKEN_SECRET')

oldID = "" # used in getMention()

# listen to arduino
def listenToArduino():
    msg=arduino.readline()
    if msg > '':
        print 'arduino msg: '+msg.strip()
        updateStatus(msg.strip())

# search for any mention of @yourUsername
def getMention():
    status = api.GetReplies()
    newID = str(status[0].id)
    global oldID
    if (newID != oldID):
        oldID = newID
        print status[0].text+", by @"+status[0].user.screen_name
        arduino.write(1) # arduino gets 49

# post new message to twitter
def updateStatus(newMsg):
    localtime = time.asctime(time.localtime(time.time()))
    tweet = api.PostUpdate(newMsg+", "+localtime)
    print "tweeted: "+tweet.text

while 1:
    listenToArduino()
    getMention()
    time.sleep(15) # avoid twitter rate limit

« Previous StepDownload PDFView All StepsNext Step »

Pro

Get More Out of Instructables

Already have an Account?

close

All Steps Viewing
View all steps of an Instructable on the same page when you're a Pro Member.

Upgrade to Pro today!
5
Followers
4
Author:pdxnat