Skip to content

Commit

Permalink
Merge pull request #16 from david-m-m/new-mqtt
Browse files Browse the repository at this point in the history
mqtt: export values as single topics as well
  • Loading branch information
datenschuft committed Apr 1, 2019
2 parents 0299e15 + be8db0a commit 86a3c1d
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 0 deletions.
2 changes: 2 additions & 0 deletions config.sample
Expand Up @@ -42,6 +42,8 @@ mqttport=1883
mqttfields=pregard,psurplus,p1regard,p2regard,p3regard,p1surplus,p2surplus,p3surplus
#topic will be exted3ed with serial
mqtttopic=SMA-EM/status
# publish all values as single topics (0 or 1)
publish_single=1
# How frequently to send updates over (defaults to 20 sec)
min_update=30
#debug output
Expand Down
4 changes: 4 additions & 0 deletions contributors.md
Expand Up @@ -22,3 +22,7 @@ SMA-EM-Daemon contributors
* "remotedebug" to allow remote debug from PyCharm
* "influxdb" and sample grafana dashboard based on this plugin
* "symcon" to supply SMA EM/HOM and PV data to "IP-Symcon"

* **[david-m-m](https://github.com/david-m-m)**

* enhance mqtt module to export topics for all metrics, works with [mqtt_exporter](https://github.com/bendikwa/mqtt_exporter)
13 changes: 13 additions & 0 deletions features/mqtt.py
Expand Up @@ -2,6 +2,7 @@
Send SMA values to mqtt broker.
2018-12-23 Tommi2Day
2019-03-02 david-m-m
Configuration:
Expand All @@ -18,6 +19,9 @@
#topic will be exted3ed with serial
mqtttopic=SMA-EM/status
# publish all values additionally as single topics (0 or 1)
publish_single=1
# How frequently to send updates over (defaults to 20 sec)
min_update=5
Expand Down Expand Up @@ -53,6 +57,7 @@ def run(emparts,config):
mqttpass = config.get('mqttpass', None)
mqtttopic = config.get('mqtttopic',"SMA-EM/status")
mqttfields = config.get('mqttfields', 'pregard,psurplus')
publish_single = int(config.get('publish_single',0))

# mqtt client settings
myhostname = platform.node()
Expand Down Expand Up @@ -94,6 +99,13 @@ def run(emparts,config):
client.connect(str(mqtthost), int(mqttport))
client.on_publish = on_publish
client.publish(topic, payload)
# publish each value as separate topic
if publish_single == 1:
for item in data.keys():
itemtopic=topic+'/'+item
if mqtt_debug > 0:
print("mqtt: publishing %s:%s" % (itemtopic,data[item]) )
client.publish(itemtopic,str(data[item]))
if mqtt_debug > 0:
print("mqtt: sma-em data published %s:%s" % (
format(time.strftime("%H:%M:%S", time.localtime(mqtt_last_update))),payload))
Expand Down Expand Up @@ -121,6 +133,7 @@ def stopping(emparts,config):
pass

def on_publish(client,userdata,result):
time.sleep(0.01) # experimental value, seems to work...
pass

def config(config):
Expand Down

0 comments on commit 86a3c1d

Please sign in to comment.