forked from Joshua-Newman/SmartDogCollar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
distanceTest.py
76 lines (67 loc) · 2.29 KB
/
distanceTest.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import I2C_LCD_driver
import rssi
from time import sleep
import board
import busio
import adafruit_lsm9ds1
# I2C connection:
i2c = busio.I2C(board.SCL, board.SDA)
sensor = adafruit_lsm9ds1.LSM9DS1_I2C(i2c)
interface = 'wlan0'
rssi_scanner = rssi.RSSI_Scan(interface)
ssids = ['pi-mobile']
# sudo argument automatixally gets set for 'false', if the 'true' is not set manually.
# python file will have to be run with sudo privileges.
# Variables from power regression of RSSI data
# distance=A*(r/t)^B+C
# where r is the RSSI value and t is the RSSI at 1m
A = 0.812080813887844
B = 3.63550212364731
C = 0.1879191861
t = -37
mylcd = I2C_LCD_driver.lcd()
rollingaverage = []
while True:
# get Gyro data
accel_x, accel_y, accel_z = sensor.acceleration
mag_x, mag_y, mag_z = sensor.magnetic
gyro_x, gyro_y, gyro_z = sensor.gyro
# get RSSI data
ap_info = rssi_scanner.getAPinfo(networks=ssids, sudo=True)
if(ap_info != False):
name = ap_info[0]["ssid"]
signal = ap_info[0]["signal"]
rollingaverage.append(signal)
if len(rollingaverage) == 60:
rollingaverage.pop(0)
rssi_average = sum(rollingaverage)/len(rollingaverage)
distance = A*(rssi_average/t)**B + C
# Write RSSI info
mylcd.lcd_clear()
mylcd.lcd_display_string(u"SSID: {}".format(name), 1)
mylcd.lcd_display_string(u"RSSI: {} dBm".format(signal), 2)
sleep(2)
mylcd.lcd_clear()
mylcd.lcd_display_string(u"RSSI (avg): {} dBm".format(rssi_average), 1)
mylcd.lcd_display_string(u"Distance: {} m".format(distance), 2)
sleep(2)
else:
mylcd.lcd_clear()
mylcd.lcd_display_string(u"OUT OF RANGE", 1)
sleep(2)
# Write Gyro info
mylcd.lcd_clear()
mylcd.lcd_display_string(u"Accel:({0:0.3f}x,".format(accel_x), 1)
mylcd.lcd_display_string(
u"{0:0.3f}y,{0:0.3f}z".format(accel_y, accel_z), 2)
sleep(2)
mylcd.lcd_clear()
mylcd.lcd_display_string(u"Mag: ({0:0.3f}x)".format(mag_x), 1)
mylcd.lcd_display_string(
u"{0:0.3f}y,{0:0.3f}z".format(mag_y, mag_z), 2)
sleep(2)
mylcd.lcd_clear()
mylcd.lcd_display_string(u"Gyro: ({0:0.3f}x)".format(gyro_x), 1)
mylcd.lcd_display_string(
u"{0:0.3f}y,{0:0.3f}z".format(gyro_y, gyro_z), 2)
sleep(2)