I can now slap my table to turn on my PC, thanks to a simple vibration sensor

2 weeks ago 5

The act of turning on your PC is a simple one, but pressing the power button isn't the only way to do it. In fact, I would argue the power button is archaic, and there are significantly more convenient ways to switch on your PC when you're ready to use it. That's why I slap my table to turn it on instead.

Okay, to be fair, calling a power button "archaic" was extreme hyperbole and said in jest, but it is true that there are more convenient ways to switch on a computer. With tools like Home Assistant, you can automate your PC's boot-up sequence with the Wake-on-LAN (WoL) integration, and you could send a WoL magic packet based on any trigger that you can think of. I did by detecting when my toothbrush was active before, but I've come up with something fun: a vibration sensor.

I've been getting into ESPHome a lot recently, and I picked up a couple of ESP32-WROOM-32 modules alongside a ton of random sensors for experimentation. It's been a great way to learn how to build my own smart home devices, and many of them can be built for cheaper than premade alternatives that you'd find online. I had the idea of taking a vibration sensor, strapping it to my desk, and being able to slap my desk to boot up my PC. Why reach under the table and press the power button when an overdramatic slap of the desk can boot my PC instead? Here's how I did it, and how you can too.

The Home Assistant add-ons page

Related

10 of the best add-ons for Home Assistant

Boost the capabilities of your smart home with these amazing Home Assistant plugins

An ESP32, some jumper wires, and a vibration sensor are all you need

It's really cheap, too

ky-002 vibration sensor plugged into an ESP32

There aren't a lot of requirements for building this, and you can get all of the parts required for this for approximately $5. The ESP32 will undoubtedly be the most expensive part of this project, typically coming in at around $4, though smaller variants can be found for cheaper. All you need to make sure of is that there's a data pin, a ground pin, and a 3.3V or 5V pin. I'm using the KY-002, which can be found for as little as $0.50, and the beauty of this particular sensor is that you don't need it to be all too accurate, plus there are many alternatives too. The reason it doesn't need to be all too accurate is that once your PC is on, receiving any additional Wake-on-LAN packets won't cause any issues.

Connecting the ESP32 to ESPHome is pretty easy, especially in Home Assistant using the official add-on. Plug it into your PC, navigate to ESPHome, and click Add device. You'll then need to select the serial port corresponding to your ESP32, and it'll flash it with custom software that will enable you to then control the device remotely in your home. This is fantastic for making changes and tweaking remotely, as more advanced ESP32-based projects can be modified remotely without needing to take anything apart. Plus, any breaking changes that prevent it from booting will cause it to boot into safe mode so that it can still be tweaked and modified.

The other beauty of ESPHome is how it significantly simplifies the development and deployment process of new devices. Sites like ESPBoards can provide prebuilt code to interface with these devices, which, in the case of ESPHome, is the YAML to initialize the sensor and expose it to Home Assistant.

ESPHome KY-002 YAML

Note that most of the code above is generated by ESPHome by default, and the "output" and "light" keys were made for testing an RGB LED that I had connected. These aren't necessary for a simple vibration sensor. The only code that's relevant to the vibration sensor I'm using is the "binary_sensor" key, which initializes the sensor and exposes it to Home Assistant as follows:

binary_sensor:
- platform: gpio
pin:
number: GPIO14
mode: INPUT_PULLUP
name: "KY-002 Vibration Sensor"
filters:
- delayed_on: 10ms
- delayed_off: 10ms
on_press:
- then:
- lambda: |-
ESP_LOGD("sensor", "Vibration detected!");

ESPHome will handle the Home Assistant part of things, so it'll simply show up as a binary sensor that you can read from or react to.

KY-002 binary sensor in Home Assistant

In the device created for my ESP32 in the ESPHome integration, I can now see a binary sensor that tracks its current state. It will always be "on" when nothing is detected, and switches to "off" when it detects movement. It takes just a few moments to wire up appropriately, a minute or two to connect to ESPHome, and it's ready to go for our automation.

Keep in mind that when wiring up a vibration sensor, you'll need to be mindful of what pins connect to where. You can find diagrams online for specific pins on these devices to find where they go, and the signal pin will always go to a pin labelled "DX" on the ESP32, where X is a number. These are data pins, and in my case, D14 is GPIO14. Keep in mind that some GPIO pins are what's known as "strapping pins," which can define the boot behavior. Try to avoid these where possible, though devices won't always cause issues when connected to these.

Arduino Uno and some other microcontrollers

Related

Here’s how you can connect your microcontrollers with Home Assistant

Want to inject some microcontroller magic into your smart home? You've come to the right place!

Automating PC wake-up

A simple "if-then" setup

Waking up a PC when vibration sensor changes

The best part about this setup is just how easy it was to automate the wake-up of my PC. Home Assistant has a built-in Wake-on-LAN integration, so you just need to enter the MAC address of your PC as the device to target, and you can turn on a PC based on any configuration, so long as WoL is enabled on it.

In this case, we simply listen for a change in the sensor from "On" to "Off", and this can send our WoL packet to our PC. I taped the vibration sensor to the underside of my desk, and slapping my desk (not even that hard, either) is enough to trigger it. It's not sensitive enough that a simple bump into the desk would trigger it (nor is it really possible for me to based on the layout of my working space), but it's sensitive enough that I can just slap my desk in the morning and have my PC turn on.

It's pretty silly, but honestly, it has uses. It's pretty funny to just hit my desk to turn on my PC, but there are other uses for this kind of setup as well. A vibration sensor can be extremely useful, from a home security standpoint or detecting when something has finished running. For example, I could place this on my washing machine, and have an automation trigger based on whether roughly enough time has passed that it should be done, and it hasn't detected any vibrations in five minutes.

I've been loving experimenting with an ESP32, and with sensors being so cheap to purchase and hook up, I'm probably done buying premade sensors for the most part. For more "advanced" things, I'll almost certainly just buy something pre-made, but for something basic, like a temperature sensor, then a DHT11 and a small ESP32 board are much cheaper.

Read Entire Article