|
| 1 | +# SPDX-FileCopyrightText: 2022 @Skicka for Adafruit Industries / Hakcat |
| 2 | +# Logging data to .CSV file on CircuitPython Disk |
| 3 | +# SPDX-License-Identifier: MIT |
| 4 | + |
| 5 | +# If you get a read-only filesystem error, add "storage.remount('/', False)" in boot.py |
| 6 | +# Make sure you add a way to reverse this in boot.py or your CP device won't show up via USB |
| 7 | +# See example below: |
| 8 | +# https://learn.adafruit.com/getting-started-with-raspberry-pi-pico-circuitpython/data-logger |
| 9 | + |
| 10 | +import os |
| 11 | +import random |
| 12 | +import circuitpython_csv as csv |
| 13 | + |
| 14 | +# Check if .CSV file is already present. If not, we write CSV headers. |
| 15 | +all_files = os.listdir() ## List all files in directory |
| 16 | +if "datelog.csv" not in all_files: |
| 17 | + with open("datelog.csv", mode="w", encoding="utf-8") as writablefile: |
| 18 | + csvwriter = csv.writer(writablefile) |
| 19 | + csvwriter.writerow(["Year", "Month", "Day", "Hour", "Minute"]) |
| 20 | + |
| 21 | +# Now that the file exists (or already did) we make a random date |
| 22 | +year = random.randint(1999, 2022) |
| 23 | +month = random.randint(1, 12) |
| 24 | +day = random.randint(1, 30) |
| 25 | +hour = random.randint(0, 23) |
| 26 | +minute = random.randint(0, 60) |
| 27 | + |
| 28 | +# We append this to the .CSV file |
| 29 | +with open("datelog.csv", mode="a", encoding="utf-8") as writablefile: |
| 30 | + csvwriter = csv.writer(writablefile) |
| 31 | + csvwriter.writerow([year, month, day, hour, minute]) |
| 32 | + |
| 33 | +# Finally, we try to read back the last line in the CSV file to make sure it wrote. |
| 34 | +with open("datelog.csv", "r", encoding="utf-8") as file: |
| 35 | + data = file.readlines() |
| 36 | + print(data[-1]) |
0 commit comments