Hello everyone,
I'm working on moving an old hobbyproject to the new Arduino Giga. I had a working bluetooth function with a MIT app inventor app. The Giga has BLE insted of bluetooth so I'm trying to convert everything. Packing, sending the data and reading the data are all so different from Bluetooth classic that I find it quite the challenge. I've gotten as far as reading 1 sensor value but I would like at least 3 and if that works step it up to all 12.
I found this thread by @Klaus_K that gave me the idea to put multiple values in a union and send it as 1 characteristic. I have little to no experience coding myself and reading up on the method used still leaves me with a lot of questions. I tried making seperate smaller sketches with these examples , here ... and here
The truncated sketch looks like this:
//credit to Klaus_K @ https://forum.arduino.cc/t/ble-very-weak-signal/631751/33
#include <ArduinoBLE.h>
//----------------------------------------------------------------------------------------------------------------------
// BLE UUIDs
//----------------------------------------------------------------------------------------------------------------------
#define BLE_UUID_SENSOR_DATA_SERVICE "ECBCAD86-1010-441B-BE5D-C11AB3415169"
#define BLE_UUID_MULTI_SENSOR_DATA "ECBCAD86-2010-441B-BE5D-C11AB3415169"
#define NUMBER_OF_SENSORS 3
union multi_sensor_data
{
struct __attribute__( ( packed ) )
{
float values[NUMBER_OF_SENSORS];
};
uint8_t bytes[ NUMBER_OF_SENSORS * sizeof( float ) ];
};
union multi_sensor_data multiSensorData;
float airTempAverage = 25,6
float humidityAverage = 67,5
float pressureAverage = 1012
//----------------------------------------------------------------------------------------------------------------------
// BLE
//----------------------------------------------------------------------------------------------------------------------
BLEService sensorDataService( BLE_UUID_SENSOR_DATA_SERVICE );
BLECharacteristic multiSensorDataCharacteristic( BLE_UUID_MULTI_SENSOR_DATA, BLERead | BLENotify, sizeof multiSensorData.bytes );
#define BLE_LED_PIN = LEDB;
void setup()
{
Serial.begin( 9600 );
//while ( !Serial );
pinMode( BLE_LED_PIN, OUTPUT );
if ( !setupBleMode() )
{
Serial.println( "Failed to initialize BLE!" );
while ( 1 );
}
else
{ Serial.println( "BLE initialized. Waiting for clients to connect." );
}
for ( int i = 0; i < NUMBER_OF_SENSORS; i++ )
{
multiSensorData.values[i] = i;
}
}
void loop()
{
static long previousMillis = 0;
if (millis() - previousMillis >= 2000)
{
updateStatistics();
updateAverageReadings = true;
}
// listen for BLE peripherals to connect:
BLEDevice central = BLE.central();
if ( central )
{
Serial.print( "Connected to central: " );
Serial.println( central.address() );
multiSensorData.values[1] = airTempAverage;
multiSensorData.values[2] = humidityAverage;
multiSensorData.values[3] = pressureAverage;
while ( central.connected() )
{
unsigned long currentMillis = millis();
if ( currentMillis - previousMillis >= 2000 )
{
previousMillis = currentMillis;
if ( central.rssi() != 0 )
{
for ( int i = 0; i < NUMBER_OF_SENSORS; i++ )
{
multiSensorData.values[i] = multiSensorData.values[i] + 0.1;
}
multiSensorDataCharacteristic.writeValue( multiSensorData.bytes, sizeof multiSensorData.bytes );
}
}
}
Serial.print( F( "Disconnected from central: " ) );
Serial.println( central.address() );
}
}
bool setupBleMode()
{
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( sensorDataService );
// BLE add characteristics
sensorDataService.addCharacteristic( multiSensorDataCharacteristic );
// add service
BLE.addService( sensorDataService );
// set the initial value for the characeristic:
multiSensorDataCharacteristic.writeValue( multiSensorData.bytes, sizeof multiSensorData.bytes );
// start advertising
BLE.advertise();
return true;
}
My main uncertainty is if my declaration of
multiSensorData.values[1] = airTempAverage;
multiSensorData.values[2] = humidityAverage;
multiSensorData.values[3] = pressureAverage;
is correct,
and what is the 0.1
doing in multiSensorData.values[i] = multiSensorData.values[i] + 0.1;
It is transmitting, and my app is reading the bytes (when BluetoothLE1.BytesReceived
set TextLabel.Text
to get byteValues
). Or at least there is a rapid stream of numbers. I'm searching topics and reading to understand a way to seperate them and put them on the correct (seperate) tekst fields.