Bonjour
malgres plusieurs heures de recherche , j echoue
Voila mon projet : afin de commander a distance un pc windows 10 par bluetooth , je desire realiser un mini clavier composé pour le moment de 3 lettres A, M et N
Le but est de piloter un programme qui tournera dessus
Pour cela , j ai en stock un esp 32 c3 et un esp32 D1 mini , au choix ( elles vienne d aliexp )
Etant novice en programmation je me suis retrouner vers chatgpt , mais malgres plusieurs essais , ajout de bibliotheques , j ai toujours un erreur , attention les numero de pin n ont pas encore ete definis correctement suivant la carte
je selection en type de carte esp32 wroom DA module, et j ai besoin que mon pc le reconnaisse bien comme un clavier
voici le code
#include <BLEDevice.h>
#include <BLEServer.h>
#include <BLEUtils.h>
#include <BLEHIDDevice.h>
#include <HIDTypes.h> // Ceci est essentiel pour l'utilisation de HID avec BLE
// Définition des broches des boutons
#define Sw1 19 // touche M
#define Sw2 18 // touche N
#define Sw3 14 // touche A
// Déclaration des variables globales
BLEHIDDevice* hid;
BLECharacteristic* input;
BLEAdvertising* pAdvertising;
bool lastStateM = HIGH;
bool lastStateN = HIGH;
bool lastStateA = HIGH;
void setup() {
// Initialisation des broches des boutons comme entrées
pinMode(Sw1, INPUT_PULLUP); // Bouton M
pinMode(Sw2, INPUT_PULLUP); // Bouton N
pinMode(Sw3, INPUT_PULLUP); // Bouton A
// Initialisation du Bluetooth
BLEDevice::init("ESP32 Keyboard");
BLEServer* pServer = BLEDevice::createServer();
// Création de l'objet HID (clavier)
hid = new BLEHIDDevice(pServer);
input = hid->inputReport(1); // Clavier HID
// Configuration du périphérique HID
hid->manufacturer()->setValue("ESP32 HID");
hid->pnp(0x02, 0x1234, 0x5678, 0x0210);
hid->hidInfo(0x00, 0x01);
hid->startServices();
// Démarrer la publicité pour Bluetooth
pAdvertising = BLEDevice::getAdvertising();
pAdvertising->addServiceUUID(hid->hidService()->getUUID());
pAdvertising->setScanResponse(true);
pAdvertising->start();
Serial.begin(115200);
Serial.println("ESP32 prêt et en mode clavier Bluetooth.");
}
void loop() {
// Vérifie l'état des boutons et envoie la touche correspondante
handleButton(Sw1, lastStateM, 0x10); // Touche M
handleButton(Sw2, lastStateN, 0x11); // Touche N
handleButton(Sw3, lastStateA, 0x04); // Touche A
delay(5); // Petit délai pour éviter les rebonds des boutons
}
void handleButton(int buttonPin, bool &lastState, uint8_t key) {
bool currentState = digitalRead(buttonPin);
// Détecte un changement d'état (appui sur le bouton)
if (lastState == HIGH && currentState == LOW) {
Serial.print("Appui sur le bouton, envoi de la touche : ");
Serial.println(key, HEX);
sendKey(key); // Envoie la touche
}
lastState = currentState; // Met à jour l'état précédent
}
void sendKey(uint8_t key) {
uint8_t report[8] = {0}; // Rapport HID (8 octets)
report[2] = key; // Code de la touche
input->setValue(report, sizeof(report));
input->notify(); // Envoie la touche au PC
delay(10); // Attente avant de relâcher la touche
// Relâche la touche
memset(report, 0, sizeof(report));
input->setValue(report, sizeof(report));
input->notify();
}