-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
94 lines (69 loc) · 2.37 KB
/
main.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import argparse
import re
from datetime import datetime
from re import Pattern
from influxdb_client import InfluxDBClient, Point
from serial import Serial, SerialException
import config
RE_PATTERN: Pattern = re.compile(r'(\d+)\|(\d+\.\d+)\|(\d+\.\d+)\|(\d+\.\d+)')
parser = argparse.ArgumentParser()
serial: Serial
client = InfluxDBClient(f"http://{config.HOST}:{config.PORT}", config.TOKEN, org=config.ORG, bucket=config.BUCKET)
write_api = client.write_api()
def sync_time():
print("Syncing time with Arduino")
serial.timeout = 3
while True:
serial.write(f"TIME|{datetime.utcnow().timestamp()}".encode())
data = serial.readline().decode().strip()
if data == 'SYNCED':
break
serial.timeout = None
def save_data(u_time: int, temp: float, hum: float, index: float):
point = Point(config.NAME) \
.field('temperature', temp) \
.field('humidity', hum) \
.field('heat_index', index) \
.time(datetime.fromtimestamp(u_time))
write_api.write(
config.BUCKET,
config.ORG,
point
)
def start():
print("Starting loop")
while True:
try:
data = serial.readline().decode().strip()
if not (match := RE_PATTERN.match(data)):
print("Invalid data")
continue
u_time, temp, hum, index = map(float, match.groups())
u_time = int(u_time)
if args.verbose:
print(f"{u_time} | Temperature: {temp}°C | Humidity: {hum}% | Heat Index: {index}°C")
save_data(u_time, temp, hum, index)
except KeyboardInterrupt:
print("Exiting ...")
break
except SerialException:
print("Something went wrong. Check connections and run this script again.")
break
client.close()
write_api.close()
serial.close()
def main():
try:
global serial
serial = Serial(args.port, args.baud)
except SerialException:
print("couldn't open this port")
return
sync_time()
start()
if __name__ == '__main__':
parser.add_argument('port', type=str, help='Serial port')
parser.add_argument('baud', type=int, help='Baud rate')
parser.add_argument('-v', '--verbose', type=bool, action=argparse.BooleanOptionalAction, help='Verbose mode')
args = parser.parse_args()
main()