Measure ambient temperature with TEMPer and Linux

Sep 24, 2020 by Thibault Debatty | 1866 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\n" $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!

Getting started with Ansible
Ansible is an extremely powerful configuration management system. Ansible is able to perform a wide range of tasks on a large and heterogeneous ensemble of devices. Indeed, it relies on a system of modules (plugins) that allow to perform different tasks like software provisioning, system and application configuration management, application-deployment etc. Moreover, Ansible modules exist to manage a wide variety of devices like Linux and Windows computers, but also network equipment like switches and routers, kubernetes clusters and other cloud services like AWS, GCE and Azure. For this reasons, Ansible is sometimes coined as an infrastructure automation engine.
A light NAT router and DHCP server with Alpine Linux
Alpine Linux is a very light Linux distribution, that can run with less than 100MB of harddisk space. Here is how to configure Alpine Linux to run as a NAT router and DHCP server.
DNS over HTTPS and DNS over TLS with dnsdist
DNS is a real corner stone of privacy and security on the Internet! From the security point of view, DNS is used by your machine to link a domain name to an IP address. DNS is also used to find the email server of a recipient (using MX records). On a desktop computer, DNS is used by email clients to perform auto-configuration. Over the Internet, DNS records are used to prevent BGP hijacking (using RPKI). The list is long! From the privacy point of view, the DNS queries sent by your computer offer a clear view of the services you use.