Martin's corner on the web

RaspberryPi+TinySensor as EmonBase

I now have working setup with a TinySensor + Rasberry Pi  acting as an emonbase. I previously used PHP for this task, but the PHP serial class seems to have some issues on the transmission part plus it was a bit “heavy” for the CPU. I have created a perl script that will read the serial input that the TinySensor provides. The TinySensor is running the ATTiny84 version of RF12 Demo firmware, thus outputing to serial the RFM12B packets it receives. The perl script configures the TinySensor and the forwards the received data to emonCMS running on the same RaspberryPi or another server. This is possible with the latst emonCMS version that allows CSV input and the multinode concept that Trystan Lea has introduced. The script uses the TinySensor to send out time to emonGLCD as well.

Here is the PERL script, pretty basic stuff:

edit: most recent script is available on github 

#!/usr/bin/perl -w
# Reads data from serial port and posts to emoncms

# Run apt-get install libdevice-serialport-perl
# if you ger "Can't locate device/SerialPort.pm in @INC (@INC includes ..."

# use lib '/usr/lib/perl5/Device'
# sudo apt-get install libwww-mechanize-perl

# Martin Harizanov
# http://harizanov.com

# Declare the subroutines
sub trim($);

BEGIN {
        push @INC,"/usr/lib/perl5/";
        }

use strict;
use Device::SerialPort qw( :PARAM :STAT 0.07 );
use WWW::Mechanize;
use Time::localtime;

print "Serial to EmonCMS gateway for RaspberryPi with TinySensor\r\n";

my $PORT = "/dev/ttyAMA0";

my $ob = Device::SerialPort->new($PORT);
$ob->baudrate(9600); 
$ob->parity("none"); 
$ob->databits(8); 
$ob->stopbits(1); 
$ob->handshake("xoff"); 
$ob->write_settings;

$ob->lookclear; 

$ob->write("\r\n");
$ob->write("22i");   #Node ID=22
sleep 2;
$ob->write("8b");    #868Mhz
sleep 2;
$ob->write("210g");  #Group=210
sleep 2;
$ob->lookclear;

open(SERIAL, "+>$PORT");
my $line = <SERIAL>;  #The TinySensor's RFM12Demo sketch responds when setting configuration, so flush these three messages
$line = <SERIAL>;
$line = <SERIAL>;
$line = <SERIAL>;

while ($line = trim(<SERIAL>)) {
       my @values = split(' ', $line);
       if($values[0] >=1 && $values[0]<=31) {
          my $msubs ="";
          for(my $i=1; $i<@values; $i+=2){
            $msubs .= $values[$i] + $values[$i+1]*256;
            if($i) {$msubs .= ","};
 	  }
          post2emoncms($values[0],$msubs);

	  my $hour=localtime->hour();
          my $min=localtime->min();
          $ob->write("$hour,00,$min,00,s\r\n");
          sleep 2;
          $line=<SERIAL>;
          $line=<SERIAL>;
       }
}

sub post2emoncms {

my $ua = WWW::Mechanize->new();
my $url = "http://************/emoncms3/api/post?apikey=**************&node=" . $_[0] ."&csv=" . $_[1];
print $url; print "\r\n";
my $response = $ua->get($url);
if ($response->is_success)
{
print "Success!\n";
#my $c = $ua->content;
#print ("$c");
}
else
{
print "Failed to update emoncms!";
#die $response->status_line;
}

}

# Perl trim function to remove whitespace from the start and end of the string
sub trim($)
{
	my $string = shift;
	$string =~ s/^\s+//;
	$string =~ s/\s+$//;
	return $string;
}
#

To have the script run in the background as a deamon, I use “sudo nohup perl serial.pl &”. To stop it, you must find the processID with “ps -A” and “kill” it.

2 thoughts on “RaspberryPi+TinySensor as EmonBase

  1. Pingback: Geektool Setup « saelaenx

  2. Pingback: RFM12B to Raspberry Pi board | Martin's corner on the web