Sending Numeric Data to Device Cloud in C

A client application can send numeric data, such as readings from a sensor, to Device Cloud using the telemetry APIs.

Numeric data (telemetry) appears as properties on the thing details page. Click the property name to see the values published from the application in graph and table format. For more information about viewing properties, see Device Cloud Management Portal User's Guide: Viewing the Properties of a Thing.

The function parameters max_time_out and txn are for future use.

You must have previously initialized your application (see Initializing Your C Application).

The thing definition that corresponds to your application must have the telemetry object defined as a property, or the thing definition must have Auto def properties selected. For more information about defining properties, see Device Cloud Management Portal User's Guide: Defining Properties.

To send data to Device Cloud, the application must be connected to Device Cloud.

  1. Allocate the memory for the data object.

    For the handle parameter, specify the value returned from the iot_initialize function, which you called when you registered your application.

    Specify a name and a data type for the telemetry object. The value of the name parameter must match the property key specified in the thing definition.

    iot_telemetry_t* temperature;
    temperature = iot_telemetry_allocate(agentLib, "temperature", IOT_TYPE_FLOAT32);
  2. Register the telemetry object with the agent.

    For the txn parameter, specify NULL, and for the max_time_out parameter, specify zero.

    result = iot_telemetry_register(temperature, NULL, 0);
  3. Collect the data.
  4. (Optional) Specify the data collection time.

    In the following example, the collection time is the current time, which is the default.

    iot_timestamp_t tstamp;
    
    tstamp = iot_timestamp_now();
    result = iot_telemetry_timestamp_set(temperature, tstamp);

    You can also specify the timestamp as an offset in milliseconds from the current time to specify an earlier or later collection time. In the following example, the collection time is two minutes earlier than the current time.

    iot_timestamp_t tstamp;
    
    tstamp = iot_timestamp_now() - 120000;
    result = iot_telemetry_timestamp_set(temperature, tstamp);
  5. Send the data to Device Cloud.

    For the txn parameter, specify NULL and for the max_time_out parameter, specify zero.

    result = iot_telemetry_publish(temperature, NULL, 0, IOT_TYPE_FLOAT64, 17.5);

    When your application runs, the application transmits the numeric value to Device Cloud, and it appears under the property name on the thing details page.

Examples

The iot-app-simple-telemetry sample application provided with the agent shows example code for sending numeric data to Device Cloud.

If you do not need the object after you send the data, call the iot_telemetry_deregister, iot_telemetry_free functions to free the memory allocated for the object.