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