Recording data from a sensor

Updated: April 19, 2023

Applications can record the data that's streaming from a sensor to a file.

The data that you record can be data from a sensor unit, or interim data.

In general when you're recording data that's streaming from a sensor, do the following:

  1. Open a connection to the sensor whose data you want to record. See Opening and closing a connection to a sensor unit.
  2. Open a sensor roll file for writing the sensor data to.
  3. Start recording the sensor data.

    If you want to record all the data that's streamed from the sensor, start recording before the sensor starts streaming. Otherwise, you can capture portions of the data by starting and stopping the recording as many times as your application requires while the sensor is streaming.

  4. Start streaming from the sensor whose data you want to record. See Starting and stopping a sensor unit or Starting an interim data unit. Usually, it's the publishers that control the streaming of interim data. Therefore, subscribers are unlikely to call the start and stop functions.
  5. Stop streaming from the sensor whose data you want to record. See Starting and stopping a sensor unit or Starting an interim data unit. Usually, it's the publishers that control the streaming of interim data. Therefore, subscribers are unlikely to call the start and stop functions.
  6. Stop recording the sensor data.

    If you want to record all the data that's streamed from the sensor, stop recording after the sensor stops streaming. Otherwise, you can capture portions of the data by starting and stopping the recording as many times as your application requires while the sensor is streaming.

  7. Close the sensor roll file that the sensor data was writing to.

The sensor roll is a directory where the Sensor library saves files for your application. The Sensor library manages unique filenames for all the files on behalf of applications. The files that the Sensor library uses to save the data can be created in uncompressed or compressed formats. The Sensor library names the files according to the following convention:

SENSOR_date_timestamp.file_format

where date is the date that the file was created, timestamp is the time that the file was created and file_format is format of the file (.raw or .gz).

For example, SENSOR_20170719_1334.raw or SENSOR_20170719_1334.gz can be filenames for a file in raw format and in compressed format respectively.

Open a sensor roll file

To open a file for writing to, call sensor_roll_open_file(). For example:

...
int err;
sensor_handle_t handle = NULL;
char fullpath[SENSOR_MAX_FILENAME_LEN];
int fd;
...
err = sensor_roll_open_file(handle, 
                            &fd, fullpath, SENSOR_MAX_FILENAME_LEN, SENSOR_ROLL_FORMAT_RAW);
...

Specify the format in which you want the Sensor library to record the data by specifying the ext parameter when you call the function. The format is one of the sensor_roll_format_t types.

The sensor_roll_open_file() function returns a pointer to the file descriptor and name of the file that the Sensor library creates. We recommend that you use SENSOR_MAX_FILENAME_LEN as the value for the length of the filename (namelen).

Start recording sensor data

When to start recording sensor data is dependent on you application. If you want to record all the data that's streamed from the sensor, then start recording before the sensor starts streaming and stop recording after the senstor stops streaming. If you want to record only portions of the streamed data, then you can call sensor_start_recording() after the sensor has started streaming and only when you want to start recording. You can start and stop recording sensor data multiple times over the duration while the sensor is streaming.

To start recording sensor data, call sensor_start_recording(). For example:

...
int err;
char fullpath[SENSOR_MAX_FILENAME_LEN];
...
err = sensor_start_recording(handle, fullpath, NULL);
...
      

You can control the time that the Sensor library starts recording the sensor data by specifying the time parameter. Use this parameter to specify an absolute time (with CLOCK_MONOTONIC as the reference) at when you want to start recording the sensor data. Specify NULL if you want to start recording immediately.

If not already started, you can start streaming data from the sensor that you've started recording on. See Starting and stopping a sensor unit.

Stop recording sensor data

When to stop recording sensor data is dependent on you application. If you want to record all the data that's streamed from the sensor, then start recording before the sensor starts streaming and stop recording after the senstor stops streaming. If you want to record only portions of the streamed data, then you can call sensor_stop_recording() when you want to stop recording. You can start and stop recording sensor data multiple times over the duration while the sensor is streaming.

To stop recording sensor data, call sensor_stop_recording(). For example:

...
int err;
...
err = sensor_stop_recording(handle, NULL);
...
      

You can control the time that the Sensor library stops recording the sensor data by specifying the time parameter. Use this parameter to specify an absolute time (with CLOCK_MONOTONIC as the reference) at when you want to stop recording the sensor data. Specify NULL if you want to stop recording immediately.

After all your recording is complete, you can stop streaming data from the sensor that you've stopped recording on. See Starting and stopping a sensor unit.

Close the sensor roll file

To close the file, call sensor_roll_close_file(). For example:

...
int err;
int fd;
...
err = sensor_roll_close_file(fd);
...
      

Use the file descriptor that was returned to you from calling sensor_roll_open_file().