Skip to content

Instantly share code, notes, and snippets.

@kotobuki
Last active June 12, 2023 14:23
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kotobuki/f549dbab67e3cb5754fa1285c6683c3d to your computer and use it in GitHub Desktop.
Save kotobuki/f549dbab67e3cb5754fa1285c6683c3d to your computer and use it in GitHub Desktop.
Web Bluetooth with micro:bit (Acceleration)
// https://lancaster-university.github.io/microbit-docs/resources/bluetooth/bluetooth_profile.html
const ACCELEROMETER_SERVICE_UUID = "e95d0753-251d-470a-a062-fa1922dfa9a8";
const ACCELEROMETER_DATA_UUID = "e95dca4b-251d-470a-a062-fa1922dfa9a8";
let uBitDevice;
let sinThetaX = 0;
let sinThetaY = 0;
let lastAccelerationX = 0;
let lastAccelerationY = 0;
function setup() {
createCanvas(400, 400, WEBGL);
const connectButton = createButton("Connect");
connectButton.mousePressed(connectButtonPressed);
const disconnectButton = createButton("Disconnect");
disconnectButton.mousePressed(disconnectButtonPressed);
}
function draw() {
background(0);
orbitControl();
translate(0, 0, 0);
normalMaterial();
push();
rotateZ(asin(sinThetaX));
rotateX(-asin(sinThetaY));
box(150, 150, 150);
pop();
}
async function connectButtonPressed() {
try {
console.log("Requesting Bluetooth Device...");
uBitDevice = await navigator.bluetooth.requestDevice({
filters: [{ namePrefix: "BBC micro:bit" }],
optionalServices: [ACCELEROMETER_SERVICE_UUID]
});
// e.g. BBC micro:bit [votiv]
console.log("Connected to " + uBitDevice.name);
console.log("Connecting to GATT Server...");
const server = await uBitDevice.gatt.connect();
console.log("Getting Service...");
const service = await server.getPrimaryService(ACCELEROMETER_SERVICE_UUID);
console.log("Getting Characteristics...");
const characteristic = await service.getCharacteristic(
ACCELEROMETER_DATA_UUID
);
characteristic.startNotifications();
characteristic.addEventListener(
"characteristicvaluechanged",
onAccelerometerValueChanged
);
} catch (error) {
console.log(error);
}
}
function disconnectButtonPressed() {
if (!uBitDevice) {
return;
}
if (uBitDevice.gatt.connected) {
uBitDevice.gatt.disconnect();
console.log("Disconnected");
}
}
function onAccelerometerValueChanged(event) {
// Retrieve acceleration values,
// then convert from milli-g (i.e. 1/1000 of a g) to g
const accelerationX = event.target.value.getInt16(0, true) / 1000.0;
const accelerationY = event.target.value.getInt16(2, true) / 1000.0;
const accelerationZ = event.target.value.getInt16(4, true) / 1000.0;
const smoothedAccelerationX = accelerationX * 0.2 + lastAccelerationX * 0.8;
const smoothedAccelerationY = accelerationY * 0.2 + lastAccelerationY * 0.8;
lastAccelerationX = smoothedAccelerationX;
lastAccelerationY = smoothedAccelerationY;
sinThetaX = constrain(smoothedAccelerationX, -1, 1);
sinThetaY = constrain(smoothedAccelerationY, -1, 1);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.7.2/p5.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.7.2/addons/p5.dom.min.js"></script>

Web Bluetooth Test for micro:bit

This is a demo that illustrates a way to connect a micro:bit and a PC running the Chome web browser using the Web Bluetooth API and displays acceleration values measured by an acceleration sensor on the micro:bit as a simple 3D animation.

You can get the code for micro:bit at https://makecode.microbit.org/_dafDeM1zEa4o

Note: You have to open this Pen in the Debug Mode to use the Web Bluetooth API. Otherwise, the Feature Policy (implemented in Chrome 64) prevents you from connecting. See https://developers.google.com/web/updates/2018/06/feature-policy

A Pen by Shigeru Kobayashi on CodePen.

License.

Web Bluetooth with micro:bit (Acceleration)

This is a demo that illustrates a way to connect a micro:bit and a PC running the Chome web browser using the Web Bluetooth API and displays acceleration values measured by an acceleration sensor on the micro:bit as a simple 3D animation.

You can get the code for micro:bit at https://makecode.microbit.org/_dafDeM1zEa4o

Note: You have to open this Pen in the Debug Mode, since the Web Bluetooth doesn't work on cross-origin iframes.

WebBluetoothCG/web-bluetooth#180 https://bugs.chromium.org/p/chromium/issues/detail?id=518042&desc=2

A Pen by Shigeru Kobayashi on CodePen.

License.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment