I am quite the beginner at coding but I have managed to make a program which when an arduino connection is made automatically takes a shot on the attached webcam and uploads that shot directly to a twitter page.
The problem I am having right now is that every time I trigger the connection now it uploads the first photo I took/uploaded every time rather than a new photo of what the camera is currently looking at.
I can't figure out how to fix this issue, any help would be much appreciated.
Here is my code:
import twitter4j.conf.*;
import twitter4j.*;
import twitter4j.auth.*;
import twitter4j.api.*;
import java.util.*;
import processing.video.*;
import processing.serial.*;
int picCount = 0;
Capture webcam;
Twitter twitter;
Serial myPort; // The serial port
String inString; // Input string from serial port
int lf = 10; // ASCII linefeed
void setup()
{
size(640, 480);
webcam = new Capture(this, 640, 480);
String[] devices = Capture.list();
println(devices);
webcam.start();
ConfigurationBuilder cb = new ConfigurationBuilder();
cb.setOAuthConsumerKey("****");
cb.setOAuthConsumerSecret("****");
cb.setOAuthAccessToken("****");
cb.setOAuthAccessTokenSecret("****");
TwitterFactory tf = new TwitterFactory(cb.build());
twitter = tf.getInstance();
myPort = new Serial(this, Serial.list()[0], 9600);
myPort.bufferUntil(lf);
}
void draw()
{
if (webcam.available() == true) {
webcam.read();
image(webcam, 0, 0);
}
if (inString != null) {
inString = inString.trim();
int val = int(inString);
if (val == 1) {
println("saving...");
save("cam" + picCount + ".png");
picCount++;
//send tweet
File file = new File("C:\\Users\\Jake\\Documents\\Processing\\Twitter test 2\\Twittertest2\\cam" + picCount + ".png");
delay (3000);
tweetPic(file, "");
}
}
}
void testPassingFile(File _file)
{
println(_file.exists());
println(_file.getName());
println(_file.getPath());
println(_file.canRead());
}
void tweetPic(File _file, String theTweet)
{
try
{
StatusUpdate status = new StatusUpdate(theTweet);
status.setMedia(_file);
twitter.updateStatus(status);
}
catch (TwitterException te)
{
println("Error: "+ te.getMessage());
}
}
void serialEvent(Serial p) {
inString = p.readString();
}
Here's one thing that doesn't make sense:
save("cam" + picCount + ".png"); //picCount is 3 here, you save cam3.png
picCount++; //picCount is now 4
File file = new File("C:\\Users\\Jake\\Documents\\Processing\\Twitter test 2\\Twittertest2\\cam" + picCount + ".png"); //but you send cam4.png
Notice that you're incrementing picCount
before you load the file for tweeting. This doesn't make a ton of sense- a file with that name won't even exist yet!
Fix that issue by moving the increment to the bottom. If that doesn't solve your problem, then you have to really make sure that the files being saved by save("cam" + picCount + ".png");
are the same files being loaded for tweeting. Judging by the way your code works now, it looks like they're in different directories.
Try saving a weird file name using the save("WeirdFileName.png")
and seeing where that shows up. Is it in the same directory you're loading the file from?