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("---------------------------------------")
%d
things, just do this:f"PM 1.0: {aqdata["pm10 env"]} ..."
python script.py
and you like the output as it is, you can just run it withpython script.py > results.txt &
If you want to peek at the file while it is growing, usetail -f results.txt
and thenCTRL-C
to stop tailing it.