/pico-w-garage-door-sensor

Wireless garage door sensor for Home Assistant powered by Raspberry Pi Pico W

Primary LanguagePython

Raspberry Pi Pico W Garage Door Sensor

Lint

I wanted to build a sensor to determine the state of my garage door—open or closed—and send that state to Home Assistant so I can display the status in a dashboard and build automation from it (e.g. warning me if I'm asleep and the garage door is open!).

If I wanted some sort of cloud integration, I could pay for the kit that connects to my garage door opener, but since the cloud is just someone else's computer, and I'd rather not rely on some company's weak security to protect data about my home... I want it all local.

Garage Door sensors

The actual sensors I use are the 'Enforcer' (model SM-4201-LQ) from Seco-Larm. I chose these because they are rugged and purpose-built for mounting to garage door tracks.

Watch this video to see how I installed the sensors on my garage doors, and how I wired them to the Pico W.

RPi Pico Breakout Board wired to garage door sensor

I used a RPi Pico Breakout board mounted directly to my garage wall, and wired one wire of each sensor to ground, and the others thusly:

  • east_garage_door to GPIO pin 2
  • west_garage_door to GPIO pin 3

You can pick any GPIO connection, though—just change the appropriate lines inside garage-door.yml before flashing the Pico W using the instructions below.

Flashing the Pico W

I decided to use a Raspberry Pi Pico W for this project—you could probably also use an ESP32 or ESP8266 with slight modifications, since this project uses ESPHome.

You will have to flash the firmware to the Pico W in order for it to work.

Note: I also have a MicroPython-based setup in the micropython subdirectory.

Preparing to flash the Pico W

Before you can flash anything to the Pico W, you have to define a few secrets that ESPHome will use when it compiles the program.

Create a secrets.yaml file inside this directory, and add the following:

---
wifi_ssid: your-wifi-ssid-here
wifi_password: your-wifi-password-here
ota_password: choose-an-ota-password

# Encryption key for HA API access.
# Generate a random 32-bit key with `openssl rand -base64 32`
api_encryption_key: choose-an-api-encryption-key

Note: The ESPHome docs API page can generate an encryption key in-browser.

Using Docker

In this directory, I run docker-compose up -d to start an esphome container that I'll use to flash the Pico.

Then enter the container:

$ docker exec -it esphome bash
root@docker-desktop:/config#

This drops you into the container inside the config directory, which is shared from this repository.

Compile the binary for the garage door sensor:

$ esphome compile garage-door.yml

Copy the generated binary into the current directory:

$ cp .esphome/build/rpi-pico/.pioenvs/rpi-pico/firmware.uf2 ./rpi-pico.uf2

Then on your host computer, with the Pico W booted into BOOTSEL mode (hold down the BOOTSEL button while plugging in the USB cable), copy the rpi-pico.uf2 file over to the Pico W. When the copy is complete, the Pico should reboot and start working as a garage door sensor.

Using pip source install

Make sure you have Python 3 installed on your computer, then run:

$ pip3 install esphome

Make sure your installation is working:

$ esphome version
Version: 2023.11.6

Then plug in your Pico W, while holding the BOOTSEL button, and when it mounts on your computer, run:

$ esphome run garage-door.yml --device /Volumes/RPI-RP2

On Raspberry Pi or other Linux devices, the --device should be /dev/sda1 (or whatever mount point)

After 20-30 seconds, ESPHome should compile and upload the firmware to the Pico.

Note: There's also an led-blink.yml configuration if you want to upload it quickly to verify esphome and your Pico are all wired up correctly. If you run that file, it should make your Pico start blinking it's onboard LED 2x per second.

Installation from a Raspberry Pi

If you are using a Raspberry Pi, first install Pip:

sudo apt install python3-pip git

Then follow the directions above for 'Using pip source install'.

First time install

For now, if you're not running things on a Mac at least, the first install must be done by copying a manually-downloaded .uf2 file to the Pico in BOOTSEL mode.

After ensuring esphome installed (see 'Using pip source install' above):

  1. Run esphome dashboard ./
  2. In a browser, visit the Pi's IP address at port :6052
  3. Click the three dots next to the project
  4. Click 'Install'
  5. Click 'Manual download'
  6. Click the link and wait for the download to be generated, then click the Download link.

Copy the downloaded rpi-pico.uf2 file to the Pico's filesystem while it's in BOOTSEL mode (hold down BOOTSEL while plugging it in). Unplug and replug the Pico.

Debugging with ESPHome

If you want to see debug output, after the first time you flash the Pico, you can change the run command to:

$ esphome run led-blink.yml --device /dev/tty.usbmodem2101

And it will compile ESPHome, load it onto the Pico, and start displaying logged output. You can change the log level using the logger.level setting.