usb hid device -> esp32s3 -> bluetooth hid output

hvns_lab
Posts: 1
Joined: Mon Feb 03, 2025 4:57 pm

usb hid device -> esp32s3 -> bluetooth hid output

Postby hvns_lab » Mon Feb 03, 2025 5:45 pm

hello,

i'm trying to incorporate an esp32s3 into a custom keyboard build. basically i want a wireless qmk setup, and instead of using the more expensive nRF52840, i opted for the esp. however as someone who is relatively new to programming i severely underestimated how much trouble this board would give me. what i'm trying to do is

rp2040 outputting HID via usb -> esp32s3 acting as host and wirelessly sending HID to laptop

i've been able to get the example hid host project to flash and work (confirmed keypresses coming from a connected keyboard while monitoring), but i have no idea how to modify the code to get it to send those keypresses over bluetooth.

i tried using chatgpt to modify it but it's using outdated information and i can't seem to compile at all.. would anyone be able to point me in the right direction/tell me what i'm doing wrong?
  1. #include <stdio.h>
  2. #include <stdbool.h>
  3. #include <string.h>
  4. #include <unistd.h>
  5. #include "freertos/FreeRTOS.h"
  6. #include "freertos/task.h"
  7. #include "freertos/event_groups.h"
  8. #include "freertos/queue.h"
  9. #include "esp_err.h"
  10. #include "esp_log.h"
  11. #include "driver/gpio.h"
  12. #include "usb/usb_host.h"
  13. #include "usb/hid_host.h"
  14. #include "usb/hid_usage_keyboard.h"
  15. #include "usb/hid_usage_mouse.h"
  16. #include "esp_bt.h"
  17. #include "esp_bt_main.h"
  18. #include "esp_gap_bt_api.h"
  19. #include "esp_hid_gap.h"
  20. #include "esp_hidh.h"
  21.  
  22. static const char *TAG = "HID_BT_USB";
  23. static esp_hidh_dev_t *hid_dev = NULL;
  24.  
  25. /**
  26.  * @brief Bluetooth HID event callback
  27.  */
  28. static void hidd_event_callback(void *arg, esp_hidh_event_t event, void *param) {
  29.     switch (event) {
  30.         case ESP_HIDH_OPEN_EVENT:
  31.             ESP_LOGI(TAG, "Bluetooth HID device connected");
  32.             break;
  33.         case ESP_HIDH_CLOSE_EVENT:
  34.             ESP_LOGI(TAG, "Bluetooth HID device disconnected");
  35.             break;
  36.         default:
  37.             break;
  38.     }
  39. }
  40.  
  41. /**
  42.  * @brief Initialize Bluetooth HID Device mode
  43.  */
  44. void bluetooth_hid_init(void) {
  45.     esp_bt_controller_config_t bt_cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT();
  46.     ESP_ERROR_CHECK(esp_bt_controller_init(&bt_cfg));
  47.     ESP_ERROR_CHECK(esp_bt_controller_enable(ESP_BT_MODE_BTDM));
  48.     ESP_ERROR_CHECK(esp_bluedroid_init());
  49.     ESP_ERROR_CHECK(esp_bluedroid_enable());
  50.     ESP_ERROR_CHECK(esp_hid_gap_init());
  51.     ESP_ERROR_CHECK(esp_hidh_init(hidd_event_callback, NULL));
  52.     ESP_LOGI(TAG, "Bluetooth HID Initialized");
  53. }
  54.  
  55. /**
  56.  * @brief Send keyboard event over Bluetooth
  57.  */
  58. void send_keyboard_event(uint8_t modifier, uint8_t key, bool pressed) {
  59.     if (hid_dev) {
  60.         uint8_t report[8] = {modifier, 0, key, 0, 0, 0, 0, 0};
  61.         esp_hidh_dev_input_report(hid_dev, 1, report, sizeof(report));
  62.         ESP_LOGI(TAG, "Sent key %02X %s over Bluetooth", key, pressed ? "PRESSED" : "RELEASED");
  63.     }
  64. }
  65.  
  66. /**
  67.  * @brief Send mouse movement over Bluetooth
  68.  */
  69. void send_mouse_event(int8_t x, int8_t y, uint8_t buttons) {
  70.     if (hid_dev) {
  71.         uint8_t report[4] = {buttons, x, y, 0};
  72.         esp_hidh_dev_input_report(hid_dev, 2, report, sizeof(report));
  73.         ESP_LOGI(TAG, "Sent mouse X:%d Y:%d Buttons:%02X over Bluetooth", x, y, buttons);
  74.     }
  75. }
  76.  
  77. /**
  78.  * @brief app_main() - Initialize USB HID & Bluetooth HID
  79.  */
  80. void app_main(void) {
  81.     ESP_LOGI(TAG, "Starting HID Host example");
  82.    
  83.     bluetooth_hid_init();
  84.    
  85.     // USB HID Host Initialization
  86.     ESP_ERROR_CHECK(hid_host_install(NULL));
  87.     ESP_LOGI(TAG, "USB HID Initialized");
  88. }

igi540
Posts: 2
Joined: Thu Jan 23, 2025 1:00 pm

Re: usb hid device -> esp32s3 -> bluetooth hid output

Postby igi540 » Tue Feb 04, 2025 8:22 am

Hi,
It looks like there’s a fundamental mix-up in the roles being implemented. In your intended setup, the PC is the host, and the keyboard is the device. That means your ESP32S3 needs to act as a Bluetooth HID device (like a standard wireless keyboard), not as a HID host. Currently, your code is using the HID host API (with functions such as esp_hidh_init() and esp_hidh_dev_input_report()), which is meant for when the ESP32S3 is connecting to and receiving input from HID devices. This is the opposite of what you need.

To clarify:

HID Device: This is what a keyboard or mouse is. It sends HID reports to the host. In your case, the ESP32S3 should be configured as a Bluetooth HID device that sends keyboard reports to the PC.
HID Host: This is typically the PC that receives the HID reports from the device.

By switching from the HID host API to the Bluetooth HID Device API and properly handling the report descriptors and event callbacks, your ESP32S3 should be able to pair with your PC as a wireless keyboard.

Hope this helps!

ahsrabrifat
Posts: 23
Joined: Sat Jan 18, 2025 2:31 pm

Re: usb hid device -> esp32s3 -> bluetooth hid output

Postby ahsrabrifat » Tue Feb 04, 2025 1:46 pm

You need to use Bluetooth Low Energy (BLE) instead of Bluetooth Classic (BT). The esp_hidh library is primarily for HID host, not for acting as a Bluetooth HID device

Who is online

Users browsing this forum: Bing [Bot], nopnop2002, oz1cmdk and 84 guests