Skip to content

Instantly share code, notes, and snippets.

@sangwon090
Last active September 5, 2021 08:34
Show Gist options
  • Save sangwon090/0b1b0311fc288b13504b22f11386e81e to your computer and use it in GitHub Desktop.
Save sangwon090/0b1b0311fc288b13504b22f11386e81e to your computer and use it in GitHub Desktop.
주변의 비콘을 스캔하고 그 비콘까지의 거리를 계산하는 코드
import time
import serial
scanner = serial.Serial('COM5', 9600)
def getBeacons(ser: serial.Serial):
beacons = []
ser.write(b'S')
res = b''
while True:
res += ser.read(ser.inWaiting())
time.sleep(0.01)
if res.endswith(b'OK+DISCE\r\n'):
break
for line in res.split(b'\r\n'):
if line == b'OK+DISCS':
continue
elif line == b'OK+DISCE':
break
else:
temp = line.decode('ascii').split(':')
if temp[0] != 'OK+DISC':
continue
factory_id = temp[1]
uuid = temp[2]
identifier = temp[3]
mac = temp[4]
rssi = int(temp[5])
beacons.append({
'factory_id': factory_id,
'uuid': uuid,
'identifier': identifier,
'mac': mac,
'rssi': rssi
})
return beacons
def getDistance(power, rssi, n):
return 10 ** ((power - rssi) / (10 * n))
#include <SoftwareSerial.h>
SoftwareSerial BluetoothSerial(2, 3);
void setup() {
Serial.begin(9600);
BluetoothSerial.begin(9600);
}
void loop() {
while(BluetoothSerial.available()) {
Serial.write(BluetoothSerial.read());
}
while(Serial.available()) {
switch(Serial.read()) {
case 'S':
BluetoothSerial.write("AT+DISI?\r\n");
break;
default:
break;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment