Search code examples
phpparsingtemperatureraw-data

PHP Parsing ELA Temperature Sensor raw data


I have successfully converted the positive temperature data from below raw data

array (
'timestamp' => '2020-06-11T11:09:21.335Z',
'type' => 'Unknown',
'mac' => 'F64BB46181EF',
'bleName' => 'P RHT 900350',
'rssi' => -63,
'rawData' => '02010605166E2AC90A04166F2A240D09502052485420393030333530',

)

$cutdata = str_split($rawData,2);
$humidity_cut = hexdec($cutdata[13]);
$x_cut = $cutdata[8].$cutdata[7]; //gives 0AC9
$c_cut = hexdec($x_cut);
$temp_cut = $c_cut/100;
echo $temp_cut;exit;

But when i am getting negative temperature values it giving me issues it increase the temp value more then 600

Here is the negative Temp Raw Data

array (
'timestamp' => '2020-07-03T10:05:53.049Z',
'type' => 'Unknown',
'mac' => 'EDF2F589DCAE',
'bleName' => 'P RHT 900351',
'rssi' => -79,
'rawData' => '02010605166E2AB4FA04166F2A310D09502052485420393030333531',

)

I have asked the support team they said

You have to do a 2 complement, which is reversing all the bits, and add 1 in binary.

enter image description here


Solution

  • I'm assuming that the output is OK, but for a 32 bit number, this code checks if the high bit is set (using & 32768) and if it is, it xors the number with 65535 (all 16 bits set) to invert it and then just adds 1 (the result is then made a -ve number)...

    if ( $c_cut & 32768  )  {
        $c_cut = -(($c_cut ^ 65535)+1);
    }
    

    which gives -13.56 as the result.