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

Twitter Mention Mood Light
Twitter Mention Mood Light -- a mood light that alerts you when @username is mentioned on Twitter.

This is a simple intro in how to control your Arduino from Twitter. If you are new to Arduino Twitter / Arduino Processing Twitter / Arduino Python Twitter / Twitter Mood Light, then please refer to simpleTweet_00_processing  and simpleTweet_01_python (and How to install Python packages on Windows 7 ) for a crash course.


 
 
Remove these adsRemove these ads by Signing Up
 

Step 1Concept: Arduino {Processing / Python} Twitter

Concept: Arduino {Processing / Python} Twitter
The Arduino Twitter mood light has been around for a few years. I made one in early 2010 and recently decided to update it for Twitter OAuth protocol (step 5.) This instructable has both the [ Arduino Processing Twitter ] and [ Arduino Python Twitter ] versions.

The general idea: You've got a LED generating a peaceful glow, cycling through a bunch of colors, and meanwhile you've got Processing or Python listening to Twitter for any mention of @yourUsername. When it finds a Twitter status with @yourUsername it writes to Arduino over Serial and Arduino changes the LED from peaceful glow to alert.

The circuit board (step 6) has two buttons: Reset and Send. The reset button changes the LED from "alert" back to "peaceful glow." The send button sends a Twitter status update to your Processing or Python layer, and from there on up to Twitter. When pressing the SEND button, hold it down until you see the flash of WHITE LIGHT (approx 1 second.)  This confirms the button push has been read by Arduino.

There is some confusion about Twitter Rate Limits . The Rate Limit is the number of times Twitter will let you hit it's servers per hour, currently 350 hits per hour. Go over that number and Twitter will block you. I made the same provision for this in both Processing and Python: wait 15 seconds between hits. In Processing, in void draw() , see delay(15000); . In the Python, in the while clause, see time.sleep(15).  This does not affect the timing of any button pushing or Serial calls.

The Arduino code is the same for both Processing and Python except when Arduino listens to Serial. Both Processing and Python pass an integer value of 1 (arduino.write(1) ) but Arduino receives a different value (numeric / ascii) from each, which is why you'll see these two lines in the Arduino code:

    if (serialMsg == 1) state = "mention"; // processing
    if (serialMsg == 49) state = "mention"; // python


This is because I'm not sure how to pass a string value of "mention " over Serial in Python. It's not critical, so I'll leave working as above, but if anyone has a solution for sending a string (arduino.write("mention") ), particularly in Python, please post in the comments below. 

The center of this project is the getMentions() function. It's interesting to compare how they look in Processing and in Python.

// Processing (java)
// search for any mention of @yourUsername
void getMention() {
  List mentions = null;
  try {
    mentions = twitter.getMentions();
  }
  catch(TwitterException e) {
    println("Exception: " + e + "; statusCode: " + e.getStatusCode());
}
  Status status = (Status)mentions.get(0);
  String newID = str(status.getId());
  if (oldID.equals(newID) == false){
    oldID = newID;
    println(status.getText()+", by @"+status.getUser().getScreenName());
   arduino.write(1); // arduino gets 1
  }
}

# Python
# 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

In both cases the id value received is in "long" format. By converting from "long" to "string" with str() we're able to compare the value and act on it. I had trouble doing that in long format. 
« Previous StepDownload PDFView All StepsNext Step »
20 comments
May 28, 2012. 7:01 AMstarwood says:
Dear all,

When trying out the python script I get the following error:

Traceback (most recent call last):
File "C:\Users\RMB\Downloads\FG1LI7VGOW49P6U(1).py", line 51, in
getMention()
File "C:\Users\RMB\Downloads\FG1LI7VGOW49P6U(1).py", line 36, in getMention
newID = str(status[0].id)
IndexError: list index out of range

Could use some help. Thanks!

Cheers

RJ
Apr 30, 2012. 7:45 PMbroadleyutb says:
Hello.
Great tutorial, finally got it working for both Arduino+python and arduino+processing :) I moved the rgb LED from 5v to GND and it just started working?

Only problems now is that I can't seem to find were to change your tweet from (default for mines is #peaceful glow)
And also some of the LED descriptive colors on arduino code aren't actually changing to that color.

E.g ; for alert my arduino should flash red, black (off) but insted flashes light blue, white?

Can you help me please
Apr 6, 2012. 6:15 PMeniacmutiny says:
This tutorial looks fantastic. Any chance you'd be able to provide a list of the necessary parts? I'm new to this, and I wasn't sure I had all the parts by just looking at the photo. Thanks and great job!
Apr 5, 2012. 8:35 AM03coupr says:
Does Twitter4j work on Mac?
Apr 4, 2012. 9:41 AM03coupr says:
Thanks a lot man. I'll do research into the analog v digital, is there anything I should know about them. Things I might miss on the internet.

I also have a RGB Piranha Super Flux Super Flux LED - Common Anode, will this work for the LED.

Thanks a lot for the help, Appreciate it.

Thanks
Apr 4, 2012. 7:38 AM03coupr says:
Thank you. Sorry for all the questions. When I first asked about the breadboard, is there any tips on how I would change the layout of the circuit. I only have the small breadboard that comes with the Arduino uno ?

Many thanks
Apr 4, 2012. 3:17 AM03coupr says:
Thank you for getting back to me. I was also wondering, I have an ethernet board as well. Do I use that with this tutorial ?

Many Thanks
Apr 3, 2012. 3:10 AM03coupr says:
Can you use a simple Bread Board, or do you need a circuit board?

Many Thanks
Dec 29, 2011. 10:59 AMklaurens says:
Love your work man! can you do it with 4 normal leds?
Dec 30, 2011. 1:08 AMklaurens says:
I want to create the mood light like you have but what color you tweet, is what color light, lights up. I am really new to all of this and only got my first Arduino yesterday. And I am a twitter fanatic, so wanted to do this, but do not have RGB LED's.

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