-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathget_data.py
More file actions
56 lines (45 loc) · 1.47 KB
/
get_data.py
File metadata and controls
56 lines (45 loc) · 1.47 KB
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
#!/usr/bin/python
import Adafruit_DHT
import datetime
import time
#import wandb
#wandb.init(project="ceiling_temperature")
# Sensor should be set to Adafruit_DHT.DHT11,
# Adafruit_DHT.DHT22, or Adafruit_DHT.AM2302.
sensor = Adafruit_DHT.DHT22
pins = [17, 4]
path = "data_log.csv"
def get_readings(pin):
# Try to grab a sensor reading. Use the read_retry method which will retry up
# to 15 times to get a sensor reading (waiting 2 seconds between each retry).
humidity, temperature = Adafruit_DHT.read_retry(sensor, pin)
# Note that sometimes you won't get a reading and
# the results will be null (because Linux can't
# guarantee the timing of calls to read the sensor).
# If this happens try again!
if humidity is not None and temperature is not None:
now = datetime.datetime.now().strftime("%Y-%m-%d-%H-%M-%S")
return now,pin,temperature,humidity
else:
print('Failed to get reading. Try again!')
return None
# Check if data exists if it does then append else headers
from pathlib import Path
my_file = Path("/home/pi/dht_sensor/data.csv")
if not my_file.is_file():
with open(my_file, "w") as f:
f.write("date,pin,temperature,humidity\n")
while True:
for p in pins:
data = get_readings(p)
# d = {}
# d["time"] = data[0]
# d["temperature_"+str(data[1])] = data[2]
# d["humidity_"+str(data[1])] = data[3]
# wandb.log(d)
with open(my_file, "a") as f:
strdata = '{},{},{},{}'.format(*data)
f.write(strdata+"\n")
time.sleep(0.1)
time.sleep(60*30)
# time.sleep(1)