The next phase of my project was to send the serial communication from my Arduino, over to Processing, which then would formulate a status update based on the button you pressed and send it over to Twitter. I’ve worked with several Twitter API’s in the past, so I decided to go with Twitter4J, the “J” in this case stands for Java, which works perfectly with Processing.
Bellow is an updated version of my previous Arduino coding. I plan on switching the status updating to the Processing side, instead of the Arduino side. If this where to be a real product, it would be easier to download a software update, then to have to reprogram the toy itself. I will add that to my todo list, once i get all the major functionality working.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 | // BUTTONS const int buttonGreen = 8; const int buttonRed = 9; const int buttonYellow = 10; //LEDS const int ledGreen = 2; const int ledRed = 3; const int ledYellow = 4; //STORE BUTTON STATES int greenState = 0; int redState = 0; int yellowState = 0; int lastActiveLED = 0; // Keep track of the activeLED void setup() { Serial.begin(9600); pinMode(ledGreen, OUTPUT); pinMode(ledRed, OUTPUT); pinMode(ledYellow, OUTPUT); pinMode(buttonGreen, INPUT); pinMode(buttonRed, INPUT); pinMode(buttonYellow, INPUT); } void loop(){ // CHECK BTN STATES greenState = digitalRead(buttonGreen); redState = digitalRead(buttonRed); yellowState = digitalRead(buttonYellow); /********** * Each of the following conditionals only sends one Serial message * per button press **********/ //GREEN if (greenState == HIGH) { //check if last active LED was not Green if(lastActiveLED != ledGreen){ //print the respective message for each button only once per mouse press. End message transmission with '10' which is linefeed. Serial.print("green"); Serial.print(10, BYTE); } //store green as last active press lastActiveLED = ledGreen; digitalWrite(ledGreen, HIGH); } else { //clear out last active so you could reclick it if(lastActiveLED == ledGreen){ lastActiveLED = 0; } digitalWrite(ledGreen, LOW); delay(100); } //RED if (redState == HIGH) { if(lastActiveLED != ledRed){ Serial.print("red"); Serial.print(10, BYTE); } lastActiveLED = ledRed; digitalWrite(ledRed, HIGH); delay(100); } else { if(lastActiveLED == ledRed){ lastActiveLED = 0; } digitalWrite(ledRed, LOW); } //YELLOW if (yellowState == HIGH) { if(lastActiveLED != ledYellow){ Serial.print("yellow"); Serial.print(10, BYTE); } lastActiveLED = ledYellow; digitalWrite(ledYellow, HIGH); delay(100); } else { if(lastActiveLED == ledYellow){ lastActiveLED = 0; } digitalWrite(ledYellow, LOW); } } |
Now that we are sending message, we need to listen. I decided to go with Processing since I’ve always been curious about it, and it is about as seamless of a coding experience as you could possibly get with Arduino.
In order to get the Twitter4J Java Library working you first needed to drag the .jar file directly onto your Processing sketch, and that is pretty much it. Bellow is the Processing code that simply listens for a Serial Event (Arduino button press), and then sends the status message over to Twitter.
Being a total N00bie to Processing, one additional tip that confused me was Fonts. Processing doesn’t allow True Type Fonts, so if you want to add text to your project you need to go to:
Tools > Create Fonts…
and then select the font you would like to use. The Processing IDE then converts the font to a .vlw file. You could find the file in a folder named ‘data’ that gets created within your projected folder.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | /************* * based on: http://processing.org/reference/libraries/serial/serialEvent_.html *************/ import processing.serial.*; Serial myPort; // The serial port PFont myFont; // The display font String inString; // Input string from serial port int lf = 10; // ASCII linefeed Twitter twitter; // Twitter //Going to get oAuth working instead of this, but this will do for now String username = "YOUR-TWITTER-USERNAME"; // you Twitter Username Here String password = "YOUR-TWITTER-PASSWORD"; // your Twitter Password Here void setup() { size(400,200); twitter = new Twitter(username,password); myFont = loadFont("AppleGothic-48.vlw"); textFont(myFont, 18); println(Serial.list()); myPort = new Serial(this, Serial.list()[0], 9600); myPort.bufferUntil(lf); //wiat for line feed to specify end of serial buffer } void draw() { background(100); text("color selected: " + inString, 10,50); } void serialEvent(Serial p) { inString = p.readString();//read serial string //For some reason this only wanted to work in a try catch try { Status status1 = twitter.updateStatus("Arduino's favorite color is "+inString);//update twitter status } catch( TwitterException e) { println(e.getStatusCode()); } } |
Popularity: 12% [?]
arduino, physical computing, source, twitter











Update Twitter Status w/ Arduino & Processing http://blog.unthinkmedia.com/?p=520
[...] This post was mentioned on Twitter by Renzo. Renzo said: digging on: Update Twitter Status w/ Arduino & Processing: video Demo and Code snippets to enable Arduino to updat… http://bit.ly/awRUqN [...]
Hi, I noticed in the embedded video you also added the feature of having a 4th LED light up when you send/receive a new tweet (@ 1:30). I don't think those steps/code were in the tutorial, so I was wondering if you would be willing to explain how you did that, or at least point me in the right direction to figuring that out.
*I was confused when you explained the additional LED in the video. Does it light up only when you send Tweets (via the actual keyboard), or does it also light up when a tweet is posted from someone you are following?
Thanks.
Hey try adding this Processing code, on the arduino side just look for the 'A' when someone in your friends list types the word 'Arduino', in your serialEvent and have it do something. Pretty sure that would work. Let me know if that help.
========================================================
//Date today = new Date();
Date latestTweetDate = new Date();
long previousMillis = 0; // will store last time we checked Twitter for updates
long interval = 10000; // interval at which to blink (milliseconds)
void draw() {
background(100);
text("color selected: " inString, 10,50);
//borrowed from Blink w/o Delay Arduino sketch
//Search for updates once every minute
if (millis() – previousMillis > interval) {
println("Call Made");
// save the last time you blinked the LED
previousMillis = millis();
try {
// The factory instance is re-useable and thread safe.
List statuses = twitter.getFriendsTimeline();
for( int i=0; i<statuses.size(); i )
{
Status status = (Status)statuses.get(i);
// Check to see if there is a match
String[] m1 = match(status.getText(), "Arduino");
if (m1 != null) {
// Check to see if tweet is new
Date tweetDate = status.getCreatedAt();
boolean isAfter = tweetDate.after(latestTweetDate);
if(isAfter){
println("Found a NEW match in '" " " status.getText() "'");
myPort.write('A');
latestTweetDate = status.getCreatedAt();
}
}
}
}
catch (TwitterException te) {
println("Couldn't connect: " te);
}
}
}
Thanks for the help and the code. I have tried it out but I still get some errors, here is how I have it setup:
1. I inserted the new processing code (from the comment above) right before the end curly bracket of the void serialEvent (line 43 in the processing code of the tutorial).
2. Just to check for errors I decided to run processing without updating the Arduino code. It gave me the error: "Syntax Error, maybe missing right parenthesis?" and highlighted this line of code:
if (millis() – previousMillis > interval) {
If I got rid of that line of code (and the corresponding end curly bracket) then I would get the same error message except this line of code would be highlighted:
for( int i=0; i<statuses.size(); i )
I am not that great when it comes to coding, but it seems like the proper parenthesis (open and closing) exist for both lines of code. Any idea about what is going wrong?
Also, on the Arduino side if I want to make an additional LED light up, what exactly am I looking for to come through the serial port? Is the capital letter "A" being sent through the serial port from Processing? or is the work "Arduino" being sent?
If it is a word/letter being sent through serial then I probably just have to set up an "if statement" relating to that text string to trigger an LED to turn on, correct?
Also, just for clarification: This new processing code checks the date to see if there are any new status updates (tweets) from anybody that I am following, if there is a new status update then it sends out "A" through the serial port back to my Arduino? Or does this code check to see if anybody I am following tweeted the word "Arduino"?
I know this is asking for a lot of help and clarification so if you don't have the time that is quite understandable. Thanks.
Hi all, the example above is helpful and I thought I’d add my own instructable here in case it helps anyone searching for arduino processing twitter w/ twitter4j
http://www.instructables.com/id/Simple-Tweet-Arduino-Processing-Twitter/
Spread the love
Thanks for taking the time and creating an instructable!
[...] the code: source Share this:TwitterFacebookLike this:LikeBe the first to like this [...]