TEMPer (Temperature sensor 413d:2107) on Linux tutorial

Software builded and deployed on Fedora 31 and Centos 7

Update 2022-11-19
Page is no longer maintained. I’m not using the sensor anymore.


Update 2020-11-25:

A beter script on Centos88 is:

 

#!/bin/bash
 
exec 5<> /dev/hidraw1
echo -e ‘\x00\x01\x80\x33\x01\x00\x00\x00\x00\c’ >&5
OUT=$(dd count=1 bs=8 <&5 2>/dev/null | xxd -p)
HEX4=${OUT:4:4}
DVAL=$((16#$HEX4))
CTEMP=$(bc <<< “scale=2; $DVAL/100”)
echo $CTEMP

source:  https://gist.github.com/artms/5356eafcd1244c6fabc0f735e5de7096

 

 

updated: 2019-11-13

There are several expensive solution to monitor room temperature, rely on cloud solutions or maybe you need to pick up the soldering iron. There is an alternative on the market that comes cheap: The TEMPer device. It can be bought from Ebay or big known e-commerce websites like dx.com. I bought my TEMPer devices (413d:2107)  from Ebay for 8,58 USD each.

To be able to successfully deploy a TEMPer device you need to have a little knowledge how to compile C code and Linux. To monitor properly the room temperature you want to use an USB extension cord.

The sellers I bought the hardware from are:

Step 1: Look up the device USB ID

There are several TEMPer devices sold as the same. They all do the same job, however be aware that the USB ID’s are different. Currently the 413:2107 is being sold and does not work out of the box with code to be found on the internet. Since I’m having the 413:2107 I’ll explaining how to make it work. I do not have the other versions. USB ID’s can be found with:

$ lsusb

If it is correct, there will be a rule containing this: Bus 006 Device 003: ID 413d:2107 , to be sure we also verify this with dmesg. You will find the following rules:

$ dmesg
input: HID 413d:2107 as /devices/pci0000:00/0000:00:10.0/usb6/6-2/6-2:1.0/0003:413D:2107.0003/input/input13
hid-generic 0003:413D:2107.0003: input,hidraw1: USB HID v1.11 Keyboard [HID 413d:2107] on usb-0000:00:10.0-2/input0
hid-generic 0003:413D:2107.0004: hiddev97,hidraw2: USB HID v1.10 Device [HID 413d:2107] on usb-0000:00:10.0-2/input1

If both are verified we are good to go and know that Linux recognizes the TEMPer device.

 

Step 2: Get the developer Distro packages needed to compile the TEMPer software

$ sudo dnf install hidapi-devel bc

Step 3: Git clone the needed software

$ sudo -i
$ cd /opt/
$ git clone https://github.com/edorfaus/TEMPered
$ cd TEMPered
$ cmake .
$ cd /opt/TEMPered/utils
$ make
$ cp hid-query /usr/local/bin

Step 4: Find out which hidraw device is our TEMpered device by trying to get metrics out of it:

$ ./hid-query /dev/hidraw1 0x01 0x80 0x33 0x01 0x00 0x00 0x00 0x0
0
No data was read from the device (timeout).

./hid-query /dev/hidraw2 0x01 0x80 0x33 0x01 0x00 0x00 0x00 0x00
Device /dev/hidraw2 : 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 0a 21   4e 20 00 00

Our TEMPered device is allocated at /dev/hidraw2

 

Step 5: Create TEMPered script

$ vi TEMPered

#!/bin/bash
OUTLINE=”/usr/local/bin/hid-query /dev/hidraw2 0x01 0x80 0x33 0x01 0x00 0x00 0x00 0x00|grep -A1 ^Response|tail -1″
OUTNUM=”echo $OUTLINE|sed -e ‘s/^[^0-9a-f]*[0-9a-f][0-9a-f] [0-9a-f][0-9a-f] \([0-9a-f][0-9a-f]\) \([0-9a-f][0-9a-f]\) .*$/0x\1\2/'”
HEX4=${OUTNUM:2:4}
DVAL=$(( 16#$HEX4 ))
CTEMP=$(bc <<< “scale=2; $DVAL/100”)
echo $CTEMP

$ chmod +x TEMPered

 

Test the script:

$ ./TEMPered

25.93

This is ‘all’ it takes to make it working. I tested the TEMPered sensor next to an analogue temperature sensor and the values matched. Unfortunately this script needs to be executed as root.

28 thoughts on “TEMPer (Temperature sensor 413d:2107) on Linux tutorial”

  1. To avoid running the script as root, I suggest this udev rule (all on one line):

    SUBSYSTEMS==”usb”, ACTION==”add”, ATTRS{idVendor}==”413d”, ATTRS{idProduct}==”2107″, MODE=”666″

    Works on a debian 9 machine running 4.14 kernel.
    I put this into /etc/udev/rules.d/99-tempsensor.rules

    Reply
  2. Having a problem in centos 7 using either line at
    ./hid-query /dev/hidraw

    gets this response

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

    Write failed: (null)

    Reply
      • It responded to /dev/hidraw4
        But now when executing the TEMPer script I get
        ./TEMPered: line 5: 16#ho: value too great for base (error token is “16#ho”)
        (standard_in) 1: syntax error

        Thanks

        Reply
  3. The difference I see is in the dmesg output.
    [591817.570035] usb 6-2: New USB device found, idVendor=413d, idProduct=2107, bcdDevice= 0.00
    [591817.570040] usb 6-2: New USB device strings: Mfr=0, Product=0, SerialNumber=0
    [591817.576427] input: HID 413d:2107 as /devices/pci0000:00/0000:00:1d.0/usb6/6-2/6-2:1.0/input/input12
    [591817.627401] hid-generic 0003:413D:2107.0006: input,hidraw3: USB HID v1.11 Keyboard [HID 413d:2107] on usb-0000:00:1d.0-2/input0
    [591817.630411] hid-generic 0003:413D:2107.0007: hiddev0,hidraw4: USB HID v1.10 Device [HID 413d:2107] on usb-0000:00:1d.0-2/input1

    note: usb-0000:00:1d.0-2/input0
    Jim

    Reply
  4. Good Day
    I keep getting this error even though I have corrected all the backward quotes. I am running Ubuntu 16.04.

    ./TEMPered: line 5: 16#ho: value too great for base (error token is “16#ho”)
    (standard_in) 1: syntax error

    Reply
        • Did some scripting on my own, based on the work described above, so here is my version. You can divide by 100 if you need to

          /usr/local/bin/hid-query /dev/hidraw1 0x01 0x80 0x33 0x01 0x00 0x00 0x00 0x00|grep -A1 ^Response|tail -1 > /tmp/temper
          sed -i -e ‘s/^[^0-9a-f]*[0-9a-f][0-9a-f] [0-9a-f][0-9a-f] \([0-9a-f][0-9a-f]\) \([0-9a-f][0-9a-f]\) .*$/0x\1\2/’ -e ‘s/[a-z]/\U&/g’ /tmp/temper
          hex=$(cat /tmp/temper)
          printf ‘%d\n’ $hex > /tmp/temper
          echo $(date +%H:%M)”,””$(cat /tmp/temper)” >> path/to/file

          Reply
      • I was able to modify this to work in debian/ubuntu

        #!/bin/bash
        OUTLINE=$(/usr/local/bin/hid-query /dev/hidraw1 0x01 0x80 0x33 0x01 0x00 0x00 0x00 0x00|grep -A1 ^Response|tail -1)
        OUTNUM=$(echo $OUTLINE|sed -e ‘s/^[^0-9a-f]*[0-9a-f][0-9a-f] [0-9a-f][0-9a-f] \([0-9a-f][0-9a-f]\) \([0-9a-f][0-9a-f]\) .*$/0x\1\2/’)
        echo $OUTNUM
        HEX4=${OUTNUM:2:4}
        DVAL=$(( 16#$HEX4 ))
        CTEMP=$(bc <<< "scale=2; $DVAL/100")
        FTEMP=$(echo "scale=2;((9/5) * $CTEMP) + 32" |bc)
        echo $CTEMP
        echo $FTEMP

        Sample output looks like this, first line is celcius, second line is ferenhight

        Reply
        • If the ambient temperature is subzero this gives a wrong result. To manage also negative hex-numbers you need to add
          if [[ $DVAL -gt 32767 ]]; then
          DVAL=$(($DVAL-65536))
          fi

          Reply
        • I have corrected badly copy-paste ‘ and ‘ characters. This shall work.

          #!/bin/bash
          OUTLINE=$(/usr/local/bin/hid-query /dev/hidraw4 0x01 0x80 0x33 0x01 0x00 0x00 0x00 0x00|grep -A1 ^Response|tail -1)
          OUTNUM=$(echo $OUTLINE|sed -e ‘s/^[^0-9a-f]*[0-9a-f][0-9a-f] [0-9a-f][0-9a-f] \([0-9a-f][0-9a-f]\) \([0-9a-f][0-9a-f]\) .*$/0x\1\2/’)
          HEX4=${OUTNUM:2:4}
          DVAL=$(( 16#$HEX4 ))
          CTEMP=$(bc <<< "scale=2; $DVAL/100")
          FTEMP=$(echo "scale=2;((9/5) * $CTEMP) + 32" |bc)
          echo $CTEMP
          #echo $FTEMP

          Reply
  5. Thanks. Now have this device working on CentOS 7.8, Fedora 31 and ClearOS 7.7 (a derivative of CentOS)
    Had the usual problem with back ticks and single quotes 🙂

    Reply
  6. Hi Maikel
    First glitch – rebooted the system and the device changed from /dev/hidraw1 to /dev/hidraw3. Now using “/usr/local/bin/hid-query -e” to check which is active to use…

    My script version can be downloaded from https://www.sraellis.tk/TEMPered/TEMPered
    Wonder if this is a user code page problem?

    see also

    https://www.clearos.com/clearfoundation/social/community/what-is-your-office-or-computer-room-temperature#reply-295911

    https://www.sraellis.tk/frame.php?number=26&monitor=room_temp

    Reply
  7. Just to show the device detection used here, pretty simplistic and could be better written – but this does the job. Is now part of my script…

    device=`/usr/local/bin/hid-query -e | grep “413d:2107 interface 1” | awk ‘{print $1}’`

    Tony

    Reply
  8. You can avoid hid-query and stay in bash:
    `
    #!/bin/bash
    exec 5\ /dev/hidraw1
    echo -e ‘\x00\x01\x80\x33\x01\x00\x00\x00\x00\c’ >&5
    OUT=$(dd count=1 bs=8 /dev/null | xxd -p)
    HEX4=${OUT:4:4}
    DVAL=$((16#$HEX4))
    CTEMP=$(bc <<< "scale=2; $DVAL/100")
    echo $CTEMP
    `

    Reply
  9. How did you get the hex bytes to use to query the device?

    The reason I ask is that I need humidity also, and the device is advertised to supply both.

    Reply
  10. Hi Thanks for the bash code, super helpful. Like others my hidraw reference changed between a Pi and other hardware, so did a check with dmesg to find the device name:

    #!/bin/bash
    #
    # Get the temperature from a USB Temper Thermometer
    #
    hid=$(dmesg | grep -E -m 1 ‘Device.*413d:2107’ | awk ‘{ p=index($4,”,”); print “/dev/” substr($4,p+1,7) }’)
    if [ $hid = “” ]; then
    echo “No TEMPer device found”
    else
    echo “TEMPer device found on: $hid”
    exec 5 $hid
    echo -e ‘\x00\x01\x80\x33\x01\x00\x00\x00\x00\c’ >&5
    OUT=$(dd count=1 bs=8 /dev/null | xxd -p)
    HEX4=${OUT:4:4}
    DVAL=$((16#$HEX4))
    CTEMP=$(bc <<< "scale=2; $DVAL/100")
    echo "$CTEMP 'C"
    fi

    Reply
  11. I use
    sudo ./hid-query /dev/hidraw1 0x01 0x80 0x33 0x01 0x00 0x00 0x00 0x0
    Device /dev/hidraw1 : 413d:2107 interface 1 : (null) (null)

    result :

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

    No data was read from the device (timeout).

    how can I do ? QQ

    Reply

Leave a Comment