Using twitter4j, processing, and the serial port, load twitter feeds in arduino.
cb.setOAuthConsumerKey("yourconsumerkey"); cb.setOAuthConsumerSecret("yourConsumerSecret"); cb.setOAuthAccessToken("yourAccessToken"); cb.setOAuthAccessTokenSecret("yourAccessTokenSecret");
Try running the program. You should see the word hello scrolling across the screen. Now click the mouse on the sketch. Does the word hello change? Did you grab a tweet?! If so, congrats you're on your way! Otherwise, double check that you got the right keys and access tokens in the right place. Make sure you imported the twitter4j library into your sketch and that you have an internet connection. Make sure there are tweets indexed to you current search term. The search term is whatever is in quotes in the line:
Query query = new Query("#HeartMatrix");
String portName = Serial.list()[0]; myPort = new Serial(this, portName, 9600);Change the 0 to whichever port you want to use. All the ports are listed on the console by the previous line in the setup function. Now on the Arduino side of things, you will need to listen to the Serial port and store what it sends you in a String variable. This is the code to do just that:
while (Serial.available() ){ char c = Serial.read(); if (c == 8) message = ""; else { String s = String(c); message += s; hm.set_message(message); } }
Check out the whole Arduino code file below. We show how to use it with our heart matrix, but you could grab the tweet and parse it to control a robot, lights, or a motor. Let your imagination go twitter crazy!
String get_tweet(){ String a_tweet = ""; ConfigurationBuilder cb = new ConfigurationBuilder(); cb.setOAuthConsumerKey("yourconsumerkey"); cb.setOAuthConsumerSecret("yourConsumerSecret"); cb.setOAuthAccessToken("yourAccessToken"); cb.setOAuthAccessTokenSecret("yourAccessTokenSecret"); Twitter twitter = new TwitterFactory(cb.build()).getInstance(); Query query = new Query("#HeartMatrix"); try { QueryResult result = twitter.search(query); } catch (TwitterException te) { println("Couldn't connect: " + te); a_tweet = "No Tweet Found :("; }; query.setCount(100); try { QueryResult result = twitter.search(query); ArrayList tweets = (ArrayList) result.getTweets(); if (tweets.size() > 0 ){ Status t = (Status) tweets.get(0); String user = t.getUser().getScreenName(); a_tweet = t.getText(); Date d = t.getCreatedAt(); println("Tweet by " + user + " at " + d + ": " + cur_tweet); } else a_tweet = "No Tweet Found :("; } catch (TwitterException te) { println("Couldn't connect: " + te); a_tweet = "Twitter Exception :("; }; return a_tweet;}
If nothing can be found or the credential don't work, we just set the return text to "Twitter Exception :(" or "No Tweet Found :(" so you don't have to do any error-catching. You can grab much more than just a tweet, but that is all we needed for our project. Check the
twitter4j documentation
for more capabilities. So every time the mouse is pressed we query twitter for more tweets. We grab the first one and compare it to the current one. If it is a new tweet, we send it out on the serial port and update the cur_tweet variable. This all happens in the check twitter function:
void check_twitter(){ String new_tweet = get_tweet(); if (!new_tweet.equals(cur_tweet)){ cur_tweet = new_tweet; myPort.write(8); for (int i = 0; i < cur_tweet.length(); i++){ myPort.write(cur_tweet.charAt(i)); } }}
Careful about checking twitter too frequently, like in the the draw loop, for example, they may start to think you are spamming them.
/* LucidTronix Processing sketch * Twitter API to the serial port * for more info see the tutorial at: * http://www.lucidtronix.com/tutorials/32*/import processing.serial.*;import java.util.*;Serial myPort; // Create object from Serial classint val; // Data received from the serial port//Build an ArrayList to hold all of the words that we get from the imported tweetsArrayListwords = new ArrayList();String cur_tweet = "Hello";int cur_x = 600;int last_check = 0;void setup() { size(600, 600); // I know that the first port in the serial list on my mac // is always my FTDI adaptor, so I open Serial.list()[0]. // On Windows machines, this generally opens COM1. // Open whatever port is the one you're using. println(Serial.list()); String portName = Serial.list()[0]; myPort = new Serial(this, portName, 9600);}void draw() { //Draw a faint black rectangle over what is currently on the stage so it fades over time. background(0); fill(40,205,50); text(cur_tweet, cur_x, 150); cur_x -= 2; if ( cur_x < -1*textWidth(cur_tweet )) cur_x = width; //if (millis() - last_check > 3000) check_twitter; }void mousePressed(){ check_twitter();}void check_twitter(){ String new_tweet = get_tweet(); if (!new_tweet.equals(cur_tweet)){ cur_tweet = new_tweet; myPort.write(8); for (int i = 0; i < cur_tweet.length(); i++){ myPort.write(cur_tweet.charAt(i)); } } }String get_tweet(){ String a_tweet = ""; ConfigurationBuilder cb = new ConfigurationBuilder(); cb.setOAuthConsumerKey("yourconsumerkey"); cb.setOAuthConsumerSecret("yourConsumerSecret"); cb.setOAuthAccessToken("yourAccessToken"); cb.setOAuthAccessTokenSecret("yourAccessTokenSecret"); Twitter twitter = new TwitterFactory(cb.build()).getInstance(); Query query = new Query("#HeartMatrix"); try { QueryResult result = twitter.search(query); } catch (TwitterException te) { println("Couldn't connect: " + te); a_tweet = "No Tweet Found :("; }; query.setCount(100); try { QueryResult result = twitter.search(query); ArrayList tweets = (ArrayList) result.getTweets(); if (tweets.size() > 0 ){ Status t = (Status) tweets.get(0); String user = t.getUser().getScreenName(); a_tweet = t.getText(); Date d = t.getCreatedAt(); println("Tweet by " + user + " at " + d + ": " + cur_tweet); } else a_tweet = "No Tweet Found :("; } catch (TwitterException te) { println("Couldn't connect: " + te); a_tweet = "Twitter Exception :("; }; return a_tweet;}
while (Serial1.available() ) { char c = Serial1.read(); if (c == '/') message = ""; else { String s = String(c); message += s; hm.set_message(message); } if (debug) { Serial.print("got the letter:"); Serial.println(c); } }
This code uses our
heart matrix library
.
/* LucidTronix write messages from * the serial port on the Heart Matrix. * For instructions details and schematic * http://www.lucidtronix.com/tutorials/32 * If debug is true outputs whatever was received * On serial1 to the standard USB serial. */ #include#include String message = " Hi ";bool debug = true;HeartMatrix hm = HeartMatrix(5,6,7); // dataPin is on 5, Latch is on 6, and clock is on 7void displayer2(){ hm.displayer();}void setup(){ if(debug) Serial.begin(9600); // initialize Serial Serial1.begin(9600); // initialize Serial1 MsTimer2::set(1,displayer2); MsTimer2::start(); hm.set_message(" Hello I'm the Heart Matrix from LucidTroniX! ");}void loop() { hm.on(); while (Serial1.available() ){ char c = Serial1.read(); if (c == '/') message = ""; else { String s = String(c); message += s; hm.set_message(message); } if(debug){ Serial.print("got the letter:"); Serial.println(c); } } }
| Title | Description | # | Cost | Link | Picture |
| Heart Matrix Kit | The heart matrix kit. Shift register based wearable heart-shaped LED display. | 1 | $17.95 | Link |
|
Comments: