Skip to content

Instantly share code, notes, and snippets.

@artms
Last active December 10, 2022 05:00
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save artms/5356eafcd1244c6fabc0f735e5de7096 to your computer and use it in GitHub Desktop.
Save artms/5356eafcd1244c6fabc0f735e5de7096 to your computer and use it in GitHub Desktop.
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

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

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

#!/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

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.

@bluthundr
Copy link

bluthundr commented Dec 10, 2022

I am attempting to use the TEMPerX232 in serial mode with some of the code above, and it seems to work, but is giving me some weird values:

Temperature:
174.40
Humidity:
177.46

After snooping the COM port with the included Windows software, it seems all you have to do is send the command "READTEMP" to get data from it.

I came up with this and it seems to work fine.

#!/bin/bash
exec 5<> /dev/ttyUSB0
echo -e '\x52\x65\x61\x64\x54\x65\x6D\x70\c' >&5
OUT=$(dd count=1 bs=64 <&5 2>/dev/null | xxd -p)
echo " "
echo "Hex Out:"
echo $OUT
echo " "
echo "ASCII Out:"
echo $OUT | xxd -r -p
echo " "
echo "Temperature (C):"
TEMPC=${OUT:22:10}
echo $TEMPC | xxd -r -p
echo " "
echo " "
echo "Temperature (F):"
TEMPX=$(echo $TEMPC | xxd -r -p)
TEMPF=$(echo "1.8 * $TEMPX + 32" | bc)
echo $TEMPF
echo " "
echo "Humidity:"
HUM=${OUT:42:10}
echo $HUM | xxd -r -p
echo " "
echo " "

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