35 lines
841 B
Python
35 lines
841 B
Python
from machine import Pin, I2C
|
|
import utime as time
|
|
from dht import DHT11, InvalidChecksum
|
|
import gc
|
|
|
|
gc.enable()
|
|
|
|
#Initilialize data store
|
|
try:
|
|
f = open("data.csv","r")
|
|
f.close()
|
|
print("Found Datastore...")
|
|
except OSError: # open failed
|
|
print("Creating Datastore...")
|
|
f = open("data.csv","w")
|
|
f.write("id,temp,hum\n")
|
|
f.close()
|
|
print("Done")
|
|
|
|
|
|
#Main Program
|
|
while True:
|
|
#Measure DHT22
|
|
print("Getting Sensor Values...")
|
|
time.sleep(5)
|
|
pin = Pin(28, Pin.OUT, Pin.PULL_DOWN)
|
|
sensor = DHT11(pin)
|
|
t = (sensor.temperature)
|
|
h = (sensor.humidity)
|
|
print("SensorID: 1, Temperature: {}°C, Humidity: {:.0f}%\nWriting...".format(sensor.temperature, sensor.humidity))
|
|
f = open("data.csv","a")
|
|
f.write("1,"+str(t)+","+str(h)+"\n")
|
|
f.close()
|
|
print("Done")
|
|
gc.collect() |