Skip to content

Commit

Permalink
Merge pull request #56 from sgtwilko/MQTT_separate_topics
Browse files Browse the repository at this point in the history
Publsh to separate Mqtt subtopics
  • Loading branch information
jim-easterbrook committed Oct 29, 2017
2 parents ab33287 + fc86f9b commit 6489c63
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 7 deletions.
5 changes: 5 additions & 0 deletions src/doc/guides/integration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ See :ref:`Dependencies - MQTT <dependencies-mqtt>` for details.
user = unknown
password = unknown
template = default
multi_topic = False

[logged]
services = ['mqtt', 'underground']
Expand All @@ -145,6 +146,10 @@ This allows clients to get an immediate response when they subscribe to a topic,

``auth``, ``user`` and ``password`` can be used for MQTT authentication.

``multi_topic`` is a boolean and should be set to ``True`` or ``False``.
If set to ``True`` pywws will also publish all the data each as separate subtopics of the configured ``topic``;
i.e., with the ``topic`` set to /weather/pywws pywws will also publish the outside temperature to ``/weather/pywws/temp_out`` and the inside temperature to ``/weather/pywws/temp_in``.

If these aren't obvious to you it's worth doing a bit of reading around MQTT.
It's a great lightweight messaging system from IBM, recently made more popular when Facebook published information on their use of it.

Expand Down
2 changes: 2 additions & 0 deletions src/pywws/services/mqtt.ini
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,5 @@ port = *port
client_id = *client_id
retain = *retain
auth = *auth
multi_topic = *multi_topic

17 changes: 10 additions & 7 deletions src/pywws/toservice.py
Original file line number Diff line number Diff line change
Expand Up @@ -323,13 +323,15 @@ def mqtt_send_data(self, timestamp, prepared_data, ignore_last_update=False):
client_id = prepared_data['client_id']
retain = prepared_data['retain'] == 'True'
auth = prepared_data['auth'] == 'True'
multi_topic = prepared_data['multi_topic'] == 'True'
# clean up the object
del prepared_data['topic']
del prepared_data['hostname']
del prepared_data['port']
del prepared_data['client_id']
del prepared_data['retain']
del prepared_data['auth']
del prepared_data['multi_topic']

mosquitto_client = mosquitto.Client(client_id, protocol=mosquitto.MQTTv31)
if auth:
Expand All @@ -348,13 +350,14 @@ def mqtt_send_data(self, timestamp, prepared_data, ignore_last_update=False):
mosquitto_client.connect(hostname, int(port))
mosquitto_client.publish(topic, json.dumps(prepared_data), retain=retain)

## commented out as sending the data as a json object (above)
## for item in prepared_data:
## if prepared_data[item] == '':
## prepared_data[item] = 'None'
## mosquitto_client.publish(
## topic + "/" + item + "/" + str(timestamp), prepared_data[item])
## time.sleep(0.200)
if multi_topic:
#Publish a messages, one for each item in prepared_data to separate Subtopics.
for item in prepared_data:
if prepared_data[item] == '':
prepared_data[item] = 'None'
mosquitto_client.publish(topic + "/" + item, prepared_data[item], retain=retain)
#Need to make sure the messages have been flushed to the server.
mosquitto_client.loop(timeout=0.5)

self.logger.debug("published data: %s", prepared_data)
mosquitto_client.disconnect()
Expand Down

0 comments on commit 6489c63

Please sign in to comment.