HackADay: Controlling power outlets using the Raspberry Pi

5 March 2016

My plan was to control some power outlets(16 ampere) using any browser(for example with a mobile phone). I wanted to use electronics only and tried to avoid relays.


I installed a Debian on my Raspberry Pi(including XBMC aka Kodi) on which a lighthttpd-daemon is running. This Webserver is calling a cgi-script(perl) which enables or disables the power sockets via it’s GPIO-Pins. To make it as easy as possible I just call the gpio-command from the WiringPi-Project. I really love to make things work automagically. That’s why I also included a function for enabling or disabling a socket at a specific time. Since I kept everything as simple as possible I just used a simple at-job for this.


Raspberry Pi GPIO Pins

I am using the following Pins: GPIO23(socket 1), GPIO25(socket 2), P1-02 for 5volt power supply and P1-06 for ground.


The most difficult thing was the wiring. As I already mentioned, I really wanted to avoid relays. Therefore I created this circuit: the GPIO-Pin controls an optocoppler(MOC3041) limited by a transistor(BC547). The optocoppler itself is switching a TRIAC(BT 139). The TRIAC enables or disables the power socket.

That’s the circuit:


…and that’s how it looks like:


The Website is very very simple and looks like this:


..very basic, isn’t it?

Don’t forget to change the sudo-rights for www-data:


www-data ALL=NOPASSWD:/usr/local/bin/gpio

You can find all the sources of this project on my github.

Final Words

This project was a lot of fun. I got much support at the Meta Lab(The very best hacker-space in vienna), The project works very well. I also implemented some (green)diodes for indicating if the sockets are enabled or not. And don’t forget that I really installed a full xbmc-system. That’s why my sockets are also a full multimedia center!!

[ Linux  Programming  HackADay  Raspberry  Hardware  ]
Except where otherwise noted, content on this site is licensed under a Creative Commons Attribution 3.0 Unported License.

Copyright 2015-present Hoti