Hi!
I just wanted to present my iPad wakeup solution.
The basic idea is that an ESP32 emulates a virtual Bluetooth Low Energy (BLE) keyboard to send the “ESC” key whenever motion is detected, i.e. a GPIO pin toggles.
The solution uses ESPHome for the ESP32 firmware.
Very nice: with ESPHome you also have the possibility to do OTA FW upgrades.
ESPHome YAML file:
esphome:
name: blekeyboard-eg
platform: ESP32
board: esp32dev
platformio_options:
build_flags:
- -DUSE_NIMBLE
includes:
- esp32_ble_keyboard.h
libraries:
- "NimBLE-Arduino"
- "t-vk/ESP32 BLE Keyboard"
# Enable logging
logger:
# Enable Home Assistant API
api:
ota:
password: "xxxxxxxxxxxxxxxxxxxxxxxxxx"
wifi:
ssid: "SSID"
password: "PASSPHRASE"
# Enable fallback hotspot (captive portal) in case wifi connection fails
ap:
ssid: "Blekeyboard-Eg Fallback Hotspot"
password: "XYZXYZXYZ"
captive_portal:
switch:
- platform: gpio
name: "Status LED"
id: status_led
pin:
# Built in LED
number: GPIO5
inverted: false
on_turn_on:
- delay: 50ms
- switch.turn_off: status_led
custom_component:
- lambda: |-
return {new Esp32BLEKeyboard(App.get_name(), "ESPHome")};
components:
- id: ble_keyboard
# The variable type is just Component, so a static_cast<Esp32BLEKeyboard *> is needed everywhere
# Example configuration entry
binary_sensor:
- platform: gpio
name: "Pin GPIO23"
pin: GPIO23
on_press:
- switch.turn_on: status_led
#- lambda: ble_keyboard_text->publish_state({KEY_ESC, 0});
- lambda: (static_cast<Esp32BLEKeyboard *>(ble_keyboard))->bleKeyboard.write(KEY_ESC);
- platform: custom
lambda: |-
auto kbd = static_cast<Esp32BLEKeyboard *>(ble_keyboard);
return {kbd->connected_binarysensor};
binary_sensors:
- name: "Connected"
text_sensor:
- platform: custom
lambda: |-
auto kbd = static_cast<Esp32BLEKeyboard *>(ble_keyboard);
return {kbd->textsensor};
text_sensors:
- id: ble_keyboard_text
# Use with lambda: ble_keyboard_text->publish_state("Hello, World!")
# Note: media keys are special, they are are of type MediaKeyReport and can't be converted to strings
After creating the ESPHome project with the above YAML file, try to install it using the ESPHome Addon UI. It will complain that the header file is missing.
Just create a file and save this content:
#include <BleKeyboard.h>
#include "esphome.h"
class Esp32BLEKeyboard : public PollingComponent {
public:
BleKeyboard bleKeyboard;
BinarySensor *connected_binarysensor = new BinarySensor();
TextSensor *textsensor = new TextSensor();
Esp32BLEKeyboard(std::string name, std::string manufacturer) : PollingComponent(1000), bleKeyboard(name, manufacturer, 100) {}
void setup() override {
ESP_LOGD("Esp32BLEKeyboard", "Setting up BLE Keyboard...");
bleKeyboard.begin();
bleKeyboard.releaseAll();
this->textsensor->add_on_state_callback(
[this](std::string value) {
ESP_LOGD("Esp32BLEKeyboard", "Sending text %s", value.c_str());
this->bleKeyboard.print(value.c_str());
});
}
void update() override {
connected_binarysensor->publish_state(bleKeyboard.isConnected());
}
};
Copy this file to your config/esphome folder next to your YAML file.
This config uses GPIO pin 23 with an internal pull-down resistor. Just connect your relais (from your PIR sensor) to 3.3V and GPIO pin 23.
GPIO pin 5 is used to flash an LED whenever GPIO23 goes high.
BTW: I am not using a PIR sensor, but a radar sensor (HUBER Motion 50HFLV). The advantage is that it can be hidden behind a wall or in a wardrobe, but it still detects motion in front of it.
For the ESP32 board I am just using a cheap ESP32 Dev Kit C board.
UPDATE:
Forgot to mention to whom the credits belong to for all this: