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