So I've building on top of a sketch shared on instructables and have set almost everything up, I'm trying to make a chair tweet an update and take a photo on sit down via arduino and a reedswitch. Everything works EXCEPT the twitter part. (ftp photo uploads, reedswitch trigger, etc all are functional and operating correctly)
I've discovered that Twitter has changed and will now only accept Oauth. Here is my code below, can anyone help me morph this into OAuth friendly code? I've already created an app on dev.twitter and have all the info to input but I was looking at robotgrrrls processing/tweetcode and i'm a little overwhelmed on where to try and mash her code into this...
Here is her sketch:
https://github.com/RobotGrrl/Simple-Processing-Twitter/blob/master/SimpleProcessingTwitter.pde
Thanks in advance for any help I can get
M
//begin sketch _____________________________________________________________________________
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.*;
import cc.arduino.*;
import processing.video.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import processing.serial.*;
// originally the twittering cat mat by
bjoern@stanford.edu
//-----TWITTER VARS-------------------
String twitterID = "xxxxxxxxxxxx"; //TODO: fill in your info
String twitterPassword = "xxxxxxxxxxxx"; //TODO: fill in your info
//-----FTP VARS-----------------------
FTPClient ftp;
// Populate these variables with the necessary info.
String host = "xxxxxxx"; //TODO: fill in your info
String username = "xxxxxxxx"; //TODO: fill in your info
String password = "xxxxxxxxxx"; //TODO: fill in your info
String remotePath = "xxxxxxxxxx"; //TODO: fill in your info
String localPath = "xxxxxxxxxx";
String URLprefix = "xxxxxxxxxxxxxxx/"; //TODO: fill in your info
//-----CAMERA CAPTURE VARS--------------
Capture cam;
//-----ARDUINO VARS--------------------
Arduino arduino;
int matPin = 2;
int matCounter=0;
int curMatState = Arduino.HIGH;
int lastMatState = Arduino.HIGH;
int lastMatEvent = Arduino.LOW;
int bounceLimit= 10;
void setup() {
size(640,480);
// CAMERA If no device is specified, will just use the default.
cam = new Capture(this, 640, 480);
// ARDUINO
arduino = new Arduino(this, Arduino.list()[0]); // TODO: may have to adjust [0] to different index!
arduino.pinMode(matPin, Arduino.INPUT); //set input pin
arduino.digitalWrite(matPin, Arduino.HIGH); //enable pull-up
}
void draw() {
// read current mat state
pollMatState();
// wait a bit
delay(10);
}
// Read current mat state with debounce timer and toggle check
// calls fireMatEvent() when state of mat changed and has been stable for a while
void pollMatState() {
curMatState = arduino.digitalRead(matPin);
if(curMatState == lastMatState) {
//still in same state - incrase bounce counter
if(matCounter < bounceLimit) {
matCounter++;
} else if (matCounter==bounceLimit) {
//we've debounced enough and are ready to fire
if(lastMatEvent != curMatState) {
//only fire if this event is difft from last one
fireMatEvent(curMatState);
lastMatEvent = curMatState;
}
matCounter++;
} else if (matCounter > bounceLimit) {
// event already fired - do nothing
}
} else {
//restart count
matCounter=0;
}
lastMatState = curMatState;
}
// Fire a newstate change event
// this function builds the right tweet,
// potentially captures an image and uploads it,
// and updates the twitter status accordingly
void fireMatEvent(int state) {
String tweet;
String stat;
String filename=null;
boolean withImage = false;
if(state==Arduino.HIGH) {
//off mat event
tweet = makeTweet(false,null);
stat = "Peace Out, getting back up";
} else {
filename = makeFilename();
try{
withImage = uploadFile(captureImage(filename));
} catch (Exception e) {
println("couldn't take/upload image. tweeting without.");
}
if(withImage) {
tweet = makeTweet(true,filename);
} else {
tweet = makeTweet(true,null);
}
stat = "Just sat back down";
}
println(stat);
try{
sendTweet(tweet);
} catch (Exception e) {
println("caught exception while tweeting");
}
}
// Create a unique filename based on date & time
String makeFilename() {
Date now = new Date();
SimpleDateFormat format =
new SimpleDateFormat("yyyyMMdd-HHmmss");
return (format.format(now)+".jpg");
}
// Capture an image and save it under the passed in filename
// in the sketch data directory
String captureImage(String filename) {
if (cam.available() == true) {
cam.read();
image(cam,0,0);
save(dataPath(filename));
print("got image");
return filename;
} else {
return null; //we failed to get image
}
}
// upload a file via FTP
// returns true if successful, false if there was an error
boolean uploadFile(String filename) {
//check if we got a filename
if (filename==null) return false;
//connect to server and push file
try {
// set up client
ftp = new FTPClient();
ftp.setRemoteHost(host);
FTPMessageCollector listener = new FTPMessageCollector();
ftp.setMessageListener(listener);
// connect
println ("Connecting");
ftp.connect();
// login
println ("Logging in");
ftp.login(username, password);
// set up passive ASCII transfers
println ("Setting up passive, Binary transfers");
ftp.setConnectMode(FTPConnectMode.PASV);
ftp.setType(FTPTransferType.BINARY);
// copy file to server
println ("Putting file");
// TODO:
ftp.put(dataPath(filename), remotePath+filename);
// get directory and print it to console
println ("Directory after put");
String[] files;
files = ftp.dir(".", true);
for (int i = 0; i < files.length; i++)
println (files[i]);
// Shut down client
println ("Quitting client");
ftp.quit();
String messages = listener.getLog();
println ("Listener log:");
println(messages);
println ("Test complete");
return true;
} catch (Exception e) {
println ("Demo failed");
e.printStackTrace();
return false;
}
}
// Send the tweet passed in as String argument
void sendTweet(String msg) {
//construct a new twitter object
Twitter twitter = new Twitter(twitterID,twitterPassword);
try {
Status status = twitter.update(msg);
System.out.println("Updated the status to [" + status.getText() + "].");
} catch (Exception e) {
print(e.getStackTrace());
}
}
// Create a new status message based on mat state and captured image
// if filename is non-null, we assume file exists on server
// and add URL to end of message.
String makeTweet(boolean onMat, String filename) {
String[] onMsgs = {
"Back on my chair. Time for loud music.",
"I'm a level 85 Blood Elf.",
"Do better stuff, Zzzzzzzzzz."};
String[] offMsgs = {
"Yawn...Stretch...time to relax.",
"Time for food.",
"I dreamed of birds. Time to go.",
"Just like always.",} ;
if(onMat) {
if(filename != null) {
return onMsgs[(int)random(onMsgs.length)]+ URLprefix+filename;
} else {
return onMsgs[(int)random(onMsgs.length)];
}
} else {
return offMsgs[(int)random(offMsgs.length)];
}
}
1