Guest User

Serial to BLE keyboard on Arduino ESP32

a guest
Nov 29th, 2024
355
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.98 KB | Source Code | 0 0
  1. /*
  2.  
  3. First I needed to download and install free Arduino IDE from arduino.cc (Win/Mac/Linux)
  4.  
  5. Then I needed to install a BLE keyboard library, I went to https://github.com/T-vK/ESP32-BLE-Keyboard
  6. And downloaded the entire repository as a ZIP file, then placed the unzipped folder in my "Arduino/libraries" folder
  7. (this is a documents-level folder that appears after first run, not a "program files" type folder)
  8.  
  9. Since I also used an M5Stack-branded microcontroller, I configured M5Stack as "Additional Boards" by
  10. placing the following URL into Arduino - Preferences - Additional Boards URLs:
  11. https://m5stack.oss-cn-shenzhen.aliyuncs.com/resource/arduino/package_m5stack_index.json
  12.  
  13. Which resulted in "Boards Manager" offering to let me install "M5Stack" as a boards selection. Install.
  14. Because Arduino IDE uses URL's to learn about the existence of other brands of boards, and install them.
  15.  
  16. Then I was able to paste the code below as a new Sketch, and compile it, and flash it to an M5Stack device
  17. device over USB (specifically, a serial port emulated over USB).
  18.  
  19. M5Stack devices I tried:
  20. CORE -- because I had one handy. But nothing references the LCD screen, so it will be conspicuously off
  21. AtomS3 -- I think this is perfect because it pairs with "Atom RS232 Base" and gives you proper RS232
  22. RX/TX voltage.
  23.  
  24. Purchase links (both items are under $10)
  25. AtomS3 --> https://shop.m5stack.com/products/atoms3-lite-esp32s3-dev-kit
  26. Atom RS232 base --> https://shop.m5stack.com/products/atomic-rs232-base-w-o-atom-lite
  27.  
  28. Depending on the device you choose, you may have to adjust SERIAL_RX_PIN to properly reflect the pin
  29. number that will be physically receiving the data. On the AtomS3 it may be pin 5 not 19 when using
  30. the base (see the numbers on the bottom of the AtomS3)
  31.  
  32. */
  33. #include <BleKeyboard.h>
  34.  
  35. // Instantiate the Bluetooth Keyboard object
  36. BleKeyboard bleKeyboard("ESP32 Keyboard", "ESP32", 100);
  37.  
  38. // Define the serial input pin
  39. #define SERIAL_RX_PIN 5
  40. #define SERIAL_BAUD_RATE 9600
  41.  
  42. // Buffer for incoming serial data
  43. String inputBuffer = "";
  44.  
  45. void setup() {
  46. // Initialize serial communication
  47. Serial.begin(SERIAL_BAUD_RATE);
  48. Serial.println("Initializing...");
  49.  
  50. // Set up the RX pin for serial input
  51. Serial1.begin(SERIAL_BAUD_RATE, SERIAL_8N1, SERIAL_RX_PIN);
  52.  
  53. // Initialize the Bluetooth keyboard
  54. bleKeyboard.begin();
  55. Serial.println("Bluetooth Keyboard Ready.");
  56.  
  57. // Instructions for pairing
  58. Serial.println("To pair: Search for 'ESP32 Keyboard' on your device and connect.");
  59. }
  60.  
  61. void loop() {
  62. // Ensure the keyboard is connected
  63. if (bleKeyboard.isConnected()) {
  64. // Check for serial input
  65. while (Serial1.available()) {
  66. char receivedChar = Serial1.read();
  67.  
  68. inputBuffer += receivedChar; // Append the character to the buffer
  69. bleKeyboard.print(inputBuffer);
  70. inputBuffer = ""; // Clear the buffer after sending
  71.  
  72. }
  73. } else {
  74. Serial.println("Waiting for Bluetooth connection...");
  75. delay(1000);
  76. }
  77. }
  78.  
Advertisement
Add Comment
Please, Sign In to add comment