Skip to content

Instantly share code, notes, and snippets.

@adrianmihalko
Created December 10, 2017 14:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adrianmihalko/6e115ad442fa89a2cb4c529a1a15cddc to your computer and use it in GitHub Desktop.
Save adrianmihalko/6e115ad442fa89a2cb4c529a1a15cddc to your computer and use it in GitHub Desktop.
Miflora Domoticz
#!/usr/bin/env python3
#usage:
#python3 ./miflora-domoticz.py --idx_temp 61 --idx_lux 62 --idx_moist 63 --idx_cond 64 --backend gatttool poll C4:7C:8D:63:7A:11
import argparse
import re
import logging
#domoticz
import urllib.request
import base64
import time
from miflora.miflora_poller import MiFloraPoller, \
MI_CONDUCTIVITY, MI_MOISTURE, MI_LIGHT, MI_TEMPERATURE, MI_BATTERY
from miflora.backends.gatttool import GatttoolBackend
from miflora.backends.bluepy import BluepyBackend
from miflora import miflora_scanner
# Settings for the domoticz server
# Forum see: http://domoticz.com/forum/viewtopic.php?f=56&t=13306&hilit=mi+flora&start=20#p105255
domoticzserver = "192.168.1.2:8080"
domoticzusername = ""
domoticzpassword = ""
# So id devices use: sudo hcitool lescan
# Sensor IDs
# Create 4 virtual sensors in dummy hardware
# type temperature
# type lux
# type percentage (moisture)
# type custom (fertility)
base64string = base64.encodestring(('%s:%s' % (domoticzusername, domoticzpassword)).encode()).decode().replace('\n', '')
def domoticzrequest (url):
request = urllib.request.Request(url)
request.add_header("Authorization", "Basic %s" % base64string)
response = urllib.request.urlopen(request)
return response.read()
def valid_miflora_mac(mac, pat=re.compile(r"C4:7C:8D:[0-9A-F]{2}:[0-9A-F]{2}:[0-9A-F]{2}")):
if not pat.match(mac.upper()):
raise argparse.ArgumentTypeError('The MAC address "{}" seems to be in the wrong format'.format(mac))
return mac
def poll(args, backend):
poller = MiFloraPoller(args.mac, backend)
print("Getting data from Mi Flora")
print("FW: {}".format(poller.firmware_version()))
print("Name: {}".format(poller.name()))
print("Temperature: {}".format(poller.parameter_value(MI_TEMPERATURE)))
print("Moisture: {}".format(poller.parameter_value(MI_MOISTURE)))
print("Light: {}".format(poller.parameter_value(MI_LIGHT)))
print("Conductivity: {}".format(poller.parameter_value(MI_CONDUCTIVITY)))
print("Battery: {}".format(poller.parameter_value(MI_BATTERY)))
val_bat = "{}".format(poller.parameter_value(MI_BATTERY))
#domoticz
global domoticzserver
# Update temp
val_temp = "{}".format(poller.parameter_value(MI_TEMPERATURE))
domoticzrequest("http://" + domoticzserver + "/json.htm?type=command&param=udevice&idx=" + args.idx_temp + "&nvalue=0&svalue=" + val_temp + "&battery=" + val_bat)
# Update lux
val_lux = "{}".format(poller.parameter_value(MI_LIGHT))
domoticzrequest("http://" + domoticzserver + "/json.htm?type=command&param=udevice&idx=" + args.idx_lux + "&svalue=" + val_lux + "&battery=" + val_bat)
# Update moisture
val_moist = "{}".format(poller.parameter_value(MI_MOISTURE))
domoticzrequest("http://" + domoticzserver + "/json.htm?type=command&param=udevice&idx=" + args.idx_moist + "&svalue=" + val_moist + "&battery=" + val_bat)
# Update fertility
val_cond = "{}".format(poller.parameter_value(MI_CONDUCTIVITY))
domoticzrequest("http://" + domoticzserver + "/json.htm?type=command&param=udevice&idx=" + args.idx_cond + "&svalue=" + val_cond + "&battery=" + val_bat)
def scan(args, backend):
print('Scanning for 10 seconds...')
devices = miflora_scanner.scan(backend, 10)
print('Found {} devices:'.format(len(devices)))
for d in devices:
print(' {}'.format(d))
parser = argparse.ArgumentParser()
parser.add_argument('--idx_temp')
parser.add_argument('--idx_cond')
parser.add_argument('--idx_moist')
parser.add_argument('--idx_lux')
parser.add_argument('--backend', choices=['gatttool', 'bluepy'], default='gatttool')
parser.add_argument('-v', '--verbose', action='store_const', const=True)
subparsers = parser.add_subparsers(help='sub-command help')
parser_poll = subparsers.add_parser('poll', help='poll data from a sensor')
parser_poll.add_argument('mac', type=valid_miflora_mac)
parser_poll.set_defaults(func=poll)
parser_scan = subparsers.add_parser('scan', help='scan for devices')
parser_scan.set_defaults(func=scan)
args = parser.parse_args()
backend = None
if args.backend == 'gatttool':
backend = GatttoolBackend
elif args.backend == 'bluepy':
backend = BluepyBackend
else:
raise Exception('unknown backend: {}'.format(args.backend))
if args.verbose:
logging.basicConfig(level=logging.DEBUG)
args.func(args, backend)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment