import adafruit_sdcard
import adafruit_si7021
import board
import busio
import digitalio
import math
import microcontroller
import storage
import time
# si7021 sensor
i2c = busio.I2C(board.SCL, board.SDA)
sensor = adafruit_si7021.SI7021(i2c)
# LEDs
redled = digitalio.DigitalInOut(board.D13)
redled.direction = digitalio.Direction.OUTPUT
greenled = digitalio.DigitalInOut(board.D8)
greenled.direction = digitalio.Direction.OUTPUT
# SD card
spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
cs = digitalio.DigitalInOut(board.SD_CS)
sdcard = adafruit_sdcard.SDCard(spi, cs)
vfs = storage.VfsFat(sdcard)
storage.mount(vfs, "/sd")
# calculate current Vapour-pressure deficit
def vpd(temp, rh):
# Estimated Saturation Pressures
# Saturation Vapor Pressure method 1
es1 = 0.6108 * math.exp(17.27 * temp / (temp + 237.3))
# Saturation Vapor Pressure method 2
es2 = 6.11 * 10**((7.5 * temp) / (237.3 + temp)) / 10
# Saturation Vapor Pressure method 3
es3 = 6.112 * math.exp(17.62 * temp / (temp + 243.12)) / 10
# Saturation Vapor Pressure mean
es = (es1 + es2 + es3) / 3
# actual partial pressure of water vapor in air
ea = rh / 100 * es
# return Vapour-pressure deficit
vpd = es - ea
return vpd
# write to SD card
def sdwrite(sdlog):
with open("/sd/log.txt", "a") as fp: # open to add line to log.txt
redled.value = True # turn on Red LED when SD in use
print(sdlog) # print what your about to write to SD
fp.write(sdlog) # write to SD
fp.flush() # what does this do, hope it helps????
redled.value = False # turn off Red LED when SD is done
print("Basic Logging of Vapour-pressure Deficit to filesystem")
# write at start or reset
sdwrite('\r\n')
sdwrite('*****@@@@@@---Reset---@@@@@@*****\r\n')
sdwrite('\r\n')
while True:
try:
# CPU temperature
cput = microcontroller.cpu.temperature
# write to text filesystem
sdwrite('------------------------------\r\n')
sdwrite('CPU Temp = {0:f}\r\n'.format(cput))
sdwrite('Sensor Temp: {} C'.format(sensor.temperature))
sdwrite(' {} F\r\n'.format(sensor.temperature * 1.8 + 32))
sdwrite('Humidity: {}%\r\n'.format(sensor.relative_humidity))
sdwrite('VPD = {}\r\n'.format(vpd(sensor.temperature, sensor.relative_humidity)))
sdwrite('\r\n')
# Blink Red after writing to SD
for i in range(5):
redled.value = True
time.sleep(0.2)
redled.value = False
# Blink when not in use
for i in range(540):
greenled.value = True
time.sleep(0.5)
greenled.value = False
# Blink faster when about to write
for i in range(125):
greenled.value = True
time.sleep(0.2)
greenled.value = False
# skip errors but try to print them
except OSError as oe:
print('OSError = ', oe)
time.sleep(5)
pass
except RuntimeError as re:
print('RuntimeError = ', re)
time.sleep(5)
pass