Python Ble Distance Problem - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Python Ble Distance Problem (/thread-8204.html) Pages:
1
2
|
Python Ble Distance Problem - aleynasarcanli - Feb-09-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; } } RE: Python Ble Distance Problem - buran - Feb-09-2018 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 RE: Python Ble Distance Problem - aleynasarcanli - Feb-09-2018 i got this error :S unsupported operand type(s) for /: 'str' and 'str' RE: Python Ble Distance Problem - buran - Feb-09-2018 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 RE: Python Ble Distance Problem - aleynasarcanli - Feb-09-2018 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 RE: Python Ble Distance Problem - buran - Feb-09-2018 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 RE: Python Ble Distance Problem - aleynasarcanli - Feb-09-2018 got zerodivision error ratio = rssi/txpower ZeroDivisionError: float division by zero RE: Python Ble Distance Problem - buran - Feb-09-2018 Actually it will not work, because of line 36-37. change lines 36-38 to if 0 <= calculate_accuracy(txpower, rssi) < 2: print beaconideventually I would also replace lines#33-35 with beacon = beacon.split(',') beaconid = beacon[0] txpower = float(beacon[4]) rssi = float(beacon[5]) RE: Python Ble Distance Problem - buran - Feb-09-2018 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... RE: Python Ble Distance Problem - buran - Feb-09-2018 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..." |