Picture of simpleTweet_00 processing
Arduino Processing Twitter
Post a tweet when your sensor changes states, using Arduino, Processing, and twitter4j.
Note: I've got a Python version of this project here:

In the video you'll see two lines appear. They look like this:
new tweet --:{ Opened door at 14:21:57}:--
new tweet --:{ Closed door at 14:22:1}:--

These confirm that new messages were sent to Twitter when the switch opened and closed.
Visit to see the timeline.
Remove these adsRemove these ads by Signing Up

Step 1: Concept

Picture of Concept
The idea for this project was to develop generic code allowing you to automatically generate tweets based on input to your Arduino. This example uses a magnetic reed switch, which is your basic on / off switch. You can use this code as is for any two-state switch or modify it for more diversity.

We attach the magnetic reed sensor to my office door and door-frame. A wire runs from the magReed sensor to a pin in the Arduino circuit. Arduino watches that pin's status, HIGH or LOW, and when the status changes from one to the other Arduino reports on that change through Serial.write(). The Processing sketch picks up the Serial.write() call and checks to see if the current state is the same as the last one posted to Twitter. If the two states are the same then it will not post, but if the new state is different from the previous state, then we're in business.

Processing uses twitter4j and OAuth to post the new state to your Twitter account. Done and done.
stushapiro1 year ago
Thank you so much, worked perfectly! really awesome.
hey,suppose i have a processing or python application running ,so how can I let the user share with his friends that he is using my application by tweeting on his timeline and another thing I wanted to ask does Facebook has such api.
By the way great work ,helped a lot...!!!
bemoreins1 year ago
Thank for your good info.

I tried this project.

But Only works at just 1st time to my iphone twitter app.

and then never works again --;

My error message is here:
RXTX Warning: Removing stale lock file. /var/lock/LK.027.033.008

What's wrong with me ?

Intel iMac x64, MacOSX 10.8.3
Arduino 1.5.1
Processing 2.0b8
Twitter4j.jar that you offering one.

TobaTobias2 years ago
Hi everyone, I found a solution to all your problems. I'll post soon! ;)
broadleyutb2 years ago
hey man, great tut but I am totally stuck now. Done evrything from making the twitter dev and installed all the packages (i hope I done that right).
But i am currently getting these errors on processing:
Stable Library
Native lib Version = RXTX-2.1-7
Java lib Version = RXTX-2.1-7
[0] "COM5"
[1] "COM7"
[2] "COM8" Unknown Application
at processing.serial.Serial.(Unknown Source)
at processing.serial.Serial.(Unknown Source)
at simpleTweet_00_p.setup(
at processing.core.PApplet.handleDraw(Unknown Source)
at Source)
Exception in thread "Animation Thread" java.lang.RuntimeException: Error inside Serial.()
at processing.serial.Serial.errorMessage(Unknown Source)
at processing.serial.Serial.(Unknown Source)
at processing.serial.Serial.(Unknown Source)
at simpleTweet_00_p.setup(
at processing.core.PApplet.handleDraw(Unknown Source)
at Source)

[color=red]This is my current processing code:[/color]

import processing.serial.*;

import twitter4j.conf.*;
import twitter4j.internal.async.*;
import twitter4j.internal.logging.*;
import twitter4j.http.*;
import twitter4j.api.*;
import twitter4j.util.*;
import twitter4j.internal.http.*;
import twitter4j.*;

static String OAuthConsumerKey = "ykaw70kvBc6jVV21eLlWA";
static String OAuthConsumerSecret = "PllQqnwaZV7aYuH33vniloGW2U5fbkfYxef2LQVAK0";
static String AccessToken = "20026567-9juOyYchv9k7PXsu7kyrvhpHKkOY3Fg6xTn9vatuA";
static String AccessTokenSecret = "AcIhlVX95nvpFYMyk9oh41PWHe3TXYqhMbSy6hLgQ";

Serial arduino;
Twitter twitter = new TwitterFactory().getInstance();

void setup() {
size(125, 125);
String arduinoPort = Serial.list()[0];
arduino = new Serial(this, arduinoPort, 9600); [color=red]ERROR BEGINS ON THIS LINE[/color]

void loginTwitter() {
twitter.setOAuthConsumer(OAuthConsumerKey, OAuthConsumerSecret);
AccessToken accessToken = loadAccessToken();

private static AccessToken loadAccessToken() {
return new AccessToken(AccessToken, AccessTokenSecret);

void draw() {
text("simpleTweet_00", 18, 45);
text("@msg_box", 30, 70);

void listenToArduino() {
String msgOut = "";
int arduinoMsg = 0;
if (arduino.available() >= 1) {
arduinoMsg =;
if (arduinoMsg == 1) {
msgOut = "Opened door at "+hour()+":"+minute()+":"+second();
if (arduinoMsg == 2) {
msgOut = "Closed door at "+hour()+":"+minute()+":"+second();
compareMsg(msgOut); // this step is optional
// postMsg(msgOut);

void postMsg(String s) {
try {
Status status = twitter.updateStatus(s);
println("new tweet --:{ " + status.getText() + " }:--");
catch(TwitterException e) {
println("Status Error: " + e + "; statusCode: " + e.getStatusCode());

void compareMsg(String s) {
// compare new msg against latest tweet to avoid reTweets
java.util.List statuses = null;
String prevMsg = "";
String newMsg = s;
try {
statuses = twitter.getUserTimeline();
catch(TwitterException e) {
println("Timeline Error: " + e + "; statusCode: " + e.getStatusCode());
Status status = (Status)statuses.get(0);
prevMsg = status.getText();
String[] p = splitTokens(prevMsg);
String[] n = splitTokens(newMsg);
//println("("+p[0]+") -> "+n[0]); // debug
if (p[0].equals(n[0]) == false) {
//println(s); // debug

[color=red]And these are the errors on python:[/color]

running... simpleTweet_01_python
arduino msg: #peacefulGlow

Traceback (most recent call last):
File "C:/Users/Ciaran/Desktop/python final", line 47, in
File "C:/Users/Ciaran/Desktop/python final", line 24, in listenToArduino
File "C:/Users/Ciaran/Desktop/python final", line 31, in compareMsg
pM = ""+prevMsg[0]+""
IndexError: list index out of range

[color=red][font=Verdana]This is my current python code:[/font][/color]

print 'running... simpleTweet_01_python'

# import libraries
import twitter
import serial
import time

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

# establish OAuth id with twitter
api = twitter.Api(consumer_key='ykaw70kvBc6jVV21eLlWA',

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

# avoid duplicate posts
def compareMsg(newMsg):
# compare the first word from new and old
status = api.GetUserTimeline('yourUsername')
prevMsg = [s.text for s in status]
pM = ""+prevMsg[0]+""
pM = pM.split()
nM = newMsg.split()
print "prevMsg: "+pM[0]
print "newMsg: "+nM[0]
if pM[0] != nM[0]:
print "bam"

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

while 1:

I know atleast one of the buttons are working as it sends the signal of [color=red]arduino msg: #peacefulGlow

[/color] to pyhon when running module but as soon as the button is pressed then error messages appear.
My LED is not lighting up at all :(

I can send pictures of the circuitboard if needed.

Please will someone help me with this.
Either contact me here or and email to would be great


this is relation to the moodlight twitter mention tutorial you made.
I have now got the led to cycle through some colours but it seems to fade in and out nicely then every few seconds it will blink off then back on?
and I also do not understand how you are supposed to run python+processing (for button code) at the same time of running the arduino RGB LED code as both can not use the same COM (COM5 in my case) at the same time
pdxnat (author)  broadleyutb2 years ago
Howdy. You are not supposed to run Python and Processing at the same time. The python and processing scripts are meant to be two separate examples of the same project, just to show how it looks when done one way or the other; not supposed to work together. Perhaps this relates to the error you mention above? Try running only Processing or only Python and see what you get. Otherwise, I'm afraid I'm not much help. Good luck!
TobaTobias2 years ago
Hi mate.
Im getting errors on processing like, no libraries for twitter4j.http.
So I downloaded a jar file called, twitter4j-2.0.9 and draged it. But I was libraries using twitter4j 2.2.5-

Then another error came up, acces token is ambiguos.
And then on the code I put:
import twitter4j.http.OAuthToken*;

And then it says the Token is not visible.
Im using mac, I really need help. Thanks.
pdxnat (author)  TobaTobias2 years ago
That is so frustrating! I had to install twitter4j on a few machines and each time I had trouble with the libraries. Unfortunately I forget how I fixed it but it was something like copying the library into each of the two or three possible directories where "Processing" might look for it. Since that's not much of an answer, I suggest you try searching and asking questions at the Twitter4j google group:

Good luck! If you iron it out please come back and post the answer. 
pdxnat (author)  pdxnat2 years ago
Also, I've got this same project done using Python instead of Processing.

Not an answer, but I found Python to be really easy to use. In fact I don't even use Processing at all anymore.
Ok, thank you so much. But I've never used python before. I'll give it a try.
Im trying to figure how to install the libraries for mac.
alevisohn3 years ago
Hi pdxnat,

Thanks for the great project.

I'm trying to get this code running, but I am stuck installing the library. I've put it in the location you suggested which didn't work. I then tried the default library location (where I have installed other libraries) which is a subdirectory of the sketch folder. This didn't work either.

 I did find a discussion about installing libraries into Processing ( and tried to change the name of the twitter4j-core.jar to twitter4j.jar so that it matched the library name as they suggested. Processing then recognized the library, but gave me the following error:

"No library found for twitter4j.http"

Any thoughts on what I am doing wrong and why this isn't working?

Thanks, Aaron
pdxnat (author)  alevisohn3 years ago
The bottom line is No, I don't know what's wrong. But it's got to be a fluke and therefore fixable.

I have attached the twitter4j.jar file to this instructable page. I'm not sure if you could just use that file or if there's more installation that needs to happen, but it's here for archival purposes now.

On my Windows 7 box I've installed the twitter4j jar file here:
C:\Program Files\processing-1.5.1\modes\java\libraries\twitter4j\library\twitter4j (executable jar file)

I think I may have had to exit and reopen Processing, or restart the machine, I don't recall. Anyway, with the executable twitter4j file in that directory, it finally showed up in the Processing IDE under Menu:Sketch>Import Library>twitter4j. But you're saying that's not working for you, right?

Well, I did a little surfing and found that I'm a version or so out of date. The libraries can now go into the sketchbook directory, as you seem to know already. This what you mean by "the default library location" yes?

Over at I found this information:
"Processing now allows for a “libraries” folder inside your Processing sketchbook, which is a great deal more convenient than having your 3rd party libraries installed in the Processing application folder."

They're saying you can now install a library like twitter4j here (you add the directories into your \My Documents\Processing\ folder):
C:\My Documents\Processing\libraries\twitter4j\library\twitter4j (executable jar file)

This is mentioned again on the Processing site here:
" libraries must be ... placed within the "libraries" folder of your Processing sketchbook."
(To find the Processing sketchbook location on your computer, open the Preferences window from the Processing application and look for the "Sketchbook location" item at the top.)
And in the forums here:

You've tried this both ways and it's not working? I don't know what's up. I'm sorry. Unless someone else contributes here, I'd say ask the Processing forums. They've got a forum just for Contributed Libraries here:

Sorry I don't have the answer for you. It *should* work. ;-) 
Good luck!
I finally go it to work by taking the twitter4j.jar file attached to this page and put it into "sketchbook">libraries>twitter4j>library>
pdxnat (author)  falconphysics3 years ago
Excellent. Thanks for the info.
pdxnat (author)  pdxnat3 years ago
I've got this same project available using Python instead of Processing, in case that appeals to you. I know that's not an answer (and I want to know when you get twitter4j working) but I figured I'd mention it.
vishalapr3 years ago
pdxnat (author)  vishalapr3 years ago
Hi - can you be more explicit when you say "install twitter"? I don't see an exe in the download section at all (plus, I'm running linux, so "install" is a term that doesn't mean anything to me :) ).
pdxnat (author)  sheetmetalalchemist3 years ago
Howdy, it's actually "install twitter4j", pronounced twitter-for-jay, as in, 'for java'. It is a java library available here: . I'm not as familiar w/ linux as I'd like to be, but here's what I think you'll do. Download it from the link; uncompress it; and copy it to the proper directory so Processing knows this is a library it can import.

It needs to be in Processing's libraries folder. On Windows, that directory looks like this:
C:\Program Files\processing-1.5.1\modes\java\libraries
Probably for linux the last few directories are going to be the same:
... processing-1.5.1\modes\java\libraries
Within Processing, you'd import the library by starting with the "Sketch" menu: 
Processing > Sketch > Import Library... > twitter4j
And that should do it. 
pdxnat (author)  pdxnat3 years ago
From the link above:
Just add twitter4j-core-2.2.3.jar to your application classpath. If you are familiar with Java language, looking into the JavaDoc should be the shortest way for you to get started. twitter4j.Twitter interface is the one you may want to look at first.
Thanks for the reply! I caught the above info in the link, but that didn't seem to help me out too much. And, I don't know java at all, so the JavaDoc is pretty useless :). I may get a chance to poke away at it later...trying to get a test running kinda fast so I can have a little script like yours in my arsenal for the Red Bull Creation finals in a couple of weeks.

...and then, maybe I'll write an instructable about how to install twitter4j on ubuntu to make it simple :).
pdxnat (author)  sheetmetalalchemist3 years ago
I found two discussions in the Processing forums here and here that may / not help. Also, I wrote a Python version of this same project: simpleTweet_01 python. Definitely check that out as Python + Ubuntu go hand in hand. I knew absolutely zero about Python when I started that project, so I'm betting you can get it running in no time.

Good Luck!
Python definitely made this a lot simpler for me. Thanks a million for writing the instructable on both!
pdxnat (author)  pdxnat3 years ago
... also, the python version does not use twitter4j, it uses python-twitter and a couple other packages. In the Python v. Processing discussion, I'm native to java but impressed with Python. I will definitely use Python again. In fact, I've got another project that uses both Processing and Python in the same instructable, here: Twitter Mention Mood Light.