This repository has been archived by the owner on Feb 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathminichallenge_pi.py
82 lines (67 loc) · 2.27 KB
/
minichallenge_pi.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
import time
import datetime
import grove.grove_ultrasonic_ranger as usr
from grove.gpio import GPIO
try:
import grove.grove_temperature_humidity_sensor as dht
except:
import seeed_dht as dht
from functions import write_to_csv, write_data_to_api, load_yaml_file
DHT_PIN = 20 # rasperry pi Pin16, Grove D16
USR_PIN = 5
BUTTON_PIN = 16
def main():
config = load_yaml_file("config.yaml")
# keep track if button has been pressed
button_pressed_prev = False
try:
# Initialize
temp_sensor = dht.DHT("11", DHT_PIN)
ultra_sonic_sensor = usr.GroveUltrasonicRanger(USR_PIN)
button = GPIO(BUTTON_PIN, GPIO.IN)
while True:
if button.read():
button_pressed_prev = True
# measurements
humidity, temperature = temp_sensor.read()
distance = ultra_sonic_sensor.get_distance()
# Print the temp_sensor values
current_time = datetime.datetime.now()
humidity = int(round(humidity))
temperature = int(round(temperature))
distance = int(round(distance))
print(
"time: ",
current_time,
"temp: ",
temperature,
"hum: ",
humidity,
"distance: ",
distance,
)
write_to_csv(
time=current_time,
temp=temperature,
hum=humidity,
dist=distance,
)
write_data_to_api(
temp=temperature,
hum=humidity,
dist=distance,
config=config,
)
# run after button has been released
elif button.read() == 0 and button_pressed_prev:
button_pressed_prev = False
print("Measurement stopped by User")
exit(0)
else:
print("Press button to start measuring!", end="\r")
time.sleep(2)
except KeyboardInterrupt:
print("\nMeasurement stopped by User")
exit(0)
if __name__ == "__main__":
main()