Last active
August 26, 2021 02:55
-
-
Save oshinko/8bd90fcaca59e99d137d2c62d6ef4214 to your computer and use it in GitHub Desktop.
Broadcast the sensor value on Eddystone (BLE) on micro:bit
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const NAMESPACE = [ // sha1('your.doma.in').slice(0, 10) | |
0x8f, 0xa3, 0xfa, 0xa1, | |
0xb4, 0xa3, 0x99, 0x67, | |
0x0c, 0x0f | |
] | |
const DEVICE = 0x0000 // 2 bytes | |
const POWER = 7 // no reason | |
const CONNECTABLE = false // no reason | |
// Returns depend on the role of the device | |
function getCurrentNumber() { | |
return input.acceleration(Dimension.Strength) | |
// return input.temperature() | |
} | |
function peekIcon(icon: number) { | |
basic.showIcon(icon) | |
basic.pause(1000) | |
basic.clearScreen() | |
} | |
// Show an icon at startup | |
peekIcon(IconNames.Heart) | |
// Show an icon when the A button is pressed | |
input.onButtonPressed(Button.A, () => peekIcon(IconNames.Heart)) | |
// Show value when the B button is pressed | |
input.onButtonPressed(Button.B, () => basic.showNumber(getCurrentNumber())) | |
// Broadcasting | |
basic.forever(() => { | |
const buffer = pins.createBuffer(16) | |
NAMESPACE.forEach((byte, i) => buffer.setNumber(NumberFormat.UInt8BE, i, byte)) | |
buffer.setNumber(NumberFormat.UInt16BE, 10, DEVICE) | |
buffer.setNumber(NumberFormat.UInt32BE, 12, getCurrentNumber()) | |
bluetooth.advertiseUidBuffer(buffer, POWER, CONNECTABLE) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment