I was following the tutorial to experiment BLE and noted issues I had here.

Issue 1: Failed to initialize IMU!

My board is Nano 33 BLE Sense. After uploading the example code, it could not be functional. So I’ve debugged it by using serial communication. Quickly I found out this issue was due to fail to initialize IMU. So, I changed the IMU library from Arduino_LSM6DS3 to Arduino_LSM9DS1. However, I still have another issues and could not make the example code work:

Issue 2: Unstable USB port connection

It seems to be unstable to connect my Nano 33 BLE Sense board with USB port. It will require “waiting time” and “you have to double checking the port before uploading the code.” As the below screenshot, the USB port can not pick up automatically and disconnect randomly all the time….

Issue 3: Chrome only

It surprised me the API may be Chrome only even if the API defined by Mozilla foundation. I can not run the example p5 code with my Firefox browser.

Anyway, I got my first BLE experiment to work! But I have to note another issue here! The board/IMU seems not to be stable. After a period of times, It would be hot and stop working…..

Next step…. to build a p5 web app to interact with BLE peripheral

To continue my experiment/homework, I decided to rewrite the tutorial code and use the real time data from accelerator to change the background color by changing RGB value.

const serviceUuid = "19b10000-e8f2-537e-4f6c-d104768a1216";
const characteristicsUUID = {
  x: "19b10001-e8f2-537e-4f6c-d104768a1216",
  y: "19b10002-e8f2-537e-4f6c-d104768a1216",
  z: "19b10003-e8f2-537e-4f6c-d104768a1216"
};
var characteristicX;
var characteristicY;
var characteristicZ;

let myBLE;

let r = 0;
let g = 0;
let b = 0;

function setup() {
  // Create a p5ble class
  myBLE = new p5ble();

  createCanvas(600, 400);
  background(r,g,b);

  // Create a 'Connect and Start Notifications' button
  const connectButton = createButton('Connect and Start Notifications')
  connectButton.mousePressed(connectAndStartNotify);
  
  // Create a 'Disconnect' button
  const disconnectButton = createButton('Disconnect')
  disconnectButton.mousePressed(disconnectToBle);
}

function draw() {
  background(r,g,b);
}

function connectAndStartNotify() {
  // Connect to a device by passing the service UUID
  myBLE.connect(serviceUuid, gotCharacteristics);
}

function disconnectToBle() {
  // Disonnect to the device
  myBLE.disconnect();
  // Check if myBLE is connected
  let isConnected = myBLE.isConnected();
  if(!isConnected){
    background(0,0,0);//when disconnected, empty the canvas
  }
}

// A function that will be called once got characteristics
function gotCharacteristics(error, characteristics) {
  if (error) console.log('error: ', error);
  console.log(characteristics.length);

  for (let i = 0; i < characteristics.length; i++) {
    if (characteristics[i].uuid == characteristicsUUID.x) {
      characteristicX = characteristics[i];
      myBLE.startNotifications(characteristicX, handleX,'int8');
    } else if (characteristics[i].uuid == characteristicsUUID.y) {
      characteristicY = characteristics[i];
      myBLE.startNotifications(characteristicY, handleY,'int8');
    } else if (characteristics[i].uuid == characteristicsUUID.z) {
      characteristicZ = characteristics[i];
      myBLE.startNotifications(characteristicZ, handleZ,'int8' );
    }else {
    console.log("nothing");
  }
}
  // Start notifications on the first characteristic by passing the characteristic
  // And a callback function to handle notifications


  // You can also pass in the dataType
  // Options: 'unit8', 'uint16', 'uint32', 'int8', 'int16', 'int32', 'float32', 'float64', 'string'
  // myBLE.startNotifications(myCharacteristic, handleNotifications, 'string');
}

// A function that will be called once got characteristics
function handleX(data) {
  //console.log('r: ', data);
 r = map(Number(data),-100,100,0,255);

}

function handleY(data) {
  //console.log('g: ', data);
 g = map(Number(data),-100,100,0,255);

}

function handleZ(data) {
  //console.log('b: ', data);
  b = map(Number(data),100,-100,0,255);

}

More than more…

I am trying to figure out how to send data from p5 to my Arduino and make it two-way interactive.