xPL-PERL - Communication between xPL processess

Forum regarding Linux Software and Home Automation Domotica.

xPL-PERL - Communication between xPL processess

Postby beanz » Sun Mar 23, 2008 6:29 pm

xpl-rfxcom already tries to cope with repeated readings by not outputting repeats for 0.5 seconds. What is the time between the first and last (of the 11) readings? That is plenty for other devices but obviously the scales are a bit verbose. I'm reluctant to change this since making it bigger would stop various things I use from working - I have some controls that do one thing if you press them once but something else if you press them twice within one second - increasing the timeout would mean the second press would be missed. It is probably best to handle this in the callback.

The code is okay. A better (more efficient way) would be to use a filter on when adding the xpl callback since you only want sensor.basic messages for your device so you can do:

Code: Select all
#!/usr/bin/perl -w
use strict;
use xPL::Client;

my $xpl =  xPL::Client->new( vendor_id => 'bnz', device_id => 'xll') or die;
$xpl->add_xpl_callback(id => "weight",
                      callback => \&log_weight,
                      filter => {
                                 class => 'sensor',
                                 class_type => 'basic',
                                 device => 'bwr102.01',
                      });
$xpl->main_loop;

sub log_weight {
  my %p = @_;
  my $msg = $p{message};
  print "My weight is ", $msg->current, "kg\n";
  return 1;
}


I know I used it - I did say elsewhere that learning perl is a continuous process - but log is already a function in perl - for natural logarithms so it is best to avoid that name.
beanz
Starting Member
Starting Member
 
Posts: 20
Joined: June 2007
Location: United Kingdom

xPL-PERL - Communication between xPL processess

Postby Jfn » Mon Mar 24, 2008 7:50 am

An example of the output from the xpl-rfxcom logfile (This is from one measurement only):
Code: Select all
2008-03-24_06:23:45.88672 Processed: 38ec12dc1092e012
2008-03-24_06:23:45.88676 xpl-trig/sensor.basic: bnz-rfxcom.debian -> * - bwr102.01[weight]=92.1
2008-03-24_06:23:51.55019 Processed: 38ec12dc1092e012
2008-03-24_06:23:51.55023 xpl-trig/sensor.basic: bnz-rfxcom.debian -> * - bwr102.01[weight]=92.1
2008-03-24_06:23:54.55840 Processed: 38ec12dc1092e012
2008-03-24_06:23:54.55843 xpl-trig/sensor.basic: bnz-rfxcom.debian -> * - bwr102.01[weight]=92.1
2008-03-24_06:23:57.54961 Processed: 38ec12dc1092e012
2008-03-24_06:23:57.54968 xpl-trig/sensor.basic: bnz-rfxcom.debian -> * - bwr102.01[weight]=92.1
2008-03-24_06:24:00.55784 Processed: 38ec12dc1092e012
2008-03-24_06:24:00.55788 xpl-trig/sensor.basic: bnz-rfxcom.debian -> * - bwr102.01[weight]=92.1
2008-03-24_06:24:03.54903 Processed: 38ec12dc1092e012
2008-03-24_06:24:03.54907 xpl-trig/sensor.basic: bnz-rfxcom.debian -> * - bwr102.01[weight]=92.1
2008-03-24_06:24:06.55728 Processed: 38ec12dc1092e012
2008-03-24_06:24:06.55731 xpl-trig/sensor.basic: bnz-rfxcom.debian -> * - bwr102.01[weight]=92.1
2008-03-24_06:24:09.54851 Processed: 38ec12dc1092e012
2008-03-24_06:24:09.54855 xpl-trig/sensor.basic: bnz-rfxcom.debian -> * - bwr102.01[weight]=92.1
2008-03-24_06:24:12.56535 Processed: 38ec12dc1092e012
2008-03-24_06:24:12.56538 xpl-trig/sensor.basic: bnz-rfxcom.debian -> * - bwr102.01[weight]=92.1
2008-03-24_06:24:15.55750 Processed: 38ec12dc1092e012
2008-03-24_06:24:15.55753 xpl-trig/sensor.basic: bnz-rfxcom.debian -> * - bwr102.01[weight]=92.1
2008-03-24_06:24:18.54879 Processed: 38ec12dc1092e012
2008-03-24_06:24:18.54882 xpl-trig/sensor.basic: bnz-rfxcom.debian -> * - bwr102.01[weight]=92.1

As you can see the scale outputs the last measured weight about 11 times (I say 'about' because once in a while I have seen different amounts of readings, probably because transmitted packets get lost on the way).

If I stand on the scale within half a minute from a previous measurement, the scale will stop outputting the previous weight and start broadcasting this new measurement 11 times.

About storing measurements in a database:
- I intend to just compare the last measurement stored in the database with the current measurement. If the measurement is taken on the same day and the weight is the same, do not update anything. This should take care of duplicate values.
- When the date is the same (or different) and the weight is not the same as the last measurement, just insert a new record into the database.

I found out that the outputted weight by the scale can differ upto 400 grams when doing several consecutive measurements:
Code: Select all
2008-03-24_06:38:11.09849 xpl-trig/sensor.basic: bnz-rfxcom.debian -> * - bwr102.01[weight]=92.7
2008-03-24_06:38:16.68214 Processed: 382c1cec7292201c
2008-03-24_06:38:16.68217 xpl-trig/sensor.basic: bnz-rfxcom.debian -> * - bwr102.01[weight]=92.7
2008-03-24_06:38:23.73865 Processed: 389c18fc34929018
2008-03-24_06:38:23.73867 xpl-trig/sensor.basic: bnz-rfxcom.debian -> * - bwr102.01[weight]=92.3
2008-03-24_06:38:30.24835 Processed: 389c18fc34929018
2008-03-24_06:38:30.24838 xpl-trig/sensor.basic: bnz-rfxcom.debian -> * - bwr102.01[weight]=92.3
2008-03-24_06:38:33.24032 Processed: 389c18fc34929018
2008-03-24_06:38:33.24035 xpl-trig/sensor.basic: bnz-rfxcom.debian -> * - bwr102.01[weight]=92.3
2008-03-24_06:38:36.24740 Processed: 389c18fc34929018
2008-03-24_06:38:36.24743 xpl-trig/sensor.basic: bnz-rfxcom.debian -> * - bwr102.01[weight]=92.3
2008-03-24_06:38:39.23870 Processed: 389c18fc34929018
2008-03-24_06:38:39.23873 xpl-trig/sensor.basic: bnz-rfxcom.debian -> * - bwr102.01[weight]=92.3
2008-03-24_06:38:47.28931 Processed: 380c19cc50920019
2008-03-24_06:38:47.28935 xpl-trig/sensor.basic: bnz-rfxcom.debian -> * - bwr102.01[weight]=92.5
2008-03-24_06:38:53.41684 Processed: 380c19cc50920019
2008-03-24_06:38:53.41688 xpl-trig/sensor.basic: bnz-rfxcom.debian -> * - bwr102.01[weight]=92.5


So doing more measurements on one day (in the morning before going to work and in the evening when going to bed) should provide for a more accurate average weight.


Als het niet kapot is, niet repareren!
Jfn
Member
Member
 
Posts: 332
Joined: February 2008
Location: Netherlands

xPL-PERL - Communication between xPL processess

Postby Jfn » Mon Mar 24, 2008 7:54 pm

I have a working script that stores the measurements in a MySQL database.

When a measurement is taken, the script checks for the last weight measurement that is stored in the database. If this differs from the current measurement, an entry with the current weight is inserted into the database.

The advantage of the procedure above is that it only inserts an entry:
- When my weight changes
- When I decide to step back on the scale within 30 seconds and it gives a different reading from the previous one

If my weight does not change over time, it is of no use to insert an entry that does not differ from the last entry stored in the database.

I have attached the script to this message.

@beanz: Thanks for the modifications on my previous script. I can now run a very compact script that does exactly what I need it to do.



<b>Download Attachment:</b> [url="http://www.domoticaforum.eu/uploaded/Jfn/2008324195142_xpl-scale_to_sql.pl.txt"][img]images/icon_paperclip.gif[/img] xpl-scale_to_sql.pl.txt[/url]<br />2.83 KB

Als het niet kapot is, niet repareren!
Jfn
Member
Member
 
Posts: 332
Joined: February 2008
Location: Netherlands

xPL-PERL - Communication between xPL processess

Postby Jfn » Tue Mar 25, 2008 10:33 am

I configured the script as an xpl 'service' and I now have automated logging running. Next step is to make graphics out of the sql-data and start working on my diet [:)]
Code: Select all
debian:/# ps -ef | grep runsv
root      7260     1  0 Mar19 ?        00:00:00 runsvdir -P /var/service log: ...........................................................................................................................................................................................................................................................................................................................................................................................................
root      7606  7260  0 Mar19 ?        00:00:00 runsv xpl-rrd
root      7612  7260  0 Mar19 ?        00:00:00 runsv xpl-hub
root      7614  7260  0 Mar19 ?        00:00:00 runsv xpl-dawndusk
root     12311  7260  0 Mar20 ?        00:00:00 runsv xpl-rfxcom
root      3411  7260  0 10:28 ?        00:00:00 runsv xpl-scale_to_sql
root      3473  2983  0 10:30 pts/0    00:00:00 grep runsv
debian:/#



Als het niet kapot is, niet repareren!
Jfn
Member
Member
 
Posts: 332
Joined: February 2008
Location: Netherlands

xPL-PERL - Communication between xPL processess

Postby Snelvuur » Tue Mar 25, 2008 12:03 pm

graphs are easy.. beanz pointed me out to a nice api from google. http://code.google.com/apis/chart/

and otherwise use http://www.advsofteng.com/cdperl.html , or any other code you prefer. (this one does not depend on outside sources)

// Erik (binkey.nl)
User avatar
Snelvuur
Forum Moderator
Forum Moderator
 
Posts: 3155
Joined: April 2007
Location: Netherlands

xPL-PERL - Communication between xPL processess

Postby Jfn » Tue Mar 25, 2008 1:27 pm

I tried ChartDirector from Advanced Software Engineering, but it seems to be needing a paid license whereas I prefer a free license [:)]

Will look into it further though, as it seems a very suitable piece of software.

<blockquote id="quote"><font size="1" face="Verdana, Arial, Helvetica" id="quote">quote:<hr height="1" noshade id="quote"><i>Originally posted by snelvuur</i>
<br />...and otherwise use http://www.advsofteng.com/cdperl.html , or any other code you prefer. (this one does not depend on outside sources)

// Erik (binkey.nl)
<hr height="1" noshade id="quote"></font id="quote"></blockquote id="quote">

Als het niet kapot is, niet repareren!
Jfn
Member
Member
 
Posts: 332
Joined: February 2008
Location: Netherlands

xPL-PERL - Communication between xPL processess

Postby Snelvuur » Tue Mar 25, 2008 1:50 pm

chartdirector is pretty cheap, and easy to work with. you can always use it without paying, then you get a www.something line in the below corner. (but it still works exactly the same)

// Erik (binkey.nl)
User avatar
Snelvuur
Forum Moderator
Forum Moderator
 
Posts: 3155
Joined: April 2007
Location: Netherlands

xPL-PERL - Communication between xPL processess

Postby Jfn » Thu Mar 27, 2008 8:57 am

I slightly modified the script. It now has a built-in check to see whether we are still connected to the database or not.

I found out that after a while the connection to the database gets closed automatically. I added a check to see whether the connection is still open and if not, re-open it.



<b>Download Attachment:</b> [url="http://www.domoticaforum.eu/uploaded/Jfn/200832785743_xpl-scale_to_sql.pl.txt"][img]images/icon_paperclip.gif[/img] xpl-scale_to_sql.pl.txt[/url]<br />3.26 KB

Als het niet gerepareerd kan worden dan is het niet kapot!
Jfn
Member
Member
 
Posts: 332
Joined: February 2008
Location: Netherlands

xPL-PERL - Communication between xPL processess

Postby Snelvuur » Thu Mar 27, 2008 10:02 am

You can always just close the connection instead of keeping it open, i presume you dont measure yourself 24/7 but more on a daily basis.

// Erik (binkey.nl)
User avatar
Snelvuur
Forum Moderator
Forum Moderator
 
Posts: 3155
Joined: April 2007
Location: Netherlands

xPL-PERL - Communication between xPL processess

Postby Jfn » Thu Mar 27, 2008 2:23 pm

I intend to use the scale once or twice a day.

You are right about opening and closing the connection to the database during each measurement. This might make it in a next version of the script.

For now I will be concentrating on 1-wire stuff. Just today the mailman delivered a shipment from Hobby Boards. I will fire-up the soldering-iron as soon as I finish work today (I did not order ready-built units, but I ordered the DIY kits).




<blockquote id="quote"><font size="1" face="Verdana, Arial, Helvetica" id="quote">quote:<hr height="1" noshade id="quote"><i>Originally posted by snelvuur</i>
<br />You can always just close the connection instead of keeping it open, i presume you dont measure yourself 24/7 but more on a daily basis.

// Erik (binkey.nl)
<hr height="1" noshade id="quote"></font id="quote"></blockquote id="quote">

Als het niet gerepareerd kan worden dan is het niet kapot!
Jfn
Member
Member
 
Posts: 332
Joined: February 2008
Location: Netherlands

Previous

Return to Linux Forum

Who is online

Users browsing this forum: No registered users and 1 guest