Controlling a WiFi smart plug with the Raspberry Pi

Lead Image © Dietmar Hoepfl,

Power Play

A switched and programmable electrical plug lets you start the Raspberry Pi at predefined times, saving power consumption, and a little Python code keeps you independent of the manufacturer's cloud infrastructure.

Switched electrical plugs are common, but the selection becomes smaller when the requirements for the plug include that it access a WiFi network and have a programmable interface. These features allow the Raspberry Pi to determine a start time on its own. The hack in this article succeeds with an Edimax smart plug and some Python code.

Forums are full of perplexed conversations concerning time-controlled shutdown and bootup for the Raspberry Pi because this small PC is known for its minimal power consumption. The thinking goes that the energy savings for a time-controlled setup would probably be limited. This argument stops short of actual reality, however, because the Rasp Pi rarely works alone. It often needs peripherals in the form of hard drives and USB devices, which consume considerably more power than the computer itself.

Familiar services such as atd and crond, or the shutdown command itself, can perform a time-controlled shutdown. For example, the following command shuts the computer down 10 minutes after entering the command:

$ sudo shutdown +10

However, the Rasp Pi does not have a real-time clock (RTC), nor does it have a BIOS. The clock can be retrofitted, but it cannot handle a time-controlled boot without support from the BIOS (wake on RTC). It also cannot handle a restart at a predefined point in time; therefore, you have to search for an external solution.

Many boards are available on the Internet for retrofitting the Rasp Pi with the corresponding functions. The disadvantage to the add-on board approach is that, without exception, the extension is able to put the Rasp Pi to sleep – but not necessarily all of its peripherals. A switchable electrical plug proves to be a better and more economical solution.

Too Many Choices?

Switched plugs that work over the 433MHz radio band abound. For the Rasp Pi, small radio modules are suitable for use with a plug. Programming these modules is easy, and a previous article describes how to program a module of this type [1]. However, if the Rasp Pi switches its own plug off, it won't have a chance to shutdown cleanly. Additionally, the Rasp Pi would require external assistance to switch the plug back on for booting.

Radio plugs lacking a time control therefore are not the answer. A time-controlled plug, on the other hand, often lacks in the capability to receive radio signals. For a long time now, plugs designed for the smart home often only work in conjunction with centralized equipment that contains proprietary software, or the plugs work together with special smartphone apps via a cloud service run by the manufacturer. Open interfaces that would permit access by the Rasp Pi are definitely lacking.

When looking for a programmable plug that can be controlled wirelessly via WiFi or Bluetooth, Edimax SP1101W and SP2101W smart plugs are clearly the way to go, because other suitable devices available on the market do not offer online documentation about their communication protocol.

Information exists online about the Edimax communication protocol only because resourceful users have eavesdropped on communications and reverse-engineered the commands. The reverse engineering task did not involve a lot of effort, however, because the commands flow over the Internet in plain text.

The Hardware

The Edimax SP1101W smart plug format is the same as that for a classic adapter. One side goes into the wall socket and exposes an outlet free for use (Figure 1). You can operate the outlet manually with a pressure switch, and two status LEDs show whether the device has attached itself successfully to the WiFi network.

Figure 1: Users have successfully reverse-engineered the commands to control the switched Edimax smart plug, making it possible to automate communication – albeit unencrypted.

The SP1101W offers various switching scheme possibilities. For example, you can specify either an on/off ad hoc schedule or a schedule specified for each day of the week. The somewhat more expensive SP2101W additionally provides information about current and past power consumption; however, this function does not seem to be worth the additional expense, because buyer comments tend to be negative.

The smaller SP1101W model costs about $50 (EUR35), and to set up this plug, you need a suitable Android or iOS smartphone app. The plug worked without problem when tested under Android, although users reported some occasional hiccups under iOS.

Theoretically, setup is simple. First, download a free app for initial configuration. Next, put the plug into the wall socket and wait for a slowly blinking red light to appear, signaling installation mode. Android users select the plug directly in the app, whereas iOS users switch into the wireless network generated by the plug.

The app then allows you to set the SSID and password for your own WiFi network. After restarting, the Edimax smart plug logs in directly to this network and connects to the Edimax cloud. The plug receives the switch commands and schedules directly via WiFi on the local network, and the cloud is only used when the smartphone is not registered to the same home network (e.g., while traveling).

If you do not want to be dependent on an external entity and you have concerns about being monitored, you can use the access control in the router to forbid the plug from communicating with the Internet. Typically, this is accomplished by entering the MAC address (addresses of hardware devices) of the plug's WiFi module into a blacklist.

However, the smartphone app is clearly useless unless you have access to the Internet. Wonder of wonders, data privacy and convenience can come together at this point, because the Raspberry Pi is able to assume all of the functions that would otherwise be performed in the cloud.

Buy this article as PDF

Express-Checkout as PDF

Pages: 6

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