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

In previous posts in this series, We saw What are the GPIO pins, and how to control a LED with them from our Raspberry PI by Bash and Python, o with one web application.


On this occasion we will learn to control the power of an electrical appliance; before you start the server we have installed LAMP as we saw in the previous delivery.

To implement it, am going to use a relay Board of 5V and eight channels, It must be optoacoplada to protect the Raspberry PI; Since the devices that we are going to check with 220V, and pins GPIO Raspberry work with a maximum of 3,3V.


Relays optoacoplados plate.

We have to do between the relay and the Raspberry PI the following connections.

Module relays optoacoplados
Raspberry PI model to, B, A +, B +
+ 5V
IN 8

Connections between the PI Raspberry and relay plate.

Now we will test if the connections work, with a simple test in Bash, setting the pin GPIO 17 as output, and giving a logical high value.

#Exportamos el PIN deseado
usuario@maquina:~$ sudo echo 17 > /sys/class/gpio/export

#Establecemos la direccion (salida o entrada)
usuario@maquina:~$ sudo echo out > /sys/class/gpio/gpio17/direction

#Activamos la salida del rele dandole un valor de 1 al bit
usuario@maquina:~$ sudo echo 1 > /sys/class/gpio/gpio17/value

Once has turned on the led of the relay control, because we can turn it off and release the pin GPIO.

#Apagamos la salida del rele
usuario@maquina:~$ sudo echo 0 > /sys/class/gpio/gpio17/value

#Liberamos el PIN asociado
usuario@maquina:~$ sudo echo 17 > /sys/class/gpio/unexport

We have already tested the functioning, so now we are going to see the web page we need to schedule, It is quite more simple than the test with the LED We saw in the last delivery.

 <form action="" method="post">
 Enchufe 0 [GPIO 17]&nbsp;<input type="submit" name="encender17" value="Encender">
 <input type="submit" name="apagar17" value="Apagar">
 <input type="submit" name="parpadear17" value="Parpadear">

// Funciones PHP del pin GPIO 17
 if ($_POST[encender17]) { 
 $a- exec("sudo sh /var/www/rele/gpio/17/enciende.sh");
 echo $a;

 if ($_POST[apagar17]) { 
 $a- exec("sudo sh /var/www/rele/gpio/17/apaga.sh");
 echo $a;

 if ($_POST[parpadear17]) { 
 $a- exec("sudo sh /var/www/rele/gpio/17/parpadea.sh");
 echo $a;
// Fin de las funciónes del pin GPIO 17

We need to create these three scripts in bash to turn on, Turning off and flashing; These scripts are housed in /var/www/relay/gpio/17 /.


#Exportamos el PIN deseado
echo 17 > /sys/class/gpio/export

#Establecemos la direccion (salida o entrada)
echo out > /sys/class/gpio/gpio17/direction

#Activamos la salida del rele dandole un valor de 1 al bit
echo 1 > /sys/class/gpio/gpio17/value

#Apagamos la salida del rele
echo 0 > /sys/class/gpio/gpio17/value

#Liberamos el PIN asociado
echo 17 > /sys/class/gpio/unexport

#Exportamos el PIN deseado
echo 17 > /sys/class/gpio/export

#Establecemos la direccion (salida o entrada)
echo out > /sys/class/gpio/gpio17/direction

#Activamos la salida del rele dandole un valor de 1 al bit, y lo desactivamos 5 veces
echo 1 > /sys/class/gpio/gpio17/value
sleep 1
echo 0 > /sys/class/gpio/gpio17/value
sleep 1
echo 1 > /sys/class/gpio/gpio17/value
sleep 1
echo 0 > /sys/class/gpio/gpio17/value
sleep 1
echo 1 > /sys/class/gpio/gpio17/value
sleep 1
echo 0 > /sys/class/gpio/gpio17/value
sleep 1
echo 1 > /sys/class/gpio/gpio17/value
sleep 1
echo 0 > /sys/class/gpio/gpio17/value
sleep 1
echo 1 > /sys/class/gpio/gpio17/value
sleep 1
echo 0 > /sys/class/gpio/gpio17/value

#Liberamos el PIN asociado
echo 17 > /sys/class/gpio/unexport

When we have the scripts and the index.php on their respective routes, We have to be able to view the following hierarchy directory on the web server.

Screenshot of 2014-11-08 16:27:54On this occasion we must also make sudoer to the user www-data; for that, run sudo visudo in the terminal, and will add it pi under the user so that the user www-data You can control the pins GPIO.

#includedir /etc/sudoers.d

Now we will test the three buttons for Verify that everything is working correctly. Once work, and relay plate leds turn on, We will hear the “Click/clack” that you emit when opening and closing the circuit.

Screenshot of 2014-11-08 13:30:46

Web application to control the relay.

We will see how to connect it to a lamp or any other appliance to control it from any device with a web browser.

The schema for connect the relay between a magneto-thermal switch and a plug the wall of our House would be as follows; for schemes I have used the image of a single-channel relay, in this example it is the channel IN 8 eight-channel plate.


Connection diagram in the electrical panel of a House.

For the proof of concept, I will use the following schema with a plug that I had to change in a room of House, and a lamp. Below are the wall, and above who've ridden in this test to connect in the lamp; the relay acts as a switch.


Scheme used in this test.

There are three ports of electrical connection for each output in the relay: COM, normally open and normally closed. In COM We must connect the positive electrical It will feed the appliance, and in only one the other two the cable extending to the positive of the appliance. The negative connects directly from the power plug to the appliance.


A relay connections.

It will depend on if you connect it to normally open, or a normally closed If the appliance is on or off if you turn off the Raspberry.

In this example, the relay is connected in normally closed, so if you turn off the Raspberry, plugs work; If conectásemos it in normally open, When disconnecting the Raspberry lamp not may ignite because the circuit would be open; We will understand it better with a video.

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 LinkedIn
Share on Facebook
Tweet about this on Twitter
Share on Google+
Google +
Email this to someone

35 thoughts on"Raspberry PI - implementing an affordable home automation system [Part I]

  1. Turn on and shutdown LEDS from the internet

    Good morning, At nothing, the least I can say, Thank you, Super
    the tutorial, another look, Bravo and thank you.

    A question, I do not understand exactly as and where should I install the scripts,
    my intention is to have a WEB page, In fact, not be as I get to
    working with PHP and WEB, as the example, via a PC, connected to the Internet
    anywhere to activate the different LEDs or relays, not is how to do it,
    in advance and of course, Many thanks for any help.

    Amicably Ignacio from Belgium

  2. The scripts and the single index.php you have to copy them to /var/www/rele as shown in the tutorial, each has its route.

    index.php in /var/www/rele/index.php
    apaga.sh, enciende.sh and parpadea.sh in /var/www/rele/gpio/17 /

    For the time being as it works for a relay connected to the GPIO 17, in a few days I will end with the second part to operate the eight outputs.

    To access from the internet you should configure your router to forward incoming requests by the port 80 to your PI Raspberry. From the Virtual Server of the router or NAT option you can configure it.

    Finally, to access more easily you can sign up for NoIP and get a free domain associated with your public ip of the type casa.no-ip.biz.

    PD: Make sure you protect that page very well only you have access to it.

    Thank you very much for commenting.

  3. Good afternoon,

    Everything works perfectly, Although only by lan or with Xming via ssh, Unable to connect to remote, ' with another PC via the Internet, I have noip and dyndns, and I can not run, If you have any idea I thank it you in advance,

    A strong greeting and a see more, Thank you


    • I'm glad that it finally works, but to connect from the Internet which protocols are you using?

      You have to open the ports of the services you want to use on the router that gives you access to the Internet (HTTP port 80, HTTPS port 443, SSH port 22, etc) and do Port Forwarding to the private IP of Raspberry IP router that gives you access to the Internet.

      Finally now you will only have to start the connection from another network facing the public IP of your router, You can try from your mobile phone 3 G.

      If this works, Now you can proceed to configure NoIP or Dyn for access from a name easy to remember instead of a public IP; In addition if install NoIP on Raspberry, on the website of NoIP your public IP will change automatically whenever this change for your router.

      For added security, It would be better if you configure a VPN to make connections to your home network, and channel through it all your traffic.

  4. Hello, I have a question I have a led on the gpio4, sudo echo 1 > /sys/class/gpio/gpio4/value this I turn off led, and with sudo echo 0 > /sys/class/gpio/gpio4/value, turn on I led, should be to the revez, will be making something evil?.
    place the file I connect via the web, When I press any button (turn on, turn off, or flashing) only turn off led, but does not turn on, nor the flicker.
    I appreciate the help you can give me.

    • The led of the relay is only an indicator, have you tested with an electrical appliance connected to the relay?

      It will depend on if conectáis it in normally open, or normally closed configuration scripts in python.

      • Hello, Interestingly the led works perfectly, I turn it on and turn off to pleasure, but the relay does not work, does not open or close, does not switch 🙁

    • Hello, first of all thanks a lot worse your question 🙂

      You can follow the tutorial step by step, the relay has 3 types of pins:

      – VDC-> power (PIN 5V RPI)
      – GND-> GND RPI Pin
      – IN-> are entries, and depending on whether your State is HIGH or LOW the relay will turn on or turn off.

      Example use IN8, You should only change it IN0 or your relay IN1 and as I mentioned before follow the tutorial 🙂

      PD: Asegurate of sean optoacoplados, Los de la photo it son.

      Best regards, I hope too ayudado.

  5. I did everything recommended as above, When I write the commands in terminal, works, However, If I can get the file enciende.sh and send run the terminal doesn't work and can't even see index.php page

  6. First and foremost, Thank you for your explanations, very good.

    I have followed your instructions and I can catch a fish tank distance air pump.

    The problem I have is missing I can verify the status of the pin (True or False)

    How I can do to bring me this data on the same page I?

    Is Navy following your instructions, turns on and off the relay properly.

    • Hi Teodoro,

      This comment is something that many readers ask, by what I am preparing a next part of the tutorial where we will schedule an indicator of status of the GPIO pins in my spare time free.

      Once it is finished I will publish it on the Blog.

      see you soon.

  7. Before anything thank you so much for your blog.. is spectacular. For my part I have bought everything you need, I have followed all your tutorials, but when you run the plate of 8 relays, something goes wrong I.

    The question is as is if I have the plate octoacoplada?? It seems that not reaches you sufficient current to activate the. My connections are:

    5v-> VCC (plate)
    GND (P3) -> GND (plate)
    GPIO4-> IN4 (plate)

    When active the gpio with script, the Red control led is turned on a momoento, but does not stay on. I understand that it is because the gpio is activated to 3,3 volts and the relays are 5V. Swear however have the same plate so you have to ahber a way to make it work not?

    Apreciaria any type of comment, the relay Board is as follows
    (8 relays Module srd-05VDC-SL-C)

    Thank you very much for everything

  8. Hello Loty,

    Most of the plates of supported relays that are marketed for microcontrollers come already opto-coupled, You can check if it is in the specifications or looking for the optocouplers in the plate; which refers to the number of series you've mentioned if that thing is.

    Effectively, these plates are powered with 5V, but the output can be activated also by 3 actuators, 3V as the Raspberry IP GPIO.

    Anyway, the problem seems to be lack of power, so replace the transformer of the Raspberry with a 2A or feed externally the relay could help.

    Greetings and see you soon.

    • Hello!
      Thank you for your prompt response. I've done tests directly on the relay Board (removing the PI RASPBERRY), to see if it was that the plate had evil. I've got an external power supply of 5V at 2A.

      I connect the GND unless the VDC feeder to the + the feeder and the IN4 also to the +. and there.. WORKS!! the relay click and the LED indicator stays on, I've done the test with a 220v lamp and all still working. The problem arises with the RASPBERRY PI.

      I have connected the plate feeder external “+” the VDC “-” the GND and the IN4 I have connected the RASPBERRY gpio4, and there when I activate the GPIO, nothing happens. I have measured the GPIO and gives 3,3 v. I have read on other sites and it is said that it is because I have to connect the jumper VDC-JCC of otherwise. Not be that I do wrong. It may be that I turn the gpio through BASH?? It is necessary to make it work on the relay Board use the GPIO python library??

      Thank you very much for any contribution

  9. Wave, I was having a problem to erase the light with relay, using the code in this tutorial, could light normal, but to delete not erased and the relay was armed, only disarming when I unplugged the cable from the relay the raspberry. After several attempts I managed to do delete this tutorial with codes by the tutorial code led to delete. getting this form a part of the code: If ($_ POST[apagar17]) {
    $to- exec(“sudo python/var/www/AcenderWebRele/apaga3.py”);
    $to- exec(“sudo echo 17 > /sys/class/gpio/export”);
    $to- exec(“sudo echo 0 > /gpio/sys/class/gpio17/value”);
    $to- exec(“sudo echo 17 > /sys/class/gpio/unexport”);
    echo $a;

  10. Buenas tardes from Mexico, MI nombre es Enrique, I'm interested in part to implement domotica con raspberry are derived from un proyecto escolar, in the tutorials you mention that we must have linux for which I do not know much, It is possible to develop the project, both the website and the necessary programming in the raspberry with windows?, the web server that you mention must be installed in the same raspberry or where?, If the number of devices that I want to control exceeded the capacity of a raspberry 3 normal to ocuparia do, other raspberry or there is something that will increase me the capacity?, If you deal with another raspberry as would both with the same web application?.

    I hope, and you can help me with this.

    Best regards.

  11. Hi, how are you? It is really very interesting and useful entries in your blog. I am starting a project similar to this and I have a query:

    Materials that I have:

    – Raspberry pi 3
    – Powerbank tplink
    – current transformer of raspberry
    – Relay module (equal to yours from 8 channels)

    The idea is to put a power inlet and that where there is electridicad run the raspberry and feed channels of the module relay for turn on / turn off equipment, but, When there is electricity no (power outage) the raspberry (without relay module) keep running with the powerbank (until battery but as the probe and hold a 6 hours).

    This is the powerbank:

    From already thank you very much for your help!!!

  12. Nice day, first of all sorry to revive an old issue, I have a question…. I intend to activate 4 different bulbs in my house for a private Studio of photography, My idea was to do it from a local website, using a touch of raspberry screen to interact… I recommend

Leave a Reply

Your email address will not be published. Required fields are marked *