I am currently creating a program on a raspberry pi that will get the distance of the raspberry pi to a BBC Microbit. Once this Microbit has reached a distance of zero it will then move on to the next Microbit distance etc. To do this it will take it from Bluetooth via the Eddystone prototype and get a very rough distance through the rssi being converted as such.
The code I currently have works exactly as intended in mjs, however the issue I am encountering is with converting it into python where the rest of my code exists.
I was hoping someone might be able to help me with converting it, any suitable python Eddystone modules or even how to run this mjs file from inside python.
Many thanks!
Code for Microbit(JS)
bluetooth.advertiseUrl(
"https://openlab.ncl.ac.uk",
7,
false
)
basic.showString("B")
Code in MJS
import EddystoneBeaconScanner from '@abandonware/eddystone-beacon-scanner'
const filters = [
'e874af4651a7', // Put your first microbit ID in this string
'fbe2f9bfa973' // Put your second microbit ID in this string
]
const thresholds = {
close: 25,
medium: 50,
far: Infinity
}
let currentMicrobitIndex = 0
let distances = [null, null]
EddystoneBeaconScanner.on('updated', (beacon) => {
if (!filters.includes(beacon.id)) return
const distance = Math.trunc(beacon.distance * 10)
if (beacon.id === filters[currentMicrobitIndex]) {
distances[currentMicrobitIndex] = distance
const distanceText = getDistanceText(distance)
console.log(`Microbit ${currentMicrobitIndex + 1} distance: ${distanceText}`)
if (distance === 0) {
currentMicrobitIndex++
}
if (currentMicrobitIndex >= filters.length) {
console.log('You have arrived!')
EddystoneBeaconScanner.stopScanning()
}
}
})
function getDistanceText(distance) {
for (const [text, threshold] of Object.entries(thresholds)) {
if (distance <= threshold) {
return text
}
}
}
EddystoneBeaconScanner.startScanning(true)