Experiments with RaspberryPi and iOS for home automation

Ever since I bought a couple of RaspberryPi's a year or so ago, I've been interested in doing some home automation with them. However, I never really got down to it (for various reasons; lazy, no easy tutorial available, parts missing, life etc.) until the past couple of months or so.

I finally got around to buying a relay and female to female cables. With these in hand, I followed a few tutorials on YouTube to get a light setup to be controlled by the GPIO pins and the relay (some good videos). In the end, the whole setup ends up looking something like the below (I'll go into more detail about this setup in a later post perhaps):

RPI Setup

With the wires all setup, the next step is to actual use the setup to be able to remotely turn on or off a light. This is where the flask app I wrote comes in. As you might see, the GPIO pins being used are #9 and #2. These two represent the two ends of the relay board (in my setup anyways). Running this as a webserver on the raspberry pi (you need to run as root otherwise the rpi is unable to access the GPIO pins) allows me to then make a simple GET request to /toggle for one light and /toggle_big for the other.

Now, while I had an Android device, hitting these endpoints was easy. I simply setup a Tasker widget on my home screen that when touched would make a GET request to one of these two endpoints which would, in turn, simply toggle the light on or off. However, now that I use an iOS device, Tasker (or any other widgets) are not an option anymore (which sucks, IMO). This was a good opportunity to get my feet wet in the iOS development world. There are a bazillion tutorials online on how to get started, so I won't go into them here. I would say that making a simple GET request via swift turned out to be a harder task than I had envisioned it to be (though I guess, python, natively makes it just as hard). At the end of the day, this is what I came up with. Essentially a viewport with two buttons; each one simply toggles the specific light on or off. As you can see from the image below, I have not figured out how to center the buttons in the view; not such a big deal for me for now.

iOS App

The next step for me is to get force touch working on my iPhone 6s so that the app does not need to open up for me to be able to turn the light on or off (similar to a widget on Android devices). Additionally, I need to buy another relay board and a wireless dongle for the pi so that I can do the same thing for the lamps in my bedroom.

So that's mostly it. This setup works well for now and gave me a good opportunity to play around with a few new and interesting technologies/languages.

social