Part I: iPhone controlled home speakers switch using the RaspberryPi and the CustardPi relay board

To get started, you need to have apache2, php and mysql loaded on your raspberry pi as we will be using web access and web services and a small database to store configuration data to support multiple iPhones controlling the speaker switch.
Then you need to get familiar with the Custard Pi relay board and install all the support software on your Raspberry Pi to be able to control the Custard Pi via Python. Here is a good explanation on how to get all this done http://www.slideshare.net/SeggySegaran/custard-pi-6i2c
The scripts that turn the relays on and off work in a very simple way. The 8 relays on the Custard Pi board are grouped in pairs to support stereo setups. Relays 0-1 are switch 1, relays 2-3 are switch 2, and so on. If you want to turn switch 1 on, the scripts will ensure all relays are turned off and then relays 0-1 are turned on, so switch 1 is on. The same logic is applied to the other relays.
Here is the python code to turn switch 1 on (relays 0-1). The Python files are located in the main directory that you choose under a directory named CustardPy, filename is switch1.py:

#1/usr/bin/env/python
import RPi.GPIO as GPIO
import time
import cpi6
GPIO.setmode(GPIO.BOARD)
#start program
board1=cpi6.add1
cpi6.setasoutput(board1)
cpi6.setbit(board1,cpi6.ONrelay0)
cpi6.setbit(board1,cpi6.ONrelay1)
cpi6.clrbit(board1,cpi6.OFFrelay2)
cpi6.clrbit(board1,cpi6.OFFrelay3)
cpi6.clrbit(board1,cpi6.OFFrelay4)
cpi6.clrbit(board1,cpi6.OFFrelay5)
cpi6.clrbit(board1,cpi6.OFFrelay6)
cpi6.clrbit(board1,cpi6.OFFrelay7)
GPIO.cleanup()
import sys
sys.exit()

and here is the php code to call the python script, filename is switch1.php and should be placed in the main directory:


exec (“sudo python CustardPi/switch12345.py”)

You will call the php file from your browser or a networked app (iOS for example) to turn on the speaker switch.
The code for the other combinations is :

To turn on switches 1 and 2
#1/usr/bin/env/python
import RPi.GPIO as GPIO
import time
import cpi6
GPIO.setmode(GPIO.BOARD)
#start program
board1=cpi6.add1
cpi6.setasoutput(board1)
cpi6.setbit(board1,cpi6.ONrelay0)
cpi6.setbit(board1,cpi6.ONrelay1)
cpi6.setbit(board1,cpi6.ONrelay2)
cpi6.setbit(board1,cpi6.ONrelay3)
cpi6.clrbit(board1,cpi6.OFFrelay4)
cpi6.clrbit(board1,cpi6.OFFrelay5)
cpi6.clrbit(board1,cpi6.OFFrelay6)
cpi6.clrbit(board1,cpi6.OFFrelay7)
GPIO.cleanup()
import sys
sys.exit()

To turn on switches 1 and 3:
#1/usr/bin/env/python
import RPi.GPIO as GPIO
import time
import cpi6
GPIO.setmode(GPIO.BOARD)
#start program
board1=cpi6.add1
cpi6.setasoutput(board1)
cpi6.setbit(board1,cpi6.ONrelay0)
cpi6.setbit(board1,cpi6.ONrelay1)
cpi6.clrbit(board1,cpi6.OFFrelay2)
cpi6.clrbit(board1,cpi6.OFFrelay3)
cpi6.setbit(board1,cpi6.ONrelay4)
cpi6.setbit(board1,cpi6.ONrelay5)
cpi6.clrbit(board1,cpi6.OFFrelay6)
cpi6.clrbit(board1,cpi6.OFFrelay7)
GPIO.cleanup()
import sys
sys.exit()

and so on and so forth, you get the gist.

The iPhone code to control the switches can be accessed here.

Advertisements

About hbouzas

Born in Buenos Aires, Argentina. Studied Physics at the University of Buenos Aires. Joined Schlumberger in February 1985 in Houston, Texas, and worked in several technical and managerial positions until 2000. From 2000 until 2008 held several management positions in Abingdon, UK; Calgary, Alberta and London, UK. Worked in the areas of Geophysical Exploration, Geological Modeling, Structural Modeling, Reservoir Modeling and Petroleum Economics and holds several patents. He is currently the Norway Technology Center Manager for Schlumberger Information Solutions and is based in Oslo and Stavanger. Main interest are software, technology, innovation, 3D visualization, design, human computer interaction, energy, environment.
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s