Skip to content

Instantly share code, notes, and snippets.

@artms
Last active December 10, 2022 05:00
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
@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