Step 2: Arduino

// simpleTweet_00_a

 simpleTweet_00 arduino sketch (for use with
 simpleTweet_00 processing sketch) by @msg_box june2011

 This script is intended for use with a magnetic reed switch,
but any on/off switch plugged into pin #10 will readily work.

The Arduino is connected to a circuit with a sensor that
triggers the code: Serial.write(n); where n = 1 or 2.
The Processing sketch listens for that message and then
uses the twitter4j library to connect to Twitter
via OAuth and post a tweet.

To learn more about arduino, processing, twitter4j,
OAuth, and registering your app with Twitter...
visit <http://www.instructables.com/id/Simple-Tweet-Arduino-Processing-Twitter/>
visit <http://www.twitter.com/msg_box>

This code was made possible and improved upon with
help from people across the internet. Thank You.
Special shoutouts to the helpful lurkers at twitter4j,
arduino, processing, and bloggers everywhere, and
to the adafruit & ladydada crowdsource.
And above all, to my lovely wife, without
whom, none of this would have been possible.

Don't be a dick.

const int magReed_pin = 10; // pin number
int magReed_val = 0;
int currentDoorState = 1; // begin w/ circuit open
int previousDoorState = 1;

void setup(){
pinMode(magReed_pin, INPUT);

void loop(){

void watchTheDoor(){
magReed_val = digitalRead(magReed_pin);
if (magReed_val == LOW){ // open
currentDoorState = 1;
if (magReed_val == HIGH){ // closed
currentDoorState = 2;

void compareStates(int i){
if (previousDoorState != i){
previousDoorState = i;
delay(1000); //
Thank you so much, worked perfectly! really awesome. <br>
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. <br>By the way great work ,helped a lot...!!!
Thank for your good info. <br> <br>I tried this project. <br> <br>But Only works at just 1st time to my iphone twitter app. <br> <br>and then never works again --; <br> <br>My error message is here: <br>------------------------------------------------------------------ <br>RXTX Warning: Removing stale lock file. /var/lock/LK.027.033.008 <br> <br>What's wrong with me ? <br> <br>Intel iMac x64, MacOSX 10.8.3 <br>Arduino 1.5.1 <br>Processing 2.0b8 <br>Twitter4j.jar that you offering one. <br> <br> <br> <br>
Hi everyone, I found a solution to all your problems. I'll post soon! ;)
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). <br>But i am currently getting these errors on processing: <br>Stable Library <br>========================================= <br>Native lib Version = RXTX-2.1-7 <br>Java lib Version = RXTX-2.1-7 <br>[0] &quot;COM5&quot; <br>[1] &quot;COM7&quot; <br>[2] &quot;COM8&quot; <br>gnu.io.PortInUseException: Unknown Application <br> at gnu.io.CommPortIdentifier.open(CommPortIdentifier.java:354) <br> at processing.serial.Serial.(Unknown Source) <br> at processing.serial.Serial.(Unknown Source) <br> at simpleTweet_00_p.setup(simpleTweet_00_p.java:91) <br> at processing.core.PApplet.handleDraw(Unknown Source) <br> at processing.core.PApplet.run(Unknown Source) <br> at java.lang.Thread.run(Thread.java:662) <br>Exception in thread &quot;Animation Thread&quot; java.lang.RuntimeException: Error inside Serial.() <br> at processing.serial.Serial.errorMessage(Unknown Source) <br> at processing.serial.Serial.(Unknown Source) <br> at processing.serial.Serial.(Unknown Source) <br> at simpleTweet_00_p.setup(simpleTweet_00_p.java:91) <br> at processing.core.PApplet.handleDraw(Unknown Source) <br> at processing.core.PApplet.run(Unknown Source) <br> at java.lang.Thread.run(Thread.java:662) <br> <br>[color=red]This is my current processing code:[/color] <br> <br>import processing.serial.*; <br> <br>import twitter4j.conf.*; <br>import twitter4j.internal.async.*; <br>import twitter4j.internal.org.json.*; <br>import twitter4j.internal.logging.*; <br>import twitter4j.http.*; <br>import twitter4j.api.*; <br>import twitter4j.util.*; <br>import twitter4j.internal.http.*; <br>import twitter4j.*; <br> <br>static String OAuthConsumerKey = &quot;ykaw70kvBc6jVV21eLlWA&quot;; <br>static String OAuthConsumerSecret = &quot;PllQqnwaZV7aYuH33vniloGW2U5fbkfYxef2LQVAK0&quot;; <br>static String AccessToken = &quot;20026567-9juOyYchv9k7PXsu7kyrvhpHKkOY3Fg6xTn9vatuA&quot;; <br>static String AccessTokenSecret = &quot;AcIhlVX95nvpFYMyk9oh41PWHe3TXYqhMbSy6hLgQ&quot;; <br> <br>Serial arduino; <br>Twitter twitter = new TwitterFactory().getInstance(); <br> <br>void setup() { <br> size(125, 125); <br> frameRate(10); <br> background(0); <br> println(Serial.list()); <br> String arduinoPort = Serial.list()[0]; <br> arduino = new Serial(this, arduinoPort, 9600); [color=red]ERROR BEGINS ON THIS LINE[/color] <br> loginTwitter(); <br>} <br> <br>void loginTwitter() { <br> twitter.setOAuthConsumer(OAuthConsumerKey, OAuthConsumerSecret); <br> AccessToken accessToken = loadAccessToken(); <br> twitter.setOAuthAccessToken(accessToken); <br>} <br> <br>private static AccessToken loadAccessToken() { <br> return new AccessToken(AccessToken, AccessTokenSecret); <br>} <br> <br>void draw() { <br> background(0); <br> text(&quot;simpleTweet_00&quot;, 18, 45); <br> text(&quot;@msg_box&quot;, 30, 70); <br> listenToArduino(); <br>} <br> <br>void listenToArduino() { <br> String msgOut = &quot;&quot;; <br> int arduinoMsg = 0; <br> if (arduino.available() &gt;= 1) { <br> arduinoMsg = arduino.read(); <br> if (arduinoMsg == 1) { <br> msgOut = &quot;Opened door at &quot;+hour()+&quot;:&quot;+minute()+&quot;:&quot;+second(); <br> } <br> if (arduinoMsg == 2) { <br> msgOut = &quot;Closed door at &quot;+hour()+&quot;:&quot;+minute()+&quot;:&quot;+second(); <br> } <br> compareMsg(msgOut); // this step is optional <br> // postMsg(msgOut); <br> } <br>} <br> <br>void postMsg(String s) { <br> try { <br> Status status = twitter.updateStatus(s); <br> println(&quot;new tweet --:{ &quot; + status.getText() + &quot; }:--&quot;); <br> } <br> catch(TwitterException e) { <br> println(&quot;Status Error: &quot; + e + &quot;; statusCode: &quot; + e.getStatusCode()); <br> } <br>} <br> <br>void compareMsg(String s) { <br> // compare new msg against latest tweet to avoid reTweets <br> java.util.List statuses = null; <br> String prevMsg = &quot;&quot;; <br> String newMsg = s; <br> try { <br> statuses = twitter.getUserTimeline(); <br> } <br> catch(TwitterException e) { <br> println(&quot;Timeline Error: &quot; + e + &quot;; statusCode: &quot; + e.getStatusCode()); <br> } <br> Status status = (Status)statuses.get(0); <br> prevMsg = status.getText(); <br> String[] p = splitTokens(prevMsg); <br> String[] n = splitTokens(newMsg); <br> //println(&quot;(&quot;+p[0]+&quot;) -&gt; &quot;+n[0]); // debug <br> if (p[0].equals(n[0]) == false) { <br> postMsg(newMsg); <br> } <br> //println(s); // debug <br>} <br> <br>[color=red]And these are the errors on python:[/color] <br> <br>running... simpleTweet_01_python <br>arduino msg: #peacefulGlow <br> <br>Traceback (most recent call last): <br> File &quot;C:/Users/Ciaran/Desktop/python final&quot;, line 47, in <br> listenToArduino() <br> File &quot;C:/Users/Ciaran/Desktop/python final&quot;, line 24, in listenToArduino <br> compareMsg(msg.strip()) <br> File &quot;C:/Users/Ciaran/Desktop/python final&quot;, line 31, in compareMsg <br> pM = &quot;&quot;+prevMsg[0]+&quot;&quot; <br>IndexError: list index out of range <br>&gt;&gt;&gt; <br> <br>[color=red][font=Verdana]This is my current python code:[/font][/color] <br> <br> <br>print 'running... simpleTweet_01_python' <br> <br># import libraries <br>import twitter <br>import serial <br>import time <br> <br># connect to arduino via serial port <br>arduino = serial.Serial('COM5', 9600, timeout=1) <br> <br># establish OAuth id with twitter <br>api = twitter.Api(consumer_key='ykaw70kvBc6jVV21eLlWA', <br> consumer_secret='PllQqnwaZV7aYuH33vniloGW2U5fbkfYxef2LQVAK0', <br> access_token_key='20026567-9juOyYchv9k7PXsu7kyrvhpHKkOY3Fg6xTn9vatuA', <br> access_token_secret='AcIhlVX95nvpFYMyk9oh41PWHe3TXYqhMbSy6hLgQ') <br> <br># listen to arduino <br>def listenToArduino(): <br> msg=arduino.readline() <br> if msg &gt; '': <br> print 'arduino msg: '+msg.strip() <br> compareMsg(msg.strip()) <br> <br># avoid duplicate posts <br>def compareMsg(newMsg): <br> # compare the first word from new and old <br> status = api.GetUserTimeline('yourUsername') <br> prevMsg = [s.text for s in status] <br> pM = &quot;&quot;+prevMsg[0]+&quot;&quot; <br> pM = pM.split() <br> nM = newMsg.split() <br> print &quot;prevMsg: &quot;+pM[0] <br> print &quot;newMsg: &quot;+nM[0] <br> if pM[0] != nM[0]: <br> print &quot;bam&quot; <br> postMsg(newMsg) <br> <br># post new message to twitter <br>def postMsg(newMsg): <br> localtime = time.asctime(time.localtime(time.time())) <br> tweet = api.PostUpdate(hello) <br> print &quot;tweeted: &quot;+tweet.text <br> <br>while 1: <br> listenToArduino() <br>_______________________________________________________________________ <br> <br>I know atleast one of the buttons are working as it sends the signal of [color=red]arduino msg: #peacefulGlow <br> <br>[/color] to pyhon when running module but as soon as the button is pressed then error messages appear. <br>My LED is not lighting up at all :( <br> <br>I can send pictures of the circuitboard if needed. <br> <br>Please will someone help me with this. <br>Either contact me here or and email to broadleyutb@googlemail.com would be great <br> <br>Thanks <br> <br> <br>
this is relation to the moodlight twitter mention tutorial you made. <br>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? <br>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
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!
Hi mate. <br>Im getting errors on processing like, no libraries for twitter4j.http. <br>So I downloaded a jar file called, twitter4j-2.0.9 and draged it. But I was libraries using twitter4j 2.2.5-<br><br>Then another error came up, acces token is ambiguos.<br>And then on the code I put: <br>import twitter4j.http.OAuthToken*;<br><br>And then it says the Token is not visible. <br>Im using mac, I really need help. Thanks.<br>
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 &quot;Processing&quot; 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:<br> <a href="http://groups.google.com/group/twitter4j" rel="nofollow">http://groups.google.com/group/twitter4j</a><br> <br> Good luck! If you iron it out please come back and post the answer.&nbsp;
Also, I've got this same project done using Python instead of Processing. <a href="http://www.instructables.com/id/simpleTweet01python/" rel="nofollow">http://www.instructables.com/id/simpleTweet01python/</a><br> <br> 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. <br>Im trying to figure how to install the libraries for mac.
Hi pdxnat,<br> <br> Thanks for the great project.<br> <br> 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.<br> <br> &nbsp;I did find a discussion about installing libraries into Processing <a href="http://processing.org/discourse/yabb2/YaBB.pl?num=1237933931">(http://processing.org/discourse/yabb2/YaBB.pl?num=1237933931</a>) 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:<br> <br> &quot;No library found for twitter4j.http&quot;<br> <br> Any thoughts on what I am doing wrong and why this isn't working?<br> <br> Thanks, Aaron
The bottom line is No, I don't know what's wrong. But it's got to be a fluke and therefore fixable.<br> <br> 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.<br> <br> On my Windows 7 box I've installed the twitter4j jar file here:<br> <em>C:\Program Files\processing-1.5.1\modes\java\libraries\twitter4j\library\twitter4j (executable jar file)</em><br> <br> 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&gt;Import Library&gt;twitter4j. But you're saying that's not working for you, right?<br> <br> 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 &quot;the default library location&quot; yes?<br> <br> Over at <a href="http://www.learningprocessing.com/tutorials/libraries/">http://www.learningprocessing.com/tutorials/libraries/</a> I found this information:<br> <em>&quot;Processing now allows for a &ldquo;libraries&rdquo; folder inside your Processing sketchbook, which is a great deal more convenient than having your 3rd party libraries installed in the Processing application folder.&quot;</em><br> <br> They're saying you can now install a library like twitter4j here (you add the directories into your \My Documents\Processing\ folder):<br> <em>C:\My Documents\Processing\libraries\twitter4j\library\twitter4j (executable jar file)</em><br> <br> This is mentioned again on the Processing site here:&nbsp;<a href="http://wiki.processing.org/w/How_to_Install_a_Contributed_Library">http://wiki.processing.org/w/How_to_Install_a_Contributed_Library</a><br> <em>&quot; libraries must be ... placed within the &quot;libraries&quot; folder of your Processing sketchbook.&quot;</em><br> (To find the Processing sketchbook location on your computer, open the Preferences window from the Processing application and look for the &quot;Sketchbook location&quot; item at the top.)<br> And in the forums here:&nbsp;<a href="http://forum.processing.org/topic/how-to-install-a-contributed-library">http://forum.processing.org/topic/how-to-install-a-contributed-library</a><br> <br> 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:&nbsp;<a href="http://forum.processing.org/contributed-library-questions">http://forum.processing.org/contributed-library-questions</a><br> <br> Sorry I don't have the answer for you. It *should* work. ;-)&nbsp;<br> Good luck!
I finally go it to work by taking the twitter4j.jar file attached to this page and put it into &quot;sketchbook&quot;&gt;libraries&gt;twitter4j&gt;library&gt;
Excellent. Thanks for the info.
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.<br> <br> <a href="http://www.instructables.com/id/simpleTweet01python/">http://www.instructables.com/id/simpleTweet01python/</a>
Hi - can you be more explicit when you say &quot;install twitter&quot;? I don't see an exe in the download section at all (plus, I'm running linux, so &quot;install&quot; is a term that doesn't mean anything to me :) ).
Howdy, it's actually &quot;install twitter4j&quot;, pronounced twitter-for-jay, as in, 'for java'. It is a java library available here: <a href="http://twitter4j.org/en/index.html#download ">http://twitter4j.org/en/index.html#download</a>&nbsp;. 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.<br> <br> It needs to be in Processing's libraries folder. On Windows, that directory looks like this:<br> C:\Program Files\processing-1.5.1\modes\java\libraries<br> Probably for linux the last few directories are going to be the same:<br> ...&nbsp;processing-1.5.1\modes\java\libraries<br> Within Processing, you'd import the library by starting with the &quot;Sketch&quot; menu:&nbsp;<br> Processing &gt; Sketch &gt; Import Library... &gt; twitter4j<br> And that should do it.&nbsp;
From the link above:<br> <em>Just add twitter4j-core-2.2.3.jar to your application classpath. If you are familiar with Java language, looking into the <a href="http://twitter4j.org/en/javadoc/index.html">JavaDoc </a>should be the shortest way for you to get started.<a href="http://twitter4j.org/en/javadoc/twitter4j/Twitter.html"> twitter4j.Twitter</a> interface is the one you may want to look at first.</em>
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.<br><br>...and then, maybe I'll write an instructable about how to install twitter4j on ubuntu to make it simple :).
I found two discussions in the Processing forums <a href="http://processing.org/discourse/yabb2/YaBB.pl?num=1210090496/0">here</a>&nbsp;and <a href="http://processing.org/discourse/yabb2/YaBB.pl?num=1274657654">here</a>&nbsp;that may / not help. Also, I wrote a Python version of this same project: <a href="http://www.instructables.com/id/simpleTweet01python/">simpleTweet_01 python</a>. 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.<br> <br> Good Luck!
Python definitely made this a lot simpler for me. Thanks a million for writing the instructable on both!
... 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: <a href="http://www.instructables.com/id/Twitter-Mention-Mood-Light/">Twitter Mention Mood Light.</a>

About This Instructable


24 favorites


More by pdxnat: Twitter Mention Mood Light simpleTweet_01 python How to install Python packages on Windows 7
Add instructable to: