SOLVED: CurrentCost Monitor feed to EmonCMS

I have just started down the path of energy monitoring. My first plan is to get my CurrentCost Monitor feeding to EmonCMS, then to build the whole monitoring infrastructure from the kits on this site.

Yesterday I got my Raspberry Pi feeding the energy monitor data to COSM using a Perl script I found on the current cost site which I have attached. I'm not sure the etiquette but credit for the script goes to over on the currentcost forums. I managed to hack the script enough to get it feeding to COSM every 6 seconds when the data comes in from the monitor.

Today I installed Emoncms on my server, so now I need to hack this script, or find another one that takes the data from the currentcost via the Raspberry Pi and passes to EmonCMS.  Does anyone know if a script such as this exists? I have Googled all over but not been able to find anything.

I would appreciate any help that the community can give, I'm just trying to get the simplest software part working before launching myself with a soldering iron at the really hard parts. I have no programming experience other than the odd hack to config files etc as required.

thanks

Paul

ukmoose's picture

Re: SOLVED: CurrentCost Monitor feed to EmonCMS

would probably help if you attached the script ;-)

 

plmills's picture

Re: SOLVED: CurrentCost Monitor feed to EmonCMS

Script is hopefully attached now. Thanks for the comment.

I think I see what I did now. I attached it but without clicking list it doesn't show

ukmoose's picture

Re: SOLVED: CurrentCost Monitor feed to EmonCMS

This is UNTESTED ( i don't have a current cost monitor).  

You'll need to add your EMONCMS api (line 9)
and check/change the url (line 42)

It's a bodge of an existing script, so if it doesn't work, it should give you enough to get started

 

#!/usr/bin/perl -w

# Reads data from a Current Cost device via serial port.

use strict;
use Device::SerialPort qw( :PARAM :STAT 0.07 );
use LWP::UserAgent;

my $key = "your_emoncms api key_here";

my $PORT = "/dev/ttyUSB0";
my $meas = 0;
my $sumW = 0;
my $sumT = 0;
my $watts;
my $temp;
my $wget;

my $ob = Device::SerialPort->new($PORT)  || die "Can't open $PORT: $!\n";
$ob->baudrate(57600);
$ob->write_settings;

open(SERIAL, "+>$PORT");
while (my $line = <SERIAL>) {
    if ($line =~ m!<tmpr> *([\-\d.]+)</tmpr>.*<ch1><watts>0*(\d+)</watts></ch1>!) {
        my $watts = $2;
        my $temp = $1;
        #print "SUCCESS $meas: ... $watts, $temp\n";
        $meas++;
        $sumW += $watts;
        $sumT += $temp;
    }
    if ($meas == 10) { #time to send
     $watts = $sumW/10;
     $temp = $sumT/10;
        #print "AVERAGE: ... $watts, $temp\n";
        my $emon_ua = LWP::UserAgent->new;
        my $emon_url = "http://localhost/emoncms3/api/post?apikey=$key\&json={power:$watts,temp:$temp}";
my $emon_response = $emon_ua->get($emon_url);

if ($emon_response->is_error) {
        die $emon_response->status_line;
}       
}
}

 

ukmoose's picture

Re: SOLVED: CurrentCost Monitor feed to EmonCMS

should have mentioned if you dont have LWP module installed try:

perl -MCPAN -e 'install Bundle::LWP'

 

 

plmills's picture

Re: SOLVED: CurrentCost Monitor feed to EmonCMS

Thanks for the follow up. Yes I did have to install the LWP module, but was off trying to do it while you posted your comment.

 

Thank you very much for the script here, I now have the data loading to emoncms on my NAS. I didn't have to do anything else to the script, although I have hacked it so that it sends every feed rather than every 10th

plmills's picture

Re: SOLVED: CurrentCost Monitor feed to EmonCMS

The script seems to run and feed emoncms, but after a minute or so the script seems to stop updating. I changed it to update every 6 seconds, but even when I changed it back it does not seem to run longer than a minute. Any suggestions as to how I could debug? when I run in SSH it does not give any error message when it stops.

ukmoose's picture

Re: SOLVED: CurrentCost Monitor feed to EmonCMS

I'm not located where I can log onto my pi so this is from memory

you can log into the pi with more than one ssh session.  so fire up another one, run the command 

ps aux

which will list all the processes running, you can then see if the process is still running or has actually died.

 

The other thing is to look at the syslog which is located in /var/logs/syslog

tail -40 /var/log/syslog

and see if anything has been written there

 

Not related to the script stopping, but there are some variables which don't get used which are still in there, you might as well delete them (eg wget) ; a side effect of coding after beer in front of the TV! 

plmills's picture

Re: SOLVED: CurrentCost Monitor feed to EmonCMS

Thanks for the help again, I have had a look in the logs and there is nothing there. The job seems to show up in the listing, but it is no longer sending anything to emoncms. It seems to run for a minute or so then stop sending.  I changed it from collecting 10 then sending an average to sending every 6 seconds.

pi        2147  2.1  1.9  12932  8556 pts/0    S    22:00   0:03 perl envi.pl
 

This is the status with PS AUX it ran for a few minutes but has sent nothing to emoncms for 8 minutes now.

I reverted back to the old script for the last 24 hours in an effort to diagnose whether there is a problem getting the data from the serial port, but it seems to have been rock solid for a day sending to COSM.  I am wondering if it is something in the error handling from emoncms? I'm running a synology NAS which can sometimes be a bit slow to respond to web requests. Could it be that there is no response at all coming from the web server and it's just sat waiting for an answer?  If this is the case would it be possible to change the script so that if there is no response it just keeps sending?  If I'm sending every 6 seconds I can live with a few readings getting lost on the way if the continuity is there?

Thanks

 

edit: I've just noticed that the kill command on the above job doesn't seem to do anything, so I'm not sure if that means it is running or not.

elyobelyob's picture

Re: SOLVED: CurrentCost Monitor feed to EmonCMS

If it's of any use, I use a method where I post my data into MQTT .. from there I can just write a quick script to listen to this data. I am writing a module to bring this data in. It's work in progress, but if someone wanted to take a look, it's currently taking in my CC XML data ... https://github.com/elyobelyob/emoncms

fversteegen's picture

Re: SOLVED: CurrentCost Monitor feed to EmonCMS

I know this is quite an old post, but based on this post I managed to create a working Perl script which runs fine on the synology. Full script is:

#!/usr/bin/perl -w
# Reads data from a Current Cost device via serial port.

use strict;
use warnings;
use Device::SerialPort qw( :PARAM :STAT 0.07 );
use LWP::UserAgent;

    my $key = "YOURAPIKEY";
    my $PORT = "/dev/ttyUSB0";
    my $meas = 0;
    my $sumW = 0;
    my $sumT = 0;
    my $watts;
    my $temp;

    my $ob = Device::SerialPort->new($PORT)  || die "Can't open $PORT: $!\n";
    $ob->baudrate(57600);
    $ob->write_settings;

    open(SERIAL, "+>$PORT");
    while (my $line = <SERIAL>) {
        if ($line =~ m!<tmpr>\s*(-*[\d.]+)</tmpr>.*<ch1><watts>0*(\d+)</watts></ch1>!) {
            my $watts = $2;
            my $temp = $1;
            #print "SUCCESS $meas: ... $watts, $temp\n";
            $meas++;
            $sumW += $watts;
            $sumT += $temp;
        }
        if ($meas == 2) { #time to send
           $watts = $sumW/2;
           $temp = $sumT/2;
            #print "AVERAGE: ... $watts, $temp\n";
            my $emon_ua = LWP::UserAgent->new;
            my $emon_url = "http://diskstation/emoncms/input/post.json?apikey=$key\&json={power:$watts,temp:$temp}";
        my $emon_response = $emon_ua->get($emon_url);
        $meas = $sumW = $sumT = 0;
        }
}

The reason why the previous versions in this thread were not working was because the line "$meas = $sumW = $sumT = 0;" was omitted.

Can anyone now build on this script so it includes data from IAM's? (The Individual Appliance Monitors currentcost sells)

fversteegen's picture

Re: SOLVED: CurrentCost Monitor feed to EmonCMS

Ok, I hacked together a script to support the Individual Appliance Monitors of currentcost and corrected an error in the readings (watts were only half of what they suppose to be).

 

#!/usr/bin/perl -w
# Reads data from a Current Cost device via serial port.

use strict;
use warnings;
use Device::SerialPort qw( :PARAM :STAT 0.07 );
use LWP::UserAgent;

    my $key = "YOURKEYLOCAL";
    my $PORT = "/dev/ttyUSB0";
    my $meas = 0;
    my $sumW = 0;
    my $sumT = 0;
    my $watts;
    #2 extra variables for IAM 1    
    my $watts_sensor1;
    my $sumwatts_sensor1= 0;
    my $temp;

    my $ob = Device::SerialPort->new($PORT)  || die "Can't open $PORT: $!\n";
    $ob->baudrate(57600);
    $ob->write_settings;

    open(SERIAL, "+>$PORT");
    while (my $line = <SERIAL>) {
        if ($line =~ m!<tmpr>*([\-\d.]+)</tmpr><sensor>0</sensor>.*<ch1><watts>0*(\d+)</watts></ch1>!) {
            my $watts = $2;
            my $temp = $1;
            #print "SUCCESS $meas: ... $watts, $temp\n";
            $meas++;
            $sumW += $watts;
            $sumT += $temp;
            }
        #extra if statement to get watts for IAM 1, change the sensorname to reflect this    
        if ($line =~ m!<tmpr>*([\-\d.]+)</tmpr><sensor>1</sensor>.*<ch1><watts>0*(\d+)</watts></ch1>!) {
            my $watts_sensor1 = $2;
            #print "SUCCESS $meas: ... $watts_sensor1\n";
            $meas++;
            $sumwatts_sensor1 += $watts_sensor1;
            }
        if ($meas == 6) { #time to send
           $watts = $sumW/3;
           $temp = $sumT/3;
           #Additional line for each sensor, note that meas increases 2 times in the cases of 1 additional IAM, so watt readings need to be divided by meascount/sensoramount
           $watts_sensor1 = $sumwatts_sensor1/3;
            #print "AVERAGE: ... $watts, $temp, $sumwatts_sensor1\n";
            my $emon_ua = LWP::UserAgent->new;
            my $emon_url = "http://diskstation/emoncms/input/post.json?apikey=$key\&json={power:$watts,temp:$temp,sensor1:$watts_sensor1}";
            my $emon_response = $emon_ua->get($emon_url);
            #Line below is because I upload readings to 2 sites in parallel as backup
            my $emon_url2 = "http://EXTERNALURL/emoncms/input/post.json?apikey=YOURKEY&json={power:$watts,temp:$temp,sensor1:$watts_sensor1}";
            my $emon_response2 = $emon_ua->get($emon_url2);
            #Do not forget to reset the sum for an additional sensor    
            $meas = $sumW = $sumT = $sumwatts_sensor1 = 0;
        }
}

fversteegen's picture

Re: SOLVED: CurrentCost Monitor feed to EmonCMS

For anyone who wants to know: all the steps I took from a clean DS211J to running emoncms on DSM 4.2 are:

 

  • Install serialport for perl
    ipkg install perl-device-serialport
  1. Bootstrap synology via: http://forum.synology.com/wiki/index.php/Overview_on_modifying_the_Synology_Server,_bootstrap,_ipkg_etc
  2. Install kernel modules via: http://www.fischer-net.de/hausautomation/downloads/category/4-synology.html
  3. Install dependencies
    ipkg install lynx
    ipkg install ncftp
    ipkg install gpg
    ipkg install gpgme
    ipkg install gzip
    ipkg install unzip
    ipkg install make
    ipkg install optware-devel
    ipkg install usbutils
  4. Get rid of wrong WGET version in Synology
    wget http://ipkg.nslu2-linux.org/feeds/optware/cs08q1armel/cross/unstable/libidn_1.25-1_arm.ipk
    wget http://ipkg.nslu2-linux.org/feeds/optware/cs08q1armel/cross/unstable/wget-ssl_1.12-2_arm.ipk
    wget http://ipkg.nslu2-linux.org/feeds/optware/cs08q1armel/cross/unstable/openssl_0.9.8v-2_arm.ipk
    ipkg remove wget
    ipkg install lib* openssl* wget-ssl*
  5. Install CpanMinus
    curl -L http://cpanmin.us | perl - App::cpanminus (and get a coffee or 2 ;-))
  6. ./configure --prefix=/opt
  7. Check usb connection with lsusb
  8. Download correct toolchain: wget http://downloads.sourceforge.net/project/dsgpl/DSM%204.2%20Tool%20Chains/Marvell%2088F628x%20Linux%202.6.32/gcc421_glibc25_88f6281-GPL.tgz?r=http%3A%2F%2Fsourceforge.net%2Fprojects%2Fdsgpl%2Ffiles%2FDSM%25204.2%2520Tool%2520Chains%2FMarvell%252088F628x%2520Linux%25202.6.32%2F&ts=1372915725&use_mirror=garr
    tar xpzf gcc421_glibc25_88f6281-GPL.tgz -C /usr/local
  9. Setup symlinks for Gnueabi
    ln -sf /opt/bin/ld /opt/bin/arm-none-linux-gnueabi-ld
    ln -sf /opt/bin/ranlib /opt/bin/arm-none-linux-gnueabi-ranlib
    ln -sf /opt/bin/gcc /opt/bin/arm-none-linux-gnueabi-gcc
  10. Install dependencies of script:
    cpanm LWP::UserAgent
  11. Ensure proper access to USB port:
    chmod 666 /dev/ttyUSB0
  12. Set correct baudrate and output for USB port:

    stty -F /dev/ttyUSB0 speed 57600 raw cs8
  13. Install emoncms via: http://emoncms.org/site/docs/installlinux
  14. Create/adapt the script below to suit your needs
  15. Auto start script by adding line "/opt/bin/perl /volume1/web/emoncms/envi3.pl >/volume1/web/emoncms/envi3.log" to \etc\rc.local

 

gippy73's picture

Re: SOLVED: CurrentCost Monitor feed to EmonCMS

I've made some little improvement to the script on 12th post, hope somebody my find it useful:

#!/usr/bin/perl -w
# Reads data from a Current Cost device via serial port.

use strict;
use warnings;
use Device::SerialPort qw( :PARAM :STAT 0.07 );
use LWP::UserAgent;

    my $key  = "YOUR-API-KEY-HERE";
    my $host = "YOUR-IP-HERE";
    my $node = "1";
    my $PORT = "/dev/ttyUSB0";
    my $meas = 0;
    my $sumW = 0;
    my $sumT = 0;
    my $watts = 0;
    my $conta_sensor0 = 0;
    my $conta_sensor1 = 0;
    my $count_limit   = 6;
    #2 extra variables for IAM 1   
    my $watts_sensor1;
    my $sumwatts_sensor1= 0;
    my $temp;

    my $ob = Device::SerialPort->new($PORT)  || die "Can't open $PORT: $!\n";
    $ob->baudrate(57600);
    $ob->write_settings;

    open(SERIAL, "+>$PORT");
    while (my $line = <SERIAL>) {
# print $line;
        if ($line =~ m!<tmpr>*([\-\d.]+)</tmpr><sensor>0</sensor>.*<ch1><watts>0*(\d+)</watts></ch1>!) {
            my $watts = $2;
            my $temp = $1;
            print "SUCCESS (Sensor0) $meas: ... $watts, $temp\n";
            $meas++;
            $conta_sensor0++;
            $sumW += $watts;
            $sumT += $temp;
            }
        #extra if statement to get watts for IAM 1, change the sensorname to reflect this   
        if ($line =~ m!<tmpr>*([\-\d.]+)</tmpr><sensor>1</sensor>.*<ch1><watts>0*(\d+)</watts></ch1>!) {
            my $watts_sensor1 = $2;
            print "SUCCESS IAM (Sensor1) $meas: ... $watts_sensor1\n";
            $meas++;
            $conta_sensor1++;
            $sumwatts_sensor1 += $watts_sensor1;
            }
#       if ($meas == 36) { #time to send
        if (($meas >= $count_limit) && ($conta_sensor0 > 0) && ($conta_sensor1 > 0)) { #time to send
           $watts = $sumW/$conta_sensor0;
           $temp = $sumT/$conta_sensor0;
           #Additional line for each sensor, note that meas increases 2 times in the cases of 1 additional IAM, so watt readings need to be divided by meascount/sensoramount
           $watts_sensor1 = $sumwatts_sensor1/$conta_sensor1;
            print "AVERAGE: ... $watts, $temp, $watts_sensor1\n";
            my $emon_ua = LWP::UserAgent->new;
#            my $emon_url = "http://$host/emoncms/input/post.json?apikey=$key\&json={power:$watts,temp:$temp,sensor1:$watts_sensor1}";
            my $emon_url = "http://$host/emoncms/input/post.json?apikey=$key\&node=$node\&json={power:$watts,temp:$temp,sensor1:$watts_sensor1}";
     print "$emon_url\n";
            my $emon_response = $emon_ua->get($emon_url);
            #Line below is because I upload readings to 2 sites in parallel as backup
#            my $emon_url2 = "http://EXTERNALURL/emoncms/input/post.json?apikey=YOURKEY&json={power:$watts,temp:$temp,sensor1:$watts_sensor1}";
#            my $emon_response2 = $emon_ua->get($emon_url2);
            #Do not forget to reset the sum for an additional sensor   
            $meas = $sumW = $sumT = $sumwatts_sensor1 = 0;
            $conta_sensor0 = $conta_sensor1 = 0;
        }
}

fversteegen's picture

Re: SOLVED: CurrentCost Monitor feed to EmonCMS

As I extended the amount of IAM's I have updated the script (still some code cleaning needed)

 

#!/usr/bin/perl -w
# Reads data from a Current Cost device via serial port.
BEGIN {
        push @INC,"/usr/lib/perl5/";
        }
use warnings;
use Device::SerialPort qw( :PARAM :STAT 0.07 );
use LWP::UserAgent;

    my $PORT = "/dev/ttyUSB0";
    my $meas = 0;
    my $sumW = 0;
    my $sumT = 0;
    my $watts;
    #2 extra variables for IAM 1    
    my $watts_sensor1;
    my $sumwatts_sensor1= 0;
    my $watts_sensor2;
    my $sumwatts_sensor2= 0;
    my $watts_sensor3;
    my $sumwatts_sensor3= 0;
    my $watts_sensor4;
    my $sumwatts_sensor4= 0;
    my $temp;
    $meassensormain= 0;
    $meassensor1 = 0;
    $meassensor2 = 0;
    $meassensor3 = 0;
    $meassensor4 = 0;
    
    

    my $ob = Device::SerialPort->new($PORT)  || die "Can't open $PORT: $!\n";
    $ob->baudrate(57600);
    $ob->write_settings;

    open(SERIAL, "+>$PORT");
    while (my $line = <SERIAL>) {
        if ($line =~ m!<tmpr>*([\-\d.]+)</tmpr><sensor>0</sensor>.*<ch1><watts>0*(\d+)</watts></ch1>!) {
            my $watts = $2;
            my $temp = $1;
            print "SUCCESS mainunit $meas: ... $watts, $temp\n";
            $meassensormain = 1;
            $sumW = $watts;
            $sumT = $temp;
            }
                #extra if statement to get watts for IAM 3, change the sensorname to reflect this    
        if ($line =~ m!<tmpr>*([\-\d.]+)</tmpr><sensor>3</sensor>.*<ch1><watts>0*(\d+)</watts></ch1>!) {
            my $watts_sensor3 = $2;
            print "SUCCESS IAM3 $meas: ... $watts_sensor3\n";
            $meassensor3 = 1;
            $sumwatts_sensor3 = $watts_sensor3;
            }
        #extra if statement to get watts for IAM 4, change the sensorname to reflect this    
        if ($line =~ m!<tmpr>*([\-\d.]+)</tmpr><sensor>4</sensor>.*<ch1><watts>0*(\d+)</watts></ch1>!) {
            my $watts_sensor4 = $2;
            print "SUCCESS IAM4 $meas: ... $watts_sensor4\n";
            $meassensor4 = 1;
            $sumwatts_sensor4 = $watts_sensor4;
            }

        #extra if statement to get watts for IAM 2, change the sensorname to reflect this    
        if ($line =~ m!<tmpr>*([\-\d.]+)</tmpr><sensor>2</sensor>.*<ch1><watts>0*(\d+)</watts></ch1>!) {
            my $watts_sensor2 = $2;
            print "SUCCESS IAM2 $meas: ... $watts_sensor2\n";
            $meassensor2 = 1;
            $sumwatts_sensor2 = $watts_sensor2;
            }
        #extra if statement to get watts for IAM 1, change the sensorname to reflect this    
        if ($line =~ m!<tmpr>*([\-\d.]+)</tmpr><sensor>1</sensor>.*<ch1><watts>0*(\d+)</watts></ch1>!) {
            my $watts_sensor1 = $2;
            print "SUCCESS IAM1 $meas: ... $watts_sensor1\n";
            $meassensor1 = 1;
            $sumwatts_sensor1 = $watts_sensor1;
            }
        if ($meassensormain ==1 and $meassensor1 ==1 and $meassensor2 == 1 and $meassensor3 == 1 and $meassensor4 == 1) { #time to send
           $watts = $sumW;
           $temp = $sumT;
           #Additional line for each sensor, note that meas increases 2 times in the cases of 1 additional IAM, so watt readings need to be divided by meascount/sensoramount
           $watts_sensor1 = $sumwatts_sensor1;
           $watts_sensor2 = $sumwatts_sensor2;
           $watts_sensor3 = $sumwatts_sensor3;
           $watts_sensor4 = $sumwatts_sensor4;
            #print "AVERAGE: ... $watts, $temp, $sumwatts_sensor1, $sumwatts_sensor2, $sumwatts_sensor3, $sumwatts_sensor4\n";
            #print "AVERAGE: ... $watts, $temp, $watts_sensor1, $watts_sensor2, $watts_sensor3, $watts_sensor4\n";
            my $emon_ua = LWP::UserAgent->new;
            #Line below is because I upload readings to 2 sites in parallel as backup
            my $emon_url2 = "http://EMONCMSURL/input/post.json?node=1&apikey=APIKEY&json={power:$watts,temp:$temp,sensor1:$watts_sensor1,sensor2:$watts_sensor2,sensor3:$watts_sensor3,sensor4:$watts_sensor4}";
            my $emon_response2 = $emon_ua->get($emon_url2);
            print "$emon_url2\n";
            #Do not forget to reset the sum for an additional sensor    
            $meas = $sumW = $sumT = $sumwatts_sensor1 = $sumwatts_sensor2 = $sumwatts_sensor3 = $sumwatts_sensor4 = $meassensormain = $meassensor1 = $meassensor2 = $meassensor3 = $meassensor4 = 0;
        }
}

firefox7518's picture

Re: SOLVED: CurrentCost Monitor feed to EmonCMS

Hi

I found your PERL script here but it has some flaws for my implementation so I enhanced it and I'm using it for several IAMs at the moment.

I also use this script to send the data to pvoutput.org so that I have the data also there where my PV setup will send the data to. I always have the data on my emoncms installation and pvoutput.org :-)

Here is the script I've enhanced:
http://openenergymonitor.org/emon/node/10470#comment-29957

a_patch's picture

Re: SOLVED: CurrentCost Monitor feed to EmonCMS

Hi,

Thanks for article. With small adjustments I was able to install and make it work on my i686 DS214Play. But every so often Emon is not recording any data. I believe it is connected with updates and reboots. I did create login script, that is loading modules before scripts run, but even perl script is giving me proper output nothing is going to database. After reinstalling everything (except Emon) it used to working again... up to last week. I didn't came back. I checked API key and password gadzylion times. Manually using JSON is populating data. Script is giving me output, and database is accessible.
What am I doing wrong? and also can it be permanently installed on NAS and not wiped after every update?

 

Thanks for help

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.