Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python Ble Distance Problem
#1
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;
  }
}
Reply
#2
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
Reply
#3
i got this error :S

unsupported operand type(s) for /: 'str' and 'str'
Reply
#4
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
Reply
#5
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
Reply
#6
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
Reply
#7
got zerodivision error

ratio = rssi/txpower
ZeroDivisionError: float division by zero
Reply
#8
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])
Reply
#9
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...
Reply
#10
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..."
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Visualize Geo Map/Calculate distance zarize 1 1,900 Dec-05-2019, 08:36 PM
Last Post: Larz60+
  euclidean distance jenya56 3 2,829 Mar-29-2019, 02:56 AM
Last Post: scidam
  How to use servo motor with TFMini Distance Sensor in python script? programerguy 1 3,255 Dec-04-2018, 04:57 PM
Last Post: Larz60+
  organizing by distance gonzo620 7 3,941 Oct-16-2018, 01:41 AM
Last Post: stullis

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020