Skip to content

Commit

Permalink
Added updating to Domoticz
Browse files Browse the repository at this point in the history
Wen configured in ini file program now updates virtual BWR102 scale
sensor to enable graphing. Emailing of weight data is now optional by
config in ini.
  • Loading branch information
keptenkurk committed Mar 28, 2016
1 parent 85c21fe commit 4e9e093
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 11 deletions.
21 changes: 18 additions & 3 deletions BS440.ini
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,39 @@

# The persons secion lists all the scale users
# The list maybe extended to 8 persons
# Domoticz index is only used if domoticz IP is set
[Person1]
username: John
useremail: [email protected]
domoticz_idx: 55

[Person2]
username: Jill
useremail: [email protected]
domoticz_idx: 56

# Scale properties
[Scale]
ble_address: aa:bb:cc:11:22:33
device_name: 0202B6332211CCBBAA

# Program behaviour
# Sender email/pwd: gmail account info
# Loglevel: debug | info | critical | error
[Program]
sender_email: [email protected]
sender_pwd: yourpassword
loglevel: debug
logfile: BS440.log

# Sender email/pwd: gmail account info
# Uncomment [Email] to have emails sent
[Email]
sender_email: [email protected]
sender_pwd: yourpassword

# Uncomment [Domoticz] to enable updating
# DomoticzIP: IP adres of Domoticz server
# DomoticzUser: Username of Domoticz server
# DomoticzPwd: Password of Domoticz server
#[Domoticz]
#domoticz_url: 192.168.0.10:8080
#domoticz_user: admin
#domoticz_pwd: admin
10 changes: 7 additions & 3 deletions BS440.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from binascii import hexlify
from BS440decode import *
from BS440mail import *
from BS440domoticz import *


def processIndication(handle, values):
Expand Down Expand Up @@ -146,8 +147,11 @@ def init_ble_mode():
time.sleep(30)
device.disconnect()
log.info('Done receiving data from scale')
# mail data if all received well
# process data if all received well
if persondata and weightdata and bodydata:
BS440mail(config, persondata, weightdata, bodydata)
if config.has_section('Email'):
BS440mail(config, persondata, weightdata, bodydata)
if config.has_section('Domoticz'):
UpdateDomoticz(config, weightdata)
else:
log.error('Incomplete data received. Unable to send mail')
log.error('Unreliable data received. Unable to process')
38 changes: 38 additions & 0 deletions BS440domoticz.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
'''
BS440domoticz.py
Update weight value to Domoticz home automation system
'''
import urllib2
import base64
import logging


def UpdateDomoticz(config, weightdata):
log = logging.getLogger(__name__)
# sort to have list starting with most recent weight
wds = sorted(weightdata, key=lambda k: k['timestamp'], reverse=True)
domoticzurl = config.get('Domoticz', 'domoticz_url')
domoticzuser = config.get('Domoticz', 'domoticz_user')
domoticzpwd = config.get('Domoticz', 'domoticz_pwd')
personsection = 'Person' + str(wds[0]['person'])
if config.has_section(personsection):
domoticzidx = config.get(personsection, 'domoticz_idx')
scaleuser = config.get(personsection, 'username')
else:
log.error('Unable to update Domoticz: No details found in ini file '
'for person %d' % (wds[0]['person']))
return
try:
log.info('Updating Domoticz for user %s at index %s with weight %s' % (
scaleuser, domoticzidx, wds[0]['weight']))
url = 'http://%s/json.htm?type=command&param=udevice&hid=2&' \
'did=%s&dunit=4&dtype=93&dsubtype=1&nvalue=0&svalue=%s' % (
domoticzurl, domoticzidx, wds[0]['weight'])
req = urllib2.Request(url)
base64string = base64.encodestring('%s:%s' % (
domoticzuser, domoticzpwd)).replace('\n', '')
req.add_header('Authorization', 'Basic %s' % base64string)
resp = urllib2.urlopen(req)
log.info('Domoticz succesfully updated')
except:
log.error('Unable to update Domoticz: Error sending data.')
6 changes: 3 additions & 3 deletions BS440mail.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ def rowdata(header, dataset, property, bib):

def BS440mail(config, persondata, weightdata, bodydata):
log = logging.getLogger(__name__)
FromAddr = config.get('Program', 'sender_email')
Password = config.get('Program', 'sender_pwd')
CcAddr = [config.get('Program', 'sender_email')]
FromAddr = config.get('Email', 'sender_email')
Password = config.get('Email', 'sender_pwd')
CcAddr = [config.get('Email', 'sender_email')]

personsection = 'Person' + str(persondata[0]['person'])
if config.has_section(personsection):
Expand Down
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ https://keptenkurk.wordpress.com/2016/02/07/connecting-the-medisana-bs440-blueto

# Description
In it's current state this program listens for data from a BS440
bluetooth scale. Once connected, data is read from the scale and
the last 3 stored sets of data will be mailed to the user.
bluetooth scale. Once connected, data is read from the scale. Depending
on the config in ini the program will
* mail the last 3 stored sets of data to the user
* update a virtual sensor in Domoticz home automation system

# ini file
Before using this program change the settings in the ini file
Expand Down

0 comments on commit 4e9e093

Please sign in to comment.