Galaxynik
Posts: 43
Joined: Mon Jan 02, 2012 2:55 pm

Perl script to access CurrentCost Envi

Thu Aug 02, 2012 3:15 pm

Right - not sure if this is the correct place to post this :-) Trying to learn Linux very much a noob...
Background----- Running my pi headless with squeezeplug, as a server connected to 2Tb usb disk - Lovely low power 24/7 server uses 3 watts when asleep and 9 watts when disk is in use. Minidlna works fine, installed Webmin, apache2 (small website atm) Php and RRD.
What I want to do - connect my Currentcost Envi and log the power use and temp to a database (RRD) post data to Cosm(pachube) and view graphs on website (mine).
I have a Serial-Usb connection between Envi and the raspberry. I am getting data through ttyUSB0 (using modprobe usbserial vendor=0x067b product=0x2303 and then cat dev/ttyUSB0 which give gobbleydegook listed in the ssh window, but thats hardly surprising since I haven't the baud rate and the envi is spitting out xml)

The problem is that the perl script I have will not run (from Paul Mutton - Jibble.org- thanks)

Code: Select all

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

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

my $PORT = "/dev/ttyUSB0";

my $ob = Device::SerialPort->new($PORT);
$ob->baudrate(57600);
$ob->write_settings;

open(SERIAL, "+>$PORT");
while (my $line = <SERIAL>) {
    if ($line =~ m!<ch1><watts>0*(\d+)</watts></ch1>.*<tmpr> *([\-\d.]+)</tmpr>!) {
        my $watts = $1;
        my $temp = $2;
        print "$watts, $temp\n";
    }
}

The error message I get is
"Can't locate device/SerialPort.pm in @INC (@INC includes ... (list of directories))" :-)
ummm so after some searching I tried

Code: Select all

apt-get install libdevice-serialport-perl  
which appears to install the SerialPort.pm but not in a directory in the list included in @INC above so I have added the following line above the Use SerialPort .. in the script

Code: Select all

use lib '/usr/lib/perl5/Device' 

Unfoirtuantely I still get the same error message !!!!!

Code: Select all

ls /usr/lib/perl5/Device

returns
SerialPort.pm
:(
I am now stumped Heeeeeeeeeelppp please

saif
Posts: 74
Joined: Sun Apr 15, 2012 8:15 pm
Contact: Website

Re: Perl script to access CurrentCost Envi

Sat Aug 04, 2012 10:06 pm

To include a path in @INC (which is an array of paths) you may need to "push" the path onto the array.
so before the "use" lines insert

Code: Select all

BEGIN {
        push @INC,"/usr/lib/perl5/";
        }
You can also permanently modify the @INC array to include your path
You can also add symlinks to your path to a path in @INC list

BUT the best way of installing perl modules is not through apt-get, but CPAN. This is itself not trivial in the raspberryPi...but several ways around it are in this forum...typically installing and using cpanminus.

Galaxynik
Posts: 43
Joined: Mon Jan 02, 2012 2:55 pm

Re: Perl script to access CurrentCost Envi

Sat Aug 04, 2012 10:54 pm

Thanks for that :-)

Eventually I did install CPAN using cpanminus - that took and age with lots of 'do you want to prepend...' questions- I am ashamed to say that the problem was the difference between 'D' and 'd' - I had two different perl scripts and it took me a long time to notice 'device' not 'Device' :oops:

Happliy logging usage to a RRD and plotting graphs .... :D

Now trying to post data to pachube/Cosm - using Net::Pachube and getting the following error

'Base class package "Class::Accessor::Fast" is empty' on the following line of code

Code: Select all

use Net::Pachube;
:(

User avatar
nommo
Posts: 40
Joined: Sun Jun 10, 2012 10:17 am
Location: UK

Re: Perl script to access CurrentCost Envi

Sun Aug 12, 2012 4:44 pm

Hi - I stumbled on this thread over on the Current Cost forum where a user is using a RasPi to upload data to cosm.com (is it you OP?) who provides a perl script. I just about get by with CLI but my programming skills are non-existent.

I thought I would have a go as I've got the Envi and cable and a cosm account from a couple of years ago when I had a dabble. I have followed the steps but hit a stumbling block when running the script:
Global symbol "$response" requires explicit package name at ./envi.pl line 40.
Execution of ./envi.pl aborted due to compilation errors.
The script looks like this:

Code: Select all

#!/usr/bin/perl -w

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

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

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";
        $wget = 'wget -O - \
  --header="X-ApiKey: <--- YOUR KEY HERE -->" \
  --header="X-Http-Method-Override: put" \
  --post-data "0,'.$temp."\n".'1,'.$watts.'" http://api.cosm.com/v2/feeds/<-- YOUR FEED ID -->.csv';
		#print "$wget\n";
		$response = `$wget`;
		#print $response;
      $meas = $sumW = $sumT = 0;
    }
}
}

Line 40 is:

Code: Select all

$response = `$wget`;


I ran an apt-get on 'wget' but it said I had the latest version installed, so I'm wondering if anyone can give me any pointers?

Cheers
Paul
I ate all the Pi

User avatar
nommo
Posts: 40
Joined: Sun Jun 10, 2012 10:17 am
Location: UK

Re: Perl script to access CurrentCost Envi

Sun Aug 12, 2012 5:48 pm

Ahhh - I think I may have sorted it - I added 'my' to line 40

Code: Select all

my $response = `$wget`;
It's now running - I feel like I've learned something today :)

Next step getting to run at boot....
I ate all the Pi

Galaxynik
Posts: 43
Joined: Mon Jan 02, 2012 2:55 pm

Re: Perl script to access CurrentCost Envi

Sun Aug 12, 2012 5:54 pm

Sorry not the first poster on CC but the second - I am using Net::Pachube to update cosm

Code: Select all

#!/usr/bin/perl -w

use strict;
use lib '/usr/lib/perl5/Device';
use RRD::Simple ();
use Device::SerialPort qw(:PARAM :STAT 0.07);
use Net::Pachube ;

my $APIkey ="1WQbY1E************************_Z6jie#######1dZMjq4";
my $devid="12483";
my $pachube = Net::Pachube->new( key=>$APIkey);
my $feed=$pachube->feed($devid);
my $rrd = RRD::Simple->new( file => "/home/cc/powertemp.rrd" );
my $PORT = "/dev/ttyUSB0";
my $sumW = 0;
my $sumT = 0;
my $num = 0 ;
my $cosW = 0;
my $cosT = 0;


my $ob = Device::SerialPort->new($PORT);
$ob->baudrate(57600);
$ob->write_settings;

open(SERIAL, "+>$PORT");
while (my $line = <SERIAL>) {
	if ($line =~ m!<tmpr>([\-\d.]+).*<watts>0*(\d+)</watts>!) {
		my $watts = $2;
		my $temp = $1;
		$rrd->update(power=>$watts,temperature=>$temp);
		my $t = localtime();
		open (mypower,'>/var/www/cc/lasteng.html');
		print mypower "<!DOCTYPE html>\n";
		print mypower "<html>\n";
		print mypower "<body>\n";
		print mypower "<h1>Last Power reading $2 watts</h1\n>";
                print mypower "<h1>Temperature  $1 degrees</h1\n\n>";
		print mypower "Sent Cosm $cosW W - $cosT -- $num<br \>\n ";
		print mypower $t . " \n";
		print mypower "</body>\n";
		print mypower "</html>\n";
		close(mypower);
		$num++;
		$sumW += $watts;
		$sumT += $temp;
		if ($num >= 72) {# send reading to cosm
			$cosW = int(($sumW/72)+0.5);
			$cosT = int($sumT/72*10)/10;
			$feed->update(data => [$cosT,$cosW]) ;
			$num = $sumT = $sumW = 0 ;
			}			
	}
}
Note that I have amended my original which updated to cosm every 5 secs to every 6 mins approx - that way the data gets displayed by CurrentCost dashboard just like if you had a bridge connected - anybody want to buty a second hand current cost bridge LOL
Last edited by Galaxynik on Sun Aug 12, 2012 6:11 pm, edited 1 time in total.

Galaxynik
Posts: 43
Joined: Mon Jan 02, 2012 2:55 pm

Re: Perl script to access CurrentCost Envi

Sun Aug 12, 2012 6:06 pm

I use a perl script set as a cron job to check that the envimonit.pl process is running every 5 mins and it gets restarted if its not, this is the code I use (I am a linux and perl noob so this has been quite a learning experience - constructive critique welcome)
I have apache2 running on the pi and this also writes to 2 log files stopenvi and runenvi when it checks so I can see how many times it needs rebooting - my script fails sometimes on updating the RR database - may have to update from RRD::Simple so that it doesnt fail on update error which it has 6 times now (in 7 days).

Code: Select all

#!/usr/bin/perl

open(PS,"/bin/ps x|") || die "Cant open PS";
while (<PS>)   {
#print $_;
	if ($_ = m/envimonit.pl/ ) {
		close PS;
#		print localtime() . "--->";
#	       	print "Envimonit running\n";
		open(suclog,'>>/var/www/cc/runenvi.html');
                print suclog localtime() . "--->";
                print suclog "Envimonit running<br />\n";
		close(suclog);
 		exit;} 
		}
close PS;
open(failog,'>>/var/www/cc/stopenvi.html');
print failog localtime() . "--->";
print failog "Envimonit not found - tryng to restart<br />\n";
close(failog);

#print localtime() . "--->";
#print "Envimonit not found - tryng to restart\n";
system("/home/cc/envimonit.pl &");


User avatar
nommo
Posts: 40
Joined: Sun Jun 10, 2012 10:17 am
Location: UK

Re: Perl script to access CurrentCost Envi

Sun Aug 12, 2012 6:27 pm

Galaxynik wrote:Sorry not the first poster on CC but the second - I am using Net::Pachube to update cosm
Ahhh yes - of course, noticed the same nickname now! I don't know how I managed to troubleshoot a perl script with my brain :roll:

I am glad I didn't get a bridge also :)
I ate all the Pi

SteamyTea
Posts: 1
Joined: Tue Jun 12, 2012 8:38 pm

Re: Perl script to access CurrentCost Envi

Sun Sep 02, 2012 9:05 am

Right
I have a CurrentCost Envi (a white one) and want to just create a text file of the date and time, temp and channel values.
I have no idea how to even start (have read what is above and it is all Greek to me), Windows habits die hard.
Can anyone help?

benichols
Posts: 6
Joined: Sun Feb 17, 2013 9:04 pm

Re: Perl script to access CurrentCost Envi

Sun Feb 17, 2013 9:07 pm

Hello,
I hope someone can help. I am trying to get this perl script to run with my Raspberry Pi and my CurrentCost monitor to upload to Cosm. The script seems to run fine and I even checked in minterm to confirm that the CurrentCost is outputting goobleygook (XML). However, the Cosm feed never gets updated. If it helps, my CurrentCost has temperature and 3 energy channels (3 transmitters). Does anyone know what I might be doing wrong here? Do I need to modify the script to reflect the differing output of my monitor?
Thanks for any help!
Brian

User avatar
nommo
Posts: 40
Joined: Sun Jun 10, 2012 10:17 am
Location: UK

Re: Perl script to access CurrentCost Envi

Mon Feb 18, 2013 6:47 pm

benichols wrote:Hello,
I hope someone can help. I am trying to get this perl script to run with my Raspberry Pi and my CurrentCost monitor to upload to Cosm. The script seems to run fine and I even checked in minterm to confirm that the CurrentCost is outputting goobleygook (XML). However, the Cosm feed never gets updated. If it helps, my CurrentCost has temperature and 3 energy channels (3 transmitters). Does anyone know what I might be doing wrong here? Do I need to modify the script to reflect the differing output of my monitor?
Thanks for any help!
Brian
Hiya Brian,

I thought we should carry on discussing this on the thread rather than PM as I am lacking the skills to solve this one - hopefully someone else can help you better, so I'm posted my responses below:

Sadly it wasn't my script - I got it from user 'ghubsch' over on the CurrentCost forum... who in turn was inspired by Jibble...

I did fix an error on ghubsch's code though, and figured out a way to run it in the background... that was my contribution...

I *DO* recall reading something about multiple channels.... I am only running a single channel, so I've not tried to get more than one sensor working...

My first thought is to suggest that you try getting it working on Cosm/Pachube with just one channel first... just to make sure you've got all the other bits set up right. In the meantime - I'll try to find out if there's a way to get multiple channels going on... I had a quick look earlier but no joy so far.

I've just refreshed my memory on Cosm/Pachube - did you create separate datastreams on your feed?

On my feed my datastreams are named 0 and 1... and in the script there's this line that mentions 0 and 1:

Code: Select all

  --post-data "0,'.$temp."\n".'1,'.$watts.'" http://api.cosm.com/v2/feeds/
I reckon that might be the line that you need to edit to include 2, 3, 4, 5, 6 - but there may be more to it... I wish I knew how to write PERL!!

Cheers
Paul
I ate all the Pi

ffimon
Posts: 7
Joined: Tue Feb 19, 2013 9:55 pm

Re: Perl script to access CurrentCost Envi

Tue Feb 19, 2013 11:44 pm

@ benichols

I have a VB.NET program that will cope with as many sensors as you can throw @ it. It was a pig to get working in Wheezy as mono for Wheezy is very broke.

The program I have written has a .conf file like this

Code: Select all

CODE: SELECT ALL
temperature=CC,CT128 Temperature,300
ccport=/dev/ttyAMA0
API-Key=Uxxxxxxxxxxxx-Kxxxxxxxxxxx
feed=103823
uploadinterval=300
# Define Sensor  SensorID|ChannelID|Imediate Reading|Average Reading|Samples to Average over|Today Daily Consumption|  
sensor=0|1|Consumption Now|Average Consumption|10|Todays Consumption|Todays Total Consumpion
sensor=0|2|Generation Now|Average Generation|10|Todays Generation|Todays Total Generation
I have 2 sensors one for consumption one for generation. The CT clamp consumption will have the live feed for my incomer through it as well as the live for the Solar Generation.

This way if I am generating 2KW and exporting 500W :-
The CT sensor will see the 2000KW generated and going through it, the 500W leaving will generate an opposite field thus the reading will be the 1500W I am consuming.

The second CT sensor I have is on the Generator only. Thus I can calculate my Import and Export by

Import or (Export)= Consumption-Supply

Also I have also ditched the USB Serial Port I needed the port as I have this running on a Model A with a wireless network dongle.

To do this take a standard Cat 5 Cable and connect (Blue) pin 8 on the cable to Ground on the Pi and Brown pin 4 to the Rx on the Pi.

I intend to power the Envi through the same cable Apparently Pin 1 Is the feed and should accept 3.3V *****Warning I haven't tried this yet I will let you know if it works. /Warning***** This way I should get down to 1 Plug socket.

The program also does Temperature monitoring I should be adding DS18B20 support soon.

benichols
Posts: 6
Joined: Sun Feb 17, 2013 9:04 pm

Re: Perl script to access CurrentCost Envi

Wed Feb 20, 2013 9:28 pm

User ghubsch (who originally wrote the envi.pl script) helped me fix the script for my 3 transmitter CurrentCost Envi system. I am attaching the script for others that might also want to monitor more than 1 power channel. It is working famously with Cosm and pvoutput. Be sure to enter your API and feed number into the script and save the file.

Also, with his help, here are the instructions for making sure that the script is started on reboot.
I put the envi.pl script in my /home/pi folder. Make it executable (chmod +x envi.pl)
Modify the crontab (crontab -e) and add the following line to the crontab "@reboot /home/pi/envi.pl > /dev/null 2>&1 &" (without quotes of course). Save the crontab and exit out. Now every time you reboot the script should start automatically!

Thanks again Günther!
Brian
Attachments
envi.pl.zip
(1.53 KiB) Downloaded 378 times

User avatar
ghubsch
Posts: 1
Joined: Thu Jul 19, 2012 8:47 am

Re: Perl script to access CurrentCost Envi

Wed Feb 20, 2013 9:45 pm

Hi Brian !

I'm here too!

So, I'm the same "ghubsch" from the other forum, and if anyone need some help or additional explanation, I'm also available to help.

Regards,

GHubsch

User avatar
nommo
Posts: 40
Joined: Sun Jun 10, 2012 10:17 am
Location: UK

Re: Perl script to access CurrentCost Envi

Fri Mar 01, 2013 1:28 pm

Thanks for the update Ben and thanks for the script GHubsch! Very useful...
I ate all the Pi

ffimon
Posts: 7
Joined: Tue Feb 19, 2013 9:55 pm

Re: Perl script to access CurrentCost Envi

Sun Mar 03, 2013 10:38 pm

ffimon wrote: I intend to power the Envi through the same cable Apparently Pin 1 Is the feed and should accept 3.3V *****Warning I haven't tried this yet I will let you know if it works. /Warning***** This way I should get down to 1 Plug socket.
Ok
I have had time to try this now and it works. If you disconnect the power supply from your CC128 you can power it from the 3.3V on the raspberry pi GPIO. Connecting pin 1 (Orange/White) of the RJ45 to GPIO pin 1 will Power your CC-128.

So I currently have my household energy Consumption / Generation / Import And Export all monitored by a single raspberry PI (Model A) using a single phone charger to power it and the CC-128. The feed is on https://cosm.com/feeds/103823/#feed_metadata it has (1 Min Averages), (1 Min Peaks) And (Daily Totals W/h) totals each of (Consumption / Generation / Import And Export).

The easiest way to physically connect it for me was to use a Pressac RJ 45 Cable and 3 Dupont Jumper Cable less that £3 the lot I can mount the Pi In a Double Back Box And use a RJ45 Drop Cable to the CC128 from the Pressac Connector.

The code is Available if anyone is interested (Its not Pretty due to the fact MONO is very broke In Hard Float Wheezy). I have just bought an LCD Graphic display from Digole 128x64 I am hoping to output the data along with a graph to that. The info on the CC 128 Display is now useless as it adds the generation to the consumption so only displays meaningful info when I am not generating. Even this does not help as the CC 128 is really inaccurate when it comes to Apparent Power. My CC 128 says I am generating 125 W when I am generating 0 due to the "apparent power" from my two inverters. I find if I subtract 125 W from the Generation figure the daily total comes out reasonably accurately. (within 3%).
Attachments
dupont.jpg
dupont.jpg (10.49 KiB) Viewed 17798 times
Presac.jpg
Presac.jpg (36.56 KiB) Viewed 17798 times

gippy73
Posts: 1
Joined: Tue Sep 10, 2013 1:18 pm

Re: Perl script to access CurrentCost Envi

Tue Sep 10, 2013 1:30 pm

I've read You've been able to power Envi (CC128) from the Raspberry Pi through GPIO.

I was thinking to even use GPIO to receive Envi's data, saving a (precious) USB port on RPi.

So my question is: did You even try it? Is it a level adapter (such as Max232) needed between the two devices?

Because I don't know if Envi's Serial Port is a common +12 -12 Volt or already a TTL one, suitable for GPIO direct connection.

I hope You or someone else can help me, before starting with tests… :)

Thanks in advance.

P.S.: If needed, I could share a Perl Script to read data from Envi (through Prolific USBserial adapter) and feed it into EmonCMS. It is an improvement of this (12th post):
http://openenergymonitor.org/emon/node/1418

ffimon
Posts: 7
Joined: Tue Feb 19, 2013 9:55 pm

Re: Perl script to access CurrentCost Envi

Tue Sep 10, 2013 4:16 pm

The Envi's output it 3V so you can connect it directly to the RX on the GPIO.

Return to “Beginners”