0

I am getting data from sensor as below.

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

and i am trying to decode the RAW data with various PHP function

ORD , CHAR , UNPACK , PACK I have tried many things but i only get the device name from the parsed data

which is P RHT 900350. So how can i extract the temperature and humidity data from this RAW data.

The sensor support team pass me the document to understand the structure. but i dint get through it.

enter image description here

Kashyap Patel
  • 1,139
  • 1
  • 13
  • 29

1 Answers1

1

The values are hexadecimal.

Use $values = str_split($array['rawData'] , 2 ); to get the values per key. (note, other than in the excel, arrays start with key 0)

The humidity is easy: just convert from hex to decimal

$humidity = hexdec($values[13]); //gives 36

LSB and MSB stand for least significant bit and most significant bit (wiki) or byte

The solution is to combine them (MSB + LSB) and convert to decimal:

$x = $values[8].$values[7]; //gives 0AC9
$c = hexdec($x);    //gives 2761
$temp = $c/100;     //gives 27.61 

Key 16 - 27 (the nom's) is the device name character by character, converted to hex. So

16 : hex 50 = dec 80 (in the ascii table, 80 = P)
17 : hex 20 = dec 32 (in the ascii table, 32 = space)
18 : hex 52 = dec 82 (in the ascii table, 82 = R)
etc
Michel
  • 4,076
  • 4
  • 34
  • 52
  • 2
    it's actually `significant byte`, not `significant bit` in this case. – Nick Jun 22 '20 at 09:49
  • @Nick So how can we consider a temperature in minus – Kashyap Patel Jul 02 '20 at 11:11
  • @Michel It gives me wrong value if the temp is in minus. "02010605166E2AB4FA04166F2A310D09502052485420393030333531" the support suggested me to *You have to do a 2 complement, which is reversing all the bits, and add 1 in binary.* – Kashyap Patel Jul 03 '20 at 10:22