Change pin control timing

- The HA automation script is no longer required and can be removed.
- Renamed button/sense.
- Updated documentation.
This commit is contained in:
Erriez
2022-02-13 11:08:10 -08:00
parent ec7b76310c
commit 106e18d80d
4 changed files with 141 additions and 66 deletions

178
README.md
View File

@@ -1,31 +1,55 @@
# ESPHome PC Power Control via Home Assistant
This project contains PC power control via HomeAssistant and ESPHome ESP8266 NodeMCU. I use this project to power my ATX based NAS remotely on and off via Home Assistant.
This project contains remote PC power control via HomeAssistant and ESPHome ESP8266/ESP32 boards.
Features:
I use this project to power my PC based NAS remotely on and off via Home Assistant as alternative to Wake-On-Lan (WOL) which has known limitations.
* Short press power button to turn PC on or graceful shutdown.
* Long press power button to generate hard power-off.
* Read power status motherboard (is PC turned on or off).
* Use existing power / reset buttons at the front panel.
## Features
* Control physical PC power button (front panel) connected to the motherboard:
* Turn PC power on from any power state such as `power-off/suspended/hybernate` (Behavior short press power button).
* Graceful `shutdown/suspend/hybernate` of the operating system (Behavior short press power button).
* Force power-off (Behavior long press power-button).
* Read PC power on/off status from motherboard.
* No operating system dependency.
* Power/reset buttons front panel remains full functional.
* User authorization via Homeassistant.
* Use with any (micro/mini)-ATX computer motherboard.
Youtube video:
[![Youtube video](https://img.youtube.com/vi/rAcvqaPf830/0.jpg)](https://www.youtube.com/watch?v=rAcvqaPf830)
Homeassistant screenshot:
![Screenshot Home Assistant](images/ScreenshotHomeAssistant.jpg)
## Differences Wake-On-Lan (WOL) magic packet
[Wake-On-Lan (WOL)](https://en.wikipedia.org/wiki/Wake-on-LAN) is intended to turn computer power on remotely via Ethernet by sending a so called `magic packet`. This has several known limitations which are not applicable with this ESP project:
* Wake-On-Lan (WOL) has **no authorization**! The WOL protocol has no functionality to provide a password or allow a specific computer to wake from. Any device on the same LAN can wake a PC by sending a magic packet. The ESP is password protected via Homeassistant.
* Wake-On-Lan (WOL) can only `wake` a PC from suspend or power-off state. There is no shutdown/suspend functionality.
* To put a Linux machine in `sleep` state, a SSH connection to the remote PC can be made for example by executing `pm-suspend` command (Install via `sudo apt install pm-utils`), reference [Ubuntu pm-action documentation](https://manpages.ubuntu.com/manpages/jammy/man8/pm-action.8.html). Disadvantages:
* Requires SSH login.
* Requires sudo rights.
* Wake-On-Lan magic packets are ignored when connecting main power the first time to the power supply, even when WOL is activated in the BIOS. The reason is that WOL is disabled on most computers at first power on and requires activation by a running operating system before the PC responds on a magic packet to wake the computer. Reference: [Ubuntu Wake-On-Lan](https://help.ubuntu.com/community/WakeOnLan).
* Running Homeassistant [Wake-On-Lan](https://www.home-assistant.io/integrations/wake_on_lan) in a Docker container cannot forward broadcast magic packets with a bridged network configuration. It requires an external Docker container as bridge to forward magic packages and is beyond the scope of Homeassistant. This is currently not included in Homeassistant documentation.
## Hardware
The hardware consists of an ESP8266 and two IO pins, mounted on a PCI metal plate (see picture below). In my case I used an ESP8266 NodeMCU board. Theoretically any ESP8266 or ESP32 board can be used for this project.
The hardware consists of an ESP8266 or ESP32 and two IO pins, mounted on a PCI metal plate (see picture below). In my case I used an ESP8266 NodeMCU board. Theoretically any ESP8266 or ESP32 board can be used for this project.
One pin controls the power button by generating a long or short press and a second pin to read the power status of the ATX motherboard.
* One pin controls the power button by generating a long or short press
* A second pin reads the motherboard power status.
![Hardware](images/NodeMCU.jpg)
## Schematic
My NAS is based on an Intel Core I7 ASRock Z97 ATX motherboard and contains standard 2x5 male connectors:
My NAS is based on an Intel Core I7 ASRock Z97 ATX motherboard and contains standard 2.5mm 2x5 male headers:
![Schematic](images/Schematic.png)
**ATX header USB 2.0:**
@@ -39,15 +63,15 @@ Header `J2` contains the power, reset and GND pins:
* Short press: Turn PC on or generate graceful shutdown.
* Long press: Generate hard power off.
* Reset button pin 7.
* Used to read power status (not for hard reset)
* Used to read power status via the reset button pin:
* High: power-on
* Low: power-off
* GND pin 5.
* Pin `D1` is used to read the power status from the reset pin: High is on, Low is off. Transistor `Q1` is used for secure isolation between ESP8266 and motherboard.
* Pin `D2` is used to pull the power button low to generate a short or long press. Resistor `R1` is used to minimize current when the IO pin is accidentally set to output.
All ATX and ESP8266 pins must be operating at 3V3.
![Schematic](images/Schematic.png)
Warning: All ATX and ESP pins must be operating at 3V3.
## Downloads
@@ -55,11 +79,23 @@ All ATX and ESP8266 pins must be operating at 3V3.
## Wiring
![Wiring](images/Wiring.jpg)
A DIY breakout PCB can be mounted at the system panel header to connect power button `SW1` and reset button `SW2`.
There is sufficient space in the ATX computer case to mount the ESP8266 board and connecting a USB cable to program the ESP8266.
## WiFi stability
![Wiring](images/Wiring.jpg)
There is sufficient space in an ATX computer case to mount the ESP board. However, the computer case is metal shielded, so the WiFi distance to the base station reduces. It is recommended to place the ESP outside the computer case when the WiFi connection is unstable or distance too low. An ESP32 may result in different WiFi connection stability, but overall it depends on the environment.
ESP8266 PCB mounted at the back of the PC case:
![ESP PC mount](images/ESPMount.jpg)
## Software updates
The ESPHome application firmware can be updated via USB connection (virtual serial port) or WiFi OTA (Over The Air update).
Updating the firmware via WiFi is enabled when configuring `ota` in the .yaml file. This requires a WiFi connection between host computer and ESP. OTA update does not work when the login from ESP to WiFi base station fails due to incorrect WiFi credentials. In this case an update via serial is required or via `ap` fallback as configured in the .yaml file.
## ESPHome
@@ -70,7 +106,9 @@ Documentation:
* [ESPHome GPIO Switch](https://esphome.io/components/switch/gpio.html)
* [ESPHome Binary Sensor](https://esphome.io/components/binary_sensor/gpio.html)
### pc-power.yaml
### Project configuration
Configure the `platform` and `board` as described in [ESPHome documentation](https://esphome.io/components/esphome.html) in `pc-power.yaml`:
```yaml
esphome:
@@ -90,26 +128,42 @@ wifi:
# Enable logging
logger:
# Enable Home Assistant API
api:
password: !secret esphome_api_password
# Enable OTA update via WiFi
ota:
password: !secret esphome_ota_password
switch:
- platform: gpio
name: "PC Power button"
name: "NAS Power Toggle"
icon: "mdi:electric-switch"
pin: D2 # Power button output pin
id: power_short_press
inverted: no
id: power_pin
on_turn_on:
- delay: 150ms
- switch.turn_off: power_short_press
- platform: gpio
name: "NAS HARD POWER OFF"
icon: "mdi:electric-switch"
pin: D2 # Power button output pin
id: power_long_press
inverted: no
on_turn_on:
- delay: 3500ms
- switch.turn_off: power_long_press
binary_sensor:
- platform: gpio
pin: D1 # Power detect input pin
name: "PC Power Sense"
pin: D1 # Power detect input pin (readback from Reset button)
name: "NAS Power State"
device_class: power
```
### secrets.yaml
Configure WiFi SSID and passwords in `secrets.yaml`:
```yaml
esphome_api_password: "changeme"
@@ -119,11 +173,14 @@ esphome_wifi_ssid: "ssid"
esphome_wifi_password: "password"
```
### Program ESP8266
### Program ESP8266 or ESP32
Connect USB cable to ESP8266 board and enter the following commands. (Examples are tested on Ubuntu). For more information, refer to [ESPHome.io](https://esphome.io/guides/getting_started_command_line.html).
Connect USB cable to ESP8266 or ESP32 board and enter the following commands. (Examples are tested on Ubuntu). For more information, refer to [ESPHome.io](https://esphome.io/guides/getting_started_command_line.html).
```bash
# Clone this repository
$ git clone git@github.com:Erriez/ESPHomePCPowerControlHomeAssistant.git
# Install Python3 virtualenv
$ sudo apt install python3-virtualenv
@@ -136,16 +193,27 @@ $ source venv/bin/activate
# Install ESPHome
$ pip install esphome
# Optional: Install platformio updates
$ platformio platform update
$ platformio upgrade
# Optional: Add user permission serial port
$ sudo usermod -a -G dialout <USERNAME>
$ sudo reboot now
# Check ESPHome installation
$ esphome --help
# Upload program to ESP8266
$ esphome pc-power.yaml run
# Optional: Compile program without upload
$ esphome compile pc-power.yaml
Select serial upload port
# Upload program to ESP8266 or ESP32
$ esphome run pc-power.yaml
# Select serial port or WiFi to upload application
# Check logs
$ esphome pc-power.yaml logs
$ esphome logs pc-power.yaml
```
## Home Assistant configuration
@@ -156,58 +224,46 @@ This section describes Home Assistant configuration.
* `Configuration | Integrations: Add Integration: ESPHome`
* Select hostname or IP address of the ESP device.
* Enter password as configured in `secrets.yml` `esphome_api_password`.
* Enter password as configured in `secrets.yml` | `esphome_api_password`.
### Create Automation Scripts
### Homeassistant | Edit Dashboard | RAW Configuration Editor
Two scripts are needed to generate a short and long pulse of the power button.
`Configuration | Scripts | Add script`:
* Name: pc_power_button_short_press
* Action type: Device
* Action: Turn on PC power button
* Action type: Delay 600ms
* Action type: Device
* Action: Turn off PC power button
`Configuration | Scripts | Add script`:
* Name: pc_power_button_long_press
* Action type: Device
* Action: Turn on PC power button
* Action type: Delay 5s
* Action type: Device
* Action: Turn off PC power button
### Edit Dashboard | RAW Configuration Editor
Add PC power integration to a dashboard via raw edit:
```yaml
title: Home
views:
- title: PC
path: pc
badges: []
cards:
- type: button
entity: switch.pc_power_button
entity: switch.pc_power_toggle
show_name: true
tap_action:
action: call-service
service: script.pc_power_button_short_press
name: PC Power Button
- type: button
tap_action:
action: none
entity: binary_sensor.pc_power_sense
entity: binary_sensor.pc_power_state
hold_action:
action: none
- type: button
entity: switch.pc_power_button
show_name: true
tap_action:
action: call-service
service: script.pc_power_button_long_press
name: PC Force OFF
action: toggle
entity: switch.pc_hard_power_off
icon_height: 40px
show_state: false
show_name: true
show_icon: true
```
## Version history
Restart Home Assistant and ready to go!
### Update 13 February 2022
* The button pin timing controlled via Homeassistant scripts was not stable. The power pin control has been moved to the ESP firmware which generates a much more accurate timing.
* Renamed the following names:
* Rename `switch.pc_power_button` to `switch.pc_power_toggle`.
* Rename `switch.pc_power_button_long_press` to `switch.pc_hard_power_off`.
* Rename `switch.pc_power_sense` to `switch.pc_power_state`.

BIN
images/ESPMount.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 46 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 21 KiB

After

Width:  |  Height:  |  Size: 20 KiB

View File

@@ -3,32 +3,51 @@ esphome:
platform: ESP8266 # ESP8266 or ESP32
board: nodemcuv2 # Any ESP8266 or ESP32 board
# WiFi connection to base station
wifi:
ssid: !secret esphome_wifi_ssid
password: !secret esphome_wifi_password
# Enable fallback hotspot (captive portal) in case wifi connection fails
ap:
ssid: "PC Power Fallback Hotspot"
ssid: "PC Power Fallback Hotspot"
password: !secret esphome_ap_password
# captive_portal:
# Enable logging
logger:
# Enable Home Assistant API
api:
password: !secret esphome_api_password
# Enable OTA update via WiFi
ota:
password: !secret esphome_ota_password
switch:
- platform: gpio
name: "PC Power button"
name: "PC Power Toggle"
icon: "mdi:electric-switch"
pin: D2 # Power button output pin
id: power_short_press
inverted: no
id: power_pin
on_turn_on:
- delay: 150ms
- switch.turn_off: power_short_press
- platform: gpio
name: "PC HARD POWER OFF"
icon: "mdi:electric-switch"
pin: D2 # Power button output pin
id: power_long_press
inverted: no
on_turn_on:
- delay: 3500ms
- switch.turn_off: power_long_press
binary_sensor:
- platform: gpio
pin: D1 # Power detect input pin
name: "PC Power Sense"
pin: D1 # Power detect input pin (readback from Reset button)
name: "PC Power State"
device_class: power