This repository has been archived by the owner on Jun 30, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
serialReader.py
73 lines (60 loc) · 1.82 KB
/
serialReader.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
import time
import os
import sys
import signal
import re
import json
import psycopg2
from digi.xbee.devices import XBeeDevice
from digi.xbee.exception import TimeoutException
if os.name == "windows" or os.name == "nt" :
PORT = "COM{}".format(sys.argv[1])
elif os.name == "linux" or os.name == "posix" :
PORT = "/dev/ttyS{}".format(sys.argv[1])
BAUD_RATE = 9600
xbee = XBeeDevice(PORT, BAUD_RATE)
message = None
def signal_handler(signal, frame):
xbee.close()
sys.exit(0)
signal.signal(signal.SIGINT, signal_handler)
try:
conn = psycopg2.connect(sys.argv[2])
cursor = conn.cursor()
except:
print("Unable to connect to the database")
def main():
cursor.execute('''
CREATE TABLE IF NOT EXISTS vehicledata
(
time TIMESTAMP,
rpm INTEGER,
speed INTEGER,
oilTemp REAL,
waterTemp REAL,
volt INTEGER
);
''')
conn.commit()
print(" +------------------------+")
print(" | Reading XBee data |")
print(" +------------------------+\n")
while True :
try:
if not xbee.is_open() :
xbee.open()
time.sleep(1)
message = xbee.read_data()
if message is not None :
data = json.loads(re.split(r"(?=\{)(.*?)(?<=\})", str(message.data))[1])
print(data)
cursor.execute('''
INSERT INTO vehicledata (time, rpm, speed, oiltemp, watertemp, volt)
VALUES (%s, %s, %s, %s, %s, %s)
''', (time.ctime(), data['rpm'], data['speed'], data['oilTemp'], data['waterTemp'], data['volt']))
conn.commit()
except TimeoutException as e:
print(e)
xbee.close()
if __name__ == "__main__":
main()