Quellcode durchsuchen

Add support for ThingsBoard (#63)

* posibility to upload MQTT data to thingsboard

* use reconnect for thingsboard-json

* update readme and config example

* Update README.md

* Update config.ini.dist

* Update config.ini.dist
Artur Neumann vor 7 Jahren
Ursprung
Commit
58aeb016fc
3 geänderte Dateien mit 31 neuen und 11 gelöschten Zeilen
  1. 8 1
      README.md
  2. 8 5
      config.ini.dist
  3. 15 5
      miflora-mqtt-daemon.py

+ 8 - 1
README.md

@@ -25,6 +25,7 @@ The program can be executed in **daemon mode** to run continuously in the backgr
     * following the [Homie Convention v2.0.5](https://github.com/marvinroger/homie)
     * following the [mqtt-smarthome architecture proposal](https://github.com/mqtt-smarthome/mqtt-smarthome)
     * using the [HomeAssistant MQTT discovery format](https://home-assistant.io/docs/mqtt/discovery/)
+    * using the [ThingsBoard.io](https://thingsboard.io/) MQTT interface
 * Announcement messages to support auto-discovery services
 * MQTT authentication support
 * No special/root privileges needed
@@ -216,6 +217,13 @@ Number Balcony_Petunia_Light "Balcony Petunia Sunlight Intensity [%d lux]" <text
 Paste the presented items definition into an openHAB items file and you are ready to go.
 Be sure to install the used MQTT Binding and JSONPath Transformation openHAB addons beforehand.
 
+### ThingsBoard
+
+to integrate with [ThingsBoard.io](https://thingsboard.io/):
+
+1. in your `config.ini` set `reporting_method = thingsboard-json`
+1. in your `config.ini` assign unique sensor names for your plants
+1. on the ThingsBoard platform create devices and use `Access token` as `Credential type` and the chosen sensor name as token
 
 ----
 
@@ -227,4 +235,3 @@ Be sure to install the used MQTT Binding and JSONPath Transformation openHAB add
 > The authors will not be held responsible in the event of device failure or withered plants.
 > 
 > This project is in no way affiliated with, authorized, maintained, sponsored or endorsed by *Xiaomi* or any of its affiliates or subsidiaries.
-

+ 8 - 5
config.ini.dist

@@ -9,12 +9,14 @@
 # Currently supported:
 #
 #           mqtt-json - Publish to an MQTT broker in a proprietary json format (Default)
-#          mqtt-homie - Publish to an MQTT broker following the Homie MQTT convention 
+#          mqtt-homie - Publish to an MQTT broker following the Homie MQTT convention
 #                       (https://github.com/marvinroger/homie)
 #      mqtt-smarthome - Publish to an MQTT broker following the mqtt-smarthome proposal
 #                       (https://github.com/mqtt-smarthome/mqtt-smarthome)
 #  homeassistant-mqtt - Publish to an MQTT broker following the HomeAssistant discovery format
 #                       (https://www.home-assistant.io/docs/mqtt/discovery/)
+#    thingsboard-json - Publish to the ThingsBoard MQTT broker
+#                       (https://thingsboard.io)
 #                json - Print to stdout as json encoded strings
 #
 #reporting_method = mqtt-json
@@ -42,9 +44,11 @@
 #keepalive = 60
 
 # The MQTT base topic to publish all Mi Flora sensor data topics under.
-# Default depends on the configured reporting_mode (mqtt-json, mqtt-smarthome: miflora, mqtt-homie: homie)
-#base_topic = miflora
-#base_topic = homie
+# Default depends on the configured reporting_method
+#base_topic = miflora                   # Default for: mqtt-json, mqtt-smarthome
+#base_topic = homie                     # Default for: mqtt-homie
+#base_topic = homeassistant             # Default for: homeassistant-mqtt
+#base_topic = v1/devices/me/telemetry   # Default for: thingsboard-json
 
 # Homie specific: The device ID for this daemon instance (Default: miflora-mqtt-daemon)
 #homie_device_id = miflora-mqtt-daemon
@@ -77,4 +81,3 @@
 #Schefflera@Living = C4:7C:8D:11:22:33
 #JapaneseBonsai    = C4:7C:8D:44:55:66
 #Petunia@Balcony   = C4:7C:8D:77:88:99
-

+ 15 - 5
miflora-mqtt-daemon.py

@@ -131,6 +131,8 @@ if reporting_mode == 'mqtt-homie':
     default_base_topic = 'homie'
 elif reporting_mode == 'homeassistant-mqtt':
     default_base_topic = 'homeassistant'
+elif reporting_mode == 'thingsboard-json':
+    default_base_topic = 'v1/devices/me/telemetry'
 else:
     default_base_topic = 'miflora'
 
@@ -140,7 +142,7 @@ sleep_period = config['Daemon'].getint('period', 300)
 miflora_cache_timeout = sleep_period - 1
 
 # Check configuration
-if reporting_mode not in ['mqtt-json', 'mqtt-homie', 'json', 'mqtt-smarthome', 'homeassistant-mqtt']:
+if reporting_mode not in ['mqtt-json', 'mqtt-homie', 'json', 'mqtt-smarthome', 'homeassistant-mqtt', 'thingsboard-json']:
     print_line('Configuration parameter reporting_mode set to an invalid value', error=True, sd_notify=True)
     sys.exit(1)
 if not config['Sensors']:
@@ -150,7 +152,7 @@ if not config['Sensors']:
 print_line('Configuration accepted', console=False, sd_notify=True)
 
 # MQTT connection
-if reporting_mode in ['mqtt-json', 'mqtt-homie', 'mqtt-smarthome', 'homeassistant-mqtt']:
+if reporting_mode in ['mqtt-json', 'mqtt-homie', 'mqtt-smarthome', 'homeassistant-mqtt', 'thingsboard-json']:
     print_line('Connecting to MQTT broker ...')
     mqtt_client = mqtt.Client()
     mqtt_client.on_connect = on_connect
@@ -185,8 +187,9 @@ if reporting_mode in ['mqtt-json', 'mqtt-homie', 'mqtt-smarthome', 'homeassistan
     else:
         if reporting_mode == 'mqtt-smarthome':
             mqtt_client.publish('{}/connected'.format(base_topic), payload='1', retain=True)
-        mqtt_client.loop_start()
-        sleep(1.0) # some slack to establish the connection
+        if reporting_mode != 'thingsboard-json':
+            mqtt_client.loop_start()
+            sleep(1.0) # some slack to establish the connection
 
 sd_notifier.notify('READY=1')
 
@@ -337,11 +340,18 @@ while True:
         for param,_ in parameters.items():
             data[param] = flora['poller'].parameter_value(param)
         print_line('Result: {}'.format(json.dumps(data)))
-        
+
         if reporting_mode == 'mqtt-json':
             print_line('Publishing to MQTT topic "{}/{}"'.format(base_topic, flora_name))
             mqtt_client.publish('{}/{}'.format(base_topic, flora_name), json.dumps(data))
             sleep(0.5) # some slack for the publish roundtrip and callback function
+        elif reporting_mode == 'thingsboard-json':
+            print_line('Publishing to MQTT topic "{}" username "{}"'.format(base_topic, flora_name))
+            mqtt_client.username_pw_set(flora_name)
+            mqtt_client.reconnect()
+            sleep(1.0)
+            mqtt_client.publish('{}'.format(base_topic), json.dumps(data))
+            sleep(0.5) # some slack for the publish roundtrip and callback function
         elif reporting_mode == 'homeassistant-mqtt':
             print_line('Publishing to MQTT topic "{}/sensor/{}/state"'.format(base_topic, flora_name).lower())
             mqtt_client.publish('{}/sensor/{}/state'.format(base_topic, flora_name).lower(), json.dumps(data))