Controlling a WiFi smart plug with the Raspberry Pi

Control Software

GitHub has a number of diverse projects to control the Edimax smart plugs independent of the manufacturer's cloud, including a simple online Python class [3] and a C# class [4]. However, the Python class only supports switching between On and Off. Therefore, it is a good idea to download the code specific to this project [5]. The library comprises various classes and supports all functions of the plug. The box titled "Installing the EdiPlug" describes the installation.

Installing the EdiPlug

To begin, clone the appropriate GitHub project [5] using the command in line 1 of Listing 5. Next, copy the Python library to a path that Python uses to search for packages (lines 2 and 3). Raspbian "jessie" only needs two packages (line 5), although other platforms might be missing python-requests. The correct installation is tested with the command:

$ samples/testinstall.py

Once the plug is registered in the LAN, check the communication with the command:

$ samples/powerstate.py -p 1234

Make sure to adjust the password appropriately, then load the library from your own programs with the command:

from ediplug import *

A directory of the classes and methods of the library can be found in the documentation of the package (README.md).

Listing 5

Installing the EdiPlug

git clone https://github.com/bablokb/ediplug.git
cd ediplug/src
sudo cp -a ediplug /usr/local/lib/python2.7/dist-packages
sudo apt-get update
sudo apt-get install python-netaddr python-netifaces

Listing 6 shows a few examples. Because the Edimax SP1101W receives its IP address via DHCP, the program first recognizes the plug by way of its local network address, starting with line 6. Normally, PlugFinder searches its entire subnetwork and finds all supported plugs. This method works only when all plugs have the same password.

Listing 6

Examples

01 #!/usr/bin/python
02
03 import time
04 from ediplug import *
05
06 pf = PlugFinder(password='1234')
07 plugs = pf.search()
08
09 #plugs = pf.search(network='192.168.2.100-192.168.2.120')
10 #plugs = pf.search(maxCount=1)
11 #plugs = pf.search(plugnames=['plug1','plug2'])
12
13 plug = plugs.values()[0]
14 #plug = plugs['plug1']
15
16 info = plug.getSystemInfo()
17 print info['Device.System.Name']
18
19 plug.setPowerState(active=True)
20 time.sleep(600)
21 plug.setPowerState(active=False)

To shorten the search process, you should feed PlugFinder additional information, such as an IP address range, a list of names, or the maximum number of plugs for the class to return (lines 9-11). Alternatively, when you know the IP address, you can generate the instance of the plug object directly.

The plug object methods query the Edimax smart plug for information or change their state. Line 16 returns a map of all the information for the system. The script then turns on the plug (line 19), waits 10 minutes, and turns it off again (line 21). For this example, the Rasp Pi should not be connected to the plug.

If the Rasp Pi is handling the smart plug all on its own and saves its own time schedule, these two commands suffice to control the Edimax SP1101W completely. In this scenario, the plug does not carry out switch processes autonomously.

Wake on RTC

In Listing 7, lines 11 to 20 show the original case. The Raspberry Pi programs the plug to turn off in five minutes and back on an hour later (lines 11-13 and 17). Then, the computer executes its own shutdown via the command in line 20.

Listing 7

Wake on RTC

01 #!/usr/bin/python
02
03 import time
04 from ediplug import *
05
06 pf = PlugFinder(password='1234')
07 plugs = pf.search()
08 plug = plugs.values()[0]
09
10 # Turn power off in 5 minutes, and back on in one hour from now
11 now = TPoint.now()
12 shutdownTime = now.createAfter(0,0,5)
13 bootTime     = shutdownTime.createAfter(0,1,0)
14
15 # Programming of the switching clock in the plug:
16 # Power is not available only between shutdownTime and bootTime
17 plug.setExclusiveState(shutdownTime,bootTime,active=False)
18
19 # Power down the Rasp Pi, before the power is turned off
20 os.system("sudo shutdown now")
21
22 # ----------- After the Boot. The following lines may be in /etc/rc.local ------
23
24 # pf = PlugFinder(password='1234')
25 # plugs = pf.search()
26 # plug = plugs.values()[0]
27 # plug.clearSchedule()

The Python class implements the time period without power. The code starting at line 24 runs immediately after bootup, perhaps via the /etc/rc.local file, and deletes all time schedules as a precaution.

Aside from these examples, the class contains a series of additional methods, such as setRange(<from>,<to>,active=true). This method contributes a new time schedule or deletes the schedule without altering existing entries, making it possible to operate the smart plug from the Pi just like from the smartphone app. Because the Edimax SP1101W saves the data, control also works with the Rasp Pi as well as the app.

Buy this article as PDF

Express-Checkout as PDF

Pages: 6

Price $2.95
(incl. VAT)

Buy Raspberry Pi Geek

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content