Bluefruit Serial Port

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Post Reply
User avatar
Kyle_Steindler
 
Posts: 5
Joined: Tue Jul 05, 2022 2:02 pm

Bluefruit Serial Port

Post by Kyle_Steindler »

Hi all!

I recently purchased a Circuit Playground Bluefruit to incorporate into a project I'm working on. I need the CPB to send sensor data over bluetooth to my MacBook Pro which is going to be interpreted by Max/MSP. I first wanted to get just the bluetooth working before adding any other code. I've been using the Arduino IDE for programming and connecting to the device through Bluefruit Connect and have been powering it with an external battery pack.

The issue is that although I can connect it to the Bluefruit Connect app with what seems like no issue I can't find the serial port it should be connecting to in the Terminal. To import the data into Max I have to be able to view its serial port connection. I've tried several example sketches (I'm providing one here) and none of them display a serial port when I use the command ls -l /dev/tty.*

Any help would be greatly appreciated!

Code: Select all

/*********************************************************************
 This is an example for our nRF52 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
*********************************************************************/
#include <bluefruit.h>
#include <Adafruit_LittleFS.h>
#include <InternalFileSystem.h>

// BLE Service
BLEDfu bledfu;    // OTA DFU service
BLEDis bledis;    // device information
BLEUart bleuart;  // uart over ble
BLEBas blebas;    // battery

void setup() {
  Serial.begin(115200);

#if CFG_DEBUG
  // Blocking wait for connection when debug mode is enabled via IDE
  while (!Serial) yield();
#endif

  Serial.println("Bluefruit52 BLEUART Example");
  Serial.println("---------------------------\n");

  // Setup the BLE LED to be enabled on CONNECT
  // Note: This is actually the default behavior, but provided
  // here in case you want to control this LED manually via PIN 19
  Bluefruit.autoConnLed(true);

  // Config the peripheral connection with maximum bandwidth
  // more SRAM required by SoftDevice
  // Note: All config***() function must be called before begin()
  Bluefruit.configPrphBandwidth(BANDWIDTH_MAX);

  Bluefruit.begin();
  Bluefruit.setTxPower(4);  // Check bluefruit.h for supported values
  //Bluefruit.setName(getMcuUniqueID()); // useful testing with multiple central connections
  Bluefruit.Periph.setConnectCallback(connect_callback);
  Bluefruit.Periph.setDisconnectCallback(disconnect_callback);

  // To be consistent OTA DFU should be added first if it exists
  bledfu.begin();

  // Configure and Start Device Information Service
  bledis.setManufacturer("Adafruit Industries");
  bledis.setModel("Bluefruit Feather52");
  bledis.begin();

  // Configure and Start BLE Uart Service
  bleuart.begin();

  // Start BLE Battery Service
  blebas.begin();
  blebas.write(100);

  // Set up and start advertising
  startAdv();

  Serial.println("Please use Adafruit's Bluefruit LE app to connect in UART mode");
  Serial.println("Once connected, enter character(s) that you wish to send");
}

void startAdv(void) {
  // Advertising packet
  Bluefruit.Advertising.addFlags(BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE);
  Bluefruit.Advertising.addTxPower();

  // Include bleuart 128-bit uuid
  Bluefruit.Advertising.addService(bleuart);

  // Secondary Scan Response packet (optional)
  // Since there is no room for 'Name' in Advertising packet
  Bluefruit.ScanResponse.addName();

  /* Start Advertising
   * - Enable auto advertising if disconnected
   * - Interval:  fast mode = 20 ms, slow mode = 152.5 ms
   * - Timeout for fast mode is 30 seconds
   * - Start(timeout) with timeout = 0 will advertise forever (until connected)
   * 
   * For recommended advertising interval
   * https://developer.apple.com/library/content/qa/qa1931/_index.html   
   */
  Bluefruit.Advertising.restartOnDisconnect(true);
  Bluefruit.Advertising.setInterval(32, 244);  // in unit of 0.625 ms
  Bluefruit.Advertising.setFastTimeout(30);    // number of seconds in fast mode
  Bluefruit.Advertising.start(0);              // 0 = Don't stop advertising after n seconds
}

void loop() {
  // Forward data from HW Serial to BLEUART
  while (Serial.available()) {
    // Delay to wait for enough input, since we have a limited transmission buffer
    delay(2);

    uint8_t buf[64];
    int count = Serial.readBytes(buf, sizeof(buf));
    bleuart.write(buf, count);
  }

  // Forward from BLEUART to HW Serial
  while (bleuart.available()) {
    uint8_t ch;
    ch = (uint8_t)bleuart.read();
    Serial.write(ch);
  }
}

// callback invoked when central connects
void connect_callback(uint16_t conn_handle) {
  // Get the reference to current connection
  BLEConnection* connection = Bluefruit.Connection(conn_handle);

  char central_name[32] = { 0 };
  connection->getPeerName(central_name, sizeof(central_name));

  Serial.print("Connected to ");
  Serial.println(central_name);
}

/**
 * Callback invoked when a connection is dropped
 * @param conn_handle connection where this event happens
 * @param reason is a BLE_HCI_STATUS_CODE which can be found in ble_hci.h
 */
void disconnect_callback(uint16_t conn_handle, uint8_t reason) {
  (void)conn_handle;
  (void)reason;

  Serial.println();
  Serial.print("Disconnected, reason = 0x");
  Serial.println(reason, HEX);
}

User avatar
adafruit_support_mike
 
Posts: 65021
Joined: Thu Feb 11, 2010 2:51 pm

Re: Bluefruit Serial Port

Post by adafruit_support_mike »

BLE doesn't create a Serial port the way Bluetooth Classic does.

'Bluetooth' is a blanket term for a collection of mostly-incompatible protocols managed by the same industry working group (which really likes confusingly-similar names).

The protocol now known as Bluetooth Classic is tightly constrained, which makes life easy for OS developers. With a copy of the spec, they can write a set of libraries that support every BT-C device made, and integrate them into the operating system like other devices.

Bluetooth Low Energy, formerly known as Wibree, doesn't have those constraints. It sets some general rules about how to arrange data, but leaves third-party developers free to build their own protocols on top of that. That makes it impossible for OS developers to integrate BLE the same way as BT-C. Instead, operating systems provide basic support for communicating with the BLE radio, and leave the work of locating, connecting to, and interacting with BLE devices to programs like our Bluefruit Connect.

A few standards have been created for things like BLE keyboards, mice, and game controllers, but there isn't one for a generic Serial connection.

User avatar
Kyle_Steindler
 
Posts: 5
Joined: Tue Jul 05, 2022 2:02 pm

Re: Bluefruit Serial Port

Post by Kyle_Steindler »

"Instead, operating systems provide basic support for communicating with the BLE radio, and leave the work of locating, connecting to, and interacting with BLE devices to programs like our Bluefruit Connect."

Thanks Mike! I figured that was the case, but I couldn't easily find an answer that I understood.

Since I can communicate over BLE between two Bluefruit's would it be possible to set up one to act as a central and the other as a peripheral? One could be connected to my MacBook through a USB cable and the the other sending data that would be intercepted by the USB Bluefruit and then printed into a serial port? Information would flow like this:

Bluefruit (no USB) -> Bluefruit (USB cable) -> Macbook Serial Port

Would something like that work or do I completely misunderstand how this functions? Thanks for all of your help!

User avatar
adafruit_support_mike
 
Posts: 65021
Joined: Thu Feb 11, 2010 2:51 pm

Re: Bluefruit Serial Port

Post by adafruit_support_mike »

Kyle_Steindler wrote:Bluefruit (no USB) -> Bluefruit (USB cable) -> Macbook Serial Port
That should work, yes. The board with a USB connection to the Mac would act as a BLE to USB-Serial adapter.

We generally advise against using BLE for communication between microcontrollers because all flavors of Bluetooth are one-sided protocols.. the central initiates and controls all data connections, while the peripherals just wait for a central to tell them what to do. A peripheral can't push messages to the central, which people want to do 90% of the time.

Your application uses Bluetooth in a way consistent with its design: bascially a wireless replacement for a USB cable. You can use either board as the central, and it will be worth testing both options to see which one you find most convenient.

User avatar
Kyle_Steindler
 
Posts: 5
Joined: Tue Jul 05, 2022 2:02 pm

Re: Bluefruit Serial Port

Post by Kyle_Steindler »

Thanks Mike! This has all been super helpful! I truly appreciate your responses!

Post Reply
Please be positive and constructive with your questions and comments.

Return to “Wireless: WiFi and Bluetooth”