turn on switch from perl script

Moderator: leecollings

Post Reply
b0b99
Posts: 2
Joined: Friday 12 June 2020 16:32
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

turn on switch from perl script

Post by b0b99 »

Is it possible to turn on a switch in Domoyicz from a perl script? If so could you give me an example script?

Thanks, bob

I have tried the sample script listed in the perl wiki but no errors are returned and the light does not turn on.

#!/usr/bin/perl

no warnings 'uninitialized';
use LWP::UserAgent;

$url{domo}=>'http://myip:8080';

$idx=87;
$cmd='On'; # or 'Off'

$switch_url=$url{domo}.'/json.htm?type=command&param=switchlight&idx='.$idx.'&switchcmd='.$cmd;
$ua=LWP::UserAgent->new; $ua->timeout(5); $res=$ua->put($switch_url);
unless ($res->is_success) { warn $res->status_line };
freijn
Posts: 536
Joined: Friday 23 December 2016 16:40
Target OS: Raspberry Pi / ODroid
Domoticz version: Stable
Location: Netherlands Purmerend
Contact:

Re: turn on switch from perl script

Post by freijn »

THis is an buienradar script but basics are the same

Code: Select all

#!/usr/bin/perl -w

=head1 buienradar_rain.pl

Installation:

Perl, you need to install the LWP module:
sudo apt-get install libjson-perl

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

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;

my $lat=50.22041;
my $long=5.89744;
my $domoticz_ip="127.0.0.1";
my $domoticz_port="8080";
my $domoticz_sensor_idx="2442";
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";


b0b99
Posts: 2
Joined: Friday 12 June 2020 16:32
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: turn on switch from perl script

Post by b0b99 »

Hello freijn, Thank you for getting back to me. Unfortunately I am not all that familiar with coding so I could not pick out of your code what was necessary to accomplish what I was after. However, looking again at the sample code given in the wiki under perl, I found the problem.
The line "$url{domo}=>'http://192.168.xxx.xxx:8080';" should be "$url{domo}='http://192.168.xxx.xxx:8080';"

Removing the greater than sign solved the problem and the sample code now works.

Again, thanks for getting back to me. I appreciate your trying to help.

Bob
Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests