Page 2 of 2

Re: Buienradar settings

Posted: Sunday 25 October 2015 13:04
by SweetPants
Modified the script a little to get the Longitude and Latitude from the domoticz database

#!/usr/bin/perl -w

=head1 buienradar_rain.pl

Installation:

In Domoticz create a Dummy Percentage sensor.
Next go to the Devices overview, and write down the 'idx' value of the sensor.

Copy this file to another location and edit the setting below to match your situation.

cp /home/pi/domoticz/scripts/buienradar_rain_example.pl /home/pi/domoticz/buienradar_rain.pl
nano /home/pi/domoticz/buienradar_rain.pl

You might have to install perl or additional modules

DBI: CPAN
DBD::SQLite: CPAN

sudo perl -MCPAN -e shell
cpan{1] install DBI
cpan[2] install DBD::SQLite
exit


LWP: http://lwp.interglacial.com/ch01_03.htm
sudo apt-get install libjson-perl

Next add a Crontab rule:

crontab -e

Add the following line at the end:

*/5 * * * * perl /home/pi/domoticz/buienradar_rain.pl

In 5 minutes, the sensor should work

=cut

use strict;
use LWP::UserAgent;
use HTTP::Cookies;
use DBI;

my $domoticz_ip="127.0.0.1";
my $domoticz_port="8080";
my $domoticz_sensor_idx="307";
my $domoticz_user="pi";
my $domoticz_pass="<your pi password>";
my $driver="SQLite";
my $database="/home/pi/domoticz.db";


my $dbh = DBI->connect("DBI:$driver:dbname=$database",$domoticz_user, $domoticz_pass, { RaiseError => 1 }) or die $DBI::errstr;
my ($lat, $long) = split(/;/,$dbh->selectrow_array( qq(SELECT sValue FROM preferences WHERE Key = 'Location')));


my $duration=15;

my @user_agents = (
'Mozilla/4.0 (compatible; MSIE 4.01; Windows 98)'
);

my $ua = LWP::UserAgent->new(
#Set agent name, we are not a script! :)
agent => $user_agents[rand @user_agents],
cookie_jar => HTTP::Cookies->new(),
);

my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime();

my $startTime=($hour*60)+$min;
my $endTime=$startTime+$duration;

my $url = "http://gps.buienradar.nl/getrr.php?lat=$lat&lon=$long";
my $response = $ua->get($url);

unless ($response->is_success) {
# hmm, let's retry
$response = $ua->get($url);

unless ($response->is_success) {
# still no luck; sleep and retry
sleep 1;
$response = $ua->get($url);

unless ($response->is_success) {
print "Could not connect to buienradar.nl.\n";
exit 0;
}
}
}

my $data = $response->content;

unless ($data =~ /\A((\d{3})?\|\d{2}:\d{2}\r?\n)+\z/) {
print "Could not parse the data returned by buienradar.nl.\n";
exit 0;
}

my $total_rain_predictions=0;
my $total_rain_values=0;

while ($data =~ s/\A(\d{3})?\|(\d{2}:\d{2})\r?\n//) {
my ($value, $mtime) = ($1, $2);

if (defined $value) {

my @hour_min = split(':', $mtime);
my $mhour = $hour_min[0];
my $mmin = $hour_min[1];
my $calc_time=($mhour*60)+$mmin;
if (($calc_time>=$startTime)&&($calc_time<=$endTime)) {
$value =~ s/\A0+(\d)/$1/;
$total_rain_predictions+=$value;
$total_rain_values+=1;
}
}
}

my $result = "0.0";

if ($total_rain_values!=0) {
my $rain_0_100=($total_rain_predictions/$total_rain_values)*0.392156862745098;
$result = sprintf("%.2f", $rain_0_100);
}

$url = "http://$domoticz_ip:$domoticz_port/json.htm?type=command&param=udevice&idx=$domoticz_sensor_idx&nvalue=0&svalue=$result";
$response = $ua->get($url);

unless ($response->is_success) {
print "Error sending data to domoticz!\n";
exit 0;
}
$data = $response->content;

if (index($data, "\"OK\"") == -1) {
print "Error sending data to domoticz!\n";
exit 0;
}

print "OK, precip=$result\n";

if ($dbh) { $dbh->disconnect(); }

Re: Buienradar settings

Posted: Sunday 25 October 2015 14:20
by gizmocuz
you are not allowed to access the database directly, and on most linux systems, this might not work i think...

I leave the script as it is, its not to hard to add 2 values (latitude/longitude), and the overhead of installing perl sqlite is then also not needed

Bdw (for others), this is for the script inside the domoticz/scripts/buienradar_rain_example.pl

it is only suitable for dutch (and Belgium i suppose) users

Re: Buienradar settings

Posted: Sunday 25 October 2015 22:07
by SweetPants
gizmocuz wrote:you are not allowed to access the database directly, and on most linux systems, this might not work i think...
It works (on linux), and as far as I know multiple SELECT at the same time is supported.

Re: Buienradar settings

Posted: Sunday 25 October 2015 22:09
by Derik
The "normal" way is working great to.... :D :D
On a Cubieboard

Re: Buienradar settings

Posted: Monday 26 October 2015 8:15
by gizmocuz
SweetPants wrote:
gizmocuz wrote:you are not allowed to access the database directly, and on most linux systems, this might not work i think...
It works (on linux), and as far as I know multiple SELECT at the same time is supported.
Normally the database should be 'locked' and if one process (application) is using it, it should not (could not?) be opened by a second one

maybe it would be better to request the lat/lon value via json ?

Re: Buienradar settings

Posted: Monday 26 October 2015 8:57
by SweetPants
gizmocuz wrote:maybe it would be better to request the lat/lon value via json ?
That's another option, will get into that this evening. I want to keep my scripts as easy as possible to save me from editing them every time I change something somewhere else. Thanks for the advise

Re: Buienradar settings

Posted: Friday 30 October 2015 11:16
by SweetPants
Changed the script to use Domoticz config JSON

#!/usr/bin/perl -w

=head1 buienradar_rain.pl

Installation:

In Domoticz create a Dummy Percentage sensor.
Next go to the Devices overview, and write down the 'idx' value of the sensor.

Copy this file to another location and edit the setting below to match your situation.

cp /home/pi/domoticz/scripts/buienradar_rain_example.pl /home/pi/domoticz/buienradar_rain.pl
nano /home/pi/domoticz/buienradar_rain.pl

You might have to install perl or additional modules

DBI: CPAN
DBD::SQLite: CPAN
JSON: CPAN

sudo perl -MCPAN -e shell

cpan{1] install DBI
cpan[2] install DBD::SQLite
cpan[3] install JSON

exit

LWP: http://lwp.interglacial.com/ch01_03.htm
sudo apt-get install libjson-perl

Next add a Crontab rule:

crontab -e

Add the following line at the end:

*/5 * * * * perl /home/pi/domoticz/buienradar_rain.pl

In 5 minutes, the sensor should work

=cut

use strict;
use LWP::UserAgent;
use HTTP::Cookies;
use DBI;
use JSON;
use Data::Dumper;

my $domoticz_ip="127.0.0.1";
my $domoticz_port="8080";
my $domoticz_sensor_idx="307";
my ($long, $lat);
my ($url, $response, $data);

my $duration=15;

my @user_agents = ( 'Mozilla/4.0 (compatible; MSIE 4.01; Windows 98)' );

my $ua = LWP::UserAgent->new(
#Set agent name, we are not a script! :)
agent => $user_agents[rand @user_agents],
cookie_jar => HTTP::Cookies->new(),
);

# Get config from Domoticz
$url = "http://127.0.0.1:8080/json.htm?type=com ... =getconfig";
$response = $ua->get($url);

unless ($response->is_success)
{
print "Could not connect to Domoticz.\n"; exit 0;
}

# Extract longitude and lattitude from JSON string
$data = decode_json($response->content);

$long = $data->{Longitude};
$lat = $data->{Latitude};

my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime();

my $startTime=($hour*60)+$min;
my $endTime=$startTime+$duration;

$url = "http://gps.buienradar.nl/getrr.php?lat=$lat&lon=$long";
$response = $ua->get($url);

unless ($response->is_success) {
# hmm, let's retry
$response = $ua->get($url);

unless ($response->is_success) {
# still no luck; sleep and retry
sleep 1;
$response = $ua->get($url);

unless ($response->is_success) {
print "Could not connect to buienradar.nl.\n";
exit 0;
}
}
}

$data = $response->content;

unless ($data =~ /\A((\d{3})?\|\d{2}:\d{2}\r?\n)+\z/) {
print "Could not parse the data returned by buienradar.nl.\n";
exit 0;
}

my $total_rain_predictions=0;
my $total_rain_values=0;

while ($data =~ s/\A(\d{3})?\|(\d{2}:\d{2})\r?\n//) {
my ($value, $mtime) = ($1, $2);

if (defined $value) {

my @hour_min = split(':', $mtime);
my $mhour = $hour_min[0];
my $mmin = $hour_min[1];
my $calc_time=($mhour*60)+$mmin;
if (($calc_time>=$startTime)&&($calc_time<=$endTime)) {
$value =~ s/\A0+(\d)/$1/;
$total_rain_predictions+=$value;
$total_rain_values+=1;
}
}
}

my $result = "0.0";

if ($total_rain_values!=0) {
my $rain_0_100=($total_rain_predictions/$total_rain_values)*0.392156862745098;
$result = sprintf("%.2f", $rain_0_100);
}

$url = "http://$domoticz_ip:$domoticz_port/json.htm?type=command&param=udevice&idx=$domoticz_sensor_idx&nvalue=0&svalue=$result";
$response = $ua->get($url);

unless ($response->is_success) {
print "Error sending data to domoticz!\n";
exit 0;
}
$data = $response->content;

if (index($data, "\"OK\"") == -1) {
print "Error sending data to domoticz!\n";
exit 0;
}

print "OK, precip=$result\n";

Re: Buienradar settings

Posted: Sunday 01 November 2015 21:45
by jjnj
How do you use this script on Windows?

Re: Buienradar settings

Posted: Sunday 01 November 2015 21:47
by gizmocuz
You can't it's a bash script

Re: Buienradar settings

Posted: Sunday 01 November 2015 22:20
by SweetPants
gizmocuz wrote:You can't it's a bash script
But you probably can use it if you install ActiveState Perl on your Windows PC, install the right Perl Modules using Perls Packet Manager (ppm) and change the first line to where your Perl is installed. Make sure Perl is in your PATH when calling the script.

Re: Buienradar settings

Posted: Saturday 14 November 2015 15:52
by dutchnld
Hi, I got this up and running by reading through this thread.
Works great, but I'd like to change the percentage Icon to a different Icon.
But for this type of device I can't change is.

Does anyone know how to workaround this?

Re: Buienradar settings

Posted: Saturday 14 November 2015 16:15
by gizmocuz
That's not possible
Probably you are going to use this device for an event or script so what's an icon

Re: Buienradar settings

Posted: Saturday 14 November 2015 18:54
by dutchnld
In this case you're right. I like the overview to be nice with relevant logo's though.

But in other cases like dummy switches, I'd would be very useful.
Pity that's not possible.

Re: Buienradar settings

Posted: Tuesday 29 December 2015 17:26
by assenzuid
When i run the script I get below error

perl buienradar_rain.pl
Can't locate LWP/UserAgent.pm in @INC (@INC contains: /etc/perl /usr/local/lib/perl/5.14.2 /usr/local/share/perl/5.14.2 /usr/lib/perl5 /usr/share/perl5 /usr/lib/perl/5.14 /usr/share/perl/5.14 /usr/local/lib/site_perl .) at buienradar_rain.pl line 2.
BEGIN failed--compilation aborted at buienradar_rain.pl line 2.

I have installed Perl following this quide: http://lwp.interglacial.com/ch01_03.htm

Re: Buienradar settings

Posted: Friday 01 January 2016 23:27
by assenzuid
Solved with the following command sudo perl -MCPAN -e 'install LWP::UserAgent::Cached'

Re: Buienradar settings

Posted: Monday 23 May 2016 21:31
by JaapKoppejan
Thanks a lot for posting your solution, that helped me as well!