added logic to identify if datastore exists before running

This commit is contained in:
2023-08-27 12:54:49 -05:00
parent f7b3d250c9
commit b11b455903
3 changed files with 39 additions and 2 deletions
+2
View File
@@ -0,0 +1,2 @@
speed = str(round(machine.freq()/1000000,1))
print("The starting speed is",speed,"MHz")
+2 -2
View File
@@ -2,8 +2,8 @@ import array
import micropython
import utime
from machine import Pin
from micropython import const
from micropython import const
class InvalidChecksum(Exception):
pass
+35
View File
@@ -0,0 +1,35 @@
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()