So after alot of reading and reserch and even more help on the Arduino forums I manged to send data from the arduino and read it on BLE Scanner using this code:
#include <ArduinoBLE.h>
//----------------------------------------------------------------------------------------------------------------------
// BLE UUIDs
//----------------------------------------------------------------------------------------------------------------------
#define BLE_UUID_SENSOR_SERVICE "82D20000-87F8-C79D-9E63-A091F4171879"
#define BLE_UUID_PRESSURE "82D20001-87F8-C79D-9E63-A091F4171879"
//----------------------------------------------------------------------------------------------------------------------
// BLE
//----------------------------------------------------------------------------------------------------------------------
#define BLE_DEVICE_NAME "Arduino Nano 33 IoT"
#define BLE_LOCAL_NAME "Arduino Nano 33 IoT"
BLEService sensorService( BLE_UUID_SENSOR_SERVICE );
BLEShortCharacteristic pressureCharacteristic( BLE_UUID_PRESSURE, BLERead | BLENotify );
//----------------------------------------------------------------------------------------------------------------------
// I/O and App
//----------------------------------------------------------------------------------------------------------------------
#define BLE_LED_PIN LED_BUILTIN
#define PRESSURE_SENSOR_PIN A0
uint16_t averagePressure = 0;
bool updatePressureCharacteristic = false;
void setup()
{
Serial.begin( 9600 );
while ( !Serial );
pinMode( BLE_LED_PIN, OUTPUT );
pinMode( PRESSURE_SENSOR_PIN, INPUT );
analogReadResolution( 10 );
if ( !setupBleMode() )
{
Serial.println( "Failed to initialize BLE!" );
while ( 1 );
}
else
{
Serial.println( "BLE initialized. Waiting for clients to connect." );
}
}
void loop()
{
bleTask();
sensorTask();
}
void sensorTask()
{
#define SENSOR_SAMPLING_INTERVAL 50
#define SAMPLE_BUFFER_SIZE 10
static uint16_t sampleBuffer[SAMPLE_BUFFER_SIZE] = { 0 };
static uint32_t previousMillis = 0;
static uint32_t sampleIndex = 0;
uint32_t currentMillis = millis();
if ( currentMillis - previousMillis >= SENSOR_SAMPLING_INTERVAL )
{
previousMillis = currentMillis;
uint16_t currentPressure = analogRead( PRESSURE_SENSOR_PIN );
sampleBuffer[sampleIndex] = currentPressure;
sampleIndex = ( sampleIndex + 1 ) % SAMPLE_BUFFER_SIZE;
if ( sampleIndex == 0 )
{
uint32_t bufferTotal = 0;
for ( uint32_t i = 0; i < SAMPLE_BUFFER_SIZE; i++ )
{
bufferTotal += sampleBuffer[i];
}
averagePressure = (uint16_t) round( bufferTotal / SAMPLE_BUFFER_SIZE );
updatePressureCharacteristic = true;
}
}
}
bool setupBleMode()
{
if ( !BLE.begin() )
{
return false;
}
// set advertised local name and service UUID
BLE.setDeviceName( BLE_DEVICE_NAME );
BLE.setLocalName( BLE_LOCAL_NAME );
BLE.setAdvertisedService( sensorService );
// add characteristics
sensorService.addCharacteristic( pressureCharacteristic );
// add service
BLE.addService( sensorService );
// set the initial value for the characeristics
pressureCharacteristic.writeValue( averagePressure );
// set BLE event handlers
BLE.setEventHandler( BLEConnected, blePeripheralConnectHandler );
BLE.setEventHandler( BLEDisconnected, blePeripheralDisconnectHandler );
// start advertising
BLE.advertise();
return true;
}
void bleTask()
{
#define BLE_UPDATE_INTERVAL 10
static uint32_t previousMillis = 0;
uint32_t currentMillis = millis();
if ( currentMillis - previousMillis >= BLE_UPDATE_INTERVAL )
{
previousMillis = currentMillis;
BLE.poll();
}
if ( updatePressureCharacteristic )
{
updatePressureCharacteristic = false;
pressureCharacteristic.writeValue( averagePressure );
}
}
void blePeripheralConnectHandler( BLEDevice central )
{
digitalWrite( BLE_LED_PIN, HIGH );
Serial.print( "Connected to central: " );
Serial.println( central.address() );
}
void blePeripheralDisconnectHandler( BLEDevice central )
{
digitalWrite( BLE_LED_PIN, LOW );
Serial.print( "Disconnected from central: " );
Serial.println( central.address() );
}
I created a simple app to check if I can read the data that is being sent and with no luck.
as you can see I tryed several diffrent blocks with no luck.
what am I missing?