Simple Home Automation. RaspberryPi + Android

36,271

126

62

Introduction: Simple Home Automation. RaspberryPi + Android

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.

Share

    Recommendations

    • Water Contest

      Water Contest
    • Fix It! Contest

      Fix It! Contest
    • Metalworking Contest

      Metalworking Contest

    62 Discussions

    Hi sshlyk, what should I do if I want to connect this from the different network?

    Please reply fast, as I am in hurry.

    Thanks

    Can someone troubleshoot me please. I have the wires connected correctly to the pins according to Pi4J site and model. The config I added 0-7 and disabled mock connection. I also have tried to set the pins low on initialization and then back to high (off). When I run the server from the build all but one of the relays come on, but when I press a switch on my phone...well no switching. Is there something I am overlooking? I am on the same network and I can see the incoming request in the terminal shell connection, so I know it is receiving properly but not responding. Any assistance is appreciated. I am running N on my android phone too so I hope it is forward compat. Thanks. I have Rpi 3B btw.

    3 replies

    Hi
    Since you can see incoming requests, I am going to assume you do not have relay wired correctly. Start with one relay, make sure it is properly grounded, connected to proper voltage and you are using correct pin number. Also verify that your relay is working. Try connecting to 3.3V then 5V pin.

    Seems like you have this board: http://pi4j.com/pins/model-3b-rev1.html

    Play with GPIO 0. Relay should be connected to ground (pin 9), power (pin 1 or 2), and GPIO 0 (pin 11). In the config specify GPIO number as 0.

    I tried power 1 and 2 and 3.3V with GPIO ground to pin 9. When I start the server only a few relays light up and switch on (1,2,3,5,6,8). That tells me the default voltages for those switches are set high? Not sure why it just has a few switching on and not all as I follow the mapping.. There is a jumper on the relay board that is set to VCC-JD I will try setting it to JD-VCC instead. Nope. I will keep trying. Checking and rechecking.

    switchPinNumber in the config refers to GPIO number (0-7), not actual number on the board. For example to use GPIO 0 you must connect wire to pin 11 on the board, but config must specify

    switchPinNumber: 0

    oh and i am using a NOOB. do i need to chnage my OS on Rpi?

    1 reply

    Hi. From what I read, NOOBS is OS installer, and it installs Raspbian OS, exactly what you need. In general, app will work with any OS that can run java (assuming you can also install pi4j library). Thx

    I am making a circuit breaker automation using raspberry pi 3 and a 16 channel 12v octopular relay module.

    I like the architecture of this project. The way it discovers but
    keeps separation between the IP comms and the app. I have it running on
    an R-pi zero and a modern android. I would like to use an old smart
    phone as a dedicated controller but it wont load from playstore. Any
    ideas why it is incompatible. Is there an older version that might run
    on a gingerbread phone?

    4 replies

    Hi

    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: https://github.com/sshlyk/switch-android/blob/mast...

    Run "./gradlew assembleDebug" and you can find .apk file in build directory.

    If it works, create pull request, I'll merge it and publish to app store.

    I am having trouble building the app on windows. Do I need a .bat version of gradlew?

    You are right, for windows you need to use gradlew.bat, I am afraid I just deleted it. Looks like I am using this gradle version

    https://services.gradle.org/distributions/gradle-2...

    And you can find bat file in the bin folder. I am afraid you are on your own, I have no windows machine to try that.

    Thank you for the suggestion
    I will work on that.

    0
    None
    ssm16

    2 years ago

    Hi,

    Nice project and well written code for both Android & RPI.

    I have a few questions:

    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 & RPI, the RPI logs show SingleSwitch.java, turnOn() and turnOff() methods are called correctly, but the relays cannot be controlled. Physical pins 13 & 17 are always low.

    I have written a simpler java code that does toggle the relays correctly. I have RPI 2 B.

    Can you please advice.

    Thanks

    4 replies

    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
    Find your model and what number pi4j assigns to given gpio

    I should probably make it clear in the doc. Feel free to make necessary changes to documentation on github.

    Hi,

    I have had a look at the link you mentioned and it seems I have the correct pin numbers. Looking at this link (http://pi4j.com/pins/model-2b-rev1.html) and the switch-config.json file, my allocations are:

    "Garage Door">"switchPinNumber": 0>GPIO_0>Pin 11

    "House Ceiling Fan">"switchPinNumber": 2>GPIO_2>Pin 13

    "Furnace Blower">"switchPinNumber": 3>GPIO_3>Pin 15

    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>SingleSwitch.java>toPin().

    Could you provide further advice.

    thanks

    Hi ,

    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.

    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 https://github.com/sshlyk/switch-server/blob/maste...

    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.

    0
    None
    M L G

    2 years ago

    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?

    1 reply

    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
    Thx