User avatar
aleynasarcanli
Posts: 14
Joined: Mon Jun 29, 2015 7:29 am

Python Ble Distance Problem

Fri Feb 09, 2018 1:08 pm

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

Code: Select all

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;
  }
}

pcmanbob
Posts: 9464
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Python Ble Distance Problem

Fri Feb 09, 2018 2:58 pm

I don't do java but the maths should be the same and python has a math.pow function to.

So some thing like this will take care of the if/else statements

Code: Select all

def calculateAccuracy(txPower, rssi):
    if rssi ==0:
        print "we cannot determine accuracy"
        return -1
    else:
        ratio = (rssi *1) / txPower
        if ratio < 1.0:
            result = Math.pow(ratio, 10)
            return result
        else:
            result = (0.89976 * (Math.pow(ratio, 7.7095))) + 0.111
            return result
        
this is untested code so don't be surprised if it throws an error.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

User avatar
aleynasarcanli
Posts: 14
Joined: Mon Jun 29, 2015 7:29 am

Re: Python Ble Distance Problem

Fri Feb 09, 2018 8:31 pm

i got this error :S

unsupported operand type(s) for /: 'str' and 'str'

pcmanbob
Posts: 9464
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Python Ble Distance Problem

Fri Feb 09, 2018 8:46 pm

aleynasarcanli wrote:
Fri Feb 09, 2018 8:31 pm
i got this error :S

unsupported operand type(s) for /: 'str' and 'str'
Error indicates you are trying to apply maths function +_*/ to a string.

it would help if you post the code and the exact error as it often includes a line number.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

User avatar
aleynasarcanli
Posts: 14
Joined: Mon Jun 29, 2015 7:29 am

Re: Python Ble Distance Problem

Fri Feb 09, 2018 9:55 pm

Code below, i want to make if in range, print beacon id (range 2 meters)

Code: Select all

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

pcmanbob
Posts: 9464
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Python Ble Distance Problem

Fri Feb 09, 2018 10:06 pm

Error message would have helped, but my guess would be that these lines are returning strings not numbers.

Code: Select all

	txpower = beacon.split(",")[4]
	rssi = beacon.split(",")[5]
so you need to convert them from string to number but would need to see some examples of the data retuned to decide of you need to use int or float for the conversion.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

User avatar
aleynasarcanli
Posts: 14
Joined: Mon Jun 29, 2015 7:29 am

Re: Python Ble Distance Problem

Fri Feb 09, 2018 10:11 pm

Tried but I got zerodivision error.

Code: Select all

txpower = float(beacon.split(",")[4])
rssi = float(beacon.split(",")[5])
ratio = rssi/txpower
ZeroDivisionError: float division by zero

pcmanbob
Posts: 9464
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Python Ble Distance Problem

Fri Feb 09, 2018 10:19 pm

Again why not just post the full error message it tells you in which line the error occurred.

so you need to do some testing you need to change your program so it just prints out the returned data of txpower and rssi

then try converting it from string to number without doing the calculate bit of the program once you get that bit working, then add the calculate bit back in.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

User avatar
aleynasarcanli
Posts: 14
Joined: Mon Jun 29, 2015 7:29 am

Re: Python Ble Distance Problem

Fri Feb 09, 2018 10:51 pm

I will search, thanks for all your help :)

pcmanbob
Posts: 9464
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Python Ble Distance Problem

Fri Feb 09, 2018 11:14 pm

whats wrong with posting the full error message ?

error messages give lots of help including the line the error occurred in.

so posting all the error message and your code will help us locate the error .

when doing some thing like this were others may not be able to test run you code it always helps if you post some examples of data returned in this case txpower and rssi are good examples.

then at lease we can simulate your code ruining and test for errors

you have to help us to help your you..

from the limited error message I suspect some were in your code you try to divide by zero so I would guess one of your data strings is zero , so you need to trap this and not run the calculations is this data = zero.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

Return to “Python”