HI!
I will connect my ESP32C3 to my computer via Bluetooth.
I would like my ESP32C3 to work as a HID and send a A when I connect GPIO 2 and 3.
And I can´t get this to work.
Any ideas?
Cheers
Fredrik
I use this code now:
#include <BleKeyboard.h>
// Create a BLE Keyboard object
BleKeyboard keyboard;
// Define GPIO pins
const int gpioPin1 = 2;
const int gpioPin2 = 3;
void setup() {
// Start serial communication for debugging
Serial.begin(115200);
Serial.println("ESP32 BLE Keyboard!");
// Initialize BLE keyboard
keyboard.begin();
// Set GPIO pins as inputs with pull-up resistors
pinMode(gpioPin1, INPUT_PULLUP); // GPIO 2
pinMode(gpioPin2, INPUT_PULLUP); // GPIO 3
}
void loop() {
// Read the status of GPIO 2 and GPIO 3
int state1 = digitalRead(gpioPin1);
int state2 = digitalRead(gpioPin2);
// If both pins are shorted (i.e., both are LOW)
if (state1 == LOW && state2 == LOW) {
// If the keyboard is not already connected, connect it
if (!keyboard.isConnected()) {
Serial.println("Connecting to computer...");
while (!keyboard.isConnected()) {
delay(100);
}
Serial.println("Connected!");
}
// Send "A" when GPIO 2 and GPIO 3 are shorted
Serial.println("Shorted! Sending 'A'...");
keyboard.write('A'); // Sends the letter 'A'
delay(1000); // Wait a bit to prevent sending multiple 'A's too quickly
}
delay(100); // Small delay to avoid high CPU load
}