Skip to content

Instantly share code, notes, and snippets.

@artms
Last active Jan 2, 2022
Embed
What would you like to do?
Reading from temper temperature sensor in bash only
#!/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))
# handle negative temperatures
if [[ $DVAL -gt 32767 ]]; then
DVAL=$(($DVAL-65536))
fi
CTEMP=$(bc <<< "scale=2; $DVAL/100")
echo $CTEMP
@allureGOT
Copy link

allureGOT commented Nov 13, 2020

Great! Reading the temperature works!
Can you help me, to read the humidy-value from the device?

@artms
Copy link
Author

artms commented Nov 13, 2020

Great! Reading the temperature works!
Can you help me, to read the humidy-value from the device?

Unfortunetely my device doesn't have humidity sensor so I cannot actually test. I can check how it is read elsewhere and make equivalent

@allureGOT
Copy link

allureGOT commented Nov 13, 2020

I can test things for you when you need input?

@artms
Copy link
Author

artms commented Nov 14, 2020

Add this lines at the end of script and check with any know humidity sensor if value makes sense

HEX4=${OUT:8:4}
DVAL=$((16#$HEX4))
HUM=$(bc <<< "scale=2; $DVAL/100")
echo $HUM

@allureGOT
Copy link

allureGOT commented Nov 14, 2020

#!/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:8:4}
DVAL=$((16#$HEX4))
HUM=$(bc <<< "scale=2; $DVAL/100")
echo $HUM

Works! that gives me an value of 39 at the moment, i'm verifing that with an analog meter.

@allureGOT
Copy link

allureGOT commented Nov 14, 2020

Ok, i measured the humidity with an analog hygrometer, it seems to have an offset of 5%RH. (negative)
But it works, yeah!

@maurerr
Copy link

maurerr commented Dec 21, 2020

modified a bit to work under openwrt 19.07 (only kmod-usb-hid && xxd added to default build - tested on ath79 tl-wr703n with only 4mb flash)

#!/bin/sh
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=$(printf "%d\n" 0x$HEX4)
awk "BEGIN {print $DVAL/100}"

@tvirt308
Copy link

tvirt308 commented Dec 6, 2021

The code does not read correctly subzero temerature

@artms
Copy link
Author

artms commented Dec 8, 2021

The code does not read correctly subzero temerature

#!/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)
echo $OUT

Can you send output you get to see what values you get for negative temperatures?

@tvirt308
Copy link

tvirt308 commented Dec 9, 2021

The script reads the hex-values correctly. When the value is converted to decimal, the subzero temp comes wrong. I have a remote system with a temper device outdoors. Now the ambiet temp is -13.62 degrees. The code gives 641.74 !! My son from the Silicon Valley proposed the following revision, which appears to work.
#!/bin/bash
exec 5<> /dev/hidraw3
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))
if [[ $DVAL -gt 32767 ]]; then
DVAL=$(($DVAL-65536))
fi
CTEMP=$(bc <<< "scale=2; $DVAL/100")
echo $CTEMP

The point is that negative values in hex are coded in a peculiar way. BTW my temper is in /dev/hidraw3
OUT -reading is 8080faae4e200000

@artms
Copy link
Author

artms commented Dec 13, 2021

Device name can depend on number of USB device you have connected to your host device, it is not stable number unless you setup udev.d to always assign identical symbolic name, I myself using it in embedded device (openwrt router) so hidraw1 is stable on my device. Thanks for example fix, modified gist to accompany your improvements.

@tvirt308
Copy link

tvirt308 commented Dec 14, 2021

Tks for the reply. As I mentioned, I only recognized the problem with negative temperatures, but my son solved it. Might be, not too many users monitor frosty environment. I notized that the devices may exhibit a systematic error, in my case about 2 degrees centigrade. The offset can be easily corrected in the scripts. Now I monitor correctly wheather in our cottage 600 kms away and also log a graph with a reading every hour. But that script is another issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment