Sending String Data to Device Cloud in C

A client application on the device can send string data to Device Cloud using the attribute APIs.

String data appears as attributes under the Attributes tab on the thing details page. Click the View icon of an attribute to see the historical values published from the application. For more information about viewing attributes, see Device Cloud Management Portal User's Guide: Viewing and Updating the Attributes of a Thing.

In the options parameter of the API, you can specify additional, optional information, including the following:

  • maximum time to wait for the publish operation to complete

  • timestamp of the attribute

  • whether to publish the attribute value even if the value has not changed since the attribute was published previously

The function parameter txn is for future use.

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

The thing definition that corresponds to your application must have the attributes defined, or the thing definition must have Auto def attributes selected. For more information about defining attributes, see Device Cloud Management Portal User's Guide: Defining Attributes.

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

  1. (Optional) Specify the attribute options.

    In the following example, the collection time is two minutes earlier than the current time.

    iot_options_t *attribute_options = iot_options_allocate( agentLib );
    iot_timestamp_t tstamp;
    
    tstamp = iot_timestamp_now() - 120000;
    iot_options_set(attribute_options, "time_stamp", IOT_TYPE_UINT64, tstamp); 
    iot_options_set(attribute_options, "republish", IOT_TYPE_BOOL, IOT_TRUE);
    
  2. Send the attribute to Device Cloud.

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

    For the txn parameter, specify NULL

    If you do not want to publish any optional information, specify NULL for the options parameter.

    For the key parameter, specify the attribute key defined in the thing definition.

    result = iot_attribute_publish(agentLib, attribute_options, "myAttributeKey", "myAttributeValue");

    When your application runs, the application transmits the attribute value to Device Cloud, and it appears under the Attributes tab on the thing details page.

When you no longer need to send the data, call the iot_options_free function, if applicable, to free the associated memory.