Feb

13

Current Cost Capers

15 years ago, mid-February | 4 Comments

My Unbioctium Current Cost CC128 arrived on Thursday and although I’m not going to actually get a chance in install it for a couple of weeks I did have a play. The first thing to do once it was all up and paired with the base was get the output into my laptop. To do this you will need to buy the data cable or make up your own…

At first I tried to be smart with reading from the serial port, as that seems to be a bit unreliable I fell back on the old tried and tested method. I also decided from the outset that I was going to parse the XML that the the CC128 spat out rather than just filter the bits needed with regexp.

You will need to make sure you have the perl modules Device::SerialPort and XML::Simple installed. If you don’t have them then as root on your linux box do the following:

perl -MCPAN -e shell

then install each module, for example Device::SerialPort,

install Device::SerialPort

and here’s the script which is also available for download.Updated to make it more reliable.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#!/usr/bin/perl
 
use Device::SerialPort qw( :PARAM :STAT 0.07 );
use XML::Simple;
 
$port = "/dev/ttyUSB0";
 
$ob = Device::SerialPort->new($port) or die "Can not open port $port\n";
$ob->baudrate(57600);
$ob->write_settings;
$ob->close;
 
# using XML::Parser speeds xml parsing up lots!
$backend = 'XML::Parser';
$ENV{XML_SIMPLE_PREFERRED_PARSER} = $backend;
 
# we use this to only do 1 iteration (or not)
$escape=0; 
 
open(SERIAL, "<$port");
 
while($escape <= 0) {
	sleep(2);
 
	while ($line = <SERIAL>) {
 
		# for debug
		#print $line;
 
		$isValid = (index($line,"<msg>") != -1);
 
 
		if (!$isValid) { last; }
 
		print "This data is".($isValid==1?"":" not")." valid\n";
 
		# force XML::Simple to see this as a string not as a file
		# since XML::Simple is stupid and needs to be shot
 
		$line = "<fakeTag>$line</fakeTag>";
 
		$isHistoric = (index($line,"<hist>") != -1);
 
		$nref = XMLin($line,forcearray => 0);
 
		$ref = $nref->{msg};
 
		# just for reference, show if data is historic or not
 
		print "This data is".($isHistoric==1?"":" not")." historic\n";
 
 
		if (!$isHistoric) {
 
	        	$dsb        = 0 + $ref->{dsb};
        		$recordTime = $ref->{time};
        		$ccname     = $ref->{src};
        		$temp       = $ref->{tmpr};
			$ch1watts   = 0 + $ref->{ch1}->{watts};
			$sensor     = 0 + $ref->{sensor};
			$id         = $ref->{id};
			$type       = 0 + $ref->{type};
 
 
			if (defined $ref->{whatever}) {
				# do something based on whatever
 
			}
 
        		print "This $ccname was born $dsb days ago as at $recordTime - temperature is: $temp :: Current Watts in use on channel 1 are $ch1watts :: Sensor is $sensor, with an id of $id and a type of $type\n";
 
			# for cacti you'd probably just want to output CC_Temperature:$temp CC_Watts1:$ch1watts 
 
			# insert data into db
 
			# if you want to exit after a 'good' iteration set this to 1 otherwise set it to 0 (or don't change it to 1 :) );
			$escape=1;
		} else {
			#process or ignore historic data
		}
	}
}
 
close(SERIAL);

and here’s some example output, remember that I haven’t actually connected the loop around my electrical cable as yet.

This data is not historic
This CC128-v0.11 was born 1 days ago as at 21:16:44 - temperature is: 23.7 :: Watts used on channel 1 are 0 :: Sensor is 0, with an id of 02835 and a type of 1

Historic data isn’t processed at the moment but it would be trivial to do. I’m not processing it since I don’t really see the point if you’re going to store the results in a database.



[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

Tagged with:
February 13, 2009 9:38


Comments

Name (required)

Email (required)

Website

Speak your mind

4 Comments so far

  1. Stuart McFarlan on February 28, 2009 15:10

    Hey ScaredyCat

    I’m afraid I can’t direct message you on Twitter. could you e-mail me that invite its something I’m very excited to experiment with.

    Thank you
    Stuart McFarlan

  2. ScaredyCat / Andy Powell on February 28, 2009 16:20

    I’ve sent you the invite 🙂

  3. lyndhurst on April 24, 2010 01:15

    Andy,
    Thanks for this great script but I get some errors.

    The script works initially but after a few readings it throws the error .

    Can’t locate object method “xml_decl” via package “XML::Simple” at /usr/lib/perl5/XML/LibXML.pm line 559. at /usr/share/perl5/XML/Simple.pm line 370

    Any quick way to fix this?

    I have two transmitters, and as such two rows/lines are created.

    CC128-v1.180001109:16:2620.20007581005930053900548
    CC128-v1.180001109:16:3120.2100842100268

  4. Widget, it’s got a widget… : ..the cat came back.. on May 20, 2011 21:13

    […] Current Cost Capers […]

Current Electricity Use (15min)


iPhone/Webkit RSS Reader

Links


Tags

1-Wire android api Apple arduino currentcost DDAR development DVD FIC freerunner G1 google Google Phone gphone gprs GPS hardware image image builds inspiration iphone jailbreak kiosk linux Mac monitoring Music neo 1973 Nokia openmoko opensource OSX Pachube personal qtopia rhubarb rikki Rio slimp3 slimserver software tracking Trolltech u-boot


Twitpic


Graphy Stuff






Nasty Spam Monkeys