The MQTT Library functions provide access to publish/subscribe using MQTT protocol. All of the publish functions below use QOS level 1. For more information about Medium One MQTT, check out the MQTT section.
Note that even though you define the device in the arguments, these functions only work if you are interacting with the same device that triggered the workflow. In other words, if device1 triggers a workflow, you cannot publish an event to device2 from that workflow.
Import
To use this library and its functions, you must use the import line at the beginning of your Base Python code.
import MQTT
Publish event to client
MQTT.publish_event_to_client(device, payload, encoding='utf-8')
Publishes the string payload
encoded as encoding
via the Medium One MQTT broker to the topic 1/<project_mqtt_id>/<user_mqtt_id>/<device>/event
. To find <project_mqtt_id>
and <user_mqtt_id>
, refer to the Medium One MQTT Introduction.
Credit cost: 1
Parameters
- device:
str|unicode
sub-division of topic to publish to. - payload:
str|unicode
data to publish. - encoding:
str|unicode
encoding of payload. Supported encodings:utf-8
,latin1
Return Value
None
Example
- Sample Code:
-
import MQTT if IONode.get_input('heartrate')['event_data']['value'] > 140: MQTT.publish_event_to_client('smart-watch', 'Heartrate is too high!')
-
Exceptions
- Exception
- Any error communicating with broker
Publish update to client
MQTT.publish_update_to_client(device, update_type, payload, encoding='utf-8')
Publishes the string payload
encoded as encoding
via the Medium One MQTT broker to the topic 1/<project_mqtt_id>/<user_mqtt_id>/<device>/update/<update_type>
. To find <project_mqtt_id>
and <user_mqtt_id>
, refer to the Medium One MQTT Introduction.
Credit cost: 1
Parameters
- device:
str|unicode
sub-division of topic to publish to. - update_type:
str|unicode
sub-division of topic to publish to. - payload:
str|unicode
data to publish. - encoding:
str|unicode
encoding of payload. Supported encodings:utf-8
,latin1
Return Value
None
Example
- Sample Code:
-
import MQTT MQTT.publish_update_to_client('smart-watch', 'name', 'smart')
-
Exceptions
- Exception
- Any error communicating with broker
Publish update to group
MQTT.publish_update_to_group(group_id, update_type, payload, encoding='utf-8')
Publishes the string payload
encoded as encoding
via the Medium One MQTT broker to the topic 2/<project_mqtt_id>/<group_id>/<update_type>
. To find<project_mqtt_id>
, refer to the Medium One MQTT Introduction.
Credit cost: 1
Parameters
- group_id:
str|unicode
sub-division of topic to publish to. - update_type:
str|unicode
sub-division of topic to publish to. - payload:
str|unicode
data to publish. - encoding:
str|unicode
encoding of payload. Supported encodings:utf-8
,latin1
Return Value
None
Example
- Sample Code:
-
import MQTT MQTT.publish_update_to_group('watches', 'firmware', '2.1')
-
Exceptions
- Exception
- Any error communicating with broker