Switching Relays With Android & Raspberry Pi

Redirect to new PiRelay Website


PiRelay is an Android app  to Switch or Pulse the GPIO Pins of a Raspberry Pi
connected to Relays. To use this app you will need to setup your Raspberry Pi and Relays which the following guide will walk you through. The relays can be connected to switch an electrical circuit, perfect for controlling things like Lights, Fans, Motors, Gates, Doors, Heating, Air Conditioning or anything else you might want to control.

Download PiRelay for Android from the Google Play Store here: PiRelay

If you have any Feedback for me, Issues with the app or Questions, Please ask in my thread on the Raspberry Pi forum: http://www.raspberrypi.org/phpBB3/viewtopic.php?f=37&t=60008

UPDATE: 15/01/2017 Added an updated SD Card Image to download with Raspbian Jessie Lite (With Support for 2GB SD Cards and larger). (Click Here to download the Raspbian (Lite Version - No GUI) SD Card Image (462MB))
UPDATE: 28/01/2017 Published PiRelay Version 2.0 - With a huge range of new features. NOTE: if you are upgrading from the original PiRelay Pro app then you will need to either update your GPIO.php to the new version described below or install the new SD card image otherwise you will only be able to refresh the first 8 WiringPi Pins from the app.
UPDATE: 18/02/2017 Added a full Raspbian SD Card Image (8GB SD Card or ligger required). (Click Here to download the Raspbian (Full Version - With GUI) SD Card Image (2.4GB))

Features in PiRelay v2.0
- Control up to 100 Relays
- Use up to 5 Raspberry Pi's
- Ability to send a pulse signal (i.e. Switch relay on then immediately off)
- Swipe down to refresh relay status
- Ability to assign Icons to relays
- Alternative On/Off Icons
- Refresh relay status
- Screen rotation
- Pi Header Diagrams for Rev1 (P1) & Rev2 (J8) boards
- Ability to change the Title Bar Name
- Ability to set the Wiring Pi Pin # used by each relay
- Ad Supported Free App with optional "In App Purchase" to remove ads

Disclaimer: These instructions are provided as is for information purposes only, and the author Jason Findlay cannot accept any liability for and injury or damage caused by following these instructions. Whilst these instructions can be considered safe to follow, you proceed at your own risk.

Video of Pi Relay in Action (New video of PiRelay v2 Coming Soon)

YouTube Video



 

Install Software On Raspberry Pi
There are 2 methods of installing the software to use Pi Relay. 

Method 1. which is simple, and for people who either need it up and running quickly, and/or don't have experience running Linux commands manually.
Or
Method 2. which is the manual install for people who prefer to know/learn how to configure the software, or to customise the installation.

Method 1.
Install to SD Card From A Pre Installed Image (Quick & Easy)

1.  Download Pi Relay app from the Play Store here: PiRelay App Download

2.  Download one of the Pi Relay SD Card Images here:

Lite Raspbian Version (No GUI - 2GB SD Card Minimum): PiRelay Raspbian (Lite) SD Card Image Download (462MB)
or
Full Raspbian Version (With GUI - 8GB SD Card Minimum): PiRelay Raspbian (Full) SD Card Image Download (2.4GB)
I have put together a Raspbian Jessie Lite SD Card image based on a 2GB SD card and an alternative full Raspbian Jessie SD Card Image based on an 8GB SD card with all the components of Pi Relay already installed. You simply need to download your chosen version of the zip files above and extract the image, then restore it to your SD card:
NOTE: Not all 2GB SD Cards and 8GB SD Cards are made with the same available space, therefore if you find you are unable to restore the image to your SD Card I recommend that you try using an SD Card with a larger capacity than 2GB (Lite) / 8GB (Full) to ensure that this image fits onto it.

Updated Disk Image: 18/02/2017
Raspian Jessie Lite - Now supports SD Cards from 2GB and larger
or
Raspbian Jessie Full (8GB SD Card Required for full Raspbian)
Download Full Version: raspbian-jessie-lite-pirelay.zip (2.4GB)

Then use an SD Card Image Writer such as: http://sourceforge.net/projects/win32diskimager/files/latest/download to write the extracted image to your SD card (8GB and above recommended).

Then just put the SD card into your Raspberry Pi and power it up.

That's it... your Pi is now ready to use with PiRelay App to start switching or pulsing stats of your GPIO pins.

If you've successfully used this method, then you can now skip to the Hardware Configuration section of this guide.

NOTE: Method 1. will erase all existing data from the SD card.

Method 2.
Manual Software Install / Configuration
Setting Up Your Android Device
1.  Download Pi Relay app from the Play Store here: PiRelay App Download

NOTE: In the app settings you can define a custom Server URL. This is the network path to your Raspberry Pi. The default path is "http://raspberrypi/", however you can define an IP address or a custom URL. If you want to use PiRelay using Mobile Data then you will need to configure your own port forwarding on your router (By default PiRelay uses Apache2 on port 80).

2, Setting Up The Raspberry Pi
To install the required software on the Raspberry Pi you need to run the following commands at the Linux command line interface. To make it quicker and easier I use Putty on windows to SSH straight onto the Pi, but that's not required, you could just type in these commands directly into the command line on the Raspberry Pi. 

Raspberry Pi
You should be able to run these commands with any Debian based Linux distribution, however I would recommend using the latest version of Raspbian (Currently the latest version is Jessie) from http://www.raspberrypi.org/downloads.
NOTE: Your Raspberry Pi needs to be connected to your network either via WiFi or an Ethernet Cable for Pi Relay to be able to access the RaspberryPi.

Commands to Install: Wiring Pi
sudo apt-get install git-core

sudo apt-get update

sudo apt-get upgrade -y

git clone git://git.drogon.net/wiringPi

cd wiringPi

git pull origin

./build

Commands to Install: Apache & PHP
sudo apt-get install apache2 -y

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

Create the PHP Script
NOTE: Updated 07/01/2017 to show current status of relays after setting state of all 32 wiring pi pins (i.e. Pins 0 to 31).

Use the following command to launch the Nano File Editor and generate the gpio.php file:

sudo nano /var/www/html/gpio.php

Then copy the following into the Nano Editor:

<!DOCTYPE html>
    <html>
      <body>
         <?php
           $output = shell_exec('gpio mode '.$_GET['pin'].' out');
           $output = shell_exec('gpio write '.$_GET['pin'].' '.$_GET['status']);

           for ($i = 0; $i < 32; $i++){
             $output = shell_exec("gpio read ".$i);
             echo "Relay ".$i.":".$output."<br>";
           }
         ?>
      </body>
    </html>




To exit the Nano Editor press Ctrl+X together. When it asks if you want to save the file choose "Yes"!

That's it... your Pi is now ready to use with PiRelay App to start switching or pulsing stats of your GPIO pins.

Hardware Configuration
The Relays
You need to source some 5v relays that will work with the Raspberry Pi. The simplest solution is to purchase a 5v Relay Module Shield such as this one: eBay: 5V-1-2-4-8-Channel-Relay-Board-Module

This is the board you can see in my You Tube Video above and works very well. I am using the 8 channel relay version which is great value. It consists of 8x 250v 10Amp Relays, perfectly capable of running directly from the Pi. (Note that if all 8 relays are engaged at the same time, you will be drawing around 600mA of power from the Pi, so a 2 amp or above power supply is recommended if powering it directly from the Pi, alternately you might want to provide power to the board with an additional 5v power source)

The Connector
To connect the Raspberry Pi to the Relay Module you will need to connect wires from the GPOI pins on the Raspberry Pi to the pins on Relay Module. You can do this by any means you like, but I found using a DIY Ribbon cable with connectors the simplest. The one I used in the above video is: eBay: 5V-1-2-4-8-Channel-Relay-Board-Module.

When you buy this you need to tell the seller which connectors you want. I opted for the 1P connectors which allows you to connect each pin on the Relay Module individually.

The first 8 relays are set to default to WiringPi GPIO Pins (GPIO 0 to 7) for you which you can change in the app settings. The other 2 pins/wires shown in the images below provide a positive (Brown Wire) and negative (Red Wire) power connection to the Relay Module directly from the Pi.

If you have completed the installation and wiring of your Pi to your relay board you should now be able to switch relays with your PiRelay App.

Wiring Pi Pin Numbers with GPIO Header Details
In the diagram the GPIO# is the Wiring Pi Pin No referenced in Pi Relay 2.0 App



Pictures

Top View of Pi GPIO Connector

Bottom View of Pi GPIO Connector

View of 1P Relay Module Connectors

Connector Attached to Raspberry Pi

© 2013 JasonFindlay.com