Triggering a second Raspberry Pi in the network

Hi all,

I am still a noob, therefore please excuse stupid questions :slight_smile:

My setup:
PiOne: Raspberry Pi running Hassbian (located inside my house and connected to local router.)
PiTwo: Raspberry Pi located inside my garage (separated building) controlling the lights and garage doors via GPIOs and relays (not connected to Homeassistant yet).

What I want to do:
Create Switches in HA ibPiOne that are triggering the PiTwo’s GPIO (via Python scripts, whatever…) and control garage doors/lights etc.
As PiOne and PiTwo are far away from each other, there is no option to realize this via cable.

I have completely no clue about where to start and how to realize this project. Is there somebody out there who has done something similar with RPis? or maybe someboday could give me a hint on where to find ideas. Thank you so much in advance!

There are multiple ways to achieve this and it comes down to preference.

  1. Second HASS installation on second Pi
  2. Execute command on second Pi from first via shell-command and SSH
  3. Via MQTT subsciption
  4. probably many more.

~Cheers

I have a similar setup:

  • HA running on a linux box (ubuntu 14.04)
  • Pi 1 running osmc, connected to my TV via HDMI, and to a 16 port relay board via the GPIOs controlling the lights and the heating/cooling.

The way I do it is through php with nginx running on the pi, and command line switches on HA.
nginx default conf on pi:

# Default server configuration
#
server {
    listen 81 default_server;
    # SSL configuration
    #
     listen 443 ssl default_server;
    
     ssl_certificate     /etc/ssl/certs/my_own_certificate_file.crt;
     ssl_certificate_key /etc/ssl/private/my_own_key_file.key;

    root /var/www/html;

    # Add index.php to the list if you are using PHP
    index index.html index.htm index.nginx-debian.html;

    server_name _;

    location / {
        # First attempt to serve request as file, then
        # as directory, then fall back to displaying a 404.
        try_files $uri $uri/ =404;
    }

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
    #
    #    # With php5-cgi alone:
    #    fastcgi_pass 127.0.0.1:9000;
    #    # With php5-fpm:
        fastcgi_pass unix:/var/run/php5-fpm.sock;
    }

    
}

switch.php in /var/www/html folder on the pi:

<?php
// (c) 2015  athul jayaram
$output = shell_exec('gpio mode '.$_GET['pin'].' '.$_GET['switch']);
?> 

I have all my switches in a separate file called “switches.yaml”, so the configuration.yaml looks like this (well the relevant part)

...
switch: !include switches/switches.yaml
...

My switches.yaml first switch is like this (you can copy the last 5 lines as many times as you wish)

  - platform: command_line
    switches:

      # living room switch
      living_room:
        command_on: "/var/opt/homeassistant/scripts/up_lr.sh"
        command_off: "/var/opt/homeassistant/scripts/down_lr.sh"
        friendly_name: LivingRoom

My living room up script:

#!/bin/sh
#pin=5,living room

/usr/bin/curl -k 'https://192.168.112.250/switch.php?pin=5&switch=out'
/usr/bin/curl -X POST -d '{"state":"on"}' http://192.168.112.254:2012/api/states/switch.living_room

exit 0

Pin 5 is the wiring pi number of the appropriate pi gpio pin.

The down script:

    #!/bin/sh
    #pin=5,living room

    /usr/bin/curl -k 'https://192.168.112.250/switch.php?pin=5&switch=in'
    /usr/bin/curl -X POST -d '{"state":"off"}' http://192.168.112.254:2012/api/states/switch.living_room

    exit 0

Maybe the post part is not needed…

And that all folks…

cheers
tom

1 Like

Hi all,

thank you for your answers. It’s been a while since I found time to proceed with the project.
Meanwhile I decided to realize this project via MQTT and a NodeMCU controller. As I am still a rookie I am sure I will come back with more questions :slight_smile:

Thanks again.
Cheers