I am trying to follow this tutorial: https://cylab.be/blog/92/measure-ambient-temperature-with-temper-and-linux to get the TEMPer USB sensor to measure ambient temperature so that I can incorporate it into a Perl script that alerts me of a room's ambient temperature. In the tutorial's example they convert the following bytes of data from the device:
Response from device (8 bytes):
80 80 0b 92 4e 20 00 00
to:
In the response, the Bytes 3 and 4 (so 0b 92) indicate the ambient temperature:
0b 92 converted into decimal is 2932
2932 divided by 100 is 29.32 C
Does anyone know how I can use Perl to translate such bytes of data to a decimal and thus to celsius temperature?
CodePudding user response:
Perl's hex function can translate hexadecimal numbers in text to Perl numbers, which you can then represent any way that you like:
my $string = '0b92';
my $number = hex($string);
print $number; # 2962
But, it sounds like you may be reading raw data from a device, and that the number you want is in two octets. Read those and turn them into a Perl number with unpack (with the appropriate format that respects the octet order):
my $buffer;
read $fh, $buffer, 2;
my $number = unpack 'S>', $buffer;