ptvoinfo / zigbee-configurable-firmware

PTVO firmware for CC2530, CC2531, and CC2652 Zigbee chips
https://ptvo.info/zigbee-configurable-firmware-features/
MIT License
210 stars 22 forks source link

Ultrasonic distance sensor (UART) implementation is wrong. #274

Closed bullar closed 5 months ago

bullar commented 7 months ago

In PTVO FW version (1.9.0.15/2024-04-06) the selection of the different sensor types (e.g. US100,..) doesn't work. Independant from the type selection the FW behavior is always the same:

TX: 0x55
expected RX: 0x55 0xAA 0x11 0x02 [0x02 0x12] 0x34 0x5A
Distance value: High 8-bit: 0x02, Low 8-bit : 0x12

Correct would be for sensor JSN-sr04t, DYP-A01, A02YYUW:

TX: 0x55 
RX: 0xFF MSB LSB CRC

 0xFF – fixed header byte.

 MSB, LSB distance value 
 CRC – Checksum as a sum of all bytes before.
Example: 0xFF 0x00 0xDE 0xDE

and sensor DFRobot URM06, URM07:

TX: 0x55 0xAA 0x11 0x00 0x02 0x12
  Instruction: 
  Header----------------0x55
  Header----------------0xAA
  Address---------------0x11
  Length----------------0x00
  Read Distance---------0x02
  Check Sum-------------0x12

RX: 0x55 0xAA 0x11 0x02 0x02 [0x12 0x34] 0x5A
  High 8-bit: 0x12
  Low 8-bit : 0x34
So the distance value is “0x1234”, which convert to decimal is 4660mm.
“0x5A” is low 8-bit of the check sum.
ptvoinfo commented 6 months ago

@bullar Could you please check the latest version where I've tried to resolve this issue?

bullar commented 6 months ago

Great, will test it when I am back home next weekend...

bullar commented 6 months ago

@ptvoinfo sensor DFRobot URM06, URM07: working well now ✅ (tested with serial emulator, don't have this sensor type available)

sensor JSN-sr04t, DYP-A01, A02YYUW: The expected checksum is wrong ❌ The firmware (2024-04-29) expects [sum of all byte] instead of [sum of all bytes +1] or [sum of MSB+LSB]. The sensor specification is a bit misleading... Example: Sensor response: 0xFF 0x00 0xD5 0xD5 --> value 213 Firmware expectation: 0xFF 0x00 0xD5 0xD4 (tested with serial emulator and sensor AJ-S04M)

Screenshot 2024-05-04 at 19 45 05
ptvoinfo commented 6 months ago

@bullar Are you sure about CRC for AJ-S04M? I've read the datasheet, and it contains examples of a simple sum of all bytes (+1 is not required).

bullar commented 6 months ago

@ptvoinfo At least all my 6 sensors of type AJ-S04M show the same response with checksum [MSB+LSB]: dso_01_01_00_06_48 But you are right the specification defines [sum of all bytes]. In another sensor description I found [MSB+LSB] as checksum calculation (same as sum of all bytes+1). I am not sure if there are two different types in the market...

bullar commented 6 months ago

In PTVO FW version (1.9.0.21/2024-05-23) the type selection is extended with JSN-SR04T v2. This type works fine with my version of sensors (AJ-S04M)! ✅

@ptvoinfo There is one minor issue left. The unit of the distance value given by sensor is millimetre. But it's reported in meter to Z2M and ZHA controller. For ZHA I just had to add the following lines in the corresponding quirk file (.py).

--- /Users/<me>/Desktop/dirb_waterlevel.py
+++ /Users/<me>/Desktop/dirb_waterlevel2.py
@@ -222,6 +222,8 @@
         models={PTVO_MODEL_ID},
     )
     class PtvoAnalogInputSensorEp1Attr0(Sensor):
+        _divisor = 1000
+        _decimals = 3
         _attr_name = "Distance 1"
         _attribute_name = "value"
         _attr_translation_key = "value"
ptvoinfo commented 6 months ago

@bullar Or just change the unit name for the sensor :)