Wandering Thoughts archives

2022-08-28

Getting USB TEMPer2 temperature sensor readings into Prometheus (on Linux)

For reasons outside of the scope of this entry, we recently decided to get some inexpensive USB temperature sensors (we already have a number of old, industrial style temperature sensor boxes). What we wound up getting is the PCsensor TEMPer2; this model and PCsensor's USB temperature sensors in general seem to be a quite common choice (often resold under some other name). Getting our model going on Linux and getting metrics into our Prometheus setup took some work and head scratching, which I'd like to save other people.

The various PCsensor modules have various features and options (see eg here, also, also). The TEMPer2 is a white plastic USB stick with an additional probe wire that you plug in at the end; it has temperature sensors both in the USB stick and at the end of the probe wire, and so provides two readings if the probe wire is (fully and firmly) plugged in. Our particular copies show up in 'lsusb' as:

Bus 001 Device 005: ID 1a86:e025 QinHeng Electronics TEMPer2

In kernel messages, we see:

input: PCsensor TEMPer2 as /devices/pci0000:00/0000:00:14.0/usb1/1-4/1-4:1.0/0003:1A86:E025.0001/input/input3
hid-generic 0003:1A86:E025.0001: input,hidraw0: USB HID v1.11 Keyboard [PCsensor TEMPer2] on usb-0000:00:14.0-4/input0
input: PCsensor TEMPer2 as /devices/pci0000:00/0000:00:14.0/usb1/1-4/1-4:1.1/0003:1A86:E025.0002/input/input4
hid-generic 0003:1A86:E025.0002: input,hidraw1: USB HID v1.10 Device [PCsensor TEMPer2] on usb-0000:00:14.0-4/input1

You might wonder why a temperature sensor is claiming to be a keyboard. The reason is the same reason that some USB security keys do, which is that the TEMPer2 can optionally simply type its readings into the system. This mode is triggered by pushing the 'TXT' in the red circle for long enough (this is actually a button), and the output looks like this:

www.pcsensor.com
temper2 v3.9
caps lock:on/off/++
num lock:off/on/-- 
type:inner-tx;outer-tx
inner-temp      outer-temp      interval
23.81 [c]       23.31 [c]       1s
23.81 [c]       23.31 [c]       1s
[... repeat ...]

(The 'inner' temperature here is the USB stick temperature, the 'outer' temperature is the probe.)

This mode can be convenient if you're trying to get a sensor reading program to work and you want a check on what the TEMPer2 is seeing and reporting. The easiest way to get the TEMPer2 out of this mode is probably to unplug it and plug it back in again.

There are a variety of programs out on the Internet to read data from the TEMPer series of USB temperature (and sometimes humidity) sensors. However, the whole sensor series has had various versions over time, so any particular program may not support your copy of a 'TEMPer2' even if it supports some (older) TEMPer2s. For instance, older TEMPer2s apparently report a different USB identifier (based on looking at various source code).

The code I used is this version of 'temper.py', with a modification to support our specific TEMPer2s. PCsensor (or the actual maker) revises the firmware periodically, and temper.py is cautious about trying to talk to newer versions. Our sensors report they have firmware 'TEMPer2_V3.9', instead of the V3.7 firmware that temper.py expected. Fortunately it seems to work to just treat V3.9 firmware as V3.7; a quickly modified version of temper.py worked.

(Conveniently temper.py tells you if it found what it thinks is a TEMPer2 but it has an unsupported firmware version.)

It appears that the TEMPer2 is pretty fast to read and can be read quite often (in its 'type things at you' mode it will go as fast as once a second). Depending on how you've automated reading your TEMPer2, you may want to use a systemd timer unit to read it more often than once a minute.

To create Prometheus metrics, I opted to have temper.py output JSON, use jq to extract the particular pieces of information we care about, and then use a shell script (that wraps all of this) to create the actual Prometheus metrics using printf. The metrics are written to the Prometheus host agent's 'textfile' directory so the textfile collector can expose them as part of the regular host metrics. This is not necessarily the completely correct Prometheus approach, but it's pretty much the easiest.

The resulting readings and metrics seem to broadly correspond to reality under the right circumstances (where reality here is what our existing machine room temperature sensors report). There are some oddities and anomalies that I'm still looking into, which is for a future entry (once I understand or at least know more). For now, I will just say that if you want to use the non-probe temperature sensor, you probably want to get a USB extender cable along with your TEMPer2.

linux/USBTemper2SensorToPrometheus written at 23:21:12; Add Comment


Page tools: See As Normal.
Search:
Login: Password:
Atom Syndication: Recent Pages, Recent Comments.

This dinky wiki is brought to you by the Insane Hackers Guild, Python sub-branch.