Skip to content

stsquad/mp-depth

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 

Repository files navigation

Introduction

This is a piece of Pico-W Micropython to build a depth sensor which in theory should broadcast over Bluetooth Low Energy (BLE) Advertising Packets. The device should not be pairable.

Hardware

See the pin definitions at the top. I’m using a HCS-04 ultrasound depth sensor which has separate trigger and echo pins. You wave the trigger pin about, see how long it takes for the pulse to come back, do some basic maths and you have a number.

The LEDs are really just useful for debugging on my Pico breakout board. It shouldn’t matter if they are not connected.

The button is a hacky way to boost the scanning rate for some cycles. It is imperfect as you still need to wait for the existing sleep to wake up.

Bluetooth

In theory this is broadcasting BTHome packets. I chose this as it seemed simpler than using the BLE advertising services directly. As neither has an attribute which maps nicely to the raw pulse counter or a depth sensor I just report two raw 16bit integers.

Using

Plug your Pico-W into your computer and copy main.py using a tool like ampy:

$ ampy -p /dev/ttyACM0 put main.py

you can then connect to the device, issue a Ctrl-D and the Pico will soft reboot and start running the code:

$ tio /dev/ttyACM0
[19:52:41.722] tio v2.5
[19:52:41.722] Press ctrl-t q to quit
[19:52:41.723] Connected
>>> 
MPY: soft reboot
Pulse usecs: 1089
Surface at 186.7635 mms
type: 1, data: bytearray(b'\x06')/1
type: 9, data: b'depth'/5
bthome: b'd2fc405b4104000040ba00'/11
type: 22, data: bytearray(b'\xd2\xfc@[A\x04\x00\x00@\xba\x00')/11
payload: b'020106060964657074680c16d2fc405b4104000040ba00'/23
9 left at 5000
sleeping for 5000
Pulse usecs: 1145
Surface at 196.3675 mms
type: 1, data: bytearray(b'\x06')/1
type: 9, data: b'depth'/5
bthome: b'd2fc405b7904000040c400'/11
type: 22, data: bytearray(b'\xd2\xfc@[y\x04\x00\x00@\xc4\x00')/11
payload: b'020106060964657074680c16d2fc405b7904000040c400'/23
10 left at 5000
sleep is now 5000
Pulse usecs: 1089
Surface at 186.7635 mms
type: 1, data: bytearray(b'\x06')/1
type: 9, data: b'depth'/5
bthome: b'd2fc405b4104000040ba00'/11
type: 22, data: bytearray(b'\xd2\xfc@[A\x04\x00\x00@\xba\x00')/11
payload: b'020106060964657074680c16d2fc405b4104000040ba00'/23
9 left at 5000

Interfacing with Home Assistant

Home Assistant already has built in support for Bluetooth and has native support BTHome packets. We send a raw count which is actually usecs of the pulse width and then we send a distance (mm or 0.1 m).

About

A simple micropython depth sensor that broadcasts BTHome BLE advertising packets

Resources

Security policy

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages