Giuseppe Parrello

 

How to manage D-Link DSP-W215 Smart Plug


In this page I provide you with script codes in order to manage D-Link DSP-W215 Smart Plug.


Python language

The following script turns on the Smart Plug, shows the current power consumption in Watt, shows the device temperature, shows total power consumption, and shows the current device state:

#!python3
from pyW215.pyW215 import SmartPlug, ON, OFF

sp = SmartPlug('192.168.1.20', '******')
# Where ****** is the "code pin" printed on the setup card

# Get values if available otherwise return N/A
sp.state = ON

print(sp.current_consumption)
print(sp.temperature)
print(sp.total_consumption)

print(sp.state)

The following script turns off the Smart Plug, shows the current power consumption in Watt, shows the device temperature, shows total power consumption, and shows the current device state:

#!python3
from pyW215.pyW215 import SmartPlug, ON, OFF

sp = SmartPlug('192.168.1.20', '******')
# Where ****** is the "code pin" printed on the setup card

# Get values if available otherwise return N/A
sp.state = OFF

print(sp.current_consumption)
print(sp.temperature)
print(sp.total_consumption)

print(sp.state)

The following script shows the current power consumption in Watt, shows total power consumption, shows the device temperature, and shows the current device state:

#!python3
from pyW215.pyW215 import SmartPlug, ON, OFF

sp = SmartPlug('192.168.1.20', '******')
# Where ****** is the "code pin" printed on the setup card

# Get values if available otherwise return N/A
print("Current consumption = ", sp.current_consumption)
print("Total consumption   = ", sp.total_consumption)
print("Current temperature = ", sp.temperature)
print("Current Status      = ", sp.state)
print("  ")

The following script shows the current device state:

#!python3
from pyW215.pyW215 import SmartPlug, ON, OFF

sp = SmartPlug('192.168.1.20', '******')

print(sp.state)

The following script allows to turn on, to turn off and to show some information interactively:

#!python3
import time
from pyW215.pyW215 import SmartPlug, ON, OFF

def print_cli_usage():
  print ("Usage:")
  print ("  q|quit: quit manager")
  print ("  h|help: print this message")
  print ("  0|off : power off")
  print ("  1|on : power on")
  print ("  s|status: list current status")

def status():
  # Get values if available otherwise return N/A
  print ('Current consumption = ', sp.current_consumption)
  print ('Current temperature = ', sp.temperature)
  print ('Total consumption = ', sp.total_consumption)
  print ('Current State = ', sp.state)
  print ("")

def on():
  print ('We are going to power on')
  sp.state = ON
  print ('Current State = ', sp.state)
  print ("")

def off():
  print ('We are going to power off')
  sp.state = OFF
  print ('Current State = ', sp.state)
  print ("")

def handle_user_input():
  '''
  User interaction loop.
  '''
  while True:
    command_line = input("Enter a command: ")
    valid_cli=True
    command_line.lower() # convert all user input to lower case, i.e. cli is caseless
    argv = command_line.split() # i.e. don't allow parameters with space characters
    if len(argv) == 0:
      continue
    if argv[0] == "q" or argv[0] == "quit":
      print ("Bye!")
      return
    elif argv[0] == "h" or argv[0] == "help":
      print_cli_usage()
      continue
    elif argv[0] == "s" or argv[0] == "status":
      status()
    elif argv[0] == "1" or argv[0] == "on":
      on()
    elif argv[0] == "0" or argv[0] == "off":
      off()

    else:
      valid_cli=False

    if not valid_cli:
      print ("error: invalid command line:", command_line)
      print_cli_usage()

sp = SmartPlug('192.168.1.20', '******')

# print welcome message first
print ("Welcome to Dlink Power Plug controller")
print_cli_usage()
print ("")
status()
print ("")
# user interaction loop
handle_user_input()
# done