Hello,
I am attempting to connect two nano 33 IoT devices, with the peripheral sending the Z accelerometer data from the nano's built-in accelerometer to the central device. However, the devices aren't connecting to each other at all. I've looked at a lot of forums and can't seem to find what I'm doing wrong. This is my first project with Arduino, so apologies for whatever dumb mistake I'm making.
Peripheral:
/*
* Device: Arduino Nano 33 BLE Sense
* Peripheral
* The values of the integrated temperature sensor and
* accelerometer are sent using BLE.
*/
#include <ArduinoBLE.h>
#include <Arduino_LSM6DS3.h> //accelerometer sensor
int const d_a=2; //number of decimal to keep for the accelerometer
float xSensor=0;
float ySensor=0;
float zSensor=0;
//integer variable to send via BLE
int zBLE=zSensor*pow(10,d_a);
BLEService SensorService("1101");
BLEUnsignedIntCharacteristic ZChar("2103", BLERead | BLENotify);
void setup() {
IMU.begin();
Serial.begin(9600);
//while (!Serial);
// if (!HTS.begin()){
// Serial.println("Failed to start the HTS221 sensor.");
// while(1);
//
// if (!IMU.begin()) {
// Serial.println("Failed to start the LSM9DS sensor.");
// while (1);
pinMode(LED_BUILTIN, OUTPUT);
if (!BLE.begin()) {
Serial.println("BLE failed to Initiate");
delay(500);
while (1);
}
BLE.setLocalName("Arduino Z (peripheral)");
BLE.setAdvertisedService(SensorService);
SensorService.addCharacteristic(ZChar);
BLE.addService(SensorService);
ZChar.writeValue(zBLE);
BLE.advertise();
Serial.println("Arduino Z peripheral device is now active, waiting for connections...");
}
void loop() {
BLEDevice central = BLE.central();
if (central) {
Serial.print("Connected to central: ");
Serial.print("* Device MAC address: ");
Serial.println(central.address());
Serial.println(" ");
digitalWrite(LED_BUILTIN, HIGH);
while (central.connected()) {
//delay(200);
//read_sensor();
if (IMU.accelerationAvailable()) {
IMU.readAcceleration(xSensor, ySensor, zSensor);
}
Serial.print("xSensor ");
Serial.print(xSensor);
Serial.print(" - ySensor ");
Serial.print(ySensor);
Serial.print(" - zSensor ");
Serial.print(zSensor);
zBLE=zSensor*pow(10,d_a);
//int t_i=t_f*100;
//int x_i=x_f*100;
//int y_i=y_f*100;
//int z_i=z_f*100;
//writeCharacteristicValue (Tchar, Xchar, Ychar, Zchar);
ZChar.writeValue(zBLE);
//Serial.println("At Main Function");
Serial.print(" - ZChar ");
Serial.print(ZChar);
Serial.println("");
Serial.println("");
//Serial.println("");
//Serial.println("");
delay(1000);
}
}
//else {
delay(800);
//}
digitalWrite(LED_BUILTIN, LOW);
delay(100);
digitalWrite(LED_BUILTIN, HIGH);
delay(100);
digitalWrite(LED_BUILTIN, LOW);
Serial.print("Disconnected from central: ");
Serial.println(central.address());
BLE.advertise();
}
//void read_sensor(){
//if (IMU.accelerationAvailable()) {
//IMU.readAcceleration(x_f, y_f, z_f);
//}
//t_f=HTS.readTemperature();
//
//Serial.print("x_f ");
//Serial.print(x_f);
//Serial.print(" - y_f ");
//Serial.print(y_f);
//Serial.print(" - z_f ");
//Serial.print(z_f);
//Serial.print(" - t_f ");
//Serial.println(t_f);
//Serial.println("");
////x_i=x_f*pow(10,d);
////y_i=y_f*pow(10,d);
////z_i=z_f*pow(10,d);
////t_i=t_f*pow(10,d);
//
//int t_i=t_f;
//int x_i=x_f;
//int y_i=y_f;
//int z_i=z_f;
//
//Serial.print("x_i ");
//Serial.print(x_i);
//Serial.print(" - y_i ");
//Serial.print(y_i);
//Serial.print(" - z_i ");
//Serial.print(z_i);
//Serial.print(" - t_i ");
//Serial.println(t_i);
//Serial.println("");
//}
Central:
/*
BLE_Central_Device.ino
/*
* Device: Arduino Nano 33 BLE Sense
* Central
* The values of the integrated temperature sensor and
* accelerometer of another Nano 33 BLE are received using BLE.
*/
#include <ArduinoBLE.h>
void setup() {
Serial.begin(9600);
// while (!Serial);
if (!BLE.begin()) {
Serial.println("* Starting BLE module failed!");
while (1);
}
BLE.setLocalName("Arduino Z (Central)");
Serial.println("Arduino Z (Central)");
Serial.println(" ");
pinMode(LED_BUILTIN, OUTPUT);
//BLE.advertise();
}
void loop() {
// BLE.scan();
BLEDevice peripheral;
Serial.println("- Discovering peripheral device...");
digitalWrite(LED_BUILTIN, HIGH);
do
{
BLE.scanForUuid("1101");
peripheral = BLE.available();
} while (!peripheral);
if (peripheral) {
digitalWrite(LED_BUILTIN, LOW);
Serial.print("Found ");
Serial.print(peripheral.address());
Serial.print(" '");
Serial.print(peripheral.localName());
Serial.print("' ");
Serial.print(peripheral.advertisedServiceUuid());
Serial.println();
// Serial.println("* Peripheral device found!");
// Serial.print("* Device MAC address: ");
// Serial.println(peripheral.address());
// Serial.print("* Device name: ");
// Serial.println(peripheral.localName());
// Serial.print("* Advertised service UUID: ");
// Serial.println(peripheral.advertisedServiceUuid());
// Serial.println(" ");
//BLE.stopScan();
if (peripheral.localName() == "Arduino Z (peripheral)") {
// stop scanning
BLE.stopScan();
if (peripheral.connect()) {
Serial.println("Connected");
} else {
Serial.println("Failed to connect!");
return;
}
// discover peripheral attributes
Serial.println("Discovering attributes ...");
if (peripheral.discoverAttributes()) {
Serial.println("Attributes discovered");
} else {
Serial.println("Attribute discovery failed!");
peripheral.disconnect();
return;
}
// read and print device name of peripheral
Serial.println();
Serial.print("Device name: ");
Serial.println(peripheral.deviceName());
Serial.print("Appearance: 0x");
Serial.println(peripheral.appearance(), HEX);
Serial.println();
}
while (peripheral.connect()) {
//BLECharacteristic TChar = peripheral.characteristic( "2104" );
BLEService service = peripheral.service("1101");
Serial.print("Service ");
Serial.print(service.uuid());
BLECharacteristic Zchar = service.characteristic("2103");
//readCharacteristicValue(characteristic);
Serial.print("\tZ characteristic ");
Serial.print(Zchar.uuid());
// check if the characteristic is readable
if (Zchar.canRead()) {
//read the characteristic value
int32_t z = 0;
Zchar.readValue(&z,4);
Serial.print(", z = ");
Serial.println((float)z/100.0);
}
delay(1000);
}
// } else {
// Serial.println("* Connection to peripheral device failed!");
// Serial.println(" ");
// return;
}
// Serial.println("Disconnected from peripheral: ");
}
void readCharacteristicValue(BLECharacteristic characteristic) {
// print the UUID and properties of the characteristic
Serial.print("\tCharacteristic ");
Serial.print(characteristic.uuid());
// check if the characteristic is readable
if (characteristic.canRead()) {
// read the characteristic value
characteristic.read();
if (characteristic.valueLength() > 0) {
Serial.print(", value = ");
//characteristic.value() returns byte array
unsigned long bitShiftData = 0;
bitShiftData = (uint32_t)characteristic.value()[3] << 24 | (uint32_t)characteristic.value()[2] << 16 | (uint32_t)characteristic.value()[1] << 8 | characteristic.value()[0];
Serial.println(bitShiftData, DEC);
Serial.print(bitShiftData / 100);
Serial.print('.');
if (bitShiftData % 100 < 10)
Serial.print('0');
Serial.println(bitShiftData % 100);
}
}
}
The framework for this code I took from this forum:
Any help is appreciated. Thank you.