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