Skip to content

Commit 402a594

Browse files
committed
Updated bluetooth code
1 parent de30365 commit 402a594

File tree

4 files changed

+190
-43
lines changed

4 files changed

+190
-43
lines changed

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,5 @@ Project\ Logs
4444

4545
## Ignore ANSYS Electronics files
4646
*.aedtresults
47-
*.asol
47+
*.asol
48+
MotorDriver/Embedded/Software/app/__pycache__/siteContent.cpython-38.pyc

MotorDriver/Embedded/Software/app/bluetooth.py

+35-42
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import serial
21
import time
32
import psycopg2
43

@@ -133,50 +132,44 @@ def updateValues(cur, currentId):
133132
'''Layout for the web app'''
134133
bluetooth.layout = dbc.Container(
135134

136-
children = [
137-
138-
html.Div(
139-
html.Div(
140-
children = [
141-
html.H1('IPT Car Live Updates'),
142-
html.H4('UoA FoE Summer Workshop 2020'),
143-
]
144-
),
145-
style = {
146-
'fontFamily': 'Verdana, sans-serif',
147-
'fontSize': '2vw',
148-
'fontWeight': 'bold',
149-
'padding': '2vw 0vw 2vw 0vw',
150-
'backgroundColor': headerColor,
151-
'textAlign': 'left'
152-
}
153-
),
154-
155-
html.Div(
156-
dbc.Row( [
157-
dbc.Col(
158-
dcc.Graph(
159-
id = 'carData',
160-
animate = False
161-
),
162-
width = 9
163-
),
164-
dbc.Col(
135+
children = [
136+
137+
html.Div(
138+
dbc.Container(
139+
dbc.Jumbotron(
165140
html.Div(
166-
id = 'direction'
167-
),
168-
width = 3
141+
children = [
142+
html.H1('RC Car Live Updates', className = "display-3"),
143+
html.Hr(className = "my-2", style = {"margin-right": "50vw"}),
144+
html.P('UoA FoE Summer Workshop 2020', className = "lead", style = {"margin-top": "2vw"}),
145+
], style = {"margin-left": "2vw"} ), fluid = True, style = {"backgroundColor": "green !important"}),
146+
fluid = True),
147+
),
148+
149+
html.Div(
150+
151+
dbc.Row(
152+
[
153+
dbc.Col( dcc.Graph( id = 'carData', animate = False), width = 10),
154+
dbc.Col( children = [
155+
dbc.Row(dbc.Card( children = [dbc.CardHeader("System Information:"), dbc.CardBody(id = 'direction', className = "card-title")], color = uoaDarkBlue, inverse = True, outline = True), style = { "margin-top": "6vw"}),
156+
], width = 2),
157+
158+
159+
],
169160
),
170-
],
171161
),
172-
),
173-
174-
dcc.Interval(
175-
id='updateGraph',
176-
interval = 1*1000,
177-
n_intervals = 0
178-
),
179-
],
162+
163+
164+
165+
dcc.Interval(
166+
id='updateGraph',
167+
interval = 1*1000,
168+
n_intervals = 0
169+
),
170+
171+
],
172+
fluid = True, style = {"backgroundColor" : "#FAFAFA"},
180173
)
181174

182175
@bluetooth.callback(Output('direction', 'children'),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import bleak
2+
import asyncio
3+
from bleak import BleakClient, BleakScanner
4+
5+
characteristicUUID = '0000ffe1-0000-1000-8000-00805f9b34fb'
6+
macUUID = 'D3FBCEE5-A512-45F4-B322-971D41E18A6C'
7+
name = 'A52'
8+
9+
10+
async def run():
11+
async with BleakScanner() as scanner:
12+
await asyncio.sleep(5.0)
13+
devices = await scanner.get_discovered_devices()
14+
for d in devices:
15+
if ((d.name).upper() != ("UNKNOWN")):
16+
print("Name: ", (d.name))
17+
print("Address: ", d.address)
18+
print("UUIDS: ", d.metadata['uuids'])
19+
20+
loop = asyncio.get_event_loop()
21+
loop.run_until_complete(run())
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
import bleak
2+
import asyncio
3+
import psycopg2
4+
from bleak import BleakClient, BleakScanner
5+
6+
'''set up BLE address, characteristic UUID, and database query text'''
7+
global characteristicUUID, macUUID, updateCarVCS, updateCarDS
8+
9+
characteristicUUID = '0000ffe1-0000-1000-8000-00805f9b34fb'
10+
macUUID = 'D3FBCEE5-A512-45F4-B322-971D41E18A6C'
11+
12+
updateCarVCS = """ UPDATE carVCS
13+
SET id = %s,
14+
voltage = %s,
15+
current = %s
16+
WHERE pos = %s
17+
"""
18+
19+
updateCarDS = """ update carDS
20+
SET direction = %s,
21+
speedGrade = %s
22+
where id = 0
23+
"""
24+
25+
def openDatabaseConnection():
26+
con = None
27+
28+
try:
29+
con = psycopg2.connect(database = "d4odufthffmam6", user = "rhahsgujdzpjxa", port = "5432",
30+
password = "2fe675581f9680a9958f466cd1953b7fecaea654271a9d6f614250e36bf389fc",
31+
host = "ec2-54-164-233-77.compute-1.amazonaws.com")
32+
except Exception as err:
33+
print("Couldn't connect to database")
34+
35+
return con
36+
37+
def decode(string):
38+
39+
validInput = False
40+
41+
if (string[0] == 'M' and string[len(string) - 1] == 'N'):
42+
validInput = True
43+
string = string[1:(len(string) - 1)]
44+
45+
if validInput:
46+
listOfUnits = ['V', 'A', 'D', 'G']
47+
48+
indexList = []
49+
calculations = []
50+
51+
index = 0
52+
count = 0
53+
54+
for unit in listOfUnits:
55+
56+
index = string.find(unit)
57+
58+
calculations.append(string[:(index)])
59+
string = string[(index + 1):len(string)]
60+
61+
count = count + 1
62+
63+
return calculations
64+
65+
else:
66+
print("Invalid output")
67+
return -1
68+
69+
def processData(sender, data):
70+
global pos, maxID
71+
72+
print(str(data))
73+
'''Figure out how to translate data from NSlineData to python string'''
74+
stringToDecode = str(data)
75+
stringToDecode = stringToDecode[2:-5]
76+
print(stringToDecode)
77+
78+
updateString = decode(stringToDecode)
79+
80+
if (updateString != -1):
81+
pos = pos + 1
82+
83+
if pos == 10:
84+
pos = 0
85+
86+
maxID = maxID + 1
87+
88+
'''change indexes when speed calculation has been done'''
89+
cur.execute(updateCarVCS, (maxID, updateString[0], updateString[1], pos))
90+
cur.execute(updateCarDS, (updateString[2], updateString[3]))
91+
92+
con.commit()
93+
94+
95+
async def run(address):
96+
async with BleakClient(macUUID) as client:
97+
x = await client.is_connected()
98+
while True:
99+
await client.start_notify(characteristicUUID, processData)
100+
await asyncio.sleep(2.0)
101+
await client.stop_notify(characteristicUUID)
102+
103+
client.disconnect()
104+
105+
global pos, maxID
106+
'''connect to database/bluetooth'''
107+
con = openDatabaseConnection()
108+
109+
cur = con.cursor()
110+
111+
cur.execute(""" SELECT MAX(id) from carVCS """)
112+
113+
'''store the last id that was created'''
114+
maxID = cur.fetchone()
115+
116+
maxID = maxID[0]
117+
118+
cur.execute("""
119+
SELECT pos FROM carVCS WHERE id = %s
120+
""", (maxID, ))
121+
122+
'''select and store the position of the highest id'''
123+
output = cur.fetchone()
124+
125+
pos = output[0]
126+
127+
if __name__ == "__main__":
128+
loop = asyncio.get_event_loop()
129+
loop.run_until_complete(run(macUUID))
130+
'''Close bluetooth connections'''
131+
cur.close()
132+
con.close()

0 commit comments

Comments
 (0)