Here in my garage...
just bought this new Raspberry Pi Pico W here, in Missouri.
But you know what I like a lot more than materialistic things?
The knowledge... that I can use this Pi to see when my garage door is open.
On my phone.
All kidding aside, this is a Pico W. It's a tiny little circuit board with a microcontroller,
that's this little black chip in the middle.
And it also has a WiFi chip under this metal shield, that lets you interact with it from
anywhere.
Microcontrollers like this are everywhere.
That smart light bulb you can control form your phone?
Microcontroller.
The loud toy that plays a song you can't get outta your head?
[cute but annoying voice singing] Spin Spin a letter...
Look all around...
Also a microcontroller.
Microcontrollers are like tiny little computers that let you program electronics to do almost
anything.
When I reviewed this Pico W in June, I mentioned how I wanted to hack my garage doors so I
could impress my wife by not leaving them open all the time.
After a few months of health issues holding me back, I'm happy to say I finally did it!
Now I have no excuse for leaving the garage doors open.
And to do it?
I didn't even have to write one line of programming.
I did it with something called ESPHome.
If you've heard of ESPHome, you might be wondering how something built for EspressIf devices,
like this ESP32, runs on the Raspberry Pi Pico.
Well, right around the time the Pico W came out, I reached out to Nabu Casa, the company
behind the popular Home Assistant software.
They were actually working on supporting the original Pi Pico, but it wasn't a priority.
With the Pico W and it's built-in WiFi, they decided to make it official.
ESPHome supports the Pico and Pico W now!
But why should you care?
Well like I mentioned earlier, I programmed this Pico W to do exactly what I wanted, but
I didn't have to write any code.
No Python.
No C, nothing.
All I had to do was give ESPHome this YAML file, and ESPHome built software to do exactly
what I wanted.
When I open the garage door, this sensor on the door tells the Pico W the door is open.
Then the Pico W sends a message to Home Assistant.
Then, if I leave the garage door open for more than 5 minutes, Home Assistant starts
nagging me until I close it.
And this setup is all running inside my house, with nothing going out to any cloud anywhere.
There's no subscriptions, no cloud accounts, no special hardware.
But how did I get here?
Well, there were two main challenges: getting ESPHome to work on the Pico, and installing
the Pico in my garage.
Most programming projects start with a 'Hello, world.'
You wanna get a very simple program running just to make sure all the basics work.
Like if I'm building a website, the first thing I'll do is add a file that spits out
"hello world" to my browser.
If I can get at least that working, I'm good.
If not, there's no use writing any more code until I get the basics sorted out.
In the case of microcontrollers, 'hello world' usually means blinking an LED.
There's usually a built-in LED, and the Pico has this nice little one here.
So I wrote up some MicroPython code to make sure my board worked, and it did!
I got that LED blinking, then I added a little more code to connect to WiFi.
That all worked great.
So I swapped over to the development release of ESPHome, and wrote up this little 'led
blink' configuration.
This file tells ESPHome everything about what I want to do with the Pico.
First, there's a little section to give my Pico a name, I'm calling it the led-blink-pico.
Then you describe the hardware you're configuring.
In my case, it's an RP2040, and the specific board is the RPi Pico W, or I guess if you
read it fast, the RPi-Pi-Cow.
I added this little bit since I'm working on the bleeding edge here, and not everything
in this video is stable yet.
By the time you're watching this, it might not be necessary.
Then I configure hardware outputs I wanna control—in this case, just the LED on the
board.
And then there's a WiFi configuration section, and for the configuration values it refers
out to a secrets file that stores my WiFi network name and password.
Finally, there's a section called 'Interval', where I tell ESPHome to start running some
code every half second to turn on the LED, wait, then turn off the LED.
So I ran this esphome command to compile the hello world LED blinker, and waited... and...
Well that's strange.
The LED blinks, technically.
But it's definitely not blinking consistently.
I turned on debugging messages and watched from my computer, and I saw that it seemed
to be getting stuck in some sort of boot loop...
Long story short: I found out that the software library ESPHome uses for the Pico's WiFi seems
to have a bug.
Right now at least, if you have a newer WiFi 6 network like I do, you might have to force
your 2.4 Gigahertz network into 'legacy' mode.
How did I figure that out?
Well, after trying two different WiFi 6 routers, I pulled out a Raspberry Pi 4, set up RaspAP
on it, and connected to that, and it worked!
So I knew it was something about the WiFi 6 network settings, and it was just a matter
of figuring out what setting caused the problem.
That problem took about two full days to solve, but hey, sometimes a hello world is hard.
Once I figured that out, WiFi worked and the LED blinked like it had never blunk before.
That—that's not a word.
Anyway.
So next up, I decided to re-test everything on another Pico W, because in my debugging
I did weird stuff to that first Pico, and I wanted to make sure everything would work
on a brand new Pico.
I popped the Pico onto a breadboard with some headers and soldered them on.
Then I put a couple sets of screw terminals on the same breadboard so I could use jumpers
to connect to the garage door sensors I bought.
For the sensors, I bought two of these Enforcer switches, since they seemed pretty rugged.
These sensors cost $20 each, but that's not a bad price for a device that has to survive
a harsh garage environment.
There are some other options too, but I liked how these mounted to the track.
Before I install the sensors, I just need to test 'em in my office, so that's why I'm
using these screw terminals on the breadboard.
With ESPHome, these sensors are treated like any other kind of 'open'/'close' or 'on'/'off'
sensor: you plug one wire into ground, and the other into a GPIO pin, and then set it
up like this.
These settings tell ESPHome about how the sensor behaves.
The switch 'closes' when it's near the sensor, and that'll tell ESPHome the garage door is
closed when the switch is closed—when it's near the sensor—and it's open when the switch
is open.
One of the most important parts is this little "pullup: true".
If you don't set that, then the Pico might show random values sometimes because the pin
can be 'floating' where it doesn't really know if it's open or closed.
Usually for a sensor like this you have to enable an internal pullup or pulldown resistor.
I also set up the LED on the Pico to turn on if the sensor shows the garage door is
open, and to turn off if the sensor shows it's closed, using these little if and else
statements down here.
So yeah... maybe I am doing a little programming.
But it's YAML, just kinda crazy YAML.
At least it's not as complex as Kubernetes!
There are a few other 'components' I added to this file, like this OTA section that'll
let me upgrade the Pico Over the Air.
There's also this API option that's required for it to connect to Home Assistant later.
And before you go taking notes, like everything else, all the code I wrote for this video
is open source, in a GitHub repository linked down below.
To flash the Pico, I ran the `esphome run` command.
After it was done and the Pico connected to my WiFi, I was able to see the LED going on
and off as I moved the sensor.
So this setup works, but it's not very useful yet.
Right now the little LED on the Pico lights up when the garage door opens, but that's
it.
I wanna know when the garage door opens from inside the house.
And for that, I need to tie it into Home Assistant.
Luckily, as long as you have that 'API' option set in your ESPHome config, the Pico W should
be picked up automatically.
All I had to do was go into Home Assistant's settings, click on Devices, then click Configure
under the garage-door-pico.
Home Assistant automatically found it for me.
After I entered the API password I set up, I could see, straight from Home Assistant,
the status of my garage door.
Or, well, the sensor on my desk.
So I added a new card for the sensor in my dashboard, and moving around the sensor I
could confirm it definitely works!
And that's great!
Except, of course, it's still just sitting on my desk.
So it's time to put everything in the garage.
The first step was to set up a nicer mounting option than a breadboard stuck on the wall.
And at first I was gonna use this 3D printed wall mount, but then I found this little RPi
Pico Breakout board on Amazon.
It comes with screws and spacers, and even includes some headers pre-cut for the Pico.
The best part is it has labeled screw terminals for every connection.
So I took everything out to my garage, and mounted the Enforcer sensor to the garage
door track.
It was a little tricky.
You have to put one metal piece in the sensor portion, then kinda squish it onto the track.
Then there's a set screw that can be really hard to turn, to give a little extra clamping
pressure.
And when I mean really hard to turn, I mean I gave myself a nice bruise trying to get
the screw to back out of one of the two sensors I bought!
The magnet side was easier, though—it has two sets of screw holes depending on how you
position it on the door.
In my case, there were already holes in the metal brackets here, so I just screwed it
into the holes with the self-tapping screws that came with the sensor.
The magnet doesn't have to line up perfectly, you can keep it an inch or two away.
The next step was routing the cable up the wall, and I'm glad it comes with this armored
cable attachment.
You never know what the kids will stack against things on the wall.
It also makes routing the cable along the wall easier, since I can just use these plastic
screw-in cable clamps.
Then I screwed the breakout board to the wall.
Luckily this is pretty close to where I used to have my irrigation system controller plugged
in, so I can get power from there.
I screwed the two wires coming from the sensor into ground and GPIO pin 2, then I grabbed
my Pico and plugged it into the breakout board.
I plugged power in, and of course nothing happened, because the LED defaults to staying
off unless the garage door is open.
So now the moment of truth...
[garage door opening]
It worked!
Well, at least the LED on the Pico lit up when the garage door was open, and turned
off when it was closed.
I had to see if the Home Assistant integration was also working so I pulled out my phone,
and sure enough, it updates less than a second after the garage door starts opening.
Pretty neat!
Since I can update the Pico over the air, I'll be able to update it and add the other
sensor when that's ready.
I started installing it but realized I didn't have any spare two-conductor wire to get over
to the Pico, so I'll have to put off doing that until after my next trip to the hardware
store.
But just showing the garage door status in Home Assistant isn't that helpful.
I want Home Assistant to remind me to close the garage door!
And that's actually really easy to do.
I already have the app on my phone, so I can set up a push notification using an automation.
Now, before all you Home Assistant experts start yelling at me about writing up some
Alert YAML instead, I decided to use an automation because I want to do this through the UI.
And in that UI, I added an Automation that triggers on the garage door opening.
After a five minute delay, it notifies everyone the garage door is still open, pauses for
10 minutes, and repeats, until the garage door's closed.
To test it, I had my wife open the garage door while I was down in my office, and it
worked!
After five minutes, I got this notification on my iPhone and also on my Apple Watch, and
now I'll never have an excuse for leaving the garage door open again.
At least, for more than five minutes or so.
So that's that.
I got it all working, and the hardest part of the whole process was getting that LED
blinking at the beginning.
But the cool thing is, ESPHome now supports the Raspberry Pi Pico and Pico W. And when
I mentioned that on Twitter, a lotta people asked why I'd choose a Pico over an ESP32
or ESP8266.
Well, for one, having more options is usually better.
But on a more practical note, the last time I visited my local Micro Center, they had
about a hundred Raspberry Pi Pico W and only about 3 ESP32 devices.
Having one standard microcontroller versus the hundred or so variants of ESP32 boards
also means it's easier to standardize support for it.
There are other RP2040 boards but the majority of projects using Raspberry Pi's chip seem
to build around the Pico board.
And that also means there are third party kits like Pimoroni's Galactic Unicorn—which
runs on a Pico W—that I might be able to integrate with ESPHome out of the box!
I'm also building a full wall-mounted dashboard with this 10.1" Pi display.
I'll be integrating it and this Galactic Unicorn into my Home Assistant setup soon, so subscribe
and like this video for more.
Until next time, I'm Jeff Geerling.