Sending Data to Device Cloud in Python
The procedure to send alarms, string data, numeric data, and events to Device Cloud requires a single function call.
The function to publish attributes (string data) specifies the key of the item and the value.
The function to publish alarms specifies the key of the item and the value. It has an optional parameter to specify whether to republish the alarm even if the timestamp and state have not changed since the alarm was published previously.
The function to publish to properties (numeric data) specifies the key of the item and the value. It has optional parameters to specify a timestamp and whether to wait for confirmation that the value was published to Device Cloud.
The function to publish events specifies the message that appears in the event logs.
To publish an alarm, attribute, or property, you need the key of the alarm, attribute, or property defined in the thing definition.
To publish an attribute or property that is not defined in the thing definition, Auto def attributes or Auto def properties, respectively, must be selected in the thing definition. For more information about defining attributes and properties, see the following:
- Device Cloud Management Portal User's Guide: Defining Properties
- Device Cloud Management Portal User's Guide: Defining Attributes
To send data to Device Cloud, the application must be connected to Device Cloud.
The following example publishes an property to Device Cloud with a specific timestamp and does not wait for a response from Device Cloud.
ts = datetime(2018, 01, 01,11,34,22,11)
client.telemetry_publish(myPropertyKey, value, False, timestamp=ts)
Examples
The iot-simple-telemetry.py sample application provided with the agent shows example code for sending data to Device Cloud.