Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- First I needed to download and install free Arduino IDE from arduino.cc (Win/Mac/Linux)
- Then I needed to install a BLE keyboard library, I went to https://github.com/T-vK/ESP32-BLE-Keyboard
- And downloaded the entire repository as a ZIP file, then placed the unzipped folder in my "Arduino/libraries" folder
- (this is a documents-level folder that appears after first run, not a "program files" type folder)
- Since I also used an M5Stack-branded microcontroller, I configured M5Stack as "Additional Boards" by
- placing the following URL into Arduino - Preferences - Additional Boards URLs:
- https://m5stack.oss-cn-shenzhen.aliyuncs.com/resource/arduino/package_m5stack_index.json
- Which resulted in "Boards Manager" offering to let me install "M5Stack" as a boards selection. Install.
- Because Arduino IDE uses URL's to learn about the existence of other brands of boards, and install them.
- Then I was able to paste the code below as a new Sketch, and compile it, and flash it to an M5Stack device
- device over USB (specifically, a serial port emulated over USB).
- M5Stack devices I tried:
- CORE -- because I had one handy. But nothing references the LCD screen, so it will be conspicuously off
- AtomS3 -- I think this is perfect because it pairs with "Atom RS232 Base" and gives you proper RS232
- RX/TX voltage.
- Purchase links (both items are under $10)
- AtomS3 --> https://shop.m5stack.com/products/atoms3-lite-esp32s3-dev-kit
- Atom RS232 base --> https://shop.m5stack.com/products/atomic-rs232-base-w-o-atom-lite
- Depending on the device you choose, you may have to adjust SERIAL_RX_PIN to properly reflect the pin
- number that will be physically receiving the data. On the AtomS3 it may be pin 5 not 19 when using
- the base (see the numbers on the bottom of the AtomS3)
- */
- #include <BleKeyboard.h>
- // Instantiate the Bluetooth Keyboard object
- BleKeyboard bleKeyboard("ESP32 Keyboard", "ESP32", 100);
- // Define the serial input pin
- #define SERIAL_RX_PIN 5
- #define SERIAL_BAUD_RATE 9600
- // Buffer for incoming serial data
- String inputBuffer = "";
- void setup() {
- // Initialize serial communication
- Serial.begin(SERIAL_BAUD_RATE);
- Serial.println("Initializing...");
- // Set up the RX pin for serial input
- Serial1.begin(SERIAL_BAUD_RATE, SERIAL_8N1, SERIAL_RX_PIN);
- // Initialize the Bluetooth keyboard
- bleKeyboard.begin();
- Serial.println("Bluetooth Keyboard Ready.");
- // Instructions for pairing
- Serial.println("To pair: Search for 'ESP32 Keyboard' on your device and connect.");
- }
- void loop() {
- // Ensure the keyboard is connected
- if (bleKeyboard.isConnected()) {
- // Check for serial input
- while (Serial1.available()) {
- char receivedChar = Serial1.read();
- inputBuffer += receivedChar; // Append the character to the buffer
- bleKeyboard.print(inputBuffer);
- inputBuffer = ""; // Clear the buffer after sending
- }
- } else {
- Serial.println("Waiting for Bluetooth connection...");
- delay(1000);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment