Sensor is JSN-SR04T configured to MODE 1 which is supposed to automatically send data trough serial, baudrate 9600, connected to RPi3 GPIO pins 14 and 15 which by default should correspond to /dev/ttyS0 (/dev/ttyAMA0 is main uart used by BT chip if I understand correctly).
Tried enabling this secondary uart with:
Use `ha` to access the Home Assistant CLI.
# grep uart /mnt/boot/config.txt
#enable_uart=1 // should've enabled /dev/ttyS0
#dtoverlay=pi3-miniuart-bt // supposedly switches main and secondary serials in place?
No matter what I do sensor state is unknown.
Am I missing something?
UPDATE:
I connected separate USB-UART dongle, discovered as ttyUSB0:
[ 3.509971] usb 1-1.2: new full-speed USB device number 4 using dwc_otg
[ 3.605264] usb 1-1.2: New USB device found, idVendor=10c4, idProduct=ea60, bcdDevice= 1.00
[ 3.608461] usb 1-1.2: New USB device strings: Mfr=1, Product=2, SerialNumber=3
[ 3.610139] usb 1-1.2: Product: CP2104 USB to UART Bridge Controller
[ 3.611960] usb 1-1.2: Manufacturer: Silicon Labs
[ 3.613603] usb 1-1.2: SerialNumber: 0123602E
[ 3.693908] usb 1-1.5: new full-speed USB device number 5 using dwc_otg
[ 3.787874] usb 1-1.5: New USB device found, idVendor=10c4, idProduct=ea60, bcdDevice= 1.00
[ 3.790162] usb 1-1.5: New USB device strings: Mfr=1, Product=2, SerialNumber=3
[ 3.791436] usb 1-1.5: Product: Sonoff Zigbee 3.0 USB Dongle Plus
[ 3.792609] usb 1-1.5: Manufacturer: ITead
[ 3.793731] usb 1-1.5: SerialNumber: 52379637b839ec118e24e0680aac08d5
[ 6.605623] usbcore: registered new interface driver cp210x
[ 6.617121] usbserial: USB Serial support registered for cp210x
[ 6.641513] usb 1-1.2: cp210x converter now attached to ttyUSB0
[ 6.654496] usb 1-1.5: cp210x converter now attached to ttyUSB1
And yet this piece still gives me sensor state “unknown”:
Doublechecked the sensor itself on other computer, works fine transmits trough serial 4 byte sequences being FF XX YY ZZ
FF - header (constant)
XX - first hexadecimal distance value
YY - second HEX
ZZ - checksum
An update - I put together a small piece of python code to make sure process inside homeassistant container can read off serial device:
import serial
sensor = serial.Serial("/dev/ttyUSB0")
while True:
data = sensor.read(4) # reads 4 bytes off serial - being header, 1st byte of value, 2nd byte of value, checksum
if data[3] == sum(data[:-1]).to_bytes(4,'little')[0]: # compares checksum to sum of header and two value bytes
print((data[1] << 8 ) + data[2]) # prints out the distance seen by the sensor
This works fine, so serial device works, communication works.
Now I took a peek into serial platform code, and we do get up to the line 199:
The logger output from L199 can be seen in the logs, followed by another info log I added before read attempt in L202:
2023-04-24 14:53:10.200 INFO (MainThread) [homeassistant.components.serial.sensor] Serial device /dev/ttyUSB0 connected
2023-04-24 14:53:10.201 INFO (MainThread) [homeassistant.components.serial.sensor] Serial attempt to read line
The except block logger (L204) is not present in the logs, so I assume the except block wasn’t triggered and the flow should continue to ELSE (L209) block, where I added another two loggers:
else:
_LOGGER.info("Attempting to decode the line")
line = line.decode("utf-8").strip()
_LOGGER.info("Line decoded: %s", line)
None of them prints, so I know it’s failing somewhere between L202 and L210…