# read/write serial port win32
use strict;
use warnings;
# VARIABLE INIT
my $i; # general purpose iterator
my $epoch_seconds; # internal time monitor to avoid being naughty with Twitter
my $message; # the string to output to the display
my @timedata; # for timing twitter requests
my $refresh_period = 180; # how many seconds to go between checking twitter?
my $inmsg;
my $outmsg;
my $tweets = 0;
my @tweets = ("You've reached the last tweet!"); # list of appropriate tweets for the day
my @new_tweets; # contents of search page
my @append_tweets; # just new tweets since last check to be added to @tweets
my $list_pos = -1; # euughhh.. init as -1 is hack so that the first status request increments it to 0
my $newthings = 0; # are there new tweets?
my $searchtag = shift;
if ($searchtag eq "") { $searchtag = "instructables"; } # ... or whatever :)
# NETWORK INIT
# Create a user agent object
use LWP::UserAgent;
my $ua = LWP::UserAgent->new;
$ua->agent("MyApp/0.1 ");
# Create a request
my $req = HTTP::Request->new(POST => 'http://search.twitter.com/search.atom?q=' . $searchtag);
$req->content_type('application/x-www-form-urlencoded');
$req->content('query=libwww-perl&mode=dist');
$| = 1; # make perl flush output buffers after every print
use Win32::SerialPort qw( :STAT 0.19 );
my $port = Win32::SerialPort->new('COM3'); # set up the serial port to talk to the Arduino
if( ! defined($port) ) {
die("Can't open COM3: $^E\n");
}
# we have a serial port- initialise
$port->initialize();
$port->baudrate(9600);
$port->parity('none');
$port->databits(8);
$port->stopbits(1);
$port->write_settings();
# initialise tweet-getting loop
@timedata = localtime(time);
$epoch_seconds = $timedata[0] + ($timedata[1] * 60) + ($timedata[2] * 3600);
# $epoch_seconds = 0;
# get some tweets!
my $res = $ua->request($req);
if ($res->is_success) {
print "Success, getting new titles into new_tweets\n";
# deal with res->content to extract the titles
@tweets = ($res->content =~ m/
]*>(.*?)<\/title>/g);
shift( @tweets ); # remove page title from list
$tweets = @tweets;
for $inmsg (@tweets) {
print substr($inmsg, 0, 10), "\n";
}
}
else {
print "Error'd: ", $res->status_line, "\n";
}
while(1){
$inmsg = &readline($port);
@timedata = localtime(time);
if ($inmsg eq "status\n") {
print("SR- ");
# GET LATEST TWEET
if ($timedata[0] + ($timedata[1] * 60) + ($timedata[2] * 3600) > $epoch_seconds + $refresh_period)
{
print "Getting new tweets NOW\n";
# reset timer
$epoch_seconds = $timedata[0] + ($timedata[1] * 60) + ($timedata[2] * 3600);
my $res = $ua->request($req);
# Check the outcome of the response
if ($res->is_success) {
print " Getting new tweets\n";
# parse res->content to extract the titles
@new_tweets = ($res->content =~ m/]*>(.*?)<\/title>/g);
shift( @new_tweets );
}
else {
print $res->status_line, "\n";
}
# now got tweets, find out how many are new
$i = 0;
@append_tweets = ();
while ($new_tweets[$i] ne $tweets[0]) {
print substr($new_tweets[$i], 0, 10) . " not equal " . substr($tweets[0], 0, 10) . "\n";
push @append_tweets, $new_tweets[$i];
$i = $i + 1;
}
print substr($new_tweets[$i], 0, 10) . " equal " . substr($tweets[0], 0, 10) . "?\n";
if ($i > 0) { # if any new tweets
$newthings = 1;
unshift(@tweets, @append_tweets);
}
} else {
print (($epoch_seconds + $refresh_period) - ($timedata[0] + ($timedata[1] * 60) + ($timedata[2] * 3600)));
print " secs\n";
}
if ($newthings == 1) {
$newthings = 0;
$list_pos = 0;
$tweets = @tweets; # eughh.. only in Perl
$outmsg = "T$tweets tweets";
}
else
{
$list_pos = $list_pos + 1;
$outmsg = "tNO NEW";
}
$port->write($outmsg);
print " No new\n";
}
if ($inmsg eq "tweet\n") {
print("TR\n");
if ($list_pos == -1) { $list_pos = 0; } # safety
if ($list_pos == $tweets) { # zero-based index, dumbass
$list_pos = 0;
}
print(" T$list_pos ($tweets) : ", $tweets[$list_pos], "\n");
$port->write($list_pos . ": " . $tweets[$list_pos]);
}
}
$port->close();
exit(0);
############# SUBROUTINES #################
sub readline {
my $buffer = '';
my $b = '';
my $count = 0;
my $object = shift;
while(1)
{
$b = $port->read(1);
$buffer .= $b;
if($b eq '') {
$count += 1;
if($count > 100) {
return($buffer);
}
}
if (($b eq "\r") or ($b eq "\n")) {
return($buffer);
}
}
}