Arduino - processing - twitter

Hello everyboday!

Im trying to use accelerometer data to trigger a tweet on twitter. but for know im just trying to create a tweet just by clicking the mouse.

i'm using processing

I've been using this http://tinkerlondon.com/now/2010/09/14/oauth-twitter-and-processing/ tutorial.

I've deleted the " " around the keys and tokens but it says i have an unexpected char '' when i get to this line "Status st = twitter.updateStatus(msg + ” ” + second());"

one of the comments said that processing doesn't understand quotations so i deleted them all and i got a syntax error...

/*
Posts a message to a Twitter account when you press the mouse button.
Uses Twitter4j, http://twitter4j.org.
For more info: http://tinkerlondon.com/now/2010/09/13/oauth-twitter-and-processing/
Daniel Soltis, September 2010
*/

import twitter4j.conf.*;
import twitter4j.internal.async.*;
import twitter4j.internal.org.json.*;
import twitter4j.internal.logging.*;
import twitter4j.json.*;
import twitter4j.internal.util.*;
import twitter4j.management.*;
import twitter4j.auth.*;
import twitter4j.api.*;
import twitter4j.util.*;
import twitter4j.internal.http.*;
import twitter4j.*;
import twitter4j.internal.json.*;


String msg = Automatically posted from Processing;

//copy and paste these from your application in dev.twitter.com
String consumer_key = cv50u99znWOAOnOEUkhDA;
String consumer_secret = dzVnD4nLzQd9FAUg47AyEVJKPQgY6ey6dsdOZDsj2FY;
String oauth_token = 276648172-s7m9Z4BPDlREyGJkllFKu84lguDeWkJU4ZKm6Y9E;
String oauth_token_secret = zKN1i9SGNeh6PDjrEmv8BzeAYrErRac3Mjfs9XjU;

color bgcolor = color(255);
long timer;

void setup() {
size(640,480);
}

void draw() {
background(bgcolor);
if (millis()-timer > 2000) bgcolor = color(255);
}

void mousePressed() {
Twitter twitter = new TwitterFactory().getOAuthAuthorizedInstance (
consumer_key, consumer_secret,
new AccessToken( oauth_token, oauth_token_secret) );
try {
Status st = twitter.updateStatus(msg + ” ” + second());
println(“Successfully updated the status to [" + st.getText() + "].”);
bgcolor = color(0,0,255);
timer = millis();
}
catch (TwitterException e) {
println(e.getStatusCode());
}
}

can anyone help? Please?

Thanks.

one of the comments said that processing doesn't understand quotations so i deleted them all and i got a syntax error...

Then the comment was added by an idiot. Put them back.

Actually, the commenter was right, you just misunderstood what he was saying. You need the quotes. However, the way html formats quotes is misunderstood by processing, so you have to delete each quotation mark and replace it with a normal quotation mark once you have pasted the code into processing. (Find and replace is useful for this)
Edit: I here is the code you should use:

/*
Posts a message to a Twitter account when you press the mouse button.
Uses Twitter4j, http://twitter4j.org.
For more info: http://tinkerlondon.com/now/2010/09/13/oauth-twitter-and-processing/
Daniel Soltis, September 2010
*/

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

String msg = "Automatically posted from Processing";

//copy and paste these from your application in dev.twitter.com
String consumer_key = "publicconsumerkeyfromtwitterdevpage";
String consumer_secret = "secretconsumerkeyfromtwitterdevpage";
String oauth_token = "accesstokenforyouraccount";
String oauth_token_secret = "secretaccesstokenforyouraccount";

color bgcolor = color(255);
long timer;

void setup() {
size(640,480);
}

void draw() {
background(bgcolor);
if (millis()-timer > 2000) bgcolor = color(255);
}

void mousePressed() {
Twitter twitter = new TwitterFactory().getOAuthAuthorizedInstance (
consumer_key, consumer_secret,
new AccessToken( oauth_token, oauth_token_secret) );
try {
Status st = twitter.updateStatus(msg + " " + second());
println("Successfully updated the status to [" + st.getText() + "].");
bgcolor = color(0,0,255);
timer = millis();
}
catch (TwitterException e) {
println(e.getStatusCode());
}
}

Yeah I replaced them with new quotations and that worked but now processing is saying that the function getOAuthAuthorizedInstance(String, String, AccessToken) does not exist.

I found this on the processing site. http://wiki.processing.org/w/Error_Messages

"The function XXXX does not exist.
Processing has found something that it thinks is a function, but it can't find a match. Either the function has the wrong name or the wrong number and type of parameters."

This is the problem I'm having but im really new to processing so i don't know how to fix this.

Did you download twitter4j and place it in the right location?

i downloaded Twitter4j and i believe i'v installed it properly as it shows up in my import library menu.

is it possible that iv done it wrong?

Did you do this exactly?

The library you’ve downloaded will be in a folder labeled Twitter4j-2.1.3. As with any Processing library, place this folder inside your Documents–>Processing–>libraries folder.

Step 3: Change the name and structure of the folder
Processing kicks up a fuss at punctuation, so rename the folder to something like Twitter4j213.

Inside this folder, make a new folder called “library” and drag all of the contents of the Twitter4j213 folder inside the library folder. (So it changes from Twitter4j213–>all-the-contents to Twitter4j213–>library–>all-the-contents.)

Finally, change the name of Twitter4j-core-2.1.3.jar to “Twitter4j213.jar” (or whatever matches the name of the folder you just renamed).

Step 4: Reboot
Restart Processing, and look in Sketch–>Import Library. You should see “Twitter4j213? as an option.

  1. i downloaded twitter4j

  2. unzipped the file

  3. put twitter4j folder in my processing libraries folder

  4. created and new folder named library and put the contense of twitter4j into it

  5. changed the name of twitter4j-core-2.2.1.jar to twitter4j221.jar in the lib folder

  6. pulled that file from the lib folder and put it in the Library folder with the rest of the contence

this is the only way that i seemed to be able to get the twitter4j to show up in my import library menu

i should note that i have more of these than the tutorial code has...

import twitter4j.conf.;
import twitter4j.internal.async.
;
import twitter4j.internal.org.json.;
import twitter4j.internal.logging.
;
import twitter4j.json.;
import twitter4j.internal.util.
;
import twitter4j.management.;
import twitter4j.auth.
;
import twitter4j.api.;
import twitter4j.util.
;
import twitter4j.internal.http.;
import twitter4j.
;
import twitter4j.internal.json.*;

Why arent you using the tutorial code exactly?
Ok, I looked at the documentation. That function is now deprecated. You will have to adapt your code to use setOAuthConsumer instead.

i believe i am...the list of "import twitter4j" that importing is just longer that the list it shows in the tutorial. i assumed its because i downloaded an updated version of twitter4j.

As far as moving the twitter4j221.jar from the lib folder to the Library folder, I'm only doing that because thats the only way that it registers in my import libraries menu.

Yes you are correct to change the imports... but that function is no longer available. See this guide : http://twitter4j.org/en/versions.html#migration21x-22x

Does that mean i can no longer do what im trying to do?

twitter changed so that you can't login in using the username and password and you have to use the access token things.

im a little sceptical about incorporating these new lines.

  1. Twitter.getOAuthAccessToken(screenName, password)
  2. getInstance(Authorization)

and im confused as to what else i would need to change when they start talking about the json

I think if you leave the changed imports the way you changed them, then you just have to replace getOAuthAuthorizedInstance with getInstance

Im getting a new error "the method getinstance(AccessToken) in the type TwitterFactory is not applicable for the arguments (String, String, AccessToken)"

/*
Posts a message to a Twitter account when you press the mouse button.
Uses Twitter4j, http://twitter4j.org.
For more info: http://tinkerlondon.com/now/2010/09/13/oauth-twitter-and-processing/
Daniel Soltis, September 2010
*/

import twitter4j.conf.*;
import twitter4j.internal.async.*;
import twitter4j.internal.org.json.*;
import twitter4j.internal.logging.*;
import twitter4j.json.*;
import twitter4j.internal.util.*;
import twitter4j.management.*;
import twitter4j.auth.*;
import twitter4j.api.*;
import twitter4j.util.*;
import twitter4j.internal.http.*;
import twitter4j.*;
import twitter4j.internal.json.*;

String msg = "Automatically posted from Processing";

//copy and paste these from your application in dev.twitter.com
String consumer_key = "cv50u99znWOAOnOEUkhDA";
String consumer_secret = "dzVnD4nLzQd9FAUg47AyEVJKPQgY6ey6dsdOZDsj2FY";
String oauth_token = "276648172-s7m9Z4BPDlREyGJkllFKu84lguDeWkJU4ZKm6Y9E";
String oauth_token_secret = "zKN1i9SGNeh6PDjrEmv8BzeAYrErRac3Mjfs9XjU";

color bgcolor = color(255);
long timer;

void setup() {
size(640,480);
}

void draw() {
background(bgcolor);
if (millis()-timer > 2000) bgcolor = color(255);
}

void mousePressed() {
Twitter twitter = new TwitterFactory().getInstance (
consumer_key, consumer_secret, 
new AccessToken( oauth_token, oauth_token_secret)) ;
try {
Status st = twitter.updateStatus(msg + " " + second());
println("Successfully updated the status to [" + st.getText() + "].");
bgcolor = color(0,0,255);
timer = millis();
}
catch (TwitterException e) {
println(e.getStatusCode());
}
}

Might it be easier to post to twitter using this method? http://arduino-tweet.appspot.com/

If you have an ethernet shield, yes.
Otherwise, download the old version of Twitter4j here: http://twitter4j.org/en/twitter4j-2.1.3.zip, install it like the tutorial said so, and use the code exactly as it is provided in your tutorial. (except for the fixed quotation marks).

Oh man, after all that it was just the files.

Thank you SOOOOOO much for you help.

p.s. congrats on being a full member (100+ posts) :wink:

Glad to know it worked! Good luck with the rest of your project.

Hey mvaughan19, I thought you might like to see an example I made for Arduino Processing Twitter w/ twitter4j. The code for both Arduino and Processing are pasted below and you can go here: http://www.instructables.com/id/Simple-Tweet-Arduino-Processing-Twitter/ for directions on using OAuth.

I use Arduino to accept input from a switch to digital pin #10 and based on that value perform Serial.write(), which the Processing script reads and then posts a tweet using the twitter4j lib and OAuth.

It's bare bones as I tried to make it as uncomplicated as possible. It foundational code.

Good luck.

// 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(){
Serial.begin(9600);
pinMode(magReed_pin, INPUT);
}

void loop(){
watchTheDoor();
}

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

void compareStates(int i){
if (previousDoorState != i){
previousDoorState = i;
Serial.write(i);
delay(1000); //
}
}
// simpleTweet_00_p

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

 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.

compareMsg() is added in case you want to compare
the current and previous tweets to prevent retweets.

This Processing code requires the twitter4j library
and your registered app info from dev.twitter.com

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.
*/

import processing.serial.*;

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

static String OAuthConsumerKey = "YOUR CONSUMER KEY";
static String OAuthConsumerSecret = "YOUR CONSUMER SECRET";
static String AccessToken = "YOUR ACCESS TOKEN";
static String AccessTokenSecret = "YOUR ACCESS TOKEN SECRET";

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

void setup() {
size(125, 125);
frameRate(10);
background(0);
println(Serial.list());
String arduinoPort = Serial.list()[0];
arduino = new Serial(this, arduinoPort, 9600);
loginTwitter();
}

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

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

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

void listenToArduino() {
String msgOut = "";
int arduinoMsg = 0;
if (arduino.available() >= 1) {
arduinoMsg = arduino.read();
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) {
postMsg(newMsg); 
}
//println(s); // debug
}