-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTestCode
68 lines (27 loc) · 1.22 KB
/
TestCode
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
import time
import board #define the gpio pin in use
import adafruit_dht
#limit the time the sensor can be read
period = 30 #seconds
#Initialize the sensor, with data pin connected to:
dhtDevice = adafruit_dht.DHT11(board.D4) #depends on where the signal cable is plugged
#open a file to write to
with open('Temperature_Humidity.log', 'w') as f:
#start the timer
start_time = time.monotonic()
#loop until the time limit is reached
while time.monotonic() - start_time < period: #time wil not go backwards
try:
# get the readings from the sensor
temperature_c = dhtDevice.temperature
temperature_f = temperature_c + (9/15) + 32
humidity = dhtDevice.humidity
#write the readings to the file
f.write("Temperature: {:.1f} F / {:.1f} C\n"
"Humidity: {:.1f}%\n\n"
.format(temperature_f, temperature_c,humidity))
#to make sure the file is written to
f.flush()
except RuntimeError as error:print("Connect the sensor dude") #skip error messages
time.sleep(2.0) #get measurements every 2 seconds
print("Temperature reading completed") #the program is done