Controlling a WiFi smart plug with the Raspberry Pi


The Edimax SP1101W has an integrated web server in the form of the familiar Lighttpd. The plug is controlled via HTTP POST requests, and a suitable script or program sends commands written in XML to the web server and receives a response in the same format.

To activate the smart plug, you should copy the lines from Listing 1 [2] to the an.xml file and then call the command:

Listing 1

Activating the Smart Plug

<?xml version="1.0" encoding="utf-8"?>
  <SMARTPLUG id="edimax">
    <CMD id="setup">
$ curl -d@an.xml http://admin:1234@

If the curl command-line tool has not already been installed, you can find it in the repository, and you should adapt the IP address for the plug to the local network in the URL by replacing in the curl command with the correct address of your LAN. This address is normally found in a registry inside the router, because the router keeps track of registered devices and their addresses. If you change the password for the plug during its configuration, then 1234 should be replaced with the pertinent value.

The commands for turning the smart plug off and various other commands are created in analogous fashion, specifying OFF instead of ON in the fourth line. Along with setup commands (line 3), you also can use queries with id="get" as an attribute. The XML format from Listing 2 allows the plug to report on the current status.

Listing 2

Reporting Current Status

<?xml version="1.0" encoding="UTF8"?>
  <SMARTPLUG id="edimax">
    <CMD id="get">

Now you have access to some of the functions available on the Edimax plug. Because the commands – and therefore the XML files – are static, it would appear to be sufficient merely to work with curl scripts. However, controlling the plug via a time schedule is somewhat more complex and requires coding, which is where Python comes in handy.

Time Control

The SP1101W smart plug supports schedules via programmed switching times and something like a timetable. If switching times contradict the timetable, the timetable takes precedence. As an aside, the schedule is invisible in the app. During testing, I could not figure out exactly why Edimax stored the data twice. However, because control occurs exclusively through the app, it did not present any practical problems.

You should specify separately when the power is on and not for each day of the week (Figure 2). This approach does not seem optimal, because it restricts you to setting start times for just the upcoming seven days. On the other hand, this type of scheduling suffices for most niche uses. Listing 3 shows the query of the currently stored schedule. Listing 4 shows the answer by the web server with some of the zeros removed for convenience.

Listing 3

Querying the Schedule

<?xml version="1.0" encoding="UTF8"?>
<SMARTPLUG id="edimax">
  <CMD id="get">

Listing 4

Web Server Response

01 <?xml version="1.0" ?>
02 <SMARTPLUG id="edimax">
03   <CMD id="get">
04    <SCHEDULE>
05     <Device.System.Power.Schedule.0.List>30401-50701</Device.System.Power.Schedule.0.List>
06     <Device.System.Power.Schedule.1.List/>
07     <Device.System.Power.Schedule.2.List/>
08     <Device.System.Power.Schedule.3.List/>
09     <Device.System.Power.Schedule.4.List/>
10     <Device.System.Power.Schedule.5.List/>
11     <Device.System.Power.Schedule.6.List/>
12     <Device.System.Power.Schedule.0 value="ON">000000000000000000000000000000000000000000000FFFFFFFFFFFFFFF000000000000000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFF0000...000</Device.System.Power.Schedule.0>
13     <Device.System.Power.Schedule.1 value="ON">000...000</Device.System.Power.Schedule.1>
14     <Device.System.Power.Schedule.2 value="ON">000...000</Device.System.Power.Schedule.2>
15     <Device.System.Power.Schedule.3 value="ON">000...000</Device.System.Power.Schedule.3>
16     <Device.System.Power.Schedule.4 value="ON">000...000</Device.System.Power.Schedule.4>
17     <Device.System.Power.Schedule.5 value="ON">000...000</Device.System.Power.Schedule.5>
18     <Device.System.Power.Schedule.6 value="ON">000...000</Device.System.Power.Schedule.6>
19    </SCHEDULE>
20   </CMD>
Figure 2: The schedule allows you to determine on/off times.

At first glance, the letters and digits don't make sense; however, their meaning quickly becomes evident. The entries in the Device.System.Power.Schedule.<n>.List nodes code for the times in HourMinuteHourMinute1 format. The first HourMinute pair designates the time for turning the plug on, and the second HourMinute pair is the time to switch off. A redundant 1 is added at the end.

Line 5 shows that the plug is turned on from 3am to 4am (30401) and 5am to 7am (50701) on Sunday. Hours and minutes greater than 9 are always programmed using letters of the alphabet – first lowercase, then uppercase, up to the letter X which represents the number 59.

The nodes named Device.System.Power.Schedule.<n> contain the same information in a different format. The plug uses binary codes for each minute and represents four minutes as a hexadecimal digit. Hex 1111b therefore means that the plug should remain on for four consecutive minutes. For an entire day, you specify 360 hex digits, which is not a very efficient approach. Nonetheless, this type of coding prevents the appearance of strange symbols in the XML code that are not understood by the parser.

Because of the complex coding, it is not easy to enter a new time period. The better way would be first to query for the current state of the plug, modify the hex digits accordingly, and then send the entire new string back.

When the Rasp Pi controls the smart plug, this query becomes unnecessary, provided the Pi has recorded the current state. Specifying the time for a reboot works even more simply. The plug is usually switched ON, which is indicated in the schedule by one entry of the symbol F. Before shutting down, the Rasp Pi calculates the time period to the next boot and sets this value at 0. Once booted again, the Rasp Pi deletes the zeros.

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