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
@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