Introduction: Raspberry Pi With 4 Relay Module ( for Home Automation )

Picture of Raspberry Pi With 4 Relay Module ( for Home Automation )

Firstly we will connect the raspberry and the 4 array module and test it locally. The next step is to make a web app that will control our module . We will make it with apache server , php , javascript , jquery , bootstrap , bootstrap- switch buttons and some python . Everything is explained with a comments when you open the file , you also can see the documentation in github. The files are uploaded here and can be also find in github .

Step 1: What You Need

Picture of What You Need

We need :

  1. Raspberry Pi
  2. 4 Array Relay Module
  3. 6 Wires
  4. Wifi Dongle
  5. Filezilla (optional)
  6. SHH (optional)

Step 2: Wiring

Picture of Wiring

We need to connect our Raspberry and the module .We will get the 5V from the Raspberry (not very recommended but its easy ) .So lets begin :

PI GPIO | Relay module

Pin 4 ---------VCC

Pin 6 ---------GND

Pin 8 ----------IN4

Pin 10 ---------IN3

Pin 11 ---------IN2

Pin 12 ---------IN1

Step 3: Testing

Picture of Testing

Now we need to start the raspberry and to connect to the power supply and connect to it over FTP with Filezilla or start it on monitor . Next download and put the test_relay.py file in the main dir (home/pi).Then start the python script by typing :

sudo python test_relay.py

If leds start flashing from IN1 to IN4 and shutdown from IN4 to IN1 then everything is okey and the wiring is like currect .We can move to the next step .

Explanation

The relay boards are powered by 5v and the relays are triggered by taking the input pin to ground. Since the Raspberry Pi's GPIO pins output 3.3v I used a set of transistors to take the relay pins to ground.

So when you are setting the GPIO output value:

1) 0 the relay starts ;

2) 1 the relay stops .

Step 4: Installing Apache and Php

We will need them for the server part .Helpfull link.

To install apache2 we need to run :

sudo apt-get install apache2 -y

To test the webserver we can you on the raspberry browser

http://localhost/
or
http://192.168.1.10
(raspberry ip address) .Type
ifconfig
to get the current IP .

Next install PHP5 module for APACHE :

sudo apt-get install php5 libapache2-mod-php5 -y

And remove index.html file :

sudo rm index.html

The next thing is to navigate to the default directory:

  • For Raspbian Wheezy is /var/www ,
  • For Rspbian Jessie is /var/www/html

So we type :

cd /var/www/html<br>ls -al

And this will output :

total 12<br>drwxr-xr-x  2 root root 4096 Jan  8 01:29 .<br>drwxr-xr-x 12 root root 4096 Jan  8 01:28 ..<br>-rw-r--r--  1 root root  177 Jan  8 01:29 index.html

We need to change the permission if we want to upload or modify files :

sudo chown pi: index.html

Delete the index.html file from the dir.

Step 5: Uploading the Needed Files for Your Web App

Picture of Uploading the Needed Files for Your Web App

You need to download the .rar file , extract it and copy the files with Filezilla to the main dir of Raspberry home/pi and to the var/www. For pasting the files into var/www you need to set a permision .

at var/www<br>bootstrap.css<br>bootstrap-switch.css<br>bootstrap-switch.js<br>changeState.php<br>firstCheck.php<br>index.html<br>jquery.js
at home/pi
firstCheck.py
relay_off.py
relay_on.py

When you are done coping the files , just go to your browser , type RaspberryIP/index.html (ex: 192.168.1.23/index.html) and the webpage will be opened .And also you can test it and remotely control the relays from any device that is connected to your local network(router) .

Step 6: How It Works

Picture of How It Works

I will explain it shortly. If you want to get to more details you can see the project in github.

When we click on the button , the ajax request has been called and the php file from it is called . From the php we execute a python script file with some params . The python script return us json converted array , which we pass to the php and the php passes it to html file .We can go only in the one way but if we want to reload the page we need in what position the relays are . The other point is if we dont do it that way there is no feedback and we cant see what is happening .

Please share and write a comment bellow if it was usefull .

If you like my tutorial please donate.

To the next time .

Comments

TiếnN27 (author)2017-11-12

Hello!
Can you help me?
I can not install "sudo apt-get install php5 libapache2-mod-php5 -y" on Raspberry Pi 2 run os "2017-09-07-raspbian-stretch".
I replaced "sudo apt-get install php" then PHP 7.0 @@.
After, i run file .py on php but exec not working.
Please help me!

MSargent99 (author)2017-06-17

I too have tried this and have the same problem as many others.

I went into the php and python code and put debug statements between practically every line until I found the problem (I think ???). However, I have not been able to work out a solution.

The problem seems to center on the line in all the Python scripts:

GPIO.setup(pinNum,GPIO.OUT)

When this line is encountered in any of the three Python scripts, execution stops. Or at least output to my log files stops.

I have also included the pinNum in the output of my debug and it is getting the correct pin number (14, 15, 17, 18).

Of note, if I run the Python script directly it works fine - for example, if I go to the /var/www/html directory where the php code is and I enter the same command line with the path where I put the python scripts all three run as expected. However, when the python is called from the php code - not. I still get the expected output in my debug file - just all stops when it hits the GPIO.setup... line shown above.

Any ideas?

This looks like a great project and perfect for some of my home automation projects.

Now if I can just get it to work .)

Mike

MSargent99 (author)MSargent992017-06-20

Success!

Finally found a solution. Not the best (nor secure) by far, but it is working for now.

The solution I found that worked was in this post:

https://stackoverflow.com/questions/27864804/raspb...

Basically involved changing how php called the py code - using the php shellexec and entering the www-data entry into the /etc/sudoers file. I undid the chmod +s and file ownership back to the original settings on the python modules. And it still works so now to move on and do my project - randomly turning lights on and off in the house from the office when I am working mid-night shifts and the wife is home alone sleeping ;}

kostasp27 (author)2016-12-09

Hey there, I followed everything step by step but when I go to my website and press the buttons nothing happens. Relay works perfectly with the test_relay program. Any solution? :D

kasadawa (author)2016-09-24

When you try the test_relay.py file it only Turns On the relays ? Or in some other case it stays in the On position ?

CarlosM175 (author)2016-08-12

no way to access the RPI by smartphone via network 4g starting work?

BAC6 (author)CarlosM1752016-08-15

Yes you can by setting port forwarding in your home router (googling port forward for more details)

CarlosM175 (author)2016-08-12

existe forma de aceder ao rpi por smartphone via rede 4g apartir do trabalho?

mathias2803 (author)2016-07-27

I had to change the HW, now all is running. I had to add before the Relays an extra circut or I have to connect the relays to the 3,3V what might be at 8 LEDs too low and risky. So I used this circut:

http://ras-pi.de/wp-content/uploads/2014/07/Relais...

Thanks for all who support me.

Mathias

mathias2803 (author)2016-07-27

Hey, it is running now. I have changed the power supply from 5V to 3,3 V so that the output of the GPIO and the power supply have the same voltage. Then it is running.

I will check now what circuit would be a good to add in advance to make it save running.

Any advice? Thanks a lot, Mathias

kasadawa (author)2016-07-14

When you run the test_relay.py , what do you see on your console ? Does the program prints "Pin x is working" ?

mathias2803 (author)kasadawa2016-07-21

Yes, I get this message through all the pins, but they are not switched on and off correct.

Turned on at initilisation (GPIO.setup(pins, GPIO.OUT))
and turned off at the end (GPIO.cleanup())

It looks like a driver problem. Maybe I have to install something before or have to do some one time settings in any config file.

As I told you I have another Raspberry and on this one (Raspberry 2) all is working fine. But on the new one (Raspberry 3) it is suddenly not running any more.

Any ideas from anybody? Thanks a lot to all the ideas and hints to try.

kasadawa (author)mathias28032016-07-22

So if that thing are working you are currently installed python GPIO library .

Type in console :

~: python

>> import time

>> time.sleep(1)

And tell me does the shell sleeps for 1 second or not ?

mathias2803 (author)kasadawa2016-07-23

Yes, it is seeping 1 second. So the program with all steps is running few seconds and at each step it gives new screen output. But during this the relais are not changing.
Thanks a lot

kasadawa (author)mathias28032016-07-25

check from the python console if

>> GPIO.output(pin, 1)

>> GPIO.output(pin, 0)

is working . Replace the pin with 17,15,14, or 18 .

mathias2803 (author)kasadawa2016-07-26

Today I also tried to get the GPIO direclty controlled by console:

  1. export the GPIO:

    sudo echo 16 >/sys/class/gpio/export

  2. Change the GPIO to OutPut:

    cd /sys/class/gpio/gpio16

    sudo nano direction

    (Change the value from "in" into "out" and save it)

    ==> GPIO status is changing directly to active 3,3V and the LED turns on.

  3. Change the GPIO status by changing the Value:

    sudo nano value

    (Modify from "0" to "1" or from "1" to "0" an save the file)

    ==> The stauts of the GPIO is not changing and the LED is constantly turned on. It is not possible to turn it off.

mathias2803 (author)kasadawa2016-07-26

If I enter "GPIO.output(16, 1)" in the python console without any import or anything else before I get following error message:

"name 'GPIO' is not defined"

But in the program showed before it is running after the import command.

mathias2803 (author)kasadawa2016-07-26

I have changed your program to make better analyses as following:

#START

import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
pins = [26,16,4,21]
GPIO.setup(pins, GPIO.OUT)
print("GPIO.output:pin, GPIO.HIGH")
for pin in pins :
GPIO.output(pin, GPIO.HIGH)
print(str(pin) + " set output")
time.sleep(1)
print("setting the GPIO to LOW or 0 or false for pin: " + str(pin))
GPIO.output(pin, GPIO.LOW)
time.sleep(1)
print("Checking if the current relay is running and printing it")
if not GPIO.input(pin) :
print("Pin "+str(pin)+" is working" )
time.sleep(1)
print("setting the GPIO to HIGH or 1: " + str(pin))
GPIO.output(pin, GPIO.HIGH)
time.sleep(1)
#cleaning all GPIO's
print "Shutdown All relays in 2 second"
time.sleep(2)
GPIO.cleanup()
print "Shutdown All relays"
time.sleep(2)
print "End"

# END

When I run this program on my Raspberry 3 at "GPIO.setup(pins, GPIO.OUT)" all 4 relays are turned on at once. Then nothing is happening in between even if they should change status from LOW to HIGH but no switching of Relays. The output for all pins is "Pin xx is working".
At "GPIO.cleanup()" all Relays are turned off together at once.

So as far as I understand the pins are correct because all LEDs are turned on correct. The time.sleep is working also.

Any other ideas what can be the problem? Do I need to install any drivers or anything to make it correct running.

Thanks a lot for your support!

mathias2803 (author)2016-07-14

Hi, I have been done this on my Raspberry 2B and it was running. Now I got a new Raspberrry 3 and after I get it running I also tried the test_relay.py.
The correct Relais was turned all at once turned on at the beginning and at the end all together off again. I was not able to turn off and on single Relais.
Do I need to install any drivers or other tools or what did I do wrong? I hope someone can help me. Thanks a lot!

DIY Hacks and How Tos (author)2016-07-05

This is a great way to automate things around the house.

About This Instructable

17,345views

209favorites

License:

More by kasadawa:Cheap, Effective and Modern Home AutomationRaspberry Pi with 4 Relay Module ( for home automation )
Add instructable to: