|
|
@@ -10,8 +10,9 @@ import paho.mqtt.client as mqtt
|
|
|
|
|
|
parameters = [MI_BATTERY, MI_CONDUCTIVITY, MI_LIGHT, MI_MOISTURE, MI_TEMPERATURE]
|
|
|
|
|
|
+# Intro
|
|
|
print('Xiaomi Mi Flora Plant Sensor MQTT Client/Daemon')
|
|
|
-print('Source: https://github.com/janwh/miflora-mqtt-daemon')
|
|
|
+print('Source: https://github.com/ThomDietrich/miflora-mqtt-daemon')
|
|
|
print()
|
|
|
|
|
|
# Eclipse Paho callbacks http://www.eclipse.org/paho/clients/python/docs/#callbacks
|
|
|
@@ -33,16 +34,18 @@ config.read(os.path.join(sys.path[0], 'config.ini'))
|
|
|
|
|
|
reporting_mode = config['General'].get('reporting_method', 'mqtt-json')
|
|
|
daemon_enabled = config['Daemon'].getboolean('enabled', True)
|
|
|
-sleep_period = config['Daemon'].getint('period', 300)
|
|
|
topic_prefix = config['MQTT'].get('topic_prefix', 'miflora')
|
|
|
-miflora_cache_timeout = config['MiFlora'].getint('cache_timeout', 600)
|
|
|
+sleep_period = config['Daemon'].getint('period', 300)
|
|
|
+#miflora_cache_timeout = config['MiFlora'].getint('cache_timeout', 600)
|
|
|
+miflora_cache_timeout = sleep_period - 1
|
|
|
|
|
|
+# Check configuration
|
|
|
if not reporting_mode in ['mqtt-json', 'json']:
|
|
|
print('Error. Configuration parameter reporting_mode set to an invalid value.', file=sys.stderr)
|
|
|
sys.exit(1)
|
|
|
if not config['Sensors']:
|
|
|
print('Error. Please add at least one sensor to the configuration file "config.ini".', file=sys.stderr)
|
|
|
- print('Scan for available Miflora sensors with "hcitool lescan".', file=sys.stderr)
|
|
|
+ print('Scan for available Miflora sensors with "sudo hcitool lescan".', file=sys.stderr)
|
|
|
sys.exit(1)
|
|
|
|
|
|
# MQTT connection
|
|
|
@@ -65,44 +68,44 @@ if reporting_mode == 'mqtt-json':
|
|
|
sleep(1) # some slack to establish the connection
|
|
|
|
|
|
# Initialize Mi Flora sensors
|
|
|
-flores = {}
|
|
|
-for flora in config['Sensors'].items():
|
|
|
+flores = dict()
|
|
|
+for [name, mac] in config['Sensors'].items():
|
|
|
print('Adding device from config to Mi Flora device list ...')
|
|
|
- print('Name: "{}"'.format(flora[0]))
|
|
|
- flores[flora[0]] = MiFloraPoller(mac=flora[1], cache_timeout=miflora_cache_timeout)
|
|
|
- print('Device name: "{}"'.format(flores[flora[0]].name()))
|
|
|
- print('MAC address: {}'.format(flora[1]))
|
|
|
- print('Firmware: {}'.format(flores[flora[0]].firmware_version()))
|
|
|
+ print('Name: "{}"'.format(name))
|
|
|
+ flora_poller = MiFloraPoller(mac=mac, cache_timeout=miflora_cache_timeout, retries=9)
|
|
|
+ flora_poller.fill_cache()
|
|
|
+ print('Device name: "{}"'.format(flora_poller.name()))
|
|
|
+ print('MAC address: {}'.format(flora_poller._mac))
|
|
|
+ print('Firmware: {}'.format(flora_poller.firmware_version()))
|
|
|
print()
|
|
|
+ flores[name] = flora_poller
|
|
|
|
|
|
# Sensor data retrieval and publication
|
|
|
while True:
|
|
|
- for flora in flores:
|
|
|
- data = {}
|
|
|
+ for [flora_name, flora_poller] in flores.items():
|
|
|
+ data = dict()
|
|
|
for param in parameters:
|
|
|
- data[param] = flores.get(flora).parameter_value(param)
|
|
|
+ data[param] = flora_poller.parameter_value(param)
|
|
|
timestamp = strftime('%Y-%m-%d %H:%M:%S', localtime())
|
|
|
|
|
|
if reporting_mode == 'mqtt-json':
|
|
|
- print('[{}] Attempting to publishing to MQTT topic "{}/{}" ...\nData: {}'.format(timestamp, topic_prefix, flora, json.dumps(data)))
|
|
|
- mqtt_client.publish('{}/{}'.format(topic_prefix, flora), json.dumps(data))
|
|
|
+ print('[{}] Attempting to publishing to MQTT topic "{}/{}" ...\nData: {}'.format(timestamp, topic_prefix, flora_name, json.dumps(data)))
|
|
|
+ mqtt_client.publish('{}/{}'.format(topic_prefix, flora_name), json.dumps(data))
|
|
|
sleep(0.5) # some slack for the publish roundtrip and callback function
|
|
|
print()
|
|
|
elif reporting_mode == 'json':
|
|
|
data['timestamp'] = timestamp
|
|
|
- data['name'] = flora
|
|
|
- data['mac'] = flores.get(flora)._mac
|
|
|
- print(json.dumps(data))
|
|
|
+ data['name'] = flora_name
|
|
|
+ data['mac'] = flora_poller._mac
|
|
|
+ print('Data:', json.dumps(data))
|
|
|
else:
|
|
|
raise NameError('Unexpected reporting_mode.')
|
|
|
|
|
|
- if not daemon_enabled:
|
|
|
- break
|
|
|
- else:
|
|
|
+ if daemon_enabled:
|
|
|
print('Sleeping ({} seconds) ...'.format(sleep_period))
|
|
|
sleep(sleep_period)
|
|
|
print()
|
|
|
-
|
|
|
-if reporting_mode == 'mqtt-json':
|
|
|
- mqtt_client.disconnect()
|
|
|
+ else:
|
|
|
+ mqtt_client.disconnect() if reporting_mode == 'mqtt-json'
|
|
|
+ break
|
|
|
|