Go Down

Topic: Arduino nan BLE 33 - how to send data (Read 587 times) previous topic - next topic

gregIrl

Hi,
I am new to BLE, trying to connect to Android to send some data. Unfortunately I cannot find any information how to send more than 1 byte(eg battery level) and what characteristic should I use to maximize MTU. My nano successfully reports battery level to LightBlue app. The GATT manual is not very good - I cannot find the details how to us it and maximize MTU. Please help.

Klaus_K

In BLE from an application point of view you do not send data. You fill a type of table called GATT and someone who wants the data can read it. It is like a server that has data available and if someone wants it, they can read it.

What is the nature of your more than one-byte data? Is it the same thing but changing over time or are there different data types in your application?

Check the code I have written in the following post reply #11

https://forum.arduino.cc/index.php?topic=659562.0

It has multiple characteristics and it has data changing over time. You can use a BLE Scanner app to look at the data changing every 20ms.

gregIrl

Thanks Klaus_K a lot. I made some progress thanks to your sample code.
In BLE from an application point of view you do not send data. You fill a type of table called GATT and someone who wants the data can read it. It is like a server that has data available and if someone wants it, they can read it.
What is the nature of your more than one-byte data? Is it the same thing but changing over time or are there different data types in your application?
Sending chunk of data make live easier. I need to send time stamp, and 4 analog values 20 bytes meet my requirements.
Check the code I have written in the following post reply #11

https://forum.arduino.cc/index.php?topic=659562.0

It has multiple characteristics and it has data changing over time. You can use a BLE Scanner app to look at the data changing every 20ms.
I found the way to configure MTU to 20 bytes and string. See modified Klaus_K code wit string and 20 bytes MTU added.


Code: [Select]


/*
  This example creates a BLE peripheral with a service that contains a
  couple of characteristics to test BLE connection.
  The yellow LED shows the BLE module is initialized.
  The green LED shows RSSI of zero. The more it blinks the worse the connection.

  The circuit:
  - Arduino Nano 33 BLE Sense board.

  You can use a generic BLE central app, like LightBlue (iOS and Android) or
  nRF Connect (Android), to interact with the services and characteristics
  created in this sketch.

  This example code is in the public domain.
*/

#include <ArduinoBLE.h>
//#include <Arduino_LSM9DS1.h>


//----------------------------------------------------------------------------------------------------------------------
// BLE UUIDs
//----------------------------------------------------------------------------------------------------------------------

#define BLE_UUID_TEST_SERVICE               "9A48ECBA-2E92-082F-C079-9E75AAE428B1"
#define BLE_UUID_ACCELERATION               "2713"
#define BLE_UUID_COUNTER                    "1A3AC130-31EE-758A-BC50-54A61958EF81"
#define BLE_UUID_STRING                    "1A3AC131-31EF-758B-BC51-54A61958EF82"
#define BLE_UUID_BUFFER                    "1A3AC132-31ED-758C-BC52-54A61958EF82"
#define BLE_UUID_RESET_COUNTER              "FE4E19FF-B132-0099-5E94-3FFB2CF07940"

//----------------------------------------------------------------------------------------------------------------------
// BLE
//----------------------------------------------------------------------------------------------------------------------



BLEService testService( BLE_UUID_TEST_SERVICE );
BLEFloatCharacteristic accelerationCharacteristic( BLE_UUID_ACCELERATION, BLERead | BLENotify );
BLEUnsignedLongCharacteristic counterCharacteristic( BLE_UUID_COUNTER, BLERead | BLENotify );
BLEBoolCharacteristic resetCounterCharacteristic( BLE_UUID_RESET_COUNTER, BLEWriteWithoutResponse );
BLECharacteristic stringCharacteristic( BLE_UUID_STRING, BLERead | BLENotify,"TEST1" );
BLECharacteristic buffCharacteristic( BLE_UUID_BUFFER, BLERead | BLENotify,20,(1==1) );


const int BLE_LED_PIN = LED_BUILTIN;
const int RSSI_LED_PIN = LED_PWR;
char buf[20];

void setup()
{
  Serial.begin( 9600 );
  //  while ( !Serial );

  pinMode( BLE_LED_PIN, OUTPUT );
  pinMode( RSSI_LED_PIN, OUTPUT );
  
  
  Serial.print( "Accelerometer sample rate = " );
  Serial.print( 10 );
  Serial.println( " Hz" );

  if( setupBleMode() )
  {
    digitalWrite( BLE_LED_PIN, HIGH );
  }
} // setup


void loop()
{
  static unsigned long counter = 0;
  static long previousMillis = 0;
  uint8_t x;
  String aS ="TEst1String";
  
    float accelerationX =0, accelerationY, accelerationZ;
  // listen for BLE peripherals to connect:
  BLEDevice central = BLE.central();

  if ( central )
  {
    Serial.print( "Connected to central: " );
    Serial.println( central.address() );

    while ( central.connected() )
    {
      if( resetCounterCharacteristic.written() )
      {
        counter = 0;
        Serial.println( "Reset" );
        //Serial.println(resetCounterCharacteristic.value()  );
        stringCharacteristic.writeValue("test2222");
        aS.toCharArray(buf,20);
        buffCharacteristic.writeValue( buf,20 );

      }

      long interval = 5;
      unsigned long currentMillis = millis();
      if( currentMillis - previousMillis > interval )
      {
        previousMillis = currentMillis;
        if(x++==0){
          Serial.print( "Central RSSI: " );
          Serial.println( central.rssi() );
        }

        if( central.rssi() != 0 )
        {
          digitalWrite( RSSI_LED_PIN, LOW );
        
          //if ( IMU.accelerationAvailable() )
          {
            accelerationX +=0.1;
            //IMU.readAcceleration( accelerationX, accelerationY, accelerationZ );
            accelerationCharacteristic.writeValue( accelerationX );
          }

          
          counterCharacteristic.writeValue( counter++ );
          counter+=0x1000;
        }
        else
        {
          digitalWrite( RSSI_LED_PIN, HIGH );
        }
        //counter++;
      } // intervall
    } // while connected

    Serial.print( F( "Disconnected from central: " ) );
    Serial.println( central.address() );
  } // if central
} // loop



bool setupBleMode()
{
  //String aS ="1String";
  uint8_t i=0;
  if ( !BLE.begin() )
  {
    return false;
  }

  // set advertised local name and service UUID:
  BLE.setDeviceName( "Arduino Nano 33 BLE" );
  BLE.setLocalName( "Arduino Nano 33 BLE" );
  BLE.setAdvertisedService( testService );

  // BLE add characteristics
  testService.addCharacteristic( accelerationCharacteristic );
  testService.addCharacteristic( counterCharacteristic );
  testService.addCharacteristic( resetCounterCharacteristic );
  testService.addCharacteristic( stringCharacteristic );
  testService.addCharacteristic( buffCharacteristic );



  // add service
  BLE.addService( testService );
  for(i=0;i<20;i++) buf[i]=i+1;
  // set the initial value for the characeristic:
  accelerationCharacteristic.writeValue( 0.0 );
  counterCharacteristic.writeValue( 0 );
  buf[10]=0;
  buffCharacteristic.writeValue( buf,20 );
  //buffCharacteristic.
  // start advertising
  BLE.advertise();

  return true;
}


Go Up