Posts: 5
Threads: 1
Joined: Feb 2018
Hello, I need help about calculating distance of ibeacon with raspberry pi, I am scanning and finding ibeacons, but I need to know how much meter between scanner and ibeacon. I found one formula but it is written by java :S I need python code, can anybody help me about it ? java code below, i have txpower and rssi, need only function for python
function calculateAccuracy(txPower, rssi) {
if (rssi === 0) {
return -1; // if we cannot determine accuracy, return -1.
}
var ratio = rssi * 1 / txPower;
if (ratio < 1.0) {
return Math.pow(ratio, 10);
} else {
return (0.89976) * Math.pow(ratio, 7.7095) + 0.111;
}
}
Posts: 8,151
Threads: 160
Joined: Sep 2016
Feb-09-2018, 02:53 PM
(This post was last modified: Feb-09-2018, 02:54 PM by buran.)
it's your lucky day today
def calculate_accuracy(txpower, rssi):
if rssi == 0:
return -1
else:
ratio = rssi/txpower
if ratio < 1:
return ratio**10
else:
return 0.89976 * ratio**7.7095 + 0.111
Posts: 5
Threads: 1
Joined: Feb 2018
i got this error :S
unsupported operand type(s) for /: 'str' and 'str'
Posts: 8,151
Threads: 160
Joined: Sep 2016
obviously your txpower and rssi are not numbers, but strings. where do you get them from? note that your original code also expects numbers - there is no type conversion
Posts: 5
Threads: 1
Joined: Feb 2018
Feb-09-2018, 09:57 PM
(This post was last modified: Feb-09-2018, 09:58 PM by aleynasarcanli.)
Code below, i want to make if in range, print beacon id (range 2 meters)
import blescan
import sys
import bluetooth._bluetooth as bluez
dev_id = 0
try:
sock = bluez.hci_open_dev(dev_id)
print "ble thread started"
except:
print "error accessing bluetooth device..."
sys.exit(1)
blescan.hci_le_set_scan_parameters(sock)
blescan.hci_enable_le_scan(sock)
def calculate_accuracy(txpower, rssi):
if rssi == 0:
return -1
else:
ratio = rssi/txpower
if ratio < 1:
return ratio**10
else:
return 0.89976 * ratio**7.7095 + 0.111
while True:
returnedList = blescan.parse_events(sock, 10)
print "----------"
for beacon in returnedList:
print beacon
beaconid = beacon.split(",")[0]
txpower = beacon.split(",")[4]
rssi = beacon.split(",")[5]
calculate_accuracy(txpower, rssi)
if calculate_accuracy < 2:
print beaconid
Posts: 8,151
Threads: 160
Joined: Sep 2016
Feb-09-2018, 10:03 PM
(This post was last modified: Feb-09-2018, 10:03 PM by buran.)
as expected - beacon is a s sting. you split it at commas, so taxpower and rssi are strings. use float() to convert them to numeric values
txpower = float(beacon.split(",")[4])
rssi = float(beacon.split(",")[5]) I believe this should make it work
Posts: 5
Threads: 1
Joined: Feb 2018
got zerodivision error
ratio = rssi/txpower
ZeroDivisionError: float division by zero
Posts: 8,151
Threads: 160
Joined: Sep 2016
Feb-09-2018, 10:10 PM
(This post was last modified: Feb-09-2018, 10:13 PM by buran.)
Actually it will not work, because of line 36-37. change lines 36-38 to
if 0 <= calculate_accuracy(txpower, rssi) < 2:
print beaconid eventually I would also replace lines#33-35 with
beacon = beacon.split(',')
beaconid = beacon[0]
txpower = float(beacon[4])
rssi = float(beacon[5])
Posts: 8,151
Threads: 160
Joined: Sep 2016
if you get divison by zero, then txpower is 0, I cannot do anything about your input data
would be interesting what beacon looks like...
Posts: 8,151
Threads: 160
Joined: Sep 2016
Feb-09-2018, 10:24 PM
(This post was last modified: Feb-09-2018, 10:25 PM by buran.)
I refactored your code
also added check that txpower is not 0 (this check is not in your original javascript code)
import blescan
import bluetooth._bluetooth as bluez
def calculate_accuracy(txpower, rssi):
if rssi == 0 or txpower == 0:
return -1
else:
ratio = rssi/txpower
if ratio < 1:
return ratio**10
else:
return 0.89976 * ratio**7.7095 + 0.111
def scan_sock(sock):
blescan.hci_le_set_scan_parameters(sock)
blescan.hci_enable_le_scan(sock)
while True:
returnedList = blescan.parse_events(sock, 10)
print "----------"
for beacon in returnedList:
print beacon
beacon = beacon.split(',')
beaconid = beacon[0]
txpower = float(beacon[4])
rssi = float(beacon[5])
if 0<= calculate_accuracy(txpower, rssi)calculate_accuracy < 2:
print beaconid
if __name__ == '__main__':
dev_id = 0
try:
sock = bluez.hci_open_dev(dev_id)
print "ble thread started"
scan(sock)
except:
print "error accessing bluetooth device..."
|