Can't read variable values with Nano 33 BLE

Hi everyone,

I am using a Arduino Nano BLE 33 in combination with an INO260. (Called "Nano A" from here on)
One nano I use to send the read values, and with my phone I can connect to it (via Lightblue) and read the values.

But I have trouble connecting and/or reading with another Nano BLE ("Nano B") to my "Nano A"

Here is the code used on "Nano A":


#include <Wire.h>
#include <Adafruit_INA260.h>
#include <ArduinoBLE.h>

Adafruit_INA260 ina260;

 // BLE Battery Service
BLEService Meter("19b10010-e8f2-537e-4f6c-d104768a1226");

// BLE Battery Level Characteristic
BLECharCharacteristic AmpereChar("19b10010-e8f2-537e-4f6c-d104768a1224",  // standard 16-bit characteristic UUID
    BLERead | BLENotify); // remote clients will be able to get notifications if this characteristic changes
BLECharCharacteristic VoltChar("19b10010-e8f2-537e-4f6c-d104768a1225",  // standard 16-bit characteristic UUID
    BLERead | BLENotify); // remote clients will be able to get notifications if this characteristic changes
    
int OudeWaarde = 0;  // last battery level reading from analog input
long previousMillis = 0;  // last time the battery level was checked, in ms

void setup() {
  Serial.begin(9600);    // initialize serial communication
  //while (!Serial);
  //============================================================================================================================
  // Void setup uit de Read_A-V
  //----------------------------------------------------------------------------------------------------------------------------
  uint32_t currentFrequency;
    
  Serial.println("Hello!");
  
    if (! ina260.begin()) {
    Serial.println("Failed to find INA260 chip");
    while (1) { delay(10); }
  }
   Serial.println("Measuring voltage and current...");

   //=============================================================================================================================
  pinMode(LED_BUILTIN, OUTPUT); // initialize the built-in LED pin to indicate when a central is connected

  // begin initialization
  if (!BLE.begin()) {
    Serial.println("starting BLE failed!");

    while (1);
  }

  /* Set a local name for the BLE device
     This name will appear in advertising packets
     and can be used by remote devices to identify this BLE device
     The name can be changed but maybe be truncated based on space left in advertisement packet
  */
  BLE.setLocalName("Meter");
  BLE.setAdvertisedService(Meter); // add the service UUID
  Meter.addCharacteristic(AmpereChar); // add the battery level characteristic
  Meter.addCharacteristic(VoltChar);
  BLE.addService(Meter); // Add the battery service
  AmpereChar.writeValue(OudeWaarde); // set initial value for this characteristic                          
  /* Start advertising BLE.  It will start continuously transmitting BLE
     advertising packets and will be visible to remote BLE central devices
     until it receives a new connection */

  // start advertising
  BLE.advertise();

  Serial.println("Bluetooth device active, waiting for connections...");
}

void loop() {
  // wait for a BLE central
  BLEDevice central = BLE.central();

  // if a central is connected to the peripheral:
  if (central) {
    Serial.print("Connected to central: ");
    // print the central's BT address:
    Serial.println(central.address());
    // turn on the LED to indicate the connection:
    digitalWrite(LED_BUILTIN, HIGH);

    // check the battery level every 200ms
    // while the central is connected:
    while (central.connected()) {
      long currentMillis = millis();
      // if 200ms have passed, check the battery level:
      if (currentMillis - previousMillis >= 200) {
        previousMillis = currentMillis;
        updateWaarde();
      }
    }
    // when the central disconnects, turn off the LED:
    digitalWrite(LED_BUILTIN, LOW);
    Serial.print("Disconnected from central: ");
    Serial.println(central.address());
  }
}

void updateWaarde() {
  /* Read the current voltage level on the A0 analog input pin.
     This is used here to simulate the charge level of a battery.
  */
//==================================================================================================================
  float shuntvoltage_mV = 0;
  float busvoltage_mV = 0;
  float current_A = 0;
  float loadvoltage = 0;
  float currentma = 0;

  
  shuntvoltage_mV = ina260.readBusVoltage();
  busvoltage_mV = ina260.readBusVoltage();
  currentma = ina260.readCurrent ();
  current_A = currentma/1000;
  loadvoltage = (busvoltage_mV + (shuntvoltage_mV / 1000))/1000 ;
  
  Serial.print("Load Voltage:  "); Serial.print(loadvoltage); Serial.println(" V");
  Serial.print("Current:       "); Serial.print(current_A); Serial.println(" A");
  Serial.println("");

//==================================================================================================================
  int battery = analogRead(A0);
  //int batteryLevel = map(battery, 0, 1023, 0, 100);

  if (current_A != OudeWaarde) {      // if the battery level has changed
    //Serial.print("Battery Level % is now: "); // print it
    //Serial.println(batteryLevel);
    Serial.print("Current:       "); Serial.print(current_A); Serial.println(" A");
    AmpereChar.writeValue(current_A);  // and update the battery level characteristic
    OudeWaarde = current_A;           // save the level for next comparison
  }
  
}

Code on "Nano B":

#include <ArduinoBLE.h>

BLEService Meter("19b10010-e8f2-537e-4f6c-d104768a1226");

// BLE LED Switch Characteristic - custom 128-bit UUID, read and writable by central
BLECharCharacteristic AmpereChar("19b10010-e8f2-537e-4f6c-d104768a1224");
//BLECharCharacteristic VoltChar("19b10010-e8f2-537e-4f6c-d104768a1225");

void setup() {
  Serial.begin(9600);



  // begin initialization
  if (!BLE.begin()) {
    Serial.println("starting BLE failed!");

    while (1);
  }

  // set advertised local name and service UUID:
  BLE.setLocalName("central");
  BLE.setAdvertisedService(AmpereService);
  BLE.setAdvertisedService(VoltService);
  
  // add the characteristic to the service
  AmpereService.addCharacteristic(AmpereChar);
  VoltService.addCharacteristic(VoltChar);

  // add service
  BLE.addService(AmpereService);
  BLE.addService(VoltService);
//  BLE.addService(ledService);

  // set the initial value for the characeristic:
  AmpereChar.writeValue(0);
  VoltChar.writeValue(0);
  // start advertising
  //BLE.advertise();


 
}

void loop() {
  // listen for BLE peripherals to connect:
  BLEDevice central = BLE.central();

  // if a central is connected to peripheral:
  if (central) {
    // while the central is still connected to peripheral:
    while (central.connected()) {

     
   //  int current_A = 0;
     
    // AmpereChar.read(AmpereChar);
     Serial.print(AmpereChar);
      
      if (AmpereChar.written()){
      Serial.print("Voltage: "); Serial.print(VoltChar);
     }
     
     }
    }
}

Can anyone help me with this problem?
Or do you have another suggestion to do this?

Thanks in advance

Anyone? this would be helpfull for me aswell

Welcome to the forum.

Please have a look at the following sketch.

File -> Examples -> ArduinoBLE -> Central -> Peripheral Explorer

This will show you all the steps you need to do to in a central device.

The problem of your central sketch is that you need to scan for a peripheral and then subscribe to the characteristic and then you can get the values when they are updated.

Regarding your peripheral sketch, have a look at the sketch (reply #2) I have posted for an environmental sensing service. It uses a couple of techniques to get clean code, avoid while loops, using different data types and avoid floats for characteristics and a few others that may be helpful.

https://forum.arduino.cc/t/bluetooth-communications/902610/2

I would recommend avoiding floating point data for external interfaces whenever possible. There are several floating-point standards, and they are not intuitive for humans to read. The BLE SIG has shown us a good alternative be using integer data with a known exponent. See example.

One tip for future posts, remove all code from your examples sketches that need any external hardware when possible. Add some "simulator" functions that will create random data for sensors. This will allow others to test your code on their Arduinos.

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