Connecting two Arduinos via BLE 5.0

I have this issue where I'm transmitting sensor data from Nano Sense Rev 2 (transmitter) board to Nano 33 BLE rev 2 (receiver). The receiver keep failing to detect and connect to the transmitter and is giving me a scan timeout reached stopping scan after scanning for few seconds. Note that I was able to connect to my transmitter using my phone app. This is my transmitter code :

#include <Arduino_LPS22HB.h>
#include <Arduino_HS300x.h>
#include <Arduino_APDS9960.h>
#include <ArduinoBLE.h>

// Custom 128-bit UUIDs for the service and characteristics
BLEService sensorService("12345678-1234-5678-1234-56789abcdef0");  // Custom Service UUID

BLECharacteristic pressureCharacteristic("12345678-1234-5678-1234-56789abcdef1", BLERead | BLENotify, sizeof(float));  // Custom Pressure Characteristic UUID
BLECharacteristic temperatureCharacteristic("12345678-1234-5678-1234-56789abcdef2", BLERead | BLENotify, sizeof(float)); // Custom Temperature Characteristic UUID
BLECharacteristic humidityCharacteristic("12345678-1234-5678-1234-56789abcdef3", BLERead | BLENotify, sizeof(float)); // Custom Humidity Characteristic UUID
BLECharacteristic lightIntensityCharacteristic("12345678-1234-5678-1234-56789abcdef4", BLERead | BLENotify, sizeof(int)); // Custom Light Intensity Characteristic UUID

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

  // Initialize Sensors
  if (!BARO.begin()) {
    Serial.println("Failed to initialize pressure sensor!");
    while (1);
  }

  if (!HS300x.begin()) {
    Serial.println("Failed to initialize humidity temperature sensor!");
    while (1);
  }

  if (!APDS.begin()) {
    Serial.println("Error initializing APDS9960 sensor.");
  }

  // Initialize BLE
  if (!BLE.begin()) {
    Serial.println("BLE initialization failed!");
    while (1);
  }

  BLE.setLocalName("Nano33BLESense");

  // Add characteristics to the custom service
  sensorService.addCharacteristic(pressureCharacteristic);
  sensorService.addCharacteristic(temperatureCharacteristic);
  sensorService.addCharacteristic(humidityCharacteristic);
  sensorService.addCharacteristic(lightIntensityCharacteristic);

  // Add the service to the BLE peripheral
  BLE.addService(sensorService);

  // Start advertising the BLE service (and repeat to keep it active)
  BLE.advertise();
  Serial.println("BLE Peripheral started advertising.");
}

void loop() {
  // Read sensor data
  float pressure = BARO.readPressure();
  float temperature = HS300x.readTemperature();
  float humidity = HS300x.readHumidity();

  int r, g, b;
  while (!APDS.colorAvailable()) {
    delay(5);
  }
  APDS.readColor(r, g, b);
  int lightIntensity = r + g + b;

  // Print sensor values to the Serial Monitor (for debugging)
  Serial.print("Pressure: ");
  Serial.println(pressure);
  Serial.print("Temperature: ");
  Serial.println(temperature);
  Serial.print("Humidity: ");
  Serial.println(humidity);
  Serial.print("Light Intensity: ");
  Serial.println(lightIntensity);

  // Convert float values to byte arrays
  byte pressureBytes[sizeof(float)];
  memcpy(pressureBytes, &pressure, sizeof(float));

  byte temperatureBytes[sizeof(float)];
  memcpy(temperatureBytes, &temperature, sizeof(float));

  byte humidityBytes[sizeof(float)];
  memcpy(humidityBytes, &humidity, sizeof(float));

  byte lightIntensityBytes[sizeof(int)];
  memcpy(lightIntensityBytes, &lightIntensity, sizeof(int));

  // Update BLE characteristics with the latest sensor data
  pressureCharacteristic.setValue(pressureBytes, sizeof(pressureBytes));
  temperatureCharacteristic.setValue(temperatureBytes, sizeof(temperatureBytes));
  humidityCharacteristic.setValue(humidityBytes, sizeof(humidityBytes));
  lightIntensityCharacteristic.setValue(lightIntensityBytes, sizeof(lightIntensityBytes));

  // The BLE library will automatically handle notifications when `setValue` is called
  // since BLENotify is enabled on the characteristics.

  // Keep advertising to make sure the BLE service stays visible
  BLE.advertise();
  
  delay(1000);  // Wait for 1 second before sending the next data
}
and this is the receiver code: 

#include <ArduinoBLE.h>

// Define the UUIDs for the custom service and characteristics
#define SENSOR_SERVICE_UUID "12345678-1234-5678-1234-56789abcdef0"
#define PRESSURE_CHAR_UUID "12345678-1234-5678-1234-56789abcdef1"
#define TEMPERATURE_CHAR_UUID "12345678-1234-5678-1234-56789abcdef2"
#define HUMIDITY_CHAR_UUID "12345678-1234-5678-1234-56789abcdef3"
#define LIGHT_INTENSITY_CHAR_UUID "12345678-1234-5678-1234-56789abcdef4"

// Declare BLE objects with proper UUIDs
BLEService sensorService(SENSOR_SERVICE_UUID);
BLECharacteristic pressureCharacteristic(PRESSURE_CHAR_UUID, BLERead | BLENotify, sizeof(float));
BLECharacteristic temperatureCharacteristic(TEMPERATURE_CHAR_UUID, BLERead | BLENotify, sizeof(float));
BLECharacteristic humidityCharacteristic(HUMIDITY_CHAR_UUID, BLERead | BLENotify, sizeof(float));
BLECharacteristic lightIntensityCharacteristic(LIGHT_INTENSITY_CHAR_UUID, BLERead | BLENotify, sizeof(int));

unsigned long scanStartTime;
bool scanInProgress = true;
int maxScanAttempts = 5;  // Max number of scan attempts before stopping
int scanAttempts = 0;     // Counter for scan attempts
void setup() {
  // Start Serial Monitor
  Serial.begin(9600);
  while (!Serial);

  // Initialize BLE
  if (!BLE.begin()) {
    Serial.println("BLE initialization failed!");
    while (1);
  }

  BLE.setLocalName("BLEReceiver");
  Serial.println("BLEReceiver started. Scanning for devices...");

  // Start scanning for devices advertising the sensor service
  BLE.scanForUuid(SENSOR_SERVICE_UUID);  // Scan for the service
  scanStartTime = millis();  // Start the scan timer
}
void loop() {
  BLEDevice central = BLE.available();
  
  if (central) {
    // Device found, print address and attempt to connect
    Serial.print("Found BLE Device: ");
    Serial.println(central.address());

    // Stop scanning and attempt to connect
    BLE.stopScan();
    delay(100);  // Small delay before attempting to connect
    if (central.connect()) {
      Serial.println("Connected to device.");

      BLEService sensorService = central.service(SENSOR_SERVICE_UUID);
      if (sensorService) {
        Serial.println("Sensor service found.");

        // Discover characteristics
        pressureCharacteristic = sensorService.characteristic(PRESSURE_CHAR_UUID);
        temperatureCharacteristic = sensorService.characteristic(TEMPERATURE_CHAR_UUID);
        humidityCharacteristic = sensorService.characteristic(HUMIDITY_CHAR_UUID);
        lightIntensityCharacteristic = sensorService.characteristic(LIGHT_INTENSITY_CHAR_UUID);

        if (pressureCharacteristic && temperatureCharacteristic && humidityCharacteristic && lightIntensityCharacteristic) {
          Serial.println("Characteristics found. Reading values...");

          while (central.connected()) {
            if (pressureCharacteristic.canRead()) {
              float pressure;
              memcpy(&pressure, pressureCharacteristic.value(), sizeof(pressure));
              Serial.print("Pressure: ");
              Serial.println(pressure);
            }

            if (temperatureCharacteristic.canRead()) {
              float temperature;
              memcpy(&temperature, temperatureCharacteristic.value(), sizeof(temperature));
              Serial.print("Temperature: ");
              Serial.println(temperature);
            }

            if (humidityCharacteristic.canRead()) {
              float humidity;
              memcpy(&humidity, humidityCharacteristic.value(), sizeof(humidity));
              Serial.print("Humidity: ");
              Serial.println(humidity);
            }

            if (lightIntensityCharacteristic.canRead()) {
              int lightIntensity;
              memcpy(&lightIntensity, lightIntensityCharacteristic.value(), sizeof(lightIntensity));
              Serial.print("Light Intensity: ");
              Serial.println(lightIntensity);
            }

            delay(1000);  // Wait for 1 second before reading again
          }
        } else {
          Serial.println("Failed to find required characteristics.");
        }
      } else {
        Serial.println("Sensor service not found.");
      }

      // Disconnect once done
      central.disconnect();
      Serial.println("Disconnected from device.");
      scanAttempts = 0;  // Reset scan attempts after successful connection
    } else {
      Serial.println("Failed to connect to device.");
    }
  }

  // Check scan timeout and limit scan attempts
  if (scanInProgress && millis() - scanStartTime > 10000) {  // 10 seconds timeout
    Serial.println("Scan timeout reached. Stopping scan.");
    BLE.stopScan();
    scanInProgress = false;
    scanAttempts++;

    // If max scan attempts reached, stop scanning and wait for user intervention
    if (scanAttempts >= maxScanAttempts) {
      Serial.println("Max scan attempts reached. Please reset the device.");
    } else {
      Serial.println("Resuming scan...");
      BLE.scanForUuid(SENSOR_SERVICE_UUID);  // Restart scanning after timeout
      scanStartTime = millis();  // Reset the scan timer
      scanInProgress = true;  // Set scanInProgress back to true
    }
  }

  // Continue scanning only if it is still ongoing and hasn't timed out
  if (scanInProgress && !central) {
    BLEDevice central = BLE.available();
  }
}
The two units are withing 1 meter of each others. and this is my serial monitor (receiver).

14:52:23.321 -> Resuming scan...
14:52:33.314 -> Scan timeout reached. Stopping scan.
14:52:33.314 -> Resuming scan...
14:52:43.338 -> Scan timeout reached. Stopping scan.
14:52:43.387 -> Resuming scan...
14:52:53.379 -> Scan timeout reached. Stopping scan.
14:52:53.425 -> Max scan attempts reached. Please reset the device.


Why can I not connect?

I have this issue where I'm transmitting sensor data from Nano Sense Rev 2 (transmitter) board to Nano 33 BLE rev 2 (receiver). The receiver keep failing to detect and connect to the transmitter and is giving me a scan timeout reached stopping scan after scanning for few seconds.

The more standard terminology for BLE is Peripheral (your transmitter) and Central (your receiver).

Take a closer look at the Arduino BLE library example sketches for the Central.

A Central finds and connects to an advertising Peripheral.

Instead, you have this code in your Central

void loop() {
  BLEDevice central = BLE.available();
  
  if (central) {

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.