Mobile Phone Online

Moderator: leecollings

Post Reply
Smartie
Posts: 21
Joined: Monday 14 December 2015 15:12
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Mobile Phone Online

Post by Smartie »

Domoticz Mobile Phone Switch

To check if anybody is Home or not make a Dummy Switch an look at the IDX Number

now login to SSH an add a Script.

cd /home/pi/domoticz/scripts
sudo nano ping_by_ip.pl

now past the follow script at the file an change the IP and IDX Number

# Array of (device idx, IP)
my %IP=(24=>'192.168.178.23', # TV
23=>'192.168.178.32', # Me
25=>'192.168.178.30'); # Tablet

Code: Select all

#!/usr/bin/perl
    use v5.14;
    use LWP::Simple;                # From CPAN
    use JSON qw( decode_json );     # From CPAN
    use Data::Dumper;               # Perl core module
    use strict;                     # Good practice
    use warnings;                   # Good practice
    use utf8;
    use feature     qw< unicode_strings >;
    # Configuration section, please update to your values
    my $domoticz = "127.0.0.1:8080";  # ip and port of your Domoticz server
    my $domo_cmd = "http://$domoticz/json.htm?type=devices&filter=all&used=true&order=Name";

            # Array of (device idx, IP)
                      my %IP=(24=>'192.168.178.23',     # TV
                              23=>'192.168.178.32',     # Me
                             25=>'192.168.178.30');      # Tablet
    my $debug=0;
    # Get the JSON url
    my $json = get( $domo_cmd );
    die "Could not get $domo_cmd!" unless defined $json;
    # Decode the entire JSON
    my $decoded = JSON->new->utf8(0)->decode( $json );
    my @results = @{ $decoded->{'result'} };
    #Put JSON switch and status in a Table
    my @tab;
    foreach my $f ( @results ) {
      if ($f->{"SwitchType"}) {
            $tab[$f->{"idx"}]=$f->{"Status"};
      }
    }
    # Now we go all over the IP to check if they are alive
if you have done this save the script with STRG + O and Exit with STRG + X
HINT: STRG is the german Layout for US it is CTRL

now add a crontab with

crontab -e

add in the last line

*/1 * * * * /home/pi/domoticz/scripts/ping_by_ip.pl 2>&1 >> /dev/null
and save with STRG + O and Exit with STRG + X


Now you can using the switch at Domoticz for Programming Actions...

:D
gflvern
Posts: 19
Joined: Monday 29 December 2014 11:14
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Mobile Phone Online

Post by gflvern »

Hi Smartie,

I think your code block is missing some code that checks to see if the IP addresses in the array are alive.

Cheers

gfl
Smartie
Posts: 21
Joined: Monday 14 December 2015 15:12
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Mobile Phone Online

Post by Smartie »

no it works by me i have only copy the part with the ip Adress so show what you must change.
This code Works fine by me so i habe a switch and can use Blocky
gflvern
Posts: 19
Joined: Monday 29 December 2014 11:14
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Mobile Phone Online

Post by gflvern »

Smartie wrote:no it works by me i have only copy the part with the ip Adress so show what you must change.
This code Works fine by me so i habe a switch and can use Blocky
But I would like to copy the rest of the script so I can try this for myself. To ping the IP addresses it needs the rest of the script surely?

Thanks
Smartie
Posts: 21
Joined: Monday 14 December 2015 15:12
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Mobile Phone Online

Post by Smartie »

Yes copy the script using you're in from phone or something then it works fine :) but you must change you're idx and ip address ;)

Try it
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest