Hello guys,
I can only find Arduino Nano 33 IoT via bluetooth if it is connected with USB to the computer I downloaded the code to Arduino. What causes that and any idea how I can get it to work with power bank or any other power supply. This is my code:
#include <ArduinoBLE.h>
#include <Arduino_LSM6DS3.h>
// BLE service and characteristic UUIDs
BLEService sensorService("12345678-1234-1234-1234-123456789012");
BLECharacteristic sensorCharacteristic("87654321-4321-4321-4321-210987654321", BLERead | BLENotify, 24); // 24 bytes for 6 float values
BLEService ledService("19B10000-E8F2-537E-4F6C-D104768A1214"); // Bluetooth® Low Energy LED Service
BLEByteCharacteristic switchCharacteristic("19B10001-E8F2-537E-4F6C-D104768A1214", BLERead | BLEWrite);
const int ledPin = LED_BUILTIN; // pin to use for the LED
void setup() {
Serial.begin(9600);
while (!Serial);
// set LED pin to output mode
pinMode(ledPin, OUTPUT);
// begin initialization
if (!BLE.begin()) {
Serial.println("starting Bluetooth® Low Energy module failed!");
while (1);
}
// set advertised local name and service UUID:
BLE.setLocalName("Nano33BLE");
BLE.setAdvertisedService(ledService);
BLE.setAdvertisedService(sensorService);
// add the characteristics to the services
ledService.addCharacteristic(switchCharacteristic);
sensorService.addCharacteristic(sensorCharacteristic);
// add services
BLE.addService(ledService);
BLE.addService(sensorService);
// set the initial value for the characteristic:
switchCharacteristic.writeValue(0);
// start advertising
BLE.advertise();
Serial.println("BLE LED Peripheral");
// Get the BLE address
String bleAddress = BLE.address();
// Print the BLE address
Serial.print("Bluetooth Address: ");
Serial.println(bleAddress);
// Initialize IMU
if (!IMU.begin()) {
Serial.println("Failed to initialize IMU!");
while (1);
}
}
void loop() {
// listen for Bluetooth® Low Energy peripherals to connect:
BLEDevice central = BLE.central();
// if a central is connected to peripheral:
if (central) {
Serial.print("Connected to central: ");
// print the central's MAC address:
Serial.println(central.address());
while (central.connected()) {
float ax, ay, az;
float gx, gy, gz;
// Read accelerometer and gyroscope data
if (IMU.accelerationAvailable() && IMU.gyroscopeAvailable()) {
IMU.readAcceleration(ax, ay, az);
IMU.readGyroscope(gx, gy, gz);
// Create a buffer to hold the data
byte data[24];
memcpy(data, &ax, 4);
memcpy(data + 4, &ay, 4);
memcpy(data + 8, &az, 4);
memcpy(data + 12, &gx, 4);
memcpy(data + 16, &gy, 4);
memcpy(data + 20, &gz, 4);
// Write the data to the characteristic
sensorCharacteristic.writeValue(data, sizeof(data));
Serial.println("Data sent");
delay(100); // Adjust delay as needed
}
// if the remote device wrote to the characteristic, use the value to control the LED
if (switchCharacteristic.written()) {
if (switchCharacteristic.value()) { // any value other than 0
Serial.println("LED on");
digitalWrite(ledPin, HIGH); // will turn the LED on
} else { // a 0 value
Serial.println(F("LED off"));
digitalWrite(ledPin, LOW); // will turn the LED off
}
}
}
// when the central disconnects, print it out:
Serial.print(F("Disconnected from central: "));
Serial.println(central.address());
}
}