-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcar.py
42 lines (33 loc) · 942 Bytes
/
car.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
import obd
import signal
import sys
import asyncio
import websockets
import time
from random import randint
from threading import Thread
connection = obd.OBD("/dev/ttyUSB0")
async def hello(websocket, path):
print("connected")
cmd = obd.commands.SPEED
while True:
r = connection.query(cmd)
try:
print("ready to send")
val = str(r.value)
print(val)
await websocket.send(val)
print("sent")
except:
print("breaking")
break
start_server = websockets.serve(hello, 'localhost', 8765)
asyncio.get_event_loop().run_until_complete(start_server)
asyncio.get_event_loop().run_forever()
# the callback will now be fired upon receipt of new values
def signal_handler(signal, frame):
print('You pressed Ctrl+C!')
connection.stop()
sys.exit(0)
signal.signal(signal.SIGINT, signal_handler)
signal.pause()