BT keyboard

Please tell us which board you are using.
For CircuitPython issues, ask in the Adafruit CircuitPython forum.

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
Chopper145
 
Posts: 6
Joined: Tue Jul 31, 2018 8:33 am

BT keyboard

Post by Chopper145 »

Hi

I just bought the adafruit feather 32u4 le.
My intention is to use it as a bluetooth keyboard.
Have I bought the correct item? It should work as the bluefruit ez key with a pairing button and status LED.

How do I need to configure this device without beeing a pro?

Or is there a chance to get a bluefruit ez key module?

I would like to create a footpedal to remote the opensong app. To use it handsfree so to speak.

Thanks for any help

Cheers Roger

User avatar
MrAureliusR
 
Posts: 18
Joined: Sat Apr 26, 2014 8:51 pm

Re: BT keyboard

Post by MrAureliusR »

Assuming your computer supports BLE, then using this as a HID is pretty easy from the looks of it:
https://learn.adafruit.com/adafruit-fea ... idkeyboard

You'll have to do a bit of soldering, etc to hook up a footpedal, but it shouldn't be too hard.

User avatar
Chopper145
 
Posts: 6
Joined: Tue Jul 31, 2018 8:33 am

Re: BT keyboard

Post by Chopper145 »

Thank you for your help.

I am a bit closer to the finish now.
But the most interesting for me is how to adapt the A0-A5 to send a letter.
If I Trigger A1, it should send a "s" for example.
What have i to adapt to use this function in the Keyboard mode?


Another Question:
Can I have the Bluetooth Connection Status LED Extended?
Can I solder a LED to it to have it Lit outside the BOX'

Thanks for Help

User avatar
MrAureliusR
 
Posts: 18
Joined: Sat Apr 26, 2014 8:51 pm

Re: BT keyboard

Post by MrAureliusR »

What makes you think you need to use hidcontrolkey? This works as an HID no problem. It will send whatever keys you like.

To change the example, just modify this section:

Code: Select all

 if (Serial.available())
  {
    char ch = (char) Serial.read();

    // echo
    Serial.write(ch); 

    blehid.keyPress(ch);
    hasKeyPressed = true;
    
    // Delay a bit after a report
    delay(5);
  }
To instead check if any of the logic inputs you want to monitor are pulled low, and then just send whatever character you'd like.
One source I found uses these codes for the arrow keys:
right arrow = 0x07;
left arrow = 0x0B;
up arrow = 0x0E;
down arrow = 0x0C;
enter key = 0x0D;

You can try just sending these codes directly and see if that works. If not then I believe you'd need to send the full HID string for a keypress, which looks like 00-00-code-00-00-00-00. For example, up arrow would look like 00-00-0E-00-00-00-00. The examples I've seen using this are usually sending AT commands directly to the BLE chip, which I believe the Bluefruit library would abstract away. So you probably won't have to mess with the full HID strings unless the Bluefruit library does not support sending arrow keys. I'd be surprised if it didn't support it, so try just sending those key codes, like blehid.keyPress(0x0E);

Good luck!

EDIT: Yes, you can extend the status LED. You would have to desolder the LED, solder a wire to each pad, and then solder that to an external LED.

User avatar
Chopper145
 
Posts: 6
Joined: Tue Jul 31, 2018 8:33 am

Re: BT keyboard

Post by Chopper145 »

Wow. That helps me a lot with the LED. Is the Charge LED a 2 Colour one? OR can I just solder a normal LED to that solder pads?


I got it with the Keyboard.

Now how does it work to Trigger an I/O port to send me a simple letter, for egsampple "s"?

User avatar
MrAureliusR
 
Posts: 18
Joined: Sat Apr 26, 2014 8:51 pm

Re: BT keyboard

Post by MrAureliusR »

Chopper145 wrote:Wow. That helps me a lot with the LED. Is the Charge LED a 2 Colour one? OR can I just solder a normal LED to that solder pads?


I got it with the Keyboard.

Now how does it work to Trigger an I/O port to send me a simple letter, for egsampple "s"?
Well, we know that the function call to send a key press is blehid.keyPress(); The function call to check what state a pin is in is digitalRead(). You have to make sure that pin is set as an input beforehand, though I believe most pins are set as inputs by default on reset. I'd recommend reading through https://www.arduino.cc/en/Tutorial/DigitalPins and https://www.arduino.cc/reference/en/lan ... gitalread/. I don't want to just give you the code because then you won't learn anything ;)

User avatar
Chopper145
 
Posts: 6
Joined: Tue Jul 31, 2018 8:33 am

Re: BT keyboard

Post by Chopper145 »

Okay, I juest read and learned a lot. You must know, I never ever learned programming.
I know that I can Change values or names within all these letters.

If you could hav a look on what I did this Evening:
1st I modified the Setup, with Pin Inputs and Outputs.
2nd I wrote some command lines.

3 Goals:
1: I would Show battery Low voltage with a digital Output Nr. 13. If Woltage Below 3.5V the LED shall be On.
2: See the Connection Status of the BLE Connection with digital Output Nr. 12. If Connection is ok, the LED shall be On
3: I have 4 Keys to Trigger A0-A3, If hit, a key shall be send via BLE to the IPad

Since I hae never learned that Program Language I think I have made a lot of mistakes and I would be happy if you can watch through and help me out here.

/*********************************************************************
This is an example for our nRF51822 based Bluefruit LE modules

Pick one up today in the adafruit shop!

Adafruit invests time and resources providing this open source code,
please support Adafruit and open-source hardware by purchasing
products from Adafruit!

MIT license, check LICENSE for more information
All text above, and the splash screen below must be included in
any redistribution
*********************************************************************/

/*
This example shows how to send HID (keyboard/mouse/etc) data via BLE
Note that not all devices support BLE keyboard! BLE Keyboard != Bluetooth Keyboard
*/

#include <Arduino.h>
#include <SPI.h>
#include "Adafruit_BLE.h"
#include "Adafruit_BluefruitLE_SPI.h"
#include "Adafruit_BluefruitLE_UART.h"

#include "BluefruitConfig.h"

#if SOFTWARE_SERIAL_AVAILABLE
#include <SoftwareSerial.h>
#endif

/*=========================================================================
APPLICATION SETTINGS

    FACTORYRESET_ENABLE    Perform a factory reset when running this sketch
   
    Enabling this will put your Bluefruit LE module
in a 'known good' state and clear any config
data set in previous sketches or projects, so
    running this at least once is a good idea.
   
    When deploying your project, however, you will
want to disable factory reset by setting this
value to 0.  If you are making changes to your
    Bluefruit LE device via AT commands, and those
changes aren't persisting across resets, this
is the reason why.  Factory reset will erase
the non-volatile memory where config data is
stored, setting it back to factory default
values.
       
    Some sketches that require you to bond to a
central device (HID mouse, keyboard, etc.)
won't work at all with this feature enabled
since the factory reset will clear all of the
bonding data stored on the chip, meaning the
central device won't be able to reconnect.
MINIMUM_FIRMWARE_VERSION Minimum firmware version to have some new features
-----------------------------------------------------------------------*/
#define FACTORYRESET_ENABLE 0
#define MINIMUM_FIRMWARE_VERSION "0.6.6"
/*=========================================================================*/


// Create the bluefruit object, either software serial...uncomment these lines
/*
SoftwareSerial bluefruitSS = SoftwareSerial(BLUEFRUIT_SWUART_TXD_PIN, BLUEFRUIT_SWUART_RXD_PIN);

Adafruit_BluefruitLE_UART ble(bluefruitSS, BLUEFRUIT_UART_MODE_PIN,
BLUEFRUIT_UART_CTS_PIN, BLUEFRUIT_UART_RTS_PIN);
*/

/* ...or hardware serial, which does not need the RTS/CTS pins. Uncomment this line */
// Adafruit_BluefruitLE_UART ble(BLUEFRUIT_HWSERIAL_NAME, BLUEFRUIT_UART_MODE_PIN);

/* ...hardware SPI, using SCK/MOSI/MISO hardware SPI pins and then user selected CS/IRQ/RST */
Adafruit_BluefruitLE_SPI ble(BLUEFRUIT_SPI_CS, BLUEFRUIT_SPI_IRQ, BLUEFRUIT_SPI_RST);

/* ...software SPI, using SCK/MOSI/MISO user-defined SPI pins and then user selected CS/IRQ/RST */
//Adafruit_BluefruitLE_SPI ble(BLUEFRUIT_SPI_SCK, BLUEFRUIT_SPI_MISO,
// BLUEFRUIT_SPI_MOSI, BLUEFRUIT_SPI_CS,
// BLUEFRUIT_SPI_IRQ, BLUEFRUIT_SPI_RST);

// A small helper
void error(const __FlashStringHelper*err) {
Serial.println(err);
while (1);
}

/**************************************************************************/
/*!
@brief Sets up the HW an the BLE module (this function is called
automatically on startup)
*/
/**************************************************************************/
void setup(void)
{
while (!Serial); // required for Flora & Micro
delay(500);

Serial.begin(115200);
Serial.println(F("Adafruit Bluefruit HID Keyboard Example"));
Serial.println(F("---------------------------------------"));

/* Initialise the module */
Serial.print(F("Initialising the Bluefruit LE module: "));

if ( !ble.begin(VERBOSE_MODE) )
{
error(F("Couldn't find Bluefruit, make sure it's in CoMmanD mode & check wiring?"));
}
Serial.println( F("OK!") );

if ( FACTORYRESET_ENABLE )
{
/* Perform a factory reset to make sure everything is in a known state */
Serial.println(F("Performing a factory reset: "));
if ( ! ble.factoryReset() ){
error(F("Couldn't factory reset"));
}
}

/* Disable command echo from Bluefruit */
ble.echo(false);

Serial.println("Requesting Bluefruit info:");
/* Print Bluefruit information */
ble.info();

/* Change the device name to make it easier to find */
Serial.println(F("Setting device name to 'freeplay pedal': "));
if (! ble.sendCommandCheckOK(F( "AT+GAPDEVNAME=freeplay pedal" )) ) {
error(F("Could not set device name?"));
}

/* Enable HID Service */
Serial.println(F("Enable HID Service (including Keyboard): "));
if ( ble.isVersionAtLeast(MINIMUM_FIRMWARE_VERSION) )
{
if ( !ble.sendCommandCheckOK(F( "AT+BleHIDEn=On" ))) {
error(F("Could not enable Keyboard"));
}
}else
{
if (! ble.sendCommandCheckOK(F( "AT+BleKeyboardEn=On" ))) {
error(F("Could not enable Keyboard"));
}
}

/* Add or remove service requires a reset */
Serial.println(F("Performing a SW reset (service changes require a reset): "));
if (! ble.reset() ) {
error(F("Couldn't reset??"));
}

Serial.println();
Serial.println(F("Go to your phone's Bluetooth settings to pair your device"));
Serial.println(F("then open an application that accepts keyboard input"));

Serial.println();
Serial.println(F("Enter the character(s) to send:"));
Serial.println(F("- \\r for Enter"));
Serial.println(F("- \\n for newline"));
Serial.println(F("- \\t for tab"));
Serial.println(F("- \\b for backspace"));

Serial.println();
}
/* freeplay */
{
pinMode(A0, INPUT); // Key 1
pinMode(A1, INPUT); // Key 2
pinMode(A2, INPUT); // Key 3
pinMode(A3, INPUT); // Key 4
pinMode(12, OUTPUT); // Blue LED
pinMode(13, OUTPUT); // Green LED
#define VBATPIN A9; // Battery Voltage
}


/**************************************************************************/
/*!
@brief Constantly poll for new command or response data
*/
/**************************************************************************/
void loop(void)
{
// Display prompt
Serial.print(F("keyboard > "));

// Check for user input and echo it back if anything was found
char keys[BUFSIZE+1];
getUserInput(keys, BUFSIZE);

Serial.print("\nSending ");
Serial.println(keys);

ble.print("AT+BleKeyboard=");
ble.println(keys);

if( ble.waitForOK() )
{
Serial.println( F("OK!") );
}else
{
Serial.println( F("FAILED!") );
}
}
/* freeplay */
if (digitalRead(inA0 == HIGH) // Key 1
{
blehid.keyPress(a);
delay(500);
}

if (digitalRead(inA1 == HIGH) // Key 2
{
blehid.keyPress(s);
delay(500);
}

if (digitalRead(inA2 == HIGH) // Key 3
{
blehid.keyPress(d);
delay (500);
}

if (digitalRead(inA3 == HIGH) // Key 4
{
blehid.keyPress(f);
delay(500);
}

{
float measuredvbat = analogRead(VBATPIN);
measuredvbat *= 2; // we divided by 2, so multiply back
measuredvbat *= 3.3; // Multiply by 3.3V, our reference voltage
measuredvbat /= 1024; // convert to voltage
Serial.print("VBat: " ); Serial.println(measuredvbat);
}

if (analogRead(measuredvbat) < 3.5) // LED Green to say Recharge
{
digitalWrite(out13 == High)
}

if (BLE == OK!) // LED Blue to show BLE connected
{
digitalWrite(out12 == High)
}

/**************************************************************************/
/*!
@brief Checks for user input (via the Serial Monitor)
*/
/**************************************************************************/
void getUserInput(char buffer[], uint8_t maxSize)
{
memset(buffer, 0, maxSize);
while( Serial.available() == 0 ) {
delay(1);
}

uint8_t count=0;

do
{
count += Serial.readBytes(buffer+count, maxSize);
delay(2);
} while( (count < maxSize) && !(Serial.available() == 0) );
}

Thank you.

User avatar
MrAureliusR
 
Posts: 18
Joined: Sat Apr 26, 2014 8:51 pm

Re: BT keyboard

Post by MrAureliusR »

Okay, there's a few mistakes you've made. You've added opening braces { where they aren't needed. Those are only needed to wrap functions, if statements, while and for loops, and any other multi-line statement.

You haven't declared inA0, inA1 as a variable anywhere so the compiler will complain about that. I think you should really take the time to learn the basics. Check out this video series, it will explain a lot:
https://www.youtube.com/watch?v=d8_xXNcGYgo
If you already understand a part of the video, skip ahead a little bit. Get to around 8:20, that is where he starts doing the program.

User avatar
Chopper145
 
Posts: 6
Joined: Tue Jul 31, 2018 8:33 am

Re: BT keyboard

Post by Chopper145 »

Thank you. That helped me a lot.

I figured out with the ble.print function that if i type anything in the Serial Monitor, it always will type letter "a". part of a succsess.
Then I deleted this: ble.print("AT+BleKeyboard=");
So I hoped to be able to get it to run. but it doesnt yet.
I also figured out, that I have to have a pulldown resistor to ground on my Inputs because otherwise it wont work. I did a test with an LED Output instead of bleprint. And so I was able to see that the LED was lit all the time and if I pressed the button it was lit more. So a Pulldown resistor is needed.


this is what I did this day. Can you have a look at this? I am not sure if it will work. I now will go and have a resistro soldered before the key...

Code: Select all

// A small helper
void error(const __FlashStringHelper*err) {
  Serial.println(err);
  while (1);
}

int key1Pin = A0;
int key2Pin = A1;
int key3Pin = A2;
int key4Pin = A3;
int blueledPin = 2;
int greenledPin = 3;
int blehidkeyPress = true;
int wait = 500;
int val=0;
/**************************************************************************/
/*!
    @brief  Sets up the HW an the BLE module (this function is called
            automatically on startup)
*/
/**************************************************************************/
void setup(void)
{
  while (!Serial);  // required for Flora & Micro
  delay(500);

  Serial.begin(115200);
  Serial.println(F("Adafruit Bluefruit HID Keyboard Example"));
  Serial.println(F("---------------------------------------"));

  /* Initialise the module */
  Serial.print(F("Initialising the Bluefruit LE module: "));

  if ( !ble.begin(VERBOSE_MODE) )
  {
    error(F("Couldn't find Bluefruit, make sure it's in CoMmanD mode & check wiring?"));
  }
  Serial.println( F("OK!") );

  if ( FACTORYRESET_ENABLE )
  {
    /* Perform a factory reset to make sure everything is in a known state */
    Serial.println(F("Performing a factory reset: "));
    if ( ! ble.factoryReset() ) {
      error(F("Couldn't factory reset"));
    }
  }

  /* Disable command echo from Bluefruit */
  ble.echo(false);

  Serial.println("Requesting Bluefruit info:");
  /* Print Bluefruit information */
  ble.info();

  /* Change the device name to make it easier to find */
  Serial.println(F("Setting device name to 'freeplay pedal': "));
  if (! ble.sendCommandCheckOK(F( "AT+GAPDEVNAME=freeplay pedal" )) ) {
    error(F("Could not set device name?"));
  }

  /* Enable HID Service */
  Serial.println(F("Enable HID Service (including Keyboard): "));
  if ( ble.isVersionAtLeast(MINIMUM_FIRMWARE_VERSION) )
  {
    if ( !ble.sendCommandCheckOK(F( "AT+BleHIDEn=On" ))) {
      error(F("Could not enable Keyboard"));
    }
  } else
  {
    if (! ble.sendCommandCheckOK(F( "AT+BleKeyboardEn=On"  ))) {
      error(F("Could not enable Keyboard"));
    }
  }

  /* Add or remove service requires a reset */
  Serial.println(F("Performing a SW reset (service changes require a reset): "));
  if (! ble.reset() ) {
    error(F("Couldn't reset??"));
  }

  Serial.println();
  Serial.println(F("Go to your phone's Bluetooth settings to pair your device"));
  Serial.println(F("then open an application that accepts keyboard input"));

  Serial.println();
  Serial.println(F("Enter the character(s) to send:"));
  Serial.println(F("- \\r for Enter"));
  Serial.println(F("- \\n for newline"));
  Serial.println(F("- \\t for tab"));
  Serial.println(F("- \\b for backspace"));

  Serial.println();

  /* freeplay */

  pinMode(key1Pin, INPUT);       // Key 1
  pinMode(key2Pin, INPUT);       // Key 2
  pinMode(key3Pin, INPUT);       // Key 3
  pinMode(key4Pin, INPUT);       // Key 4
  pinMode(blueledPin, OUTPUT);   // Blue LED
  pinMode(greenledPin, OUTPUT);  // Green LED
#define VBATPIN A9;            // Battery Voltage

}



/**************************************************************************/
/*!
    @brief  Constantly poll for new command or response data
*/
/**************************************************************************/
void loop(void)
{
  // Display prompt
  Serial.print(F("keyboard > "));

  // Check for user input and echo it back if anything was found
  char keys[BUFSIZE + 1];
  getUserInput(keys, BUFSIZE);


  if ( ble.waitForOK() )
  {
    Serial.println( F("OK!") );
  } else
  {
    Serial.println( F("FAILED!") );
  }

  /* freeplay */
  {
  val=digitalRead(key1Pin);    // Key 1
  if (val != 0) {
  ble.println("a");
  }
  delay(wait);
  }
}


User avatar
Chopper145
 
Posts: 6
Joined: Tue Jul 31, 2018 8:33 am

Re: BT keyboard

Post by Chopper145 »

I have another question.

I just googled over and over since the last pot...

How can I copy the built in Bluetooth Status LED?
What Kind of words do I Need herefore?

I tried some different ways but with no Idea and no clue it is quite difficult.

How can I see where the ble is active and how can I add some simple "if ble=connected, blueled=HIGH" ??

Thanks for Help

Locked
Please be positive and constructive with your questions and comments.

Return to “Feather - Adafruit's lightweight platform”