Home Automation: Christmas Edition

One of the somewhat recent tech buzzwords you may have heard everywhere is Internet of Things (IoT), referring to ‘smart’ appliances like internet connected refrigerators, baby cams, vaccuum cleaners, and egg freshness trackers (not even kidding, google “Egg Minder” for one of the most useless devices I’ve ever seen). Some of them are useful, most of them are… marginally effective. And many of them have security issues because they were rushed to the market. Many of these things are connected to the internet because that allows the manufacturer to give users some kind of app or website to control the device. I figured that since I’m usually at home when I want to use my appliances anyway (I’m not going to switch my ‘smart’ kettle on when I’m not about to make tea) it would be much more efficient and more secure to control devices on my local network instead of exposing them to the internet. A LAN-of-things, if you will. A commercial manufacturer wouldn’t do this because it causes setup hurdles for consumers, but it seemed like a cool idea to mess around with, so I’m doing it myself.


I started to wonder what I would like to do in terms of home automation. What would I actually use? I thought of the kettle but it requires that a) there is water in it when you switch it on and b) you pour the water out of the kettle and into a cup yourself, kind of reducing the convenience of controlling it from your phone. I’m still somewhat intrigued by the idea of trying to build a tap-operating, water-pouring, tea-bag dunking, drone-operating machine that will fly my tea upstairs but it’s a bit outside of my skill range right now.


Going through several other ideas, I honestly got nothing that made sense to automate from any practical standpoint. So if it can’t be useful, maybe it can be cool. I thought of cool Arduino powered displays where LEDs show stuff based on the weather or the date or the number of people online on a given minecraft server. But most of that was on the edge of my skill range and were sufficiently big projects that I could see them ending as a half-finished pile of wires on the junk pile. So in the end I decided on a much much simpler proof of concept to learn a few things from. Switching some lights on or off. And since pushing a button on my phone to swith lights on and off is about as much effort as hitting a light switch and the fun of doing it on a phone or browser wears off pretty quickly, it needed to be more automagical than that. So here’s the final deal: I made some holiday decorations light up when I come home.

Step 1, The idea:
I’m going to detect whether I’m home by checking if my phone is connected to my home WiFi. I already know I can see connected devices in my router admin panel, and I can probably also find the phone on the network with an ARP scan or something like that. So it should be possible to write a script that somehow figures out whether my phone is connected. I have a raspberry pi that I can use for this purpose, it’s no problem to leave it on for extended periods of time, and it stays connected to the network at all times. It could try to find my phone every minute or so. The pi has USB ports that could provide power to some lights or maybe I would need to connect a relay to the GPIO pins or something. In any case, it has output options.

Step 2, Research:
I don’t think writing a script to find my phone on the network is going to be a major issue, but I have no clue yet how I’m going to translate that into actually switching hardware on and off. Apart from the option of using the GPIO pins, I figured I may be able to control whether a given USB port is delivering power or not, and use this to power one of those hideous plastic USB christmas trees. So we’re off to the google machine.
This thread on the raspberry pi forums (https://lb.raspberrypi.org/forums/viewtopic.php?t=162539) has a command for a dirty hack that shut down all the USB ports as well as the ethernet port using the drivers, which is cool, but problematic. I was kind of hoping to use that ethernet port to, you know, keep the pi connected to the network. But it also supports Wifi, so it could still work. There’s also a link to this piece of code on github (https://github.com/mvp/uhubctl) that claims it can control power to specific USB ports on the pi. That feels like the ‘proper’ way to do it so that will be my first try, and we’ll keep the driver hack as a plan B.

Step 3, Shopping:
Time to buy that USB christmas tree. I walked to a few stores expecting that shopping for the most unsightly USB christmas tree would be the easiest part of this project. Turns out that USB christmas trees are VERY 2015 and nowhere to be found. It seems that what people want now is battery operated lights in everything, and office christmas swag is out of fashion. Alternative options: buying one online, or changing the plan. Because I care a great deal about my christmas decorations being the latest and greatest, the USB tree idea bites the dust. I ended up buying a battery powered string of plastic christmas trees with LEDs in them. As a masterpiece of product design the LEDs and wires are attached at the trunk of the tree, so they will spend most of their functional lives hanging upside down. In any case, if I want to control this from my Pi I have to get my hands on a relay and some wires, which I ended up buying online.

Step 4, More Research:
Now I need to figure out how to use the GPIO pins on the Raspberry Pi. This very useful page (https://learn.sparkfun.com/tutorials/raspberry-gpio/all) has pretty much everything I need. It tells me there are Python libraries to control them, which is awesome because I’m familiar with the language and it will be easy to make the ‘check if my phone is at home’ script and the ‘switch the lights on’ script all in one. I also learn that the command ‘pinout’ on the pi will give an overview of the pins on that specific model which saves me the effort of continually checking and then forgetting which model pi I own.

Pinging my phone every minue while I switch the wifi on and off.

Step 5, Making the thing:
First I’m writing the part that will detect whether I’m home. There are various neat python libraries that can perform full network scans etc, but it seems a bit excessive to search my whole internal IP range every minute hoping to find my phone. Instead I went to my router settings and gave my phone a static IP address; this way I can just ping that address and be done with it. There is a cool subprocess library that allows you to run commands on the OS and check their output, so that’s what I used. I can easily test my setup by switching the wifi on my phone on and off, and this first experiment is succesful.

Ground, power, and control wire running from the Pi's pins to the relay.
Ground, power, and control wire running from the Pi’s pins to the relay.


Then, the part with controlling the pins, or rather, the pin. The relay needs 3 wires going into it from the pi, but 2 of them are ground and power and don’t need to ever change. Only the third is intended to receive signals. So I only need to switch that one pin on and off depending on the results of the ping to my phone. No real difficulties there.


Now it’s time for the hardware part. I cut one of the wires on the lights, close to the battery pack and after stripping some of the plastic off put some solder on the exposed ends so they don’t fray. Stuff the wires of the lights into one end of the relay, in ‘normally off’ mode, and then connect the other end of the relay to the pi. I connect ground to ground, input to pin 3 and 3v3 to 3v3, and it’s finally time to plug it in and see if it works.

Step 6: Testing
I start the script while my phone’s Wifi is switched off, but immediately the lights switch on. And when I get my phone online, they switch off. It seems that everything works, but in reverse???
A quick googling session reveals that the relay probably considers low voltage on the input pin to be its ‘on’ state, which is counterintuitive but explains a lot. After reversing the pin output in the script, everything works! To fully ‘install’ my gadget, I run the necessary cables up to the top of my bookcase and drop the whole thing between my other knick-knacks there.

Step 7: Web interface
Because useless home automation would not be complete without the ability to control it from a browser, and because I might eventually get fed up with these lights, I think I’m going to need a web page with a ‘christmas switch’ that will control whether this whole thing is live or not. I initially thought I would just start and stop the python process with some PHP, but while googling what function to use for that, the internet taught me better: a button on the page can create some file, and the python script will look for that file to determine whether to do its thing or not. A bit of copy-pasting PHP examples from stackexchange, a short update to the python script, slap a picture on it and boom! A smart product is born!

Wow. So smart. Much device. Very IoT.

Lessons learned:

– Managing GPIO pins on a Pi is not hard at all if you have even a tiny bit of previous python knowledge.
– Gimmicky USB office gadgets are on the decline.
– I should probably have bought different jumper wires, because now I can’t close my Pi’s case anymore.
– I still haven’t been able to imagine an appliance that I would legitimately want to control from my phone. I must be getting old or something.

Final judgement:

Education factor: 2/5 I learned how to do some stuff I didn’t know how to do before, but those things turned out to be pretty easy.
Fun factor: 3.5/5 I am not a fan of christmas-tree shaped LED lights, or having them on in my living room while I sit at my computer upstairs, but having my own personal parody IoT device brings me joy.

Cliffhanger: When I ordered the relay and the wires of course I couldn’t help myself. I have an RFID board lying around on my desk now to do equally useless proof-of-concept projects with later. Stay tuned