loading

Control relays connected to you RaspberryPi using Android app. Setup multiple RaspberryPi devices around your house. Single device can operate up to 8 relays and all of them are self-discoverable using lightweight network protocol. No complicated setup. Name each relay, give its type (button, switch) and operate it from a dead simple app interface.

For ex. you can connect one of your RaspberryPi to a garage door and furnace blower, and another device to a house ceiling fan and be able to control them using Android app when you are on your home network with a click of a button.

Step 1: Install Android App

Install RaspberryPi Switch Android app from Google Play

When connected to home WiFi, app discovers all available switch-servers on your local network and allows you to control them. Discovery mechanism relies on broadcasting UDP messages, so your network router should not drop packets with broadcast addresses (by default broadcast messages are not dropped by consumer-grade routers unless you configured your network differently)

Step 2: Install Raspbian OS

You can find full instructions on official RaspberryPi website

Quick steps for Mac users to copy image on SD card

1.DownloadRaspberryPi image

2. Identify the disk (not partition) of your SD card. e.g. disk4 (not disk4s1)

diskutil list

3. unmount it

diskutil unmountDisk /dev/disk4

4. Copy image on your SD car (takes a very long time, to speed it up use /dev/rdisk# instead of /dev/disk#)

sudo dd bs=1m if=2012-12-16-wheezy-raspbian.img of=/dev/disk4

Default credentials that come with the image

username: pi
password: raspberry

Step 3: Install Pi4j Library

Pi4J is an API and implementation libraries for Java Applications to access the full I/O capabilities of the Raspberry Pi platform. Full instructions can be found on pi4j webiste or simply run the following command in the RaspberryPi terminal

curl -s get.pi4j.com | sudo bash

Step 4: Connect RaspberryPi to Your Local Home Network

You can connect your RaspberryPi to a home network using:

Quick tip if you are using Edimax WiFi USB adapter:

1. Replace content of the file /etc/network/interfaces (sudo vi /etc/network/interfaces) with

auto lo 

iface lo inet loopback iface eth0 inet dhcp
allow-hotplug wlan0
auto wlan0
iface wlan0 inet dhcp
   wpa-ssid "YOUR-WIFI-NETWORK-NAME-GOES-HERE”
   wpa-psk "YOUR-WIFI-NETWORK-PASSWORD-GOES-HERE"

2. Disable WiFi adapter power safe mode

sudo sh -c "echo 'options 8192cu rtw_power_mgnt=0 rtw_enusbss=0' > /etc/modprobe.d/8192cu.conf"

3. Reboot

Step 5: Install Switch-server

Switch-server is a small java app that runs on your RaspberryPi, controls its GPIOs and makes them discoverable so they can be controlled remotely.

Follow installation instructions on switch-server github page.

You can now connect relays or other gadgets to GIPOs on your raspberry pi, tell switch-server their names and pin numbers they are connected to, and control them via Android app.

You can also run switch-server locally from your laptop if you just want to play with Android App or if you make changes to the server code and want to try it first without installing on the RaspberryPi device.

Tips:

  • To start switch-service on boot add following crontab command (sudo crontab -e): "@reboot java -jar /home/pi/switch-server.jar --config /path/to/config.json"
  • If you installed Raspbian OS, you can place your switch-server jar file under /boot/ partition that is FAT32 and visible on Windows and Mac when you mount SD card. This way you can easily update server jar or config.json files from your laptop without ssh-ing to RaspberryPi.

<p>I like the architecture of this project. The way it discovers but <br>keeps separation between the IP comms and the app. I have it running on<br> an R-pi zero and a modern android. I would like to use an old smart <br>phone as a dedicated controller but it wont load from playstore. Any <br>ideas why it is incompatible. Is there an older version that might run <br>on a gingerbread phone?</p>
<p>Hi</p><p>I am pretty sure it works on old devices, it is just an artificial restriction. I do not have an old device to verify it though. You could change minSdkVersion version to match your device and build apk locally: <a href="https://github.com/sshlyk/switch-android/blob/master/app/build.gradle#L8" rel="nofollow">https://github.com/sshlyk/switch-android/blob/mast...</a></p><p>Run &quot;./gradlew assembleDebug&quot; and you can find .apk file in build directory.</p><p>If it works, create pull request, I'll merge it and publish to app store.</p>
Thank you for the suggestion<br> I will work on that.
<p>Hi,</p><p>Nice project and well written code for both Android &amp; RPI.</p><p>I have a few questions:</p><p>Have followed the steps described, both Android and RPI are running but the relays do not operate on Android key presses. I have logs enabled on both Android &amp; RPI, the RPI logs show SingleSwitch.java, turnOn() and turnOff() methods are called correctly, but the relays cannot be controlled. Physical pins 13 &amp; 17 are always low.</p><p>I have written a simpler java code that does toggle the relays correctly. I have RPI 2 B.</p><p>Can you please advice.</p><p>Thanks</p>
I am going to guess, but you are probably using wrong PIN numbers in the config. Pi4j library I am using has its own PIN number mapping witch is different from raspberry pi mapping. It enumerates them from 0 to 7. Take a look at this http://pi4j.com/usage.html#Pin_Numbering <br>Find your model and what number pi4j assigns to given gpio<br><br>I should probably make it clear in the doc. Feel free to make necessary changes to documentation on github.
<p>Hi,</p><p>I have had a look at the link you mentioned and it seems I have the correct pin numbers. Looking at this link (<a href="http://pi4j.com/pins/model-2b-rev1.html" rel="nofollow">http://pi4j.com/pins/model-2b-rev1.html</a>) and the switch-config.json file, my allocations are:</p><p>&quot;Garage Door&quot;&gt;&quot;switchPinNumber&quot;: 0&gt;GPIO_0&gt;Pin 11</p><p>&quot;House Ceiling Fan&quot;&gt;&quot;switchPinNumber&quot;: 2&gt;GPIO_2&gt;Pin 13</p><p>&quot;Furnace Blower&quot;&gt;&quot;switchPinNumber&quot;: 3&gt;GPIO_3&gt;Pin 15</p><p>These physical allocated pins (11,13,15) work with a simple test version of java code using pi4j enumerations you have described. Actually the test code uses the same code from switch-server&gt;SingleSwitch.java&gt;toPin().</p><p>Could you provide further advice.</p><p>thanks</p>
<p>Hi ,</p><p>Did you get a solution to this problem? Unfortunately, for me the server code and the android program runs fine but GPIO LEDs wont light up. They light up by default in GPIO_2 but dont turn off. Any specific pointers would be valuable.</p>
<p>The default state is ON (HIGH), that's why they light up by default. It is currently hardcoded in the source code, by you can change that to PinState.LOW <a href="https://github.com/sshlyk/switch-server/blob/master/src/main/java/com/alisa/lswitch/server/io/SingleSwitch.java#L22" rel="nofollow">https://github.com/sshlyk/switch-server/blob/maste...</a></p><p>You still should be able to control the device. Check server-side logs to see if there are incoming requests whey you use the app. If not, make sure both server and client run on the same home local network. Keep in mind, that some public routers drop broadcast packets and client wont be able to discover and send requests.</p>
<p>this is just what Ive been looking for because most other home automations required knowing quite a bit more about arduino etc. which I dont actually have but this seems to be mostly pi related and I have a couple lying around gathering dust, would be sweet to be able to put them to good use. I have the original pi B board, the 2nd model ever released and I have a pi 2 plus would either of these be suitable for this project?</p>
I am using pi B and have not tried other models. As long as you can install pi4j library it will work since server is written in java. Server code and android app are opened sourced, feel free to make changes<br>Thx
That's exactly what I need. Seems like service is up and running. <br>Next, I would verify that pi2 is actually connected to network and it is exactly same network your phone is on. Try on your pi2 shell<br><br>ping instructables.com<br><br>If it is connected, is it WiFi? Is link reliable. Try moving it closer to your router or connect via ethernet port.<br>
<p>ok, i need wlan0, not eth0...</p><p>mucho thanks</p><p>rodger</p>
<p>does this work on pi 2 ?, my android won't discover my second server, only the one on pi b.???</p><p>thanks</p><p>Rodger</p>
I do not have pi 2, so I can not test it, but it should run, since it is not tight to a specific hardware. <br>Start service manually and attach the logs. Have you installed pi4j on on pi 2?<br><br>Thx
pi@raspberrypi:/ $ sudo java -jar /home/pi/Switch-server/switch-server.jar --config /home/pi/Switch-server/config2.json<br>2016-02-18 16:00:53.594 [main] INFO com.alisa.lswitch.server.Main - Starting switch server...<br>2016-02-18 16:00:54.640 [main] INFO com.alisa.lswitch.server.Main - Found configuration file: /home/pi/Switch-server/config2.json<br>2016-02-18 16:00:55.108 [main] DEBUG com.alisa.lswitch.server.lib.AppConfig - App config: {password=helloSwitch, port=61235, devices={North gate={switchPinNumber=0, deviceType=button, buttonDelay=2500}, South gate={switchPinNumber=3, deviceType=button, buttonDelay=2500}, Lower gate={switchPinNumber=2, deviceType=button, buttonDelay=2500}}}<br>2016-02-18 16:00:55.114 [main] DEBUG com.alisa.lswitch.server.io.SingleSwitch - Raspberry switch. PinNumber: 0<br>2016-02-18 16:00:55.817 [main] DEBUG com.alisa.lswitch.server.DeviceManager - Device ID: 8a35938d-e2df-4006-8114-a2d5ccf76378<br>2016-02-18 16:00:55.820 [main] DEBUG com.alisa.lswitch.server.io.SingleSwitch - Raspberry switch. PinNumber: 3<br>2016-02-18 16:00:55.823 [main] DEBUG com.alisa.lswitch.server.DeviceManager - Device ID: 6188657a-e84d-443c-ac2b-a97ef267eda4<br>2016-02-18 16:00:55.824 [main] DEBUG com.alisa.lswitch.server.io.SingleSwitch - Raspberry switch. PinNumber: 2<br>2016-02-18 16:00:55.827 [main] DEBUG com.alisa.lswitch.server.DeviceManager - Device ID: f15583b8-7147-4728-8dd7-b12f3ca9324c<br>2016-02-18 16:00:55.842 [main] DEBUG com.alisa.lswitch.server.NetworkClient - Starting NetworkClient. Port: 61235<br>2016-02-18 16:00:55.930 [Thread-4] DEBUG com.alisa.lswitch.server.StatusRequestProcessor - StatusRequestProcessor processing incoming status requests<br>2016-02-18 16:00:55.937 [Thread-5] DEBUG com.alisa.lswitch.server.SwitchRequestProcessor - SwitchRequestProcessor processing incoming switch requests<br>not sure what logs you want??<br>thanks
Great article. Seems like as soon I initiate the swich-server, all 4 relays set to &quot;on&quot; mode. And then pressing in app doesn't work. <br><br>What am I missing?<br><br>Thank you.
<p>server is working properly...but GPIO are not accessable...</p>
<p>there is need to add url to apk file or not? plzz help</p>
Hi. Sorry, Could you clarify the question? Are you looking for compiled apk file because Google play is not available?
<p>i mean is it need to compose or add url to zip file of android using android studio or it directly works with file from google play..?</p>
App works straight from google play with the pre built server
<p>Verry Kool!, mucho gracias...</p><p>How do I write an app for iPad ?</p><p>Do you have source for C ?</p><p>Thank you </p><p>Rodger</p>
Hi. if you have iOS app, please share it with us
<p>Communication between app and server is done via simple UDP protocol. iOS app just needs to comply with it. For ex., to discover available services on your network, it broadcasts following UDP status request packet (should broadcast multiple times to increase chances of delivery, switch servers dupe duplicate requests and processed it only once):</p><p>[serializer_version|request_type|request_id|device_id|sha1_of_the_request]</p><p>serializer_version: (4 bytes) currently there is only one version, so it is always set to 1</p><p>request_type: (1 byte) 1 - for status request, 2 - switch request, 3 - status reply</p><p>request_id: (16 bytes) random UUID used to dupe duplicate requests</p><p>device_id: (16 bytes) switch server id if request designated for specific switch (for ex switch request to push button)</p><p>sha1_of_the_request: (16 bytes) sha1 of (uuid + password) (very dumb authorization protocol that has multiple attacks)</p><p>Each configured device will reply with the device_id, device_type and device state (on/off).</p><p>There are total of 3 requests in this protocol and if your app talks this protocol, you will be able to operate the devices. Take a look at: </p><p>https://github.com/sshlyk/switch-server/tree/master/src/main/java/com/alisa/lswitch/client/model</p>
<p>many thanks, amigo</p>
<p>Hello,</p><p>I don't know how install <a href="https://drive.google.com/file/d/0B_u36ffL9B2jeWUxT3RtWGlsRDA/view?usp=sharing" rel="nofollow">switch-server-1.0.jar</a> and i dont know the command &quot;gradlew&quot;</p><p>someone can help me please?</p><p>thanks</p>
<p>Just copy the file over to your SD card and then run on Raspberry Pi</p><p><em>java -jar /path/to/you/server/switch-server.jar --config /path/to/config.json</em></p><p><em><br></em></p><ul><br><li>To start switch-service on boot add following crontab command (sudo crontab -e): &quot;<em>@reboot java -jar /home/pi/switch-server.jar --config /path/to/config.json</em>&quot;</ul>
<p>i have :Error: Unable to access jarfile /path/to/you/server/switch-server.jar</p>
<p>You need to specify actual path. &quot;/path/to/you/server/switch-server.jar&quot; is just an example. </p><p>If you copied your file to &quot;<em>/home/pi/&quot; </em>folder, then the command is</p><p> <em>java -jar /home/pi/switch-server.jar</em></p><p><em> <br></em></p>
<p>thanks ,it s good now!</p>
<p>I an a new bee how do you 'connect relays or other gadgets to GIPOs on your raspberry pi, tell switch-server their names and pin numbers they are connected to, and control them via Android app.' can you show hot you actually wire to a garage door opener.</p>
<p>Given relay, locate VCC, ground and IN1. Connect your raspberry pi +5V (or +3V) to VCC, GND (ground) to GND and GPIO1 (for ex) to IN1. Now after you configure and restart switch-server you should be able to control it and you will hear relay's clicking sound. It is ready to be hooked up to your garage. </p><p>All garage doors are different, but most of them have 2-3 low voltage wires going in. One of the wires controls the light, and one the motor. I found the one I need just by trying all of them (there are 3 different combinations) you can also trace the wire from your garage button.</p><p>Now relay has 3 outputs. One is NC (normally closed) and second one is NO (normally opened). You need NO, so there is no current to your garage when relay is not operated. For relay on the picture, that would be first and second terminal (from top), you can find a drawing there that shows two disconnected lines (NO).</p>
<p>I have a small problem, the relay does not stay on long enough to open the door. Is there a way in the json script to hold that open for a second or so?</p>
<p>You can now specify button delay, just add &quot;buttonDelay&quot; key and time in milliseconds. You will need to download latest server jar (I uploaded new one) or build it yourself. </p><p>I would also make sure that relay is not too far from the motor, sometimes there is not enough current when you have long wires.</p>
Could use a loop to...
<p>At around $60 each by the time you have powered it, added wifi and put in an SD card, this is a pretty expensive switch.</p><p>A $2.50 ESP8266 or Bluetooth module with small Arduino is a much better solution - smaller, neater and natively works with relays, etc. to achieve the same result.</p><p>Sorry - not a believer.</p>
$21 Raspberry Pi model A<br>$9 (optional) wifi adapter<br>$7 relay<br>$5 power adapter<br><br>Total: $42 <br><br>It is not a competition, just another fun project for RaspberryPi owners.
<p>$2.50 ESP, $3 USB 5V, $3 relay... </p><p>The Pi is a wonderful device, it does some amazing stuff -I have 6 of them, doing such amazing stuff -but acting as a wireless relay is a complete waste of resources.</p><p>You're right, it isn't a competition. It's all about using the right tool for the job, </p><p>In this case, using a multiprocessing-capable Unix server to turn on a switch isn't. Far better would be to use that Pi to act as the central controller in a network of smart ESP8266 switches.</p><p>That would be something that uses the Pi's resources effectively.</p>
Not sure what is your point. Save 10 bucks?<br>Agree that RaspberryPi is much more powerful, but have you consideres time it takes to write software for a microcontroller? It is much easier for new people to jump in and make changes to java code than deal with low level microcontroller.<br>This is not a comercial grade product and not designed to be optimal. It would be more productive if you can give a link for similar project bult around Arduino.
<p>it's not $10, it's more than $30 -per iteration.</p><p><a href="https://www.instructables.com/id/ESP8266-WiFi-relay-switch/" rel="nofollow">https://www.instructables.com/id/ESP8266-WiFi-relay...</a></p><p>And as a &quot;new people&quot; myself, I have found Arduino and ESP infinitely easier than Java on Pi.</p><p>I don't know where you get the idea that programming an ESP requires knowing low-level programming -it uses C++ and the Arduino IDE, or even easier, Lua.</p><p>Sounds like you don't actually know what it is you are railing against. Educate yourself before taking me to task -you have been wrong about nearly every single point, so far.</p>
<p>That's a very nice project. Thank you for sharing. I am glad you prefer Arduino.</p>
<p>it's not Arduino, it's an ESP8266. Maybe if you read, rather than skimmed, you might learn more?</p><p>I don't 'prefer Arduino' - it's about the appropriate tool for the job. In this case, a $2.50 ESP is superior to a $42 Pi.</p><p>But, the Pi used as a central controller for a network of such switches, using MQTT or other simple comms protocol, should be easy for a beginner to fathom.</p><p>I didn't find it hard and I've never programmed a damned thing.</p>
<p>Dude, you're so annoying, I even created an account to tell you that.</p><p>Leave him alone. If you don't like his project, don't read it. Even if it might not be the most efficient way to do things, maybe someone who already owns the parts thinks, this is a good use for that stuff.</p><p>don't hate, appreciate!</p>
<p>it would seem the only one hating is the person that created an account purely to do that.</p><p>it's a public forum wher ideas are mooted and discussed. In this case, the discussion wasn't some sycophantic &quot;Dood ur so awesome&quot; but a critique.</p><p>Maybe the internet isn't for you, flower, if you are so easily upset?</p><p>It's a flawed concept, there are much better ways to achieve the same result.</p>
<p>I don't know, you clearly said you preferred Arduino and ESP8266. The author also never mentioned low level programming, just low level microcontroller, which they are compared to a pi. There is always the right tool for a job, but the point of this project is clearly, using a tool you have to job it is fully capable of doing. This is a great project for people who only own a pi. Post some projects using the ESP8266, the more code out there, the better. </p>
Sounds awesome. Can't wait for your instructables.
<p>Does switch server also allow to read out stuff from the Raspberry Pi? For example if I have a reed switch attached to it, can I see if the garage is open or closed?</p>
<p>no, but it can be added. Each switch replies with the status, and this information can be part of it. For ex. when garage door replies to status request, it can check other sensors and include their information.</p><p><a href="https://github.com/sshlyk/switch-server/blob/master/src/main/java/com/alisa/lswitch/server/StatusRequestProcessor.java#L40" rel="nofollow">https://github.com/sshlyk/switch-server/blob/maste...</a></p><p>If there is interest in this feature, I can add it</p>
<p>This is cool. Thanks for sharing!</p>

About This Instructable

30,681views

126favorites

License:

More by sshlyk:Simple home automation. RaspberryPi + Android 
Add instructable to: