ESPHome Ducky - MQTT keyboard/mouse control

What is it?
Control devices with a virtual usb keyboard/mouse.

How does it work?

  • The arduino is emulating HIDs (Human Interface Devices) such as keyboard/mouse via USB
  • The ESPHome board is receiving commands via wifi/mqtt and writes them to the serial port
  • The commands are in Ducky Script syntax
  • The commands are read from the serial port by the arduino and executed as keyboard/mouse actions
  • Furthermore it’s possible to get the host machine’s power state from the USB state via the serial port

For example, this will open up notepad and type some text in it:

service: mqtt.publish
data:
  topic: esphome/nodemcu/run
  payload: |
    GUI r
    DELAY 200
    STRING notepad.exe
    ENTER
    DELAY 200
    STRING Hello World

You can do basically everything that a keyboard and mouse can do. Media keys and power buttons are also supported. See github project for more information.
More examples:

2 Likes

I have just tested a rpi pico, which work out of the box as a HID device. My use case is slightly different, connecting physical buttons and a rotary encoder to a media player. You could join the pico to an esp to make a wifi device.

1 Like

I guess my arduino sketch wouldn’t work on the pico? But yeah, the concept should be applicable. Has the pico any major advantages?

The pico had the advantage of being

  1. small

  2. available within a few days in a country that goes to sleep during summer.

  3. it runs micropython and circuitpython which meant I could shamelessly use a howto I found on the net.

1 Like

Is there any chance that, if you use an esp32 that you’ll receive an command from esphome and then send a keystroke with ble to a ble connected host?

That seems like a solution for you: ESPHome BLE Keyboard

You can use the receive-command-part from my project and the send-part from that project.

2 Likes

Thank you! This is exactly what I need to wake up a PC from sleep which doesn’t have wake on LAN via the BIOS.

Can I just buy one of the Ducky built hardware dongles and flash over your project?

Hi, the device you’ve linked uses a different communication protocol (I²C instead of serial), so you would have to adjust that in both firmwares. I haven’t tried it myself though.

Really cool project, @jwm
What do you think of the idea to let the Bluetooth radio of a ESP32 emulate a Bluetooth keyboard directly to the computer?

I found this, so the emulation part could work, I guess. But don’t know if you can combine the code to use Bluetooth to the computer, and Wi-Fi to the Home Assistant server.

Edit: I now see that you wrote about something similar a few posts from the end.
But feel free to comment anyway. :slight_smile:

I tried that ble project and had some issues with android controls (power button IIRC) and some reconnect problems after sleep of the android-box. So I’m still using the arduino over usb.

In general I find NicoHood’s usb library (which my project uses) much more powerfull especially with special keys (even gamepad is possible) or different language layouts.

1 Like

Thanks for the reply. I’ll definitely build your project if the Bluetooth one isn’t stable enough.

hey, I know this is a late follow up, but did you have any success with the Wifi Duck?
I’m also looking to power on my laptop and wol does not work through my thunderbolt dock, so this looks like an interesting solution…

I successfully got this to work when plugged into my desktop. However when I plugged it into my tesmart KVM (HKS0202A2U) It wouldn’t pick up on the HID Keystrokes. Do you have any ideas why this didn’t work as expected?

Since it has hotkey switching, I guess that it’s not passing through the arduino. Instead it will interpret the keyboard commands itself first and mimic the commands to the pc?

The arduino board firmware does setup 4 hid devices in the code. Maybe the kvm switch cannot handle that. You can try to enable only the keyboard, maybe this will work.

EDIT: Here is a possibly related issue: KVM Switch (Disable CDC Serial for better BIOS/UEFi/Custom compatibility) · Issue #225 · NicoHood/HID · GitHub