Raspberry PI - implementing an affordable home automation system [Part II]

In the previous post about affordable home automation based on Raspberry PI, We learned to check the House mains connections from a panel of eight relays connected to our Raspberry PI.

Screenshot of 2015-02-01 20:20:47Today we will see how to control a total of sixteen electrical appliances Thanks to the circuit that we implemented above, and enabling boards has given me Jordi of @electronics.cat and you already could you see in the summary of the Hardware Freedom Day; designed by the same, and one hundred percent free hardware.

For this practice, I'm going to use the PI's three branches, that is responsible for adapting all the pins GPIO of Raspberry PI of 3.3V to 5V bi-directionally and will allow us to working with the second reserved for the PiCam I2C bus; I will also use plate relays optoacoplados I used in the previous article about home automation.

Pi3Ramas

IP in three ranges designed by @JordiBinefa.

All this puts at our disposal a total of sixteen Sockets domotic divided between two Raspberry, and with expandability of up to 128 in the case of the PI's 3 branches, Since it allows us to take advantage of the two buses I2C sacrificing the possibility of using the connector to the PiCam.

To connect the pi's three branches, We must weld the six corresponding to the second bus pins I2C that are not soldiers of factory, at the moment Jordi It is actively working on a new version of its plate for Raspberry PI B + and Raspberry PI models 2.

OLYMPUS DIGITAL CAMERAWe can help to weld the pins holding the two strips of three with jumpers, but We must make sure they are more aligned us as possible at all times before soldering them.

We can now connect the PI's three branches to the Raspberry I have previously edited isolating the two plates, for what Jordi Use separators such plastics.

The pi of three branches and plastic index divider connector P3.

set05_08_v02_P2P3P4

Scheme of the P3 connector made by @JordiBinefa.

Carry out the connection with the Relay adapter plate, We must bend the pins of the relay Board I want to connect to the pi's three branches, helping us with some pliers; in the next picture you can see the final result.

Depending on the position of the pins VCC and GND in our relay, We will place this jumper relays in one module, or another position to invest them; always bearing in mind that Relay plate must be powered by USB.

IN1 IN2 IN3 IN4 VCC GND

IN1 IN2 IN3 IN4 GND VCC

Now we will connect our relay of eight outputs to the adaptor plate of relays, and is in the PI's three branches; should be noted that This module is compatible with four and eight outputs relays.

Then we can see the table of connections between the Relay module, the PI's three branches, and Raspberry PI.

Module relays optoacoplados Connector P3 Raspberry PI model B
VCC + 5V + 5V
IN 7 Bit 0 GPIO 29
IN 6 Bit 1 GPIO 28
IN 5 Bit 2 GPIO 18
IN 4 Bit 3 GPIO 7
IN 3 Bit 4 GPIO 8
IN 2 Bit 5 GPIO 10
IN 1 Bit 6 GPIO 9
IN 0 Bit 7 GPIO 11
GND GND GND

Now proceed to connect the relays optoacoplados plate directly to the GPIO of the other Raspberry, in the image below you can see the final result.

The connections between the GPIO of our Raspberry they will be the same as in the previous article, However in this case I have added all of the connections table.

Module relays optoacoplados Raspberry PI model B
VCC + 5V
IN 1 GPIO 3
IN 2 GPIO 2
IN 3 GPIO 24
IN 4 GPIO 25
IN 5 GPIO 23
IN 6 GPIO 27
IN 7 GPIO 22
IN 8 GPIO 4
GND GND

To run the scripts in python of the web that we will see below, First we have to give permissions to the user www-data It is the user assigned to the web server; for what First we will install the apache web server as we have seen in this previous article and we will write sudo visudo in the terminal and at the end of the file We will add to www-data as sudoer, being the final result as below.

# See sudoers(5) for more information on "#include" directives:

#includedir /etc/sudoers.d
pi ALL=(ALL) NOPASSWD: ALL
www-data ALL=(ALL) NOPASSWD: ALL

For each of the sixteen GPIO pins employees in this practice, We need to create a subdirectory, and two scripts in Python; one to turn on, and the other to turn the relay, Here is an example with the PIN 11.

#!usr/bin/env/ python
#enciende.py
import RPi.GPIO as GPIO
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.setup(11, GPIO.IN)
#!usr/bin/env/ python
#apaga.py
import RPi.GPIO as GPIO
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.setup(11, GPIO.OUT)

In each of the Raspberry PI We will place your Sixteen scripts to turn on the connected relays or to the pins employees in each, that you can consult in the previous tables; the scripts corresponding to the Raspberry connected directly from the GPIO to relay plate will place them in the directory /Home/pi/relays/gpio.

To create the hierarchy of directories and subdirectories, and copy the programmed files in Python We will use these other two scripts in Bash; the first will be that execute in the PI connected to the PI's three branches.

#!/bin/bash
#Raspberry PI con la pi de 3 ramales.

#Creamos la jerarquía de directorios.
mkdir /home/pi/reles/gpio
mkdir /home/pi/reles/gpio/11
mkdir /home/pi/reles/gpio/9
mkdir /home/pi/reles/gpio/10
mkdir /home/pi/reles/gpio/8
mkdir /home/pi/reles/gpio/7
mkdir /home/pi/reles/gpio/18
mkdir /home/pi/reles/gpio/28
mkdir /home/pi/reles/gpio/29
ls /home/pi/reles/gpio

#Y copiamos los scripts
cp enciende.py apaga.py /home/pi/reles/gpio/11
cp enciende.py apaga.py /home/pi/reles/gpio/9
cp enciende.py apaga.py /home/pi/reles/gpio/10
cp enciende.py apaga.py /home/pi/reles/gpio/8
cp enciende.py apaga.py /home/pi/reles/gpio/7
cp enciende.py apaga.py /home/pi/reles/gpio/18
cp enciende.py apaga.py /home/pi/reles/gpio/28
cp enciende.py apaga.py /home/pi/reles/gpio/29
ls /home/pi/reles/gpio/*

The others Sixteen scripts corresponding to the control of the Raspberry connected to the PI's three branches We will keep them in the directory /var/www/relay/gpio /.

#!/bin/bash
#Raspberry pi con relé compatible con GPIO.

#Creamos la jerarquía de directorios.
mkdir /var/www/rele/gpio
mkdir /var/www/rele/gpio/4
mkdir /var/www/rele/gpio/22
mkdir /var/www/rele/gpio/27
mkdir /var/www/rele/gpio/23
mkdir /var/www/rele/gpio/25
mkdir /var/www/rele/gpio/24
mkdir /var/www/rele/gpio/2
mkdir /var/www/rele/gpio/3
ls /var/www/rele/gpio

#Y copiamos los scripts.
cp enciende.py apaga.py /var/www/rele/gpio/4
cp enciende.py apaga.py /var/www/rele/gpio/22
cp enciende.py apaga.py /var/www/rele/gpio/27
cp enciende.py apaga.py /var/www/rele/gpio/23
cp enciende.py apaga.py /var/www/rele/gpio/25
cp enciende.py apaga.py /var/www/rele/gpio/24
cp enciende.py apaga.py /var/www/rele/gpio/2
cp enciende.py apaga.py /var/www/rele/gpio/3
ls /var/www/rele/gpio/*

At this time we must create a key pair so that the Raspberry PI It is the page to run the scripts of the connected to the PI's three branches remotely, as the user who will do it is www-data must assign a password in order to login and create the key pair on your behalf.

#Asignamos un password al usuario www-data
usuario@maquina:~$ passwd www-data

Introduzca la nueva contraseña de UNIX: 
Vuelva a escribir la nueva contraseña de UNIX: 
passwd: contraseña actualizada correctamente

#Creamos el par de claves RSA de 4096 bits
www-data@maquina:~$ ssh-keygen -b 4096 -t rsa

Generating public/private rsa key pair.
Enter file in which to save the key (/var/www/.ssh/id_rsa): 
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /var/www/.ssh/id_rsa.
Your public key has been saved in /var/www/.ssh/id_rsa.pub.
The key fingerprint is:
5a:dc:bd:af:0d:ae:9d:9f:bb:c2:d4:5b:dd:67:8d:18 tmo@tmo-K55A
The key's randomart image is:
+--[ RSA 4096]----+
|                 |
|                 |
|                 |
|       . . .E    |
|        S . .+ .+|
|       o    o.o B|
|      .    oo  +.|
|           oo=.. |
|          ..=+Bo |
+-----------------+

#Copiamos la clave pública a la otra raspberry
www-data@maquina:~$ ssh-copy-id pi@192.168.*.***                 
Number of key(s) added: 1

Now try logging into the machine, with:   "ssh 'pi@192.168.*.***'"
and check to make sure that only the key(s) you wanted were added.

Once created the key pair public and private, We will check that we can access by SSH without entering the password; for operated from the same web of different Raspberry relays on the same local network, in this case I have connected them thanks to the small free router WRTNode.

WRTNode_ETHThen we will create our home page which will contain the buttons input type you will have to press to execute the scripts in Python from the web interface by using the post method; for this example I will show the web with only four starts so that the article is not too long.

<html>
 <head>
                 <!--/var/www/rele/index.php-->
 </head>
 <body>

 <!--GPIO11--> 
 <form action="" method="post">
 Enchufe 0 [GPIO 11 - P3 Bit 7 - IN1]&nbsp;<input type="submit" name="encender11" value="Encender">
 <input type="submit" name="apagar11" value="Apagar">
 </form>
 &nbsp;&nbsp;

 <!--GPIO04--> 
 <form action="" method="post">
 Electrodomestico 0 [GPIO 04 - P4 Bit 7 - IN1]&nbsp;<input type="submit" name="encender4" value="Encender">
 <input type="submit" name="apagar4" value="Apagar">
 </form>
 <br></br>

 <!--GPIO09--> 
 <form action="" method="post">
 Enchufe 1 [GPIO 09 - P3 Bit 6 - IN2]&nbsp;<input type="submit" name="encender9" value="Encender">
 <input type="submit" name="apagar9" value="Apagar">
 </form>
 &nbsp;&nbsp;

 <!--GPIO22--> 
 <form action="" method="post">
 Electrodomestico 1 [GPIO 22 - P4 Bit 6 - IN2]&nbsp;<input type="submit" name="encender22" value="Encender">
 <input type="submit" name="apagar22" value="Apagar">
 </form>
 </body>
</html>

<?php

// Funciones PHP del pin GPIO 11
 if ($_POST[encender11]) { 
 $a- exec("sudo python /var/www/rele/gpio/11/enciende.py");
 echo $a;
 }

 if ($_POST[apagar11]) { 
 $a- exec("sudo python /var/www/rele/gpio/11/apaga.py");
 echo $a;
 }
// Fin de las funciónes del pin GPIO 11

// Funciones PHP del pin GPIO 9
 if ($_POST[encender9]) { 
 $a- exec("sudo python /var/www/rele/gpio/9/enciende.py");
 echo $a;
 }

 if ($_POST[apagar9]) { 
 $a- exec("sudo python /var/www/rele/gpio/9/apaga.py");
 echo $a;
 }
// Fin de las funciónes del pin GPIO 9

// Funciones PHP del pin GPIO 04

  if ($_POST[encender4]) { 
   $a- exec("ssh root@192.168.*.*** python /home/pi/reles/gpio/4/enciende.py");
   echo $a;
  }

  if ($_POST[apagar4]) { 
   $a- exec("ssh root@192.168.*.*** python /home/pi/reles/gpio/4/apaga.py");
   echo $a;
  }

// Fin de las funciónes del pin GPIO 04

// Funciones PHP del pin GPIO 22

  if ($_POST[encender22]) { 
   $a- exec("ssh root@192.168.*.*** python /home/pi/reles/gpio/22/enciende.py");
   echo $a;
  }

  if ($_POST[apagar22]) { 
   $a- exec("ssh root@192.168.*.*** python /home/pi/reles/gpio/22/apaga.py");
   echo $a;
  }

// Fin de las funciónes del pin GPIO 22
?>

If I want to feed him everything from a single outlet, must bypass all negative outputs 220V Relay.

The connections to the mains We'll make them the same way as in the last entry, enclosed between the plug and switch schemes miniature circuit breaker to facilitate the tracking of practice.

Plug differential relay

In the next installment on affordable home automation We will learn to take advantage of the two buses I2C Thanks to the PI's three branches, and we will make a proof of concept checking 128 interchangeable LEDs by electrical appliances.

If you like you can follow me on Twitter, Facebook, Google +, LinkedIn, or share it with the buttons under this publication, If you have any questions or suggestions please do not hesitate to comment.

Help us to reach more readers Share on LinkedInShare on FacebookTweet about this on TwitterShare on Google+Email this to someone

4 thoughts on"Raspberry PI - implementing an affordable home automation system [Part II]

  1. Very good posts, continues!!
    A question, I want to also install a home automation with easy raspberry system, but not is how for example to know the status of a light,blind or some device, If it has been manually activated.
    I say this for example, If I want to turn on a light from the phone but not be in that State is at that time.

    Salu2

    • Hello Igor, Thanks for your comment.

      It occurs to me that you could use some kind of sensor that verifies the tension that is reaching the lighting you want to control, and notify it is your web server in real time to know at any time if it is on or not.

Leave a Reply