Created
May 8, 2017 08:25
-
-
Save koenkooi/da754790bb3ded30d5ea1ea64849b253 to your computer and use it in GitHub Desktop.
Ducobox python
This file contains 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/env python | |
import time | |
import serial | |
import urllib2 | |
import base64 | |
import json | |
from decimal import * | |
def ducocommand(command, verbose = 0): | |
ser.write(command) | |
reply = ser.readline().split('\r') | |
time.sleep(0.1) | |
if verbose > 0: | |
print reply | |
if reply[-1].strip() is not '>': | |
print "Reply was \"%s\", instead of \">\"" %reply[-1].strip() | |
return False | |
else: | |
return reply | |
ser = serial.Serial( | |
port='/dev/ttyUSB0', | |
baudrate = 115200, | |
parity=serial.PARITY_NONE, | |
stopbits=serial.STOPBITS_ONE, | |
bytesize=serial.EIGHTBITS, | |
timeout=1 | |
) | |
# 1 = Box | |
# 2 = klep slaapkamer (RH) idx 329 | |
# 3 = klep WC boven | |
# 4 = klep washok (RH) idx 330 | |
# 5 = klep pantry | |
# 6 = klep woonkamer (CO2) idx 324 | |
# 7 = klep WC beneden | |
# 8 = klep douche beneden (RH) idx 331 | |
# 9 = knop douche boven idx 332 | |
# 10 = knop slaapkamer idx 325 | |
# 11 = schakelaar WC boven | |
# 12 = knop douche beneden (RH) idx 333 | |
# 13 = knop logeerkamer idx 327 | |
# 14 = knop hadewych idx 328 | |
# 15 = schakelaar WC beneden | |
# 16 = knop pantry idx 326 | |
# 17 = rooster werkverdieping | |
# 18 = knop eethoek | |
# 19 = rooster keuken | |
# 20 = rooster vide | |
# 133 = ???? | |
# node struct: nodeid, type, temp, humidity, co2, temp domo idx, humidity domo idx, co2 domo idx | |
nodetypes = { | |
'BOX': 'Duco Box', | |
'SWITCH': 'schakelaar', | |
'UC': 'bedieningsschakelaar', | |
'UCCO2': 'bedieningsschakelaar met CO2 sensor', | |
'UCRH': 'bedieningsschakelaar met luchtvochtigheidsensor', | |
'VLV': 'regelklep', | |
'VLVCO2': 'regelklep met CO2 sensor', | |
'VLVRH': 'regelklep met luchtvochtigheidsensor', | |
'CLIMA': 'ventilatierooster' | |
} | |
switchstates = { 'On': 1, 'Off': 0 } | |
domoticz = "http://domoticz-gateway.local:8080/" | |
domoticzusername = "" | |
domoticzpassword = "" | |
base64string = base64.encodestring('%s:%s' % (domoticzusername, domoticzpassword)).replace('\n', '') | |
co2nodesdomoticz = {6: 324, 10: 325, 13: 327, 14: 328, 16: 326} | |
rhnodesdomoticz = {2: 329, 4: 330, 8: 331, 9: 332, 12: 333} | |
switchnodesdomoticz = {11: 334, 15: 335, 133:336} | |
debietnodesdomoticz = {1: 337, 2: 338, 3: 339, 4: 340, 5: 341, 6: 342, 7: 343, 8: 344, 17: 345, 19: 346, 20: 347} | |
tempnodesdomoticz = {2: 358, 4: 359, 6: 360, 8: 361, 9: 362, 10: 363, 12: 364, 13: 365, 14: 366, 16: 367, 17: 368, 18: 369, 19: 370, 20: 371} | |
co2nodes = [] | |
rhnodes = [] | |
tempnodes = [] | |
switchnodes = [] | |
debietnodes = [] | |
line = '' | |
commandreply = ducocommand(b'network\r') | |
start = commandreply.index(' --- start list ---') | |
end = commandreply.index(' --- end list ---') | |
result = commandreply[start+1:end] | |
#print result | |
for x in result: | |
#print x | |
try: | |
nodeid = int(x.split('|')[0]) | |
except Exception: | |
continue | |
# Type (Box, schakelaar, klep etc) | |
nodetype = x.split('|')[2].strip() | |
#print nodetype | |
print "Node %i is een %s" % (nodeid, nodetypes[nodetype]) | |
# Temperatuur in decigraden Celcius | |
tempval = x.split('|')[18].strip() | |
if tempval is not '-' and tempval is not '0': | |
tempnodes.append(nodeid) | |
temp = float(tempval) / 10.0 | |
print "\tmeet %.2f graden celcius" % (temp) | |
# /json.htm?type=command¶m=udevice&idx=IDX&nvalue=0&svalue=TEMP | |
try: | |
url = domoticz + "json.htm?type=command¶m=udevice&idx=" + str(tempnodesdomoticz[nodeid]) + "&nvalue=0&svalue=" + str(temp) | |
request = urllib2.Request(url) | |
request.add_header("Authorization", "Basic %s" % base64string) | |
result = urllib2.urlopen(request) | |
tempjson = json.loads(result.read()) | |
except urllib2.URLError: | |
print "Updating temperature for %i went wrong" % nodeid | |
if nodetype.find('CO2') >= 0 and not nodeid in co2nodes: | |
co2nodes.append(nodeid); | |
#print "co2 %i" % nodeid | |
if nodetype.find('RH') >= 0 and not nodeid in rhnodes: | |
rhnodes.append(nodeid); | |
#print "rh %i" % nodeid | |
# Todo: get switch state and only send changes | |
if nodetype.find('SWITCH') >= 0 and not nodeid in switchnodes: | |
oldswitchstate = 0 | |
switchnodes.append(nodeid); | |
#print "switch %i" % nodeid | |
switchstate = x.split('|')[19].split(':')[0].strip() | |
print "\tstatus %s" % (switchstate) | |
if switchstate.find('OFF') >= 0 or switchstate.find('FADE') >= 0: | |
switchstate = 0 | |
else: | |
switchstate = 1 | |
# /json.htm?type=devices&rid=IDX | |
try: | |
url = domoticz + "json.htm?type=devices&rid=" + str(switchnodesdomoticz[nodeid]) | |
request = urllib2.Request(url) | |
request.add_header("Authorization", "Basic %s" % base64string) | |
result = urllib2.urlopen(request) | |
switchjson = json.loads(result.read()) | |
oldswitchstate = switchstates[switchjson["result"][0]["Status"]] | |
except urllib2.URLError: | |
print "Getting switch state for %i went wrong" % nodeid | |
#print "switch %i %i" % (oldswitchstate, switchstate) | |
# only update on change | |
if oldswitchstate != switchstate: | |
# /json.htm?type=command¶m=udevice&idx=IDX&nvalue=? | |
try: | |
url = domoticz + "json.htm?type=command¶m=udevice&idx=" + str(switchnodesdomoticz[nodeid]) + "&nvalue=" + str(switchstate) | |
#print url | |
request = urllib2.Request(url) | |
request.add_header("Authorization", "Basic %s" % base64string) | |
result = urllib2.urlopen(request) | |
except urllib2.URLError: | |
print "url went wrong" | |
if nodetype.find('VLV') >= 0 or nodetype.find('CLIMA') >= 0 or nodetype.find('BOX') >= 0: | |
debietnodes.append(nodeid); | |
#print "Klep/rooster %i" % nodeid | |
debiet = int(x.split('|')[10].split(':')[0].strip()) | |
print "\tdebiet %i" % (debiet) | |
# /json.htm?type=command¶m=udevice&idx=IDX&nvalue=0&svalue=PERCENTAGE | |
try: | |
url = domoticz + "json.htm?type=command¶m=udevice&idx=" + str(debietnodesdomoticz[nodeid]) + "&nvalue=0&svalue=" + str(debiet) | |
#print url | |
request = urllib2.Request(url) | |
request.add_header("Authorization", "Basic %s" % base64string) | |
result = urllib2.urlopen(request) | |
except urllib2.URLError: | |
print "url went wrong" | |
i = 0 | |
while i < len(co2nodes): | |
commandreply = ducocommand(b'nodeparaget %i 74\r' % co2nodes[i]) | |
#print commandreply | |
co2ppm = int(commandreply[2].split('--> ')[1]) | |
print "Node %i meet %i CO2 ppm" % (co2nodes[i], co2ppm) | |
if co2ppm < 50: | |
continue | |
# /json.htm?type=command¶m=udevice&idx=IDX&nvalue=PPM | |
try: | |
url = domoticz + "json.htm?type=command¶m=udevice&idx=" + str(co2nodesdomoticz[co2nodes[i]]) + "&nvalue=" + str(co2ppm) | |
#print url | |
request = urllib2.Request(url) | |
request.add_header("Authorization", "Basic %s" % base64string) | |
result = urllib2.urlopen(request) | |
except urllib2.URLError: | |
print "url went wrong" | |
i += 1 | |
i = 0 | |
while i < len(rhnodes): | |
commandreply = ducocommand(b'nodeparaget %i 75\r' % rhnodes[i]) | |
#print commandreply | |
rh = float(commandreply[2].split('--> ')[1]) / 100.0 | |
print "Node %i meet %.2f procent luchtvochtigheid" % (rhnodes[i], rh) | |
if rh < 1: | |
continue | |
if rh > 25 and rh < 60: | |
humstat = 1 | |
if rh >= 60: | |
humstat = 3 | |
if rh <= 25: | |
humstat = 2 | |
# /json.htm?type=command¶m=udevice&idx=IDX&nvalue=HUM&svalue=HUM_STAT | |
try: | |
url = domoticz + "json.htm?type=command¶m=udevice&idx=" + str(rhnodesdomoticz[rhnodes[i]]) + "&nvalue=" + str(rh) + "&svalue=" + str(humstat) | |
#print url | |
request = urllib2.Request(url) | |
request.add_header("Authorization", "Basic %s" % base64string) | |
result = urllib2.urlopen(request) | |
except urllib2.URLError: | |
print "url went wrong" | |
i += 1 | |
print " " |
Hi,
Did you solve this issue? I get the same error.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi,
I'm getting an when i run the script the next error:
pi@PythonServer:~/Desktop/script $ python duco.py
Reply was "", instead of ">"
Traceback (most recent call last):
File "duco.py", line 92, in
start = commandreply.index(' --- start list ---')
AttributeError: 'bool' object has no attribute 'index'
Is there something i missed maybe?