code.py
 1  # SPDX-FileCopyrightText: 2021 Kattni Rembor for Adafruit Industries
 2  # SPDX-License-Identifier: MIT
 3  """
 4  CircuitPython Essentials Storage CP Filesystem code.py file
 5  For use with boards with a built-in red LED.
 6  """
 7  import time
 8  import board
 9  import digitalio
10  import microcontroller
11  
12  led = digitalio.DigitalInOut(board.LED)
13  led.switch_to_output()
14  
15  try:
16      with open("/temperature.txt", "a") as temp_log:
17          while True:
18              # The microcontroller temperature in Celsius. Include the
19              # math to do the C to F conversion here, if desired.
20              temperature = microcontroller.cpu.temperature
21  
22              # Write the temperature to the temperature.txt file every 10 seconds.
23              temp_log.write('{0:.2f}\n'.format(temperature))
24              temp_log.flush()
25  
26              # Blink the LED on every write...
27              led.value = True
28              time.sleep(1)  # ...for one second.
29              led.value = False  # Then turn it off...
30              time.sleep(9)  # ...for the other 9 seconds.
31  
32  except OSError as e:  # When the filesystem is NOT writable by CircuitPython...
33      delay = 0.5  # ...blink the LED every half second.
34      if e.args[0] == 28:  # If the file system is full...
35          delay = 0.15  # ...blink the LED every 0.15 seconds!
36      while True:
37          led.value = not led.value
38          time.sleep(delay)