Introduction: Raspberry Pi GPIO Home Automation

Picture of Raspberry Pi GPIO Home Automation

I had heard about GPIO pins on the raspberry pi and decided to do something with it. And with the holidays coming, wouldn't it be great to be able to turn on and off your light display from the web!  This is a pretty simple project that creates a web app for controlling lights.  It uses the Raspberry Pi for the internet connection and a wireless remote control to turn on and off the lights. The tricky thing is to connect the Raspberry Pi GPIOs to emulate pressing the buttons on the remote control.

Step 1: Parts

Picture of Parts

Parts needed:
• Raspberry Pi
• Remote controlled outlets
• Ribbon cable
• Solder

(Not shown)
• PCB board
• Resisters

Tools
• Soldering gun
• Dremel/Hacksaw
• Screwdriver
• Wire cutters
• Wire striper
• Multimeter

Step 2: Web Server Setup

Picture of Web Server Setup

I choose the Web2Py framework for it’s ease of installation and use.  Installation is very simple.  Follow the instructions (repeated here) at http://web2py.com/books/default/chapter/29/13#One-step-production-deployment

Connect your R-PI to the internet. Logon to your R-PI and enter the following at the command line:

wget http://web2py.googlecode.com/hg/scripts/setup-web2py-ubuntu.sh
chmod +x setup-web2py-ubuntu.sh
sudo ./setup-web2py-ubuntu.sh


Follow the instructions on the screen.  This step will take several minutes and you may see some errors in the process, but it seems to work anyway.  The mail configuration is not really important, you can just set it as “no configuration”.  When asked about your location and such, the values aren't critical, do your best.  The most important thing is to remember the admin password that you select during the installation

Once the installation is complete you should be able to connect to web2py server from your computer.  In your computer's browser enter the address to your Raspberry Pi.  It is very important that you used https when connecting to the web2py server.  Your URL will look something like:

https://192.168.2.179/  or  https://rasberrypi


You may see a security warning in your browser.  That's OK,  you can safely ignore this warning.  If everything worked correctly, you should now see the welcome page for Web2Py.


Step 3: Make the Board

Picture of Make the Board

Probably, the hardest part of this project is to figure out how your remote control works. 

As Dave Jones says, "don't turn it on, take it apart!".  Open up the case on your remote control.  I hope your's is as easy as mine,  it just had 3 screws.  Look carefully at the board on your controller and find where the buttons are, most likely they are traces on the PCB activated by rubber button.  Follow the traces connected to the button,  there should be 2 connections to each button.  One side of the button will be connected to a chip on the board (it will either be a black rectangle with legs, or a black plastic blob), the other end will be connected through a resistor  to either the battery + or the battery -.

In my case, the buttons were connected to the + of the battery.  This means that the button "pulls up" the micrcontroller line to the supply voltage when it is pressed.  To emulate the press of a button, we will connect the GPIO of the Raspberry pi through a resistor to the of the button that is connected to the chip on the board (rectangle or blob).  

The first schematic shows how the buttons were connected on my remote.  The second schematic show how to inject the signal from the Raspberry Pi.  

I created a board to make this as small as possible and be able to add the extra hardware into the case for the remote.  The layout for the board is included.  I used surface mount resistors because I had some, also the values aren't that important, 330 Ohm - 1K Ohms would be good choices.  If you have different values and through hole resistors, that's fine,  it will work fine. Also, creating a custom board for this is not required, you can certainly solder resistors directly to the remote's board. (good luck :))

Step 4: Connect to the Controller

Picture of Connect to the Controller

So for connecting to the controller all you had to do is find the pads in the back of the controller that connect to the microcontroller. I had some wire hanging around so I soldered the pads to the board and the ribbon cable with the right pins corresponding to the GPIO pins on the Rasp Pi. I had to  use my multimeter find the right pins.

Use your dremel or file to make a slot in the back of the remote case so that the ribbon cable comes neatly out the back.

Step 5: Connect to the Pi

Picture of Connect to the Pi

All I had to do to connect to the Pi was put in the ribbon cable and since I had to cut my ribbon cable because a pin was solid and blocked off. I just had to put the ribbon cable all the way to the top.

Step 6: Install App

Picture of Install App

Now that the hardware is setup, the app that controls the lights needs to be installed.  The attached file is source code for the application,  you need to install  into Web2Py.

1. Download the file, and unzip the file.
2. From your web browser (on your pc) connect to the Web2Py server using https.  https://raspberrypi/
3. Select Administrative Interface
4. Enter the password that you set when Web2Py was installed
5. Under "Upload and Install Packed Application" enter the application name of "Home_Lights" and select a the "web2py.app.Home_Lights.w2p"
6. Press install.
7. After the application installs you will see the application added to the list.

The application requires you to login to the application,  you don't want random people turning on and off you lights!  So we must create the users.

1. Under the Home_Lights application press edit.
2.Under Model, press "database administration"
3. Then press the "New Record" button next to db.auth_user
5.Enter First Name, Last Name, Email, and password.  All other entries can be ignored.  When done press "submit"

You can now navigate to the Home Lights application:  http://raspberrypi/Home_Lights and login with your email and password.






Step 7: Setup GPIO Server

Picture of Setup GPIO Server

At this point your application still won't work because access to the GPIOs on your pi needs to be set up.  Access to the GPIOs on the Raspberry Pi requires root access.  It isn't wise to run your web server under root, so a separate server was implemented to provide access to the GPIOs.  

Logon to your Raspberry Pi (ssh or at the command line, it doesn't matter) and install 1 python module:

wget http://pypi.python.org/packages/source/R/RPi.GPIO/RPi.GPIO-0.4.2a.tar.gz
tar zxvf RPi.GPIO-0.4.2a.tar.gz
cd RPi.GPIO-0.4.2a
sudo python setup.py install


With that module installed, the server can be started (must be running for the web application to work)

sudo python /home/www-data/web2py/applications/Home_Lights/modules/GPIOServer.py

Step 8: Home Automation

Picture of Home Automation

You should now be able to control your lights from any web browser,  Your computer, a smart phone, or even the PI itself.  

Comments

VisionMissionGo (author)2014-12-31

If I wanted to upscale this to more than just three switches, how would I do that?

make your own mobile app

RathnamG (author)2016-07-30

Hi, Thanks for the great manual. I have a little problem, hope you can help me.

I am getting the following error message: "RPi.GPIO.SetupException: This module can only be run on a Raspberry Pi!" when trying to run: sudo python /home/www-data/web2py/applications/Home_Lights/modules/GPIOServer.py

I have the latest raspberry pi, maybe it is not recognising it?

Thanks a lot!

AkshayC40 (author)RathnamG2017-06-17

try this command

"sudo apt-get update && sudo apt-get upgrade" and try again

sdfsdfsdfsdfsdfaaaaz (author)2015-01-14

I have a remote controler where the button "pulls down" the micrcontroller line to the ground when it is pressed (opposite to your case of pulls up).

How can I do the wiring ?

any input on this? Trying to figure this out but no luck. Any help please?!?!

I have the same situation. I am not sure whether you can configure any of the GPIO's to sink the power rather than providing it. Any help would be greatly appreciated!!

SebastianM115 (author)2017-04-25

The controller for the lights i am using is active low. It is at 12v at all times except when the button is pressed. Any ideas on how to wire this control into the RaspberryPi??

ElliotB18 (author)2017-04-10

I put into the LXTerminal "wget http://web2py.googlecode.com/hg/scripts/setup-web2py-ubuntu.shchmod +x setup-web2py-ubuntu.shsudo ./setup-web2py-ubuntu.sh" and I just get a whole load of errors that all basically say that the name or service is not found. Is my syntax in the command wrong or is it something else? Also, I'm on Raspian.

lolleri100 (author)2016-11-09

I'm confused. So What OS do I have to install to my Raspberry Pi that this will work?

LionCave (author)lolleri1002016-12-12

You will need a debian based OS. So Rapsbian

EricP6 (author)2016-09-18

I just do not see the wire connection for ALL on and off..?

AnnaMai (author)2016-08-13

Hi there, i also have a project which uses Raspberry for remote switch for light.

Please visit my blog for detail instruction

http://engineer2you.blogspot.com/2016/06/raspberry-pi-remote-light-switch.html

colmania (author)2016-08-07

Is it possible to add button with 2 seconds delay for dimming? Should be easy.

sdoup (author)2016-03-25

Does anyone has error ticket problems when the button is pressed?i got <class socket.error>[Errno 111] connection refused.any idea where is the problem?

JustinD50 (author)sdoup2016-06-15

In your hardware setup. You need to do diagnostics on your own :(

Samginoya (author)2014-08-12

hy,

wen i write this in pi terminal :

sudo python /home/www-data/web2py/applications/Home_Lights/modules/GPIOServer.py

it show me:

python: can't open file '/home/www-data/web2py/applications/Home_Lights/modules/GPIOServer.py':

[Errno 2] No such file or directory

sdoup (author)Samginoya2016-03-25

check the name that you gave when you upload and install the packed application.

damavox. (author)2016-03-04

I am using a 433mhz transceiver. Put i can not get my script to execute from web page via php. My setup up is different from yours but you seem knowledgeable so i figured i'd ask. When i sudo ./my_light_on_script.sh from the terminal it works fine but not from the web page. I have chmod 775, 777 and finally +x, the script and page but still nothing.

Have you encountered this?

As stated above it executes successfully from terminal so i know every thing is hooked up correctly

xpark (author)2015-01-05

Can u tell me exactly how i can access this server from an outside network?

Thanks

u8y7541T (author)xpark2015-11-08

Port forward the RPi, then access the IP. (Read up on port forwarding...)

rajaa5 (author)u8y7541T2015-11-22

hi..what if i use university wifi..how do i make the portforwarding if i cant access the router?

Teenage Kicks (author)rajaa52015-12-14

you could use a service like weaved,

http://www.weaved.com/

this lets you use a software only interface and requires no setup on the router

lribani (author)2013-05-13

hello, and thank you willq44 for the project, great project.
the only one problem i have is that i don't use wireless relè, but cable relè. So that when i push the web button to switch on lights, lights turn on for 1 second and immediatly switch off, the same then i push the button to turn off them.
How can i solve this problem?
many thanks
lorenzo

freakqnc (author)lribani2013-05-29

Have the same problem that Lorenzo Ribani described.

The current buttons act as a trigger which goes on and then almost immediately off. What should be modified to make each button a toggle that will behave following a pattern like: click-->ON, click again-->OFF (and so forth)

Thanks! :)

Adityar26 (author)freakqnc2015-11-16

Have you solved this? I am also doing the same to control the relay on an off as and when I want like a normal switch in homes.

freakqnc (author)2013-05-31

willq44 or anyone has any pointer on how to change the behavior of the buttons to turn them into a relay "lick-->ON, click again-->OFF" instead of having them send a pulse to simulate the push of the button on the remote as it was needed in this project (bueller... bueller... ;D)? Thanks!

Adityar26 (author)freakqnc2015-11-16

Hello friend do you found any solution for your query. I am also dealing with it.

k.r1 (author)2015-08-03

i am running raspbian. can you simply give us

setup-web2py-ubuntu.sh because i am having problem setup apache 2.2 to 2.4 . please uploade somewhere or email me kotarajeshece@gmail.com

qkarthik (author)2015-05-01

internal error was made !!!

Snellingkorey (author)2015-01-06

great idea, can you wire up the kettle and washing machine as well?

don't be silly Snellingkorey, that would take at least 2 Raspberrys and a Strawberry!

ManuelA3 (author)2014-10-14

In my case ,Installing web2py on a raspberry running raspbian, I had to modify a line on setup-web2py-ubuntu.sh

From:

sudo a2ensite default

To:

sudo a2ensite default.conf

Also, the auth types on the default.conf were on apache 2.4 format, whereas the setup script installed me apache 2.2 so I had to make changes according to http://httpd.apache.org/docs/2.4/upgrading.html#ru...

gago.babic.3 (author)ManuelA32015-01-07

i am running raspbian. can you simply give us

setup-web2py-ubuntu.sh because i am having problem setup apache 2.2 to 2.4 . please uploade somewhere or email me mejlgago@gmail.com

davidbarcomb (author)2014-11-15

Great instructable, detailed and well explained. Keep up the good work

lzkill (author)2014-10-16

Hi pals!

The resistor used on this project are dependent on the RF controller? Why were these picked?

KentR2 (author)2014-10-13

How could i set this up to run locally, that is running the program directly on the pi without internet acess?

aswinmp (author)2014-10-08

My project is home automation and CCTV. After doing the cctv function using pi, we did the automation part. Upto step 7, everything went perfectly. After that, I cannot access pi using ssh or remote desktop. And I cannot access the web page.

What to do? Please help me!

john.turley.568 (author)2014-10-04

I had trouble installing web2py but I think I got it solved by using this script instead:

web2py.googlecode.com/hg/scripts/setup-web2py-nginx-uwsgi-ubuntu.sh

and then running this at startup:

sudo uwsgi --master --die-on-term --emperor /etc/uwsgi --logto /var/log/uwsgi/uwsgi.log &

OliverL (author)2014-08-30

Hello!

I have ssl problem :Hibakód: ERR_SSL_PROTOCOL_ERROR.

I use Chrome and Explorer, the problem is in both.

I have no idea :-(

RobertCrabb1 (author)2014-08-26

Nice Job!!

Very detailed guide.

GeorgeB1 (author)2014-08-24

where do you download the home lights package

rohitgoudponnam (author)2014-08-23

Hi matey, i'm from Australia and i need this model for a demo. can you please let me know if it is possible for you to post me a working model (whole kit) and guide me to make it work.Please, respond to this message on my email "rohitgoudponnam@gmail.com" is my gmail id.

Thanks and Regards

adam.skorpik.9 (author)2014-08-15

Hi.

We already developed piece of hardware for home control. There are relays, digital inputs, 1wire, EEPROM, 0-10V.....

check indiegogo project:

http://igg.me/at/unipi/x/8241961

BennyTC (author)2014-07-30

that is a typo, delay there is 2 seconds :P

BennyTC (author)2014-07-29

Awesome project!
I intend to use it to control a door buzzer, so I added 5 second delay.

I had to change swap the output from "1" to "0", because I am using a brandless 8-relay board which operates as Active LOW (relays are turned on when output is "0").

In the hope it is of help to anyone, this is what I did:

changed the GPIOClient.py file's line 13 to this:

ON = chr(0)

and added the following lines to the GPIOServer.py file

line 3:

import time

line 12:

SleepTimeL =2

line 23: (this is after GPIO.output(pin,val))

time.sleep(SleepTimeL);

cartiman (author)2014-06-10

I use the raspberry pi from china (red color), what can?

Thanks

sadhvi (author)2014-04-21

Hi,

we are interested in doing the home lighting automation which takes care of the central lighting system. so, could you please suggest how to proceed with the project.

email-id: nangunoorilaxmijoythi1@gmail.com

Regards.

losgat (author)2014-03-22

Hi have it all setup and working as expected but is there any way to change the output for relays and not single button press applications ( using a relay that need to be powered constantly?

sahildhanda (author)2014-03-21

Traceback (most recent call last):

File "/home/www-data/web2py/applications/Home_Lights/modules/GPIOServer.py", line 29, in <module>

serversock.bind(ADDR)

File "/usr/lib/python2.7/socket.py", line 224, in meth

return getattr(self._sock,name)(*args)

socket.error: [Errno 99] Cannot assign requested address

root@raspberrypi:~/RPi.GPIO-0.4.2a#

//What should I do with this error??Please Help..:)

About This Instructable

1,497,665views

769favorites

License:

More by willq44:Raspberry Pi GPIO home automationRaspberry Pi Case
Add instructable to: