Hey all,
UPDATE: I have added functionality to ensure that changes made to WebIOPi outside of SmartThings are picked up. Please uninstall your current version by removing all references to your relays, then deleting the relays. Upgrade, and re-add them. It’s a much better experience this time
I wanted a device type to control raspberry Pi relays from SmartThings. I couldn’t find anything out there already, so this project was born.
Basically, we are going to set up our Pi with a HTTP REST service which is capable of turning on and off relays which can be connected to just about anything. Then we’ll add a device type which acts as a switch which is able to turn the relay on and off in SmartThings. You can then use this device in any SmartApp which uses switches.
This is a very ‘happy path’ guide. If you have any queries, feel free to leave a comment. I hope you find this useful.
Disclaimer: Do not mess with mains electricity, it could kill you!
Pre-Reqs
- Raspberry Pi - http://goo.gl/p7nCkZ
- Power cable for Pi - http://goo.gl/me82lz
- MicroSD card for Pi - http://goo.gl/sIhwh0
- MicroSD Card Reader - http://goo.gl/Mw9GN7
- Dupont Jumper Wire Ribbon (Female-Female) - http://goo.gl/iOiE6r
- Relay board for Raspberry Pi - General Purpose Relays for sale | eBay
- Optional - WiFi Dongle for Pi - http://goo.gl/3QKbmM
- Optional - Case for Pi - http://goo.gl/zW7cDo
- Obvious - SmartThings Hub
That’s all the hardware, now you need now onto the configuration:
Prepare the MicroSD card for Pi
- Put the MicroSD card into the card reader and plug it into your PC
- Download the latest version of Raspbian from: https://downloads.raspberrypi.org/raspbian_latest
- Extract the .img file from the zip file you just downloaded
- Download and install Win32DiskImager from: Win32 Disk Imager download | SourceForge.net
- Open Win32DiskImager, pick the .img file you just extracted
- Pick the MicroSD card from the device drop down
- Hit ‘Write’- This bit may take a while depending on how fast your card / reader is.
- Once complete, insert the sd card into your Pi
Prepare Pi
- Plugin your WiFi dongle or ethernet cable and a keyboard / mouse / HDMI into your pi and power it on
- If using WiFi: After you have been booted to the desktop, click the network icon at the top right and connect to your WiFi network
- Open terminal and type ‘ifconfig’ and take note of your IP address
- You can now unplug your keyboard / mouse / HDMI
Connect to Pi via SSH on your computer
- Download Putty from http://the.earth.li/~sgtatham/putty/latest/x86/putty.exe
- Open Putty and enter the IP address of your Pi which you previously noted down
- Click ‘Open’ you should get a prompt saying that the servers host key is not cached, click ‘Yes’ to cache it and proceed
- Log into Pi with username ‘pi’ and password ‘raspberry’
- You should now have a prompt
Install WebIOPi (This provides us with a REST interface to the GPIO)
- Type the following commands at the Pi prompt:
wget Download WebIOPi-0.7.1.tar.gz (WebIOPi)
tar -zxvf WebIOPi-0.7.1.tar.gz
cd WebIOPi-0-7.1
sudo ./setup.sh
- As your Pi is not exposed to the internet (i.e. Pi can see the internet, but the internet cannot see Pi), our next step is to remove the password for easier communication. Enter the following at the prompt:
sudo nano /etc/webiopi/passwd
- Delete the content of the file and press: Ctrl+O then Enter then Ctrl+X to return to the prompt
- Start WebIOPi as a service so that it runs every time your Pi is turned on. Enter the following at the prompt:
sudo /etc/init.d/webiopi start
- Verify that WebIOPi is set up correctly, navigate your browser to http://{IPADDRESS}:8000. You should see the WebIOPi Main Menu
http://i.imgur.com/6QMNV7m.png
Connect your Relay up to your Pi
- Connect the ground (-) terminal of your relay to one of the ground pins (6, 9, 14, 20, 25, 30, 34 or 39)
- Connect the positive (+) terminal of your relay to one of the 5V (2 or 4) pins
- Connect the switch terminal of your relay to one of the GPIO pins (7, 11, 12, 13, 15, 16, 18, 22, 29, 31, 32, 33, 35, 36, 37, 38 or 50). Take note that the pin number does not match the GPIO number. Use the image below as a reference to lookup the GPIO number of the pin you select (i.e. pin 7 is GPIO 4)
http://www.raspberrypi-spy.co.uk/wp-content/uploads/2014/07/Raspberry-Pi-GPIO-Layout-Model-B-Plus.png
Verify your Relay is working
- Navigate your browser to http://{IPADDRESS}:8000/app/gpio-list
- You should see a list of GPIO numbers, with ‘IN’ next to each of them
- Locate the GPIO you are using, and click the ‘IN’ button. It will change to ‘OUT’. The reason for this is that we are sending a signal to the relay, rather than the relay sending a signal back to Pi. (The SmartThings device type deals with this for us, we are just testing your relay at this point)
- Click the GPIO number you are using, it should change to orange, and you should hear a click from your relay, as well as an ‘on’ LED, if your relay has one
- If that worked, congratulations! You are ready to configure the SmartThings device type
Install the Device Type and SmartApp to SmartThings
Goto and follow the instructions:
Install the Pi Relay Manager SmartApp
- Goto your mobile device and open SmartThings
- Got the Marketplace tab at the bottom
- Tap SmartApps’
- Scroll to the bottom and tap My Apps’
- Tap ‘Pi Relay Manager’
- Fill in the details that have been requested (The default port is 8000) and click ‘Done’
- If you connected a relay to GPIO 17, give device 1 a name, pick the relay switch type and enter ‘17’ in ‘GPIO# or Device Name’
Your switches have now been added to your list of SmartThings. You can use them in other SmartApps just like regular switches