DIY-IOT: How to construct a device, that turns on/off other devices via the Internet – Part 1/4

IMPORTANT: READ THIS, before deciding to build something like this device. This device is connected directly to the mains power, which is potentially lethal to mess around with! I am not kidding you – the best scenario when subjected to electrical shock is that it hurts like h*ll, worst case scenario is that you DIE. You should not mess with mains power without a minimum of knowledge.

One of my good friends, Dan, is behind a new cool collaboration tool, Skarpline ( A bit like sharepoint, only much better! During the season holidays while drinking some beers, we discussed Skarpline, and Dan mentioned that he is planning to implement new functionality, where Skarpline can communicate with and control external systems. For demonstration purposes, Dan needed something to be controlled, “…and you’re good with hardware and stuff, so couldn’t you …”. Before long, the conversation turned towards Internet of Things, and how to communicate with IOT devices from an open platform, like Skarpline.

A nerd like me is a helpful being, so of course I grabbed the opportunity to add a new project to the ever-growing pile of unfinished projects, which takes up table space in my cellar-lab.

More beers and a brainstorm followed, as we started discussing what kind of device we wanted to control. We started with the usual coffee machine, but ended with a more generic device that can turn on/off two appliances via the Internet. One of the appliances could still be a coffee machine!

This is a small, but fun project, and I decided to take the opportunity to describe it to you. I suspect there are more people out there, who would like to make something similar. You can probably buy a commercial solution cheaper, but what’s the fun in that?

IOT is one of the popular buzzwords this year, and contrary to one of the other popular buzzwords, big-data, which also is part of the ongoing BS-bingo game, IOT is an area where the DIY people can have some fun making devices, what might even have a practical application at home.

This article is the first part in a 4-article series, where I will describe the project.
The text you’re currently reading is about making some fundamental choices, and establishing the scope of the project. Next article, I will grab my soldering iron and make some hardware. Part three will be about configuring the chosen platform and the final article is about me grabbing my keyboard and making you a delicious dish of spaghetti code.

Let me dazzle you with the most brilliant device, with a complexity that makes the cockpit of a Boeing Dreamliner look like the dashboard of Fred Flintstones car, and an industrial design so stunningly beautiful, that Apple will hire me on the spot…

Bzzzt (Steen enters ‘realism-mode’)

When you’re scoping a project, it will enhance the chances of success if you take into consideration the available resources:

– Number of developers available: 1 (And it’s been quite a few years since I last did full-time development)
– How many percent of my time can be dedicated this task: my sparetime
– Do you have any other tasks during your sparetime: Wife+child+house+volunteering+big garden+…
– Historic ability to finalize projects: Embarrassingly poor! My typical version of ‘done’ is a PCB with a bird’s nest of wires going in all directions. It might work, but it aint pretty!

Right… Lets start scoping the project by thinking about what we need.

We’re going to make a device with two power plugs, which can be independently controlled via the Internet. Due to the limited time available I am going to settle for encapsulation with focus on protecting the user from electric shock (I have never tried being that ‘done’ with a DIY project before ☺), rather than making something pleasing to the eye (this have absolutely nothing to do with the fact, that I suck at the visual design. NOTHING, I say!)

So a trip to the local hardware store to get a couple of power plugs, a trip to the Internet to fins a suitable box to contain the electronics.

I have a lot of drawers in my cellar-lab, and one of these drawers revealed a couple of 230V/10A relays. I probably need to make a driver circuit for the relays but this can be done with a couple of transistors and diodes (drawer#2). So far, so good.

If I wanted to make a commercial product, the BOM cost and the physical size of the device would be of importance, but since I lack time and don’t really care too much about the cost per device, I opt for a standard platform, which in terms of power and features is totally overkill – but a HUGE timer-saver.

There are plenty of options to choose from, but I have selected the Raspberry Pi as my platform. Why? Because I found one in drawer#3.

A raspberry pi is a versatile small pcb containing a complete computer with Ethernet, USB and HDMI. A lot of people use them for cheap media centers, and I use a couple of rpi’s myself for my IHC project at home (might be subject for a later article series). The rpi is built around a 700 MHz ARM CPU and typically run a variant of Linux. You could install a limited version of win 10 as well, but I will stick with Linux.

The available I/O is more than adequate for our device with 17 GPIO (General Purpose I/O), where I only need two for the relays. I could choose to use the Ethernet port for Internet connectivity, but I prefer to install a wifi dongle since you can’t expect LAN to be available where the device will be placed.

By choosing Linux, you get the advantage that the full palette of tools in a normal linux distribution is readily available, making it much easier when we start coding.

Since the device will be controlled by another software system, we must define an API (Application Programming Interface) such that the developer know how to send a command, and which results he can expect in return.

Next, since the device must be controlled via the Internet, a minimum of security must be in place, preventing bad guys from turning on your appliances.

A quick&dirty solution of implementing SSL and basic authentication will have to suffice. If it was to become a commercial product, significantly more thought must be put into making it a secure solution!

I choose to implement authentication ad standard http digest, which makes sure that the password is not sent in clear text. You could argue, that if using HTTPS further encryption of the password is not necessary. My reply would be

The API can be realized by simply calling a webpage, where the command and parameters are part of the URL. The webpage then return a reply.


Where ACTION can take following values (note: case sensitive):
ON            : Turn on the device
OFF          : Turn off the device
STATUS : Return if device is turned on or off

And DEVICE is the actual device that is queried. We implement two power outlets, so DEVICE can take the value 1 or 2

The return value could be e.g. DEVICE_1_ON or an error message.

The complete API document is available here: LINK TO API DOC

From a software point of view, we need a webserver and it must use HTTPS. I choose as programming language to use PHP, simply because it’s the programming language for webservers I am accustomed to. There are few requirements for performance or language-specific features, so choose whatever language you want.

Scoping the project:
If the device is to be installed and used by people without any IT-competencies we ought to consider how we can simplify the installation process.

We’re using wifi, so the basic installation steps will be:
1. Configure the SSID and WPA key to match the wifi router.
2. Configure the wifi router to reserve a specific IP address for the device
3. Configure a new NAT rule in the wifi router that points a specific port to the IP address of the device

This implies that you must access command line on the raspberry pi via ssh, and some basic knowledge on how to configure an internet router. No problem for the tech-savvy, but my mom would definitely have a hard time making it work. For a commercial solution a more streamlined and user-friendly installation process would have to be implemented. For our purpose, due to lack of time I choose to omit this work.

If anyone should have more time than me, I would suggest implementing WPS (Wifi-Protected Setup), so the wifi connection can be set-up by simply pressing a button on the device and the wifi router within 2 minutes. Secondly I would implement a basic UPnP functionality, enabling the device to create a NAT rule in the Internet router automagically.

With the decisions made, we can make an estimate on how much this is going to set us back moneywise. Since I already have parts in various drawers it will be cheaper for me, but if you would have to buy all the stuff

Note: I am merely estimating here. Actual cost highly dependent on your country, and your ability to shop around.

Raspberry pi with SD card: $35
Wifi USB dongle: $8
2 wall outlets: $30
2 relays: $15
a piece of veroboard + various components: $20
a suitable box: $10

Total, approximately $120 (way more expensive in Denmark – 25% V.A.T. anyone? ☹)

Is this expensive? Yes; a commercial version is available at half that price. But think about the hours of fun (and excitement from lethal danger) you get from playing with electronics, high-voltage and programming for a measly $60.

Secondly the beautiful thing with DIY is, that you can make exactly the functions that YOU need. I often bang my head against some frustrating limitation when messing with of-the-shelf products. For a DIY’er, if features are missing, just implement the missing features!

Finally by making your own device, you don’t share information about your behavior, as could be the case if using commercial solutions.

That’s it, folks. Next step is to get the missing parts, and start building the hardware.

3 thoughts on “DIY-IOT: How to construct a device, that turns on/off other devices via the Internet – Part 1/4”

Leave a Reply

Your email address will not be published. Required fields are marked *