Giuseppe Parrello

 

Come gestire lo Smart Plug D-Link DSP-W215


In questa pagina fornisco i codici script allo scopo di gestire lo Smart Plug D-Link DSP-W215.


Linguaggio Python

Il seguente script accende lo Smart Plug, visualizza l'attuale consumo di corrente in Watt, visualizza la temperatura del dispositivo, visualizza il consumo totale di corrente, e visualizza lo stato attuale del dispositivo:

#!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)

Il seguente script spegne lo Smart Plug, visualizza l'attuale consumo di corrente in Watt, visualizza la temperatura del dispositivo, visualizza il consumo totale di corrente, e visualizza lo stato attuale del dispositivo:

#!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)

Il seguente script visualizza l'attuale consumo di corrente in Watt, visualizza il consumo totale di corrente, visualizza la temperatura del dispositivo, e visualizza lo stato attuale del dispositivo:

#!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("  ")

Il seguente script visualizza lo stato attuale del dispositivo:

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

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

print(sp.state)

Il seguente script permette di accendere, spegnere e visualizzare alcune informazioni in maniera interattiva:

#!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