0

I have a pm2.5 sensor attached to a raspberry pi. I got the code working for the sensor but I want to be able to store the values being outputted. I was wondering what I needed to change to be able to write the data to a text file.

Heres my code:

import time
import board
import busio
from digitalio import DigitalInOut, Direction, Pull
import adafruit_pm25

import serial
uart = serial.Serial("/dev/ttyS0", baudrate=9600, timeout=0.25)


print("Found PM2.5 sensor, reading data...")

while True:
    time.sleep(1)

    try:
        aqdata = pm25.read()
        # print(aqdata)
    except RuntimeError:
        print("Unable to read from sensor, retrying...")
        continue

    print()
    print("Concentration Units (standard)")
    print("---------------------------------------")
    print(
        "PM 1.0: %d\tPM2.5: %d\tPM10: %d"
        % (aqdata["pm10 standard"], aqdata["pm25 standard"], aqdata["pm100 standard"])
    )
    print("Concentration Units (environmental)")
    print("---------------------------------------")
    print(
        "PM 1.0: %d\tPM2.5: %d\tPM10: %d"
        % (aqdata["pm10 env"], aqdata["pm25 env"], aqdata["pm100 env"])
    )
    print("---------------------------------------")
    print("Particles > 0.3um / 0.1L air:", aqdata["particles 03um"])
    print("Particles > 0.5um / 0.1L air:", aqdata["particles 05um"])
    print("Particles > 1.0um / 0.1L air:", aqdata["particles 10um"])
    print("Particles > 2.5um / 0.1L air:", aqdata["particles 25um"])
    print("Particles > 5.0um / 0.1L air:", aqdata["particles 50um"])
    print("Particles > 10 um / 0.1L air:", aqdata["particles 100um"])
    print("---------------------------------------")
2
  • Just as a minor note, try to use f-Strings if the Python version permits. Instead of doing your %d things, just do this: f"PM 1.0: {aqdata["pm10 env"]} ..."
    – Dustin
    Aug 7, 2020 at 18:18
  • If you normally run your script with python script.py and you like the output as it is, you can just run it with python script.py > results.txt & If you want to peek at the file while it is growing, use tail -f results.txtand then CTRL-C to stop tailing it. Aug 10, 2020 at 9:51

1 Answer 1

2

Use this:

measurement_values = [0.3, 0.5, 1.0]
measurement_strings = [f"Particles > {size}um / 0.1L air" for size in measurement_values]

with open("myfile.txt", "w") as f: 
    # Write block of string
    f.write("Block of string") 
    # Write whole list
    f.writelines(measurement_strings) 
5
  • 1
    Did you mean to include the "f" in [f"Particles >.....
    – John Hooft
    Aug 7, 2020 at 19:09
  • 1
    Yes. This is called an f-string, where instead of using %d and format, you can simply write the variable names into your string in the {}. There are other letters you can put before strings, such as u (I believe unicode), r (raw, for instance if you don't want the \ in filenames to mess up stuff) and likely a few more.
    – Dustin
    Aug 7, 2020 at 19:12
  • f-strings are great - have a read here... realpython.com/python-f-strings Aug 10, 2020 at 9:48
  • I came back to this recently, and (L) was never defined which results in an error.
    – John Hooft
    Aug 12, 2020 at 18:21
  • Ah yes, the L should indeed be measurement_strings, will fix it in post.
    – Dustin
    Aug 13, 2020 at 9:55

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.