sleepTime = 120 * 60 * 1000;
}
try {
Thread.sleep(sleepTime);
}
catch(InterruptedException ignoreMe) {}
}
}
}
void tweetAlarm() {
ConfigurationBuilder cb = new ConfigurationBuilder();
cb.setDebugEnabled(true)
.setOAuthConsumerKey(API_KEY)
.setOAuthConsumerSecret(API_SECRET)
.setOAuthAccessToken(ACCESS_TOKEN)
.setOAuthAccessTokenSecret(ACCESS_TOKEN_SECRET);
TwitterFactory tf = new TwitterFactory(cb.build());
Twitter twitter = tf.getInstance();
try {
Status status = twitter.updateStatus(
"Someone, please, take me to the beach!"
);
println(
"Successfully updated status to '" + status.getText() + "'."
);
}
catch (TwitterException e) {
e.printStackTrace();
}
}
Whenever new data arrives on the serial port, the Processing runtime environ-
ment calls the
serialEvent
method. There we try to read a line of text, and then
we check whether it contains a decimal number followed by a blank and a C
or an F character. This ensures we’ve read an actual temperature data set
and not some digital garbage.
If we got a syntactically correct temperature data set, we convert it into a
float
object and check to see whether it’s greater than
MAX_WORKING_TEMP
. (No one
should be forced to work at temperatures that high!) If yes, we call
tweetAlarm
and Tweet a message to encourage some followers to rescue us. Then we wait
for two hours until our next check. Otherwise, we wait five minutes and check
the temperature again.
tweetAlarm
updates our Twitter status and is simple. In good old Java tradition,
we create a new
Twitter
instance using a
TwitterFactory
. The factory expects a
ConfigurationBuilder
object that we have initialized with our Twitter application
report erratum • discuss
Tweeting Messages with Processing • 171
www.it-ebooks.info