Go Down

Topic: Arduino Processing Twitter w/ twitter4j (Read 2867 times) previous topic - next topic

pdxNat

Howdy y'all. Just wanted to post this Arduino code here to help those who might want it.

Below you'll find simple Arduino code that performs Serial.write() based on input to digital pin #10.
Processing code takes that Serial.write() value and posts a message to Twitter.
For a full explanation please visit the instructable here:
http://www.instructables.com/id/Simple-Tweet-Arduino-Processing-Twitter/

Cheers.

Code: [Select]

// 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); //
}
}

bld

Nice :) A bit annoying they removed the old auth system, was a lot easier to use...

But I am working on something to get in between, so an arduino again can post -almost- directly to twitter again, and do it easily :)
captain-slow.dk | non contagious!

mmcp42

difficult to see what this code has to do with twitter
there are only 10 types of people
them that understands binary
and them that doesn't

bld


difficult to see what this code has to do with twitter

Open the link, and locate the processing source.

The arduino actually only tells processing to do the twittering stuff.
captain-slow.dk | non contagious!

mmcp42

#4
Jun 12, 2011, 04:15 pm Last Edit: Jun 12, 2011, 04:16 pm by mmcp42 Reason: 1
sure the link does
but the code posted seems to be checking a door magnet!!

hadn't spotted that the PC is doing the interesting part
there are only 10 types of people
them that understands binary
and them that doesn't

pdxNat


hadn't spotted that the PC is doing the interesting part

Sorry I wasn't more explicit. You're correct. The arduino is used simply to evaluate a switch and to Serial.write() based on what the switch does. I use Processing on the PC to listen to arduino and to post to twitter. Processing could be replaced by Python, Perl, C++, Java or any such language capable of reading the Serial.write() call.

As far as I understand, Arduino cannot post directly to twitter w/out some go-between language to do the heavy lifting. I use the arduino to manage the hardware and then send that data to the PC where it's managed by a program there.

Here is the Processing code:
Code: [Select]
// 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
}

pdxNat


Nice :) A bit annoying they removed the old auth system, was a lot easier to use...

But I am working on something to get in between, so an arduino again can post -almost- directly to twitter again, and do it easily :)

Fun!

I think the OAuth system is no more difficult than the old username / password system. Sure, you've got to "Register your application" but as I explain here: http://www.instructables.com/id/Simple-Tweet-Arduino-Processing-Twitter/step4/Twitter/ that parts easy.

Good luck

pdxNat

The project was designed for this type of sequence:

Arduino manages the hardware and sends up a Serial.write() call when something happens.

Processing listens to Arduino and sends up a Twitter post when Arduino sends up that Serial.write() call.


bld



Nice :) A bit annoying they removed the old auth system, was a lot easier to use...

But I am working on something to get in between, so an arduino again can post -almost- directly to twitter again, and do it easily :)

Fun!

I think the OAuth system is no more difficult than the old username / password system. Sure, you've got to "Register your application" but as I explain here: http://www.instructables.com/id/Simple-Tweet-Arduino-Processing-Twitter/step4/Twitter/ that parts easy.

Good luck

Yeah, I actually already made it... still testing it though and working on a way to make it easy to use
captain-slow.dk | non contagious!

Go Up