Exercise: Read Temperature From Onewire Sensor

Onewire Basics

Have no fear: the setup is done already (see here). In short, Onewire sensor values are read like so,

$ cat /sys/bus/w1/devices/28-02131d959eaa/temperature

28-02131d959eaa is a directory that contains a number of metadata (represented as files). Of course the name of the directory may be different between settings. Here 28 is the vendor number (Dallas, I believe), and 02131d959eaa is the device address which is unique across all devices in the world.



Write a program onewire-temperature that reads a sensor’s temperature periodically. It takes two arguments,

  • Temperature filename (/sys/bus/w1/devices/28-02131d959eaa/temperature in case of the above example)

  • Interval in seconds

$ ./onewire-temperature /sys/bus/w1/devices/28-02131d959eaa/temperature 2


  • To convert a string (argv[2]) into an integer, the atoi() function can be used (see here)

  • To put the process to sleep for an amount of time (in seconds), you use the sleep() function (see here)

CMake Build

  • Read CMake: Local Build for how to create a simple CMake project.

    Note that the program in this exercise consists of only one source file, so you will omit the library stuff referenced there.

  • Read Cross Toolchain Setup and CMake: Cross Build for cross-building a CMake project

  • Add your file (onewire-temperature.cpp is a likely name) to the project.

Test On The Target Machine

  • Use scp to copy the program over. Note that you specify a non-default port (say, 2020) using the -P 2020 option.

  • Use ssh to login to the target and test