Measure ambient temperature with TEMPer and Linux

Sep 24, 2020 by Thibault Debatty | 5110 views

Sysadmin

https://cylab.be/blog/92/measure-ambient-temperature-with-temper-and-linux

TEMPer is a temperature sensor that you can plug on the USB port of your computer or server. You can find it online for less than 10 euro, so it is quite cheap, but it is actually very accurate. And here is how to use it on a Linux system.

Plug and check

First, and obviously, plug your TEMPer into one of your usb ports. Then open a terminal and check the device is correctly connected:

lsusb

If your TEMPer is connected you should see one device with the ID 413d:2107.

TEMPered

To query the sensor, we will use the hid-query tool, which is part of the TEMPered project.

So first we have to install the requirements: libhidapi-dev and cmake.

sudo apt install libhidapi-dev cmake

Now we can download and build TEMPered and hid-query :

git clone https://github.com/edorfaus/TEMPered
cd TEMPered 
cmake .
cd utils
make

If everything went correctly, we can now copy the hid-query tool to /usr/local/bin, so it will be globally available:

sudo cp hid-query /usr/local/bin

Query the TEMPer USB

hid-query allows to send a binary sequence to a HID device, and read the response. But there might be multiple HID devices recognized on your computer, and only one of those is actually the TEMPer USB sensor. So as a first test you will have to try all HID devices (/dev/hidrawxx) by sending a specific sequence (0x01 0x80 ...) until you get a correct answer:

sudo hid-query /dev/hidraw0 0x01 0x80 0x33 0x01 0x00 0x00 0x00 0x00

If the device is NOT a TEMPer USB sensor, you will get an answer like this one:

Device /dev/hidraw0 : 413d:2107 interface 0 : (null) (null)

Writing data (9 bytes):
     00 01 80 33   01 00 00 00   00

No data was read from the device (timeout).

If the device IS a TEMPer USB, you will get an answer like this one:

sudo hid-query /dev/hidraw1 0x01 0x80 0x33 0x01 0x00 0x00 0x00 0x00
Device /dev/hidraw1 : 413d:2107 interface 1 : (null) (null)

Writing data (9 bytes):
     00 01 80 33   01 00 00 00   00

Response from device (8 bytes):
     80 80 0b 92   4e 20 00 00

In the response, the Bytes 3 and 4 (so 0b 92) indicate the ambient temperature:

  • 0b 92 converted into decimal is 2932
  • 2932 divided by 100 is 29.32 C

temper.sh

To make the process of reading the temperature easier, you can use the following script (temper.sh). It will try all HID devices on your system, and convert the temperature into degrees:

#!/bin/bash

##
## temper.sh
## reads and print temperature from TEMPer device(s)
## requires hid-query from the TEMPered project
## https://cylab.be/blog/92/measure-ambient-temperature-with-temper-and-linux
##

for device in /dev/hidraw*; do
  ## quey $device and grep for return value
  hexvalue=`hid-query "$device" 0x01 0x80 0x33 0x01 0x00 0x00 0x00 0x00 2> /dev/null | grep -oP "80 80 K([0-9a-fA-F]{2} [0-9a-fA-F]{2})" | tr -d ' '`

  ## convert hex to dec
  decvalue=`printf "%d" $((16#$hexvalue))`

  ## divide by 100
  decvalue=`bc <<< "scale=2; ${decvalue}/100"`
  printf "%s : %s °C
" $device $decvalue
done;

Final word

The temperature measured by your TEMPer will be influence by the computer it is connected to, so don't forget to use a USB extension cable!

This blog post is licensed under CC BY-SA 4.0

This website uses cookies. More information about the use of cookies is available in the cookies policy.
Accept