Created
January 18, 2016 20:53
-
-
Save jcefoli/2ffcb0a5829fd0c18f5f to your computer and use it in GitHub Desktop.
Connects to DLink DSP-215 to print out realtime wattage used
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
# Modified from https://github.com/fake-name/DSP-W215-Poller | |
import time | |
import urllib.parse | |
import urllib.request | |
import random | |
DSP_OUTLET_IP = '192.168.1.2' | |
UPDATE_INTERVAL = 2 | |
class DSPInterface(object): | |
def __init__(self, plugIp): | |
self.ip = plugIp | |
def getReading(self): | |
pass | |
postData = {'request': 'create_chklst'} | |
postData = urllib.parse.urlencode(postData).encode("ascii") | |
req = urllib.request.Request("http://{ip}/my_cgi.cgi?{rand}".format(ip=self.ip, rand=str(random.random())), data=postData) | |
response = urllib.request.urlopen(req) | |
ret = response.read().decode("utf-8") | |
ret = ret.strip() | |
lines = ret.split("\n") | |
retVal = None | |
for line in lines: | |
if line.startswith("Meter Watt:"): | |
power = line.split()[-1] | |
if retVal: | |
raise ValueError("Two power readings in one response?") | |
try: | |
retVal = float(power) | |
except ValueError: | |
print("Error on string '%s'" % line) | |
return None | |
return retVal | |
if __name__ == "__main__": | |
outlet = DSPInterface(DSP_OUTLET_IP) | |
print("Plug opened. Handle = %s" % outlet) | |
nextRun = time.time() + UPDATE_INTERVAL | |
while 1: | |
powerReading = outlet.getReading() | |
print(powerReading) | |
while time.time() < nextRun: | |
time.sleep(1) | |
nextRun += UPDATE_INTERVAL |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment