Raspberry PI – Implementing an affordable home automation system [Part III]

After several months, I have decided to return to This project with which I intend to create a free home automation system, and economically affordable; This low microcomputer-based consumption, but cross-platform and portable to any device with OS GNU/Linux available from port GPIO.In previous posts We learned to control modules of relays optoacoplados with Raspberry PI by bash, PHP, and Python; on this occasion I have optimized the automation system, and incidentally I've migrated it to Raspberry PI 3 to dispose of wireless connectivity, But if you wish you can use any previous model.

The hardware It consists of two modules of eight relays each, that make up a total of Sixteen outs to control appliances, Jacks electric, lighting; and a without end of devices that it be converted to this small Raspberry PI 3, in everything you need for domotic house u office conventional.

Building automation scheme

Circuit breaker switch diagram domotic DIY.

The control panel is programmed in PHP, and is an optimization of the code of the previous version but intended to manage the GPIO directly through bash which greatly accelerates the process; a must, Since we will serve this via page WiFi.

FremooticsPI

The automation system web control panel.

The fact of without additional files to manage the port outputs GPIO of Raspberry by Python; Converts this simple web page, a single file, in the true starting point of which has become someday in a full home automation system that I will be developing together.

<html>
<head>
<title>Panel de control - Sistema domotico libre</title>
</head>

<body>
<div align="center">
<br></br>

 <!--GPIO4/IN1-->
 <form action="" method="post">
 Interruptor 01 &nbsp;<input type="submit" name="encender4" value="Encender">
 <input type="submit" name="apagar4" value="Apagar">

 <!--GPIO17/IN2-->
 <form action="" method="post">
 &nbsp; Interruptor 02 &nbsp;<input type="submit" name="encender17" value="Encender">
 <input type="submit" name="apagar17" value="Apagar">

<br></br>

 <!--GPIO27/IN3-->
 <form action="" method="post">
 Interruptor 03 &nbsp;<input type="submit" name="encender27" value="Encender">
 <input type="submit" name="apagar27" value="Apagar">

 <!--GPIO22/IN4-->
 <form action="" method="post">
 &nbsp; Interruptor 04 &nbsp;<input type="submit" name="encender22" value="Encender">
 <input type="submit" name="apagar22" value="Apagar">

<br></br>

 <!--GPIO18/IN5-->
 <form action="" method="post">
 Interruptor 05 &nbsp;<input type="submit" name="encender18" value="Encender">
 <input type="submit" name="apagar18" value="Apagar">

 <!--GPIO23/IN6-->
 <form action="" method="post">
 &nbsp; Interruptor 06 &nbsp;<input type="submit" name="encender23" value="Encender">
 <input type="submit" name="apagar23" value="Apagar">

<br></br>

 <!--GPIO24/IN7-->
 <form action="" method="post">
 Interruptor 07 &nbsp;<input type="submit" name="encender24" value="Encender">
 <input type="submit" name="apagar24" value="Apagar">

 <!--GPIO25/IN8-->
 <form action="" method="post">
 &nbsp; Interruptor 08 &nbsp;<input type="submit" name="encender25" value="Encender">
 <input type="submit" name="apagar25" value="Apagar">

<br></br>

 <!--GPIO5/IN1-->
 <form action="" method="post">
 Interruptor 09 &nbsp;<input type="submit" name="encender5" value="Encender">
 <input type="submit" name="apagar5" value="Apagar">

 <!--GPIO6/IN2-->
 <form action="" method="post">
 &nbsp; Interruptor 10 &nbsp;<input type="submit" name="encender6" value="Encender">
 <input type="submit" name="apagar6" value="Apagar">

<br></br>

 <!--GPIO13/IN3-->
 <form action="" method="post">
 Interruptor 11&nbsp;<input type="submit" name="encender13" value="Encender">
 <input type="submit" name="apagar13" value="Apagar">

 <!--GPIO12/IN4-->
 <form action="" method="post">
 &nbsp; Interruptor 12 &nbsp;<input type="submit" name="encender12" value="Encender">
 <input type="submit" name="apagar12" value="Apagar">

<br></br>

 <!--GPIO19/IN5-->
 <form action="" method="post">
 Interruptor 13 &nbsp;<input type="submit" name="encender19" value="Encender">
 <input type="submit" name="apagar19" value="Apagar">

 <!--GPIO16/IN6-->
 <form action="" method="post">
 &nbsp; Interruptor 14 &nbsp;<input type="submit" name="encender16" value="Encender">
 <input type="submit" name="apagar16" value="Apagar">

<br></br>

 <!--GPIO20/IN7-->
 <form action="" method="post">
 Interruptor 15 &nbsp;<input type="submit" name="encender20" value="Encender">
 <input type="submit" name="apagar20" value="Apagar">

 <!--GPIO21/IN8-->
 <form action="" method="post">
 &nbsp; Interruptor 16 &nbsp;<input type="submit" name="encender21" value="Encender">
 <input type="submit" name="apagar21" value="Apagar">

</div>
</body>
</html>

<?php

// Funciones PHP del pin GPIO 4

 if ($_POST[encender4]) {
 $a- exec("echo 4 > /sys/class/gpio/export");
 $a- exec("echo out > /sys/class/gpio/gpio4/direction");
 $a- exec("echo 1 > /sys/class/gpio/gpio4/value");
 echo $a;
 }

 if ($_POST[apagar4]) {
 $a- exec("echo 0 > /sys/class/gpio/gpio4/value");
 echo $a;
 }

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

// Funciones PHP del pin GPIO 17

 if ($_POST[encender17]) {
 $a- exec("echo 17 > /sys/class/gpio/export");
 $a- exec("echo out > /sys/class/gpio/gpio17/direction");
 $a- exec("echo 1 > /sys/class/gpio/gpio17/value");
 echo $a;
 }

 if ($_POST[apagar17]) {
 $a- exec("sudo echo 0 > /sys/class/gpio/gpio17/value");
 $a- exec("echo 17 > /sys/class/gpio/unexport");
 echo $a;
 }

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

// Funciones PHP del pin GPIO 27

 if ($_POST[encender27]) {
 $a- exec("echo 27 > /sys/class/gpio/export");
 $a- exec("echo out > /sys/class/gpio/gpio27/direction");
 $a- exec("echo 1 > /sys/class/gpio/gpio27/value");
 echo $a;
 }

 if ($_POST[apagar27]) {
 $a- exec("sudo echo 0 > /sys/class/gpio/gpio27/value");
 $a- exec("echo 27 > /sys/class/gpio/unexport");
 echo $a;
 }

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

// Funciones PHP del pin GPIO 22

 if ($_POST[encender22]) {
 $a- exec("echo 22 > /sys/class/gpio/export");
 $a- exec("echo out > /sys/class/gpio/gpio22/direction");
 $a- exec("echo 1 > /sys/class/gpio/gpio22/value");
 echo $a;
 }

 if ($_POST[apagar22]) {
 $a- exec("sudo echo 0 > /sys/class/gpio/gpio22/value");
 $a- exec("echo 22 > /sys/class/gpio/unexport");
 echo $a;
 }

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

// Funciones PHP del pin GPIO 18

 if ($_POST[encender18]) {
 $a- exec("echo 18 > /sys/class/gpio/export");
 $a- exec("echo out > /sys/class/gpio/gpio18/direction");
 $a- exec("echo 1 > /sys/class/gpio/gpio18/value");
 echo $a;
 }

 if ($_POST[apagar18]) {
 $a- exec("sudo echo 0 > /sys/class/gpio/gpio18/value");
 $a- exec("echo 18 > /sys/class/gpio/unexport");
 echo $a;
 }

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

// Funciones PHP del pin GPIO 23

 if ($_POST[encender23]) {
 $a- exec("echo 23 > /sys/class/gpio/export");
 $a- exec("echo out > /sys/class/gpio/gpio23/direction");
 $a- exec("echo 1 > /sys/class/gpio/gpio23/value");
 echo $a;
 }

 if ($_POST[apagar23]) {
 $a- exec("sudo echo 0 > /sys/class/gpio/gpio23/value");
 $a- exec("echo 23 > /sys/class/gpio/unexport");
 echo $a;
 }

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

// Funciones PHP del pin GPIO 24

 if ($_POST[encender24]) {
 $a- exec("echo 24 > /sys/class/gpio/export");
 $a- exec("echo out > /sys/class/gpio/gpio24/direction");
 $a- exec("echo 1 > /sys/class/gpio/gpio24/value");
 echo $a;
 }

 if ($_POST[apagar24]) {
 $a- exec("sudo echo 0 > /sys/class/gpio/gpio24/value");
 $a- exec("echo 24 > /sys/class/gpio/unexport");
 echo $a;
 }

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

// Funciones PHP del pin GPIO 25

 if ($_POST[encender25]) {
 $a- exec("echo 25 > /sys/class/gpio/export");
 $a- exec("echo out > /sys/class/gpio/gpio25/direction");
 $a- exec("echo 1 > /sys/class/gpio/gpio25/value");
 echo $a;
 }

 if ($_POST[apagar25]) {
 $a- exec("sudo echo 0 > /sys/class/gpio/gpio25/value");
 $a- exec("echo 25 > /sys/class/gpio/unexport");
 echo $a;
 }

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

// Funciones PHP del pin GPIO 5

 if ($_POST[encender5]) {
 $a- exec("echo 5 > /sys/class/gpio/export");
 $a- exec("echo out > /sys/class/gpio/gpio5/direction");
 $a- exec("echo 1 > /sys/class/gpio/gpio5/value");
 echo $a;
 }

 if ($_POST[apagar5]) {
 $a- exec("sudo echo 0 > /sys/class/gpio/gpio5/value");
 $a- exec("echo 5 > /sys/class/gpio/unexport");
 echo $a;
 }

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

// Funciones PHP del pin GPIO 6

 if ($_POST[encender6]) {
 $a- exec("echo 6 > /sys/class/gpio/export");
 $a- exec("echo out > /sys/class/gpio/gpio6/direction");
 $a- exec("echo 1 > /sys/class/gpio/gpio6/value");
 echo $a;
 }

 if ($_POST[apagar6]) {
 $a- exec("sudo echo 0 > /sys/class/gpio/gpio6/value");
 $a- exec("echo 6 > /sys/class/gpio/unexport");
 echo $a;
 }

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

// Funciones PHP del pin GPIO 13

 if ($_POST[encender13]) {
 $a- exec("echo 13 > /sys/class/gpio/export");
 $a- exec("echo out > /sys/class/gpio/gpio13/direction");
 $a- exec("echo 1 > /sys/class/gpio/gpio13/value");
 echo $a;
 }

 if ($_POST[apagar13]) {
 $a- exec("sudo echo 0 > /sys/class/gpio/gpio13/value");
 $a- exec("echo 13 > /sys/class/gpio/unexport");
 echo $a;
 }

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

// Funciones PHP del pin GPIO 12

 if ($_POST[encender12]) {
 $a- exec("echo 12 > /sys/class/gpio/export");
 $a- exec("echo out > /sys/class/gpio/gpio12/direction");
 $a- exec("echo 1 > /sys/class/gpio/gpio12/value");
 echo $a;
 }

 if ($_POST[apagar12]) {
 $a- exec("sudo echo 0 > /sys/class/gpio/gpio12/value");
 $a- exec("echo 12 > /sys/class/gpio/unexport");
 echo $a;
 }

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

// Funciones PHP del pin GPIO 19

 if ($_POST[encender19]) {
 $a- exec("echo 19 > /sys/class/gpio/export");
 $a- exec("echo out > /sys/class/gpio/gpio19/direction");
 $a- exec("echo 1 > /sys/class/gpio/gpio19/value");
 echo $a;
 }

 if ($_POST[apagar19]) {
 $a- exec("sudo echo 0 > /sys/class/gpio/gpio19/value");
 $a- exec("echo 19 > /sys/class/gpio/unexport");
 echo $a;
 }

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

// Funciones PHP del pin GPIO 16

 if ($_POST[encender16]) {
 $a- exec("echo 16 > /sys/class/gpio/export");
 $a- exec("echo out > /sys/class/gpio/gpio16/direction");
 $a- exec("echo 1 > /sys/class/gpio/gpio16/value");
 echo $a;
 }

 if ($_POST[apagar16]) {
 $a- exec("sudo echo 0 > /sys/class/gpio/gpio16/value");
 $a- exec("echo 16 > /sys/class/gpio/unexport");
 echo $a;
 }

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

// Funciones PHP del pin GPIO 20

 if ($_POST[encender20]) {
 $a- exec("echo 20 > /sys/class/gpio/export");
 $a- exec("echo out > /sys/class/gpio/gpio20/direction");
 $a- exec("echo 1 > /sys/class/gpio/gpio20/value");
 echo $a;
 }

 if ($_POST[apagar20]) {
 $a- exec("sudo echo 0 > /sys/class/gpio/gpio20/value");
 $a- exec("echo 20 > /sys/class/gpio/unexport");
 echo $a;
 }

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

// Funciones PHP del pin GPIO 21

 if ($_POST[encender21]) {
 $a- exec("echo 21 > /sys/class/gpio/export");
 $a- exec("echo out > /sys/class/gpio/gpio21/direction");
 $a- exec("echo 1 > /sys/class/gpio/gpio21/value");
 echo $a;
 }

 if ($_POST[apagar21]) {
 $a- exec("sudo echo 0 > /sys/class/gpio/gpio21/value");
 $a- exec("echo 21 > /sys/class/gpio/unexport");
 echo $a;
 }

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

?>

To perform the connections between Raspberry and relay, You must go to the foot of the letter the following tables based on the schema GPIO BCM; because otherwise you'll have to modify the lines in the file PHP, and replace in the code the numbers of PIN employees.

Raspberry PI model B + / 2/3 Relay module 1
+ 5V + 5V
GPIO 4
IN 1
GPIO 17
IN 2
GPIO 27 IN 3
GPIO 22
IN 4
GPIO 18 IN 15
GPIO 23
IN1 6
GPIO 24
IN1 7
GPIO 25
IN1 8
GND GND
Raspberry PI model B + / 2/3 Relay module 2
+ 5V + 5V
GPIO 5
IN 1
GPIO 6
IN 2
GPIO 13
IN 3
GPIO 12
IN 4
GPIO 19
IN1 5
GPIO 16
IN1 6
GPIO 20
IN1 7
GPIO 21
IN1 8
GND GND

Once connected all the pins of Raspberry to the respective VCC, GND, e inputs of the Relay; only remains copy the PHP file to the Raspberry PI, to what use SCP that will allow us to do so remotely.

usuario@maquina:~ $ scp index.php pi@192.168.1.*:/home/pi/
usuario@maquina:~ $ ssh pi@192.168.1.* -C sudo mv /home/pi/index.php /var/www/html/

Also remotely We will move the file to the directory of the web server, and we will assign as by owners SSH to the user and Group www-data; will add it to the Group gpio, and then We reboot to apply the changes.

usuario@maquina:~ $ ssh pi@192.168.1.* -C sudo chown www-data:www-data /var/www/html/index.php
usuario@maquina:~ $ ssh pi@192.168.1.* -C sudo usermod -a -G gpio www-data
usuario@maquina:~ $ ssh pi@192.168.1.* -C sudo reboot

At last We feed with 230V relay as we saw in the previous articles making a bridge between the entrance and the other positive poles, to use a single Plug, or switch miniature circuit breaker; But if we want to connect Sixteen devices high consumption is recommended to use two, or more plugs.

relespuenteados

Scheme of the bridge between the positive poles of the relay module.

The remaining wires, which correspond to the negative pole and to the takes of Earth, We will connect them directly to the device want to control through the Relay module, in my case a Plug domotic DIY; We should also bear in mind that for safety our relay should have 1A exit, so for 16 outputs will need a relay of 16TO.

Relay diagram plug

Diagram plug domotic DIY.

In the following video You can see the performance of this Sixteen outputs system by checking them, through a demonstration with a low energy lamp; If you like do not forget follow me on YouTube to not miss any of the unpublished videos I'm going to publish.

If you like you can follow me on Twitter, Facebook, Google +, LinkedIn, Feedly, 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

15 thoughts on"Raspberry PI – Implementing an affordable home automation system [Part III]

  1. Peatonet a doubt, I copied the code such that and works all correctly, a exception that van those buttons to the reverse, turn on shut down and vice versa.

    Many thanks for all a blog great.

    • Hello XuFLeS, Thanks for your comment.

      Surely have that see with the connections of the relay to the network power; It would be useful to check if you've connected it in normally open (NOT) instead of normally closed (NC).

      see you soon.

      • I am testing before connect the contacts to 230 V and the Red led of the relay is turned on when turn off pulse. I've modified slightly the code to reverse it and look good, but I had emerged it doubts the truth. Thank you x your response.

  2. Good contribution. I need something similar for irrigation control, but on one condition: If one of the n outputs are in 1 then GPIOx will be in 1. Bone GPIO = 0 if only if all outputs = 0. Any idea?? Where outputs are solenoid valves and GPIOx pump. From already thank you.

    • Hello Edu,

      Depending on the number end of outputs, you use an or several raspberry PI; with a would be simple, with several should rating which is the way most optimal of consult the information between devices.

      Thanks for commenting.

  3. Hello, I am interested in this because I would like to control some lights foreign from the mobile.
    It would be the first time you tried something with the raspberry.
    But I arises a doubt to the call is this post “part three”.
    PHP code with only that as not missing would use anything from parts one and two?
    I understand that this code would be index.php, where iria placed?
    Thanks a lot!

    • Hi Paul,

      Only with this code PHP can implement the version more ' current’ System.

      In the previous tutorials and reviews of them explain where is the file (index.php), as give you the permissions right, etc…

      Thanks for the comment.

      • Thank you very much for your prompt response.
        I have already connected with a single plate of eight relays.
        But a rare thing is happening to me that can't find a solution.
        Giving the web buttons, the GPIO are activated me perfectly, giving its corresponding voltage and keeping it till that turn it off, but on the other hand the relay makes its “Click” running but not switch. The sound is as if the relay is activase and is deactivated, is so fast that the tester does not detects any change.
        To could be due??
        Thank you.

        Salu2!

        • I found the bug.
          In the PHP code, in the turn off command, remove $a code- exec(“echo 18 > /sys/class/gpio/unexport”); , as you have in the first switch and now already I works.
          Now what makes, the first time you turn on the raspberry; the eight leds to medium intensity light, you have to turn on and shut once and then you turn on the led well and now works correct.

          Salu2!

          • Sorry to be so heavy with so many comments but this is the first project that I do with my raspery and it is amazing and very very simple! Is this tutorial.
            And the world that opens with this equipment is inmeso!
            On the web, without leaving the PHP, you could see that switch is active?
            The switches, could reach handled with time schedule and timing?
            Of truth,I have opened up a new world, very entertaining and economical.

            Salu2 and thanks!

          • Hello Paul GC.

            I think that in a comment I explained as check that switch is active reading through PHP State of the GPIO.

            To control time programmatically simplest thing is to use the system CRON daemon to run what you want, times at which Marquis le.

            Thank you very much for commenting.

          • already quite awning project, to read the State of the gpio is enough to use
            exec ( “GPIO read 29”, $status21 );
            If ($status21[0] == 0) {
            }
            and put the brackets you want to display if it is switched on or == status to 1 for shutdown.

            The numbering of the gpio read does not match the gpio port numbering, If you want you put here the table that did of equivalences of each port with its numbering of read.

            The only thing that I still do not understand, is because 1 It is off and 0 It is on.
            and why on the raspberry, all outputs are stateless, leaving to medium voltage and outputs not work till you turn them twice.

            Create a site reset, what it does is give the order off to all outputs, a “sleep” and again to give the order to extinguish them all, After this process already are all functional.

            It may be that they pass these two things?

            Salu2!

          • Hi Paul, Sorry for the delay but I'm lately very little time between work and studies.

            Firstly I want to thank you that you have shared your progress with the other readers of the blog, When I have free time I have pending continue this section of tutorials on Home Automation easy and affordable, but is it really a pride for me to see as you follow yourself forward with the project and going making each one in your own custom system.

            Connection on the relay by assigning a logical low value (0), and turn them with a logical value high (1) It is possible that due to that you have connected the relay to 230V as normally open instead of normally closed circuit, as I told the reader XuFLes the first comment of this article; If you try it and the problem persists, feel free to comment and try to think of any other possibility.

            To avoid having to do the outputs reset after each Raspbian ignition, You can create a simple script that export and set the pins you want to reboot, and launch it for example from rc.local.

          • Hello!
            Nothing in that afternoon in reply!
            It is logical to take you time and especially in these dates.
            Ignition and shutdown, It works the other way around, I have the relays connected.

            And what of the rc.local, to avoid having to go to the reset page, as you would.

            Salu2!

  4. Good pedestrian, I've been reading the 3 tutorials on home automation parts, but in its programming going very fast for my, the topic seems super interesting but I think that we need a detail to a complete home automation system and is to receive input from the outside, ( buttons, light sensors, etc). I'd like to collaborate and think on the electrical part I can help and in return I ask help to understand the whole programming. My email is tecnico.antono.raya@gmail.com

    A greeting and congratulations on the web

Leave a Reply