Today is: 10 January, 2012
Check todays hot topics

Nerdkit Temperature Poller

This is my very first lame attempt at writing a poller for my nerdkit. For some reason I thought I would be able to pull once a second for a single reading, however, that is not the case. It turns out the device writes to the serial port REALLY quickly. On average I'm receiving about 13 lines a second.

What I've attempted to do is read once a second and push the results into an array that are split on \n marker. I'm attempting to sample the readings I'm getting and spit back one unified average per second. The average function is not working according to plan.

There also seems to be some interesting problems related to perls tie() function. I'm having difficulty calling sysread() on the tied file handle. Because of this limitation I decided to use perl's Device::SerialPort read() method. It's a bit strange, in that, it expects an argument representing the amount of 'chars' to read. I mistakenly assumed this was in bytes.

Anyways, I've been really busy and haven't been able to get this working the way I wanted just yet. Instead, I figured I'd post this up for ghettocode because... well... Thats just what we do here.

I got a bit sidetracked during this script. I started looking into ways I can get the same behavior out of php. It's not really going to be as smooth as Perl but I think that will definately remove a layer of abstraction from any sort of "THIS IS THE TEMPERATURE IN MY HOUSE" web page. I will try to write it both ways and post em up. Good luck!

#!/usr/bin/perl
 
use strict;
use warnings;
 
use Data::Dumper;
use Device::SerialPort;
 
my $PORT = "/dev/ttyUSB0";
my $conf = "./conf";
 
sub main {
        if(-e $conf) {
                my $ob = start Device::SerialPort($conf);
 
                # For some reaosn tie() is broken here. sysread isn't able
                # to read from <FH>. :(
 
                #my $ob = tie (*FH, "Device::SerialPort", $conf) 
                #       or die("Error creating file handle:".$!."\n");
 
                while(1) {
                        my @lines = split(/\n/, $ob->read(255));
                        my $average = Average(@lines);
                        print "Sampled $#lines readings averaging $average degrees\n";
                        sleep 1;
                }
        } else {
                print "Something happened\n";
                setup_nerdkit_line();
                main();
        }
}
 
sub setup_nerdkit_line {
        my $ob = Device::SerialPort->new($PORT) or die $!;
        $ob->baudrate(115200);
        $ob->parity("none");
        $ob->databits(8);
        $ob->stopbits(1);
        $ob->write_settings;
        $ob->save($conf);
 
        $ob->close();
}
 
sub Average {
        my @sample = @_;
        print Dumper \@sample;
        my $Sum = 0;
        foreach my $item (@sample) {
                $Sum += $item;
        }
        my $Average = ($Sum / $#sample);
        return $Average;
}
 
 
main();