Sending Events to Device Cloud in C

A client application on the device can use the event APIs to send log messages to Device Cloud.

Logs appear under the Events tab on the thing details page. By default, only connect and disconnect events are logged for a thing.

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

  • maximum time to wait for the publish operation to complete

  • timestamp of the event

  • log level

The function parameter txn is for future use.

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

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

  1. (Optional) Specify the event options.

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

    iot_timestamp_t tstamp;
    
    
    iot_options_t *event_options = iot_options_allocate( agentLib );
    
    tstamp = iot_timetamp_now() - 120000;
    iot_options_set(event_options, "time_stamp", IOT_TYPE_UINT64, tstamp);
    iot_options_set(event_options, "level", IOT_TYPE_UINT32, IOT_LOG_INFO);
    
  2. Send the event to Device Cloud.

    For the handle 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.

    result = iot_event_publish(agentLib, NULL, event_options, "myEventLogMessage");

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

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