Using Raspberry Pi to control an electrical outlet


Because the GPIO code in the example I followed directly accessed memory, it needed to be run by the root user. To make my life easy, I developed and tested the code while logged in as root. All commands given here assume you'll be doing the same. Everything is done from the command line, so unless your Raspberry Pi is set to jump straight to the graphical front end, just stay at the command line.

To create a directory in which to put the source code and the executable file while under development, type:

mkdir gpio
cd gpio

You'll find the source code at my website [3]. Save the file in the directory you created. The program takes two parameters: the channel and on or off. You'll need to edit the code to replace my remote control codes with your own. My favorite editor in Linux is nano, because it's relatively easy to use, and it is usually installed on most systems. To access the nano editor type:

nano switch.cpp

Be careful to retain the quote marks around the code in the source (see the HowTo Corner on the Raspberry Pi Geek website [4] for more information on nano). When you have changed the codes, quit by pressing Ctrl+X, answer Y to the question about saving the file, and accept the filename switch.cpp.

To build the executable file, type

g++ -o switch switch.cpp

then test it with your sockets by typing:

./switch 1 on

If it works, you will probably want to be able to run it as any user. To do so, type the following commands:

chmod +s switch
mv switch /usr/bin/


This simple project offers several opportunities for enhancements. For instance, you can use a cron job to schedule the sockets to come on and off at certain times. Just type:

crontab -e

You will be able to edit a file that controls scheduled jobs. The format is described in the file, but to try something out, add these lines to the bottom of the file:

0 *  * * * switch 1 on
10 *  * * * switch 1 off

These settings will turn socket 1 on for the first 10 minutes of every hour.

So far, this project is not very user friendly. A web-based interface would be a lot nicer. My web interface allows you to switch the four channels on and off, but I have not gone so far as to add any scheduling to it.

First, install mini-httpd to act as the web server:

apt-get install mini-httpd

You can download the files for the web interface from my website [5]. In the ZIP file is the config file for mini-httpd, and the var/www directory, which is where I put web page and CGI programs. These shouldn't need any modification, but you will need to copy them to the correct locations. The HTML file uses a primitive Ajax request to run the CGI script with the channel and on/off parameters. The CGI script just pulls the parameters from the query and calls the switch program with them.

Buy this article as PDF

Express-Checkout as PDF

Pages: 4

Price $2.95
(incl. VAT)

Buy Raspberry Pi Geek

Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

  • Automating the home with the Raspberry Pi

    The Raspberry Pi GPIO interface offers the perfect starting point from which to control devices, such as turning lights on and off, starting the coffee machine in the morning, or turning on the TV and playing your favorite movies at scheduled times.

  • Android Pi

    Your Android device can be a versatile companion for Raspberry Pi. We describe some useful apps to help you make this happen.

  • Raspberry Pi IR remote

    Turn a Raspberry Pi into an IR remote control for your DSLR, TV, or any other device with an IR port.

  • A new way of no-solder prototyping

    The Grove system's standardized connector and multitude of devices allow quick and easy project prototyping with your favorite small-board computers.

  • PHP on Raspberry Pi

    Getting started with PHP on Raspberry Pi is easy. We show how to build a simple PHP app to control an LED.