Boron UART communication over ble.. example

Hi all.

Im new user of Particle device. I have custom nrf52840 board (device A), it has ultrasonic sensor and accelerometer. I programmed It using IAR and SKD 16.0, softdevice 7.0

My idea is connect the Boron (like Gateway) to device A (over the BLE). Now I can connect the boron to Device A (by ble), but I’m stuck how to send over the ble a command to device A from Boron (for example “052\s” ) (in SDK Nordic I used NUS… Nordic Uart Service)

When Device A received this command, it capture the sound by SAADC, it sends by uart (NUS) the FFT (sound in frequency domain) to the boron.

Could you orient me where can I find the example to send data UART by BLE?

thanks
Ricardo

Hi Ricardo, thanks for your question, and welcome to the Particle community!

For BLE examples, I would check our docs here. There’s a lot there, including UART central and peripheral examples you should be able to adapt for your case.

Good luck!

Hi Brandon
Thanks for your suggestion…
I have read and check the examples. but all time I have the same behavior…

I have done program to connect the Boron 1.5.0 (as central) to my device (ble_peripheral), I have the following behavior.
When the Boron is Connected to net (3G), and find the Specific BLE MAC address. it turn off the ble.scan. and it try to connect to MAC address found.
In this moment my device Turn on the led (when some device is connected with it)
But the boron all time the function result is not connected to the device.( peer.connected() and BLE.connected())

Finally after 10 second the boron turn his led red, and restart.

My device use the Nordic uuid Uart I check it With my cellphone, with app nRF Connect.
I tested the nrf52840 DK using the peripheral example BLE_APP_UART of SKD 16, I have got the same behavior.

could you check if my program is OK?

another problem, when both devices (custom device and Boron) are connected I unplugged the battery of custom device. How can I manage the Disconnection suddenly?, because I added the event onConnected and onDisconnected, but the firmware doesn’t execute this part of program…

I share the program!!!

#include "Particle.h"

//SYSTEM_MODE(MANUAL);

//const uint8_t     BLE_PEER_ADDRESS[] = {0x2c, 0x80, 0xa9, 0x4c, 0x01, 0xc9};    // MAC address OF CUSTOM DEVICE

const uint8_t     BLE_PEER_ADDRESS[] = {0x58, 0x2e, 0xd8, 0x4e, 0xb8, 0xf4};    // MAC address of NRF52840 DK USING NORDIK_UART example.


const BleAddress  bleAddress(BLE_PEER_ADDRESS);

SerialLogHandler logHandler(LOG_LEVEL_TRACE);

BlePeerDevice peer;

const size_t UART_TX_BUF_SIZE   = 150;

bool waiting_tramsmision        = false;
bool Try_to_connect             = false;
bool Ble_scan                   = true;
bool Address_Valid              = false;
int  loop_counter;

bool connected();

uint32_t loopDelay = 1000;                                                      // cadence for void loop()

void scanResultCallback(const BleScanResult *scanResult, void *context);

void onDataReceived(const uint8_t* data, size_t len, const BlePeerDevice& peer, void* context);

// These UUIDs were defined by Nordic Semiconductor and are now the defacto standard for
// UART-like services over BLE. Many apps support the UUIDs now, like the Adafruit Bluefruit app.
const BleUuid serviceUuid("6E400001-B5A3-F393-E0A9-E50E24DCCA9E");
const BleUuid rxUuid("6E400002-B5A3-F393-E0A9-E50E24DCCA9E");
const BleUuid txUuid("6E400003-B5A3-F393-E0A9-E50E24DCCA9E");

BleCharacteristic txCharacteristic("tx", BleCharacteristicProperty::NOTIFY, txUuid, serviceUuid);
BleCharacteristic rxCharacteristic("rx", BleCharacteristicProperty::WRITE_WO_RSP, rxUuid, serviceUuid, onDataReceived, NULL);

void onDataReceived(const uint8_t* data, size_t len, const BlePeerDevice& peer, void* context) {
    // Log.trace("Received data from: %02X:%02X:%02X:%02X:%02X:%02X:", peer.address()[0], peer.address()[1], peer.address()[2], peer.address()[3], peer.address()[4], peer.address()[5]);
    Log.info("Reciving Data !!!!");
    for (size_t ii = 0; ii < len; ii++)
        {
        Log.info("%02X,",data[ii]);
        }
    waiting_tramsmision = false;
}

void setup() {
    (void)logHandler; // Does nothing, just to eliminate warning for unused variable

      BLE.onDisconnected(onDisconnected, NULL);
      BLE.onConnected(onConnected, NULL);
}

void loop() {
    
    static uint32_t ms = 0;
    if (millis() - ms < loopDelay) return;
    ms = millis();

    loop_counter++;
    
        Log.info("quantity of  loop cycle : %d", loop_counter);
        Log.info("Peer connected ?  %x", peer.connected());
        Log.info("Ble connected ?   %x",BLE.connected());
        Log.info("Try to connect    %X ",Try_to_connect);
        Log.info("ble scan          %x",Ble_scan);
        Log.info("Value Peer        %x",peer);
        
    if (!BLE.connected() && Ble_scan)
    {
        Log.info("starting scan");
        int count = BLE.scan(scanResultCallback, NULL);
        BLE.stopScanning();
        if (count > 0) {
            Log.info("%d devices found", count);
        }
    }
    else
    {
    Log.info("Connecting to Target !!!!");
    if (!waiting_tramsmision)
    {
        Log.info("Device address: %02X:%02X:%02X:%02X:%02X:%02X",
        peer.address()[0], peer.address()[1], peer.address()[2],
        peer.address()[3], peer.address()[4], peer.address()[5]);
        uint8_t rxBuf[UART_TX_BUF_SIZE];
        size_t rxLen = 0;
        rxBuf[0]=0x07;
        rxBuf[1]=0x01;
        rxBuf[2]='\n';
        //txBuf[3]=0x0a;
        rxLen = 3;
        txCharacteristic.setValue(rxBuf, rxLen);
        waiting_tramsmision=true;
    }

    }
    if (Address_Valid)
    {

    }
    
}

void scanResultCallback(const BleScanResult *scanResult, void *context) {
    Log.info("MAC: %02X:%02X:%02X:%02X:%02X:%02X | RSSI: %dBm",
            scanResult->address[0], scanResult->address[1], scanResult->address[2],
            scanResult->address[3], scanResult->address[4], scanResult->address[5], scanResult->rssi);

    String name = scanResult->advertisingData.deviceName();
    if (name.length() > 0) {
        Log.info("deviceName: %s", name.c_str());
        
    }
     if (BLE_PEER_ADDRESS[0]==scanResult->address[0] && BLE_PEER_ADDRESS[1]==scanResult->address[1] && BLE_PEER_ADDRESS[2]==scanResult->address[2]
        && BLE_PEER_ADDRESS[3]==scanResult->address[3]&& BLE_PEER_ADDRESS[4]==scanResult->address[4]&& BLE_PEER_ADDRESS[5]==scanResult->address[5])
        {
            Log.info("MAC found.. manage to connect to it !!!!");
            BLE.stopScanning();
            Ble_scan=false;
            Address_Valid=true;
            Log.info("turn Off ble.scan");
            Try_to_connect = true;
            peer = BLE.connect(scanResult->address);
            Log.info("result of peer %x", peer);
            Log.info("result of peer.connect %x", peer.connected());
            if (peer.connected())
                {
                Log.info("connectec to device !!!!");
                if (!waiting_tramsmision)
                {
                Log.info("Device address : %02X:%02X:%02X:%02X:%02X:%02X",
                peer.address()[0], peer.address()[1], peer.address()[2],
                peer.address()[3], peer.address()[4], peer.address()[5]);
                uint8_t txBuf[UART_TX_BUF_SIZE];
                size_t txLen = 0;
                txBuf[0]=0x07;
                txBuf[1]=0x01;
                txBuf[2]='\n';
                //txBuf[3]=0x0a;
                txLen = 3;
                txCharacteristic.setValue(txBuf, txLen);
                waiting_tramsmision=true;
                }
        }
    }
}

void onConnected(const BlePeerDevice& peer, void* context) {
    //bleState = SEND_DATA;
    Log.info("connected to %s", (const char*)peer.address().toString());
  }


void onDisconnected(const BlePeerDevice& peer, void* context) {
    Log.info("have been disconnected from %s", (const char*)peer.address().toString());
    //bleState = DISCONNECT;
    BLE.disconnectAll();
    waiting_tramsmision        = false;
    Try_to_connect             = false;
    Ble_scan                   = true;
    Address_Valid              = false;
}

here I attach the CLI serial monitor… (I erased the cellular log info)

0000102712 [app] INFO: quantity of loop cycle : 1
0000102712 [app] INFO: Peer connected ? 0
0000102712 [app] INFO: Ble connected ? 0
0000102712 [app] INFO: Try to connect 0
0000102713 [app] INFO: ble scan 1
0000102713 [app] INFO: Value Peer 2003e314
0000102713 [app] INFO: starting scan
0000102756 [app] INFO: MAC: 50:78:6B:F0:6E:3C | RSSI: -50Bm
0000102847 [app] INFO: MAC: 58:2E:D8:4E:B8:F4 | RSSI: -23Bm
0000102848 [app] INFO: deviceName: Nordic_UART
0000102848 [app] INFO: MAC found… manage to connect to it !!!
0000102848 [app] INFO: turn Off ble.scan
0000102849 [app] INFO: 1 devices found
0000103913 [app] INFO: quantity of loop cycle : 2
0000103914 [app] INFO: Peer connected ? 0 !!! HERE my CUSTOM DEVICE show is connected to BORON (turn on the led connection)
0000103914 [app] INFO: Ble connected ? 0
0000103914 [app] INFO: Try to connect 1
0000103915 [app] INFO: ble scan 0
0000103915 [app] INFO: Value Peer 2003e314
0000103915 [app] INFO: Connecting to Target !!!
0000103916 [app] INFO: Device address: 00:00:00:00:00:00
0000104913 [app] INFO: quantity of loop cycle : 3
0000104913 [app] INFO: Peer connected ? 0
0000104913 [app] INFO: Ble connected ? 0
0000104913 [app] INFO: Try to connect 1
0000104914 [app] INFO: ble scan 0
0000104914 [app] INFO: Value Peer 2003e314
0000104914 [app] INFO: Connecting to Target !!!
0000105913 [app] INFO: quantity of loop cycle : 4
0000105913 [app] INFO: Peer connected ? 0
0000105913 [app] INFO: Ble connected ? 0
0000105914 [app] INFO: Try to connect 1
0000105914 [app] INFO: ble scan 0
0000105914 [app] INFO: Value Peer 2003e314
0000105915 [app] INFO: Connecting to Target !!!
0000106913 [app] INFO: quantity of loop cycle : 5

Any suggestion?
Thanks.
Ricardo