Created
December 2, 2016 15:57
-
-
Save dhhagan/93e8dcf4d7983ace9433fb0a4c7ff3b5 to your computer and use it in GitHub Desktop.
run_opcn2_usbiss
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
This script runs the Alphasense OPC-N2 via the USB-ISS adapter and logs | |
every X seconds | |
Written by David H Hagan | Dec. 2016 | |
Contact: [email protected] | |
""" | |
import usbiss | |
import opc | |
import argparse | |
import sys | |
import os | |
from time import sleep | |
import datetime | |
# DEFAULTS | |
SAMPLE_INT = 2.0 | |
LOGFILE = os.path.join("/home/pi/Desktop", "{}.csv".format(datetime.datetime.utcnow().strftime("%Y%m%d"))) | |
COLUMNS = ['DATETIME', 'PM1', 'PM25', 'PM10'] | |
parser = argparse.ArgumentParser( description = 'Log data from the OPC-N2') | |
parser.add_argument('--int', help = 'Define the log interval in seconds') | |
if __name__ == '__main__': | |
# Parse the args | |
args = parser.parse_args() | |
if args.int: | |
SAMPLE_INT = int | |
# Set up the instance | |
cnxn = usbiss.USBISS('/dev/ttyACM0', 'spi', spi_mode = 2, freq = 500000) | |
try: | |
alpha = opc.OPCN2(cnxn) | |
except Exception as e: | |
sys.exit(e) | |
alpha.on() | |
with open(LOGFILE, 'w') as LF: | |
LF.write(','.join(columns)) | |
while True: | |
_dt_ = datetime.datetime.utcnow().isoformat() | |
_data_ = alpha.pm() | |
# Log to file | |
LF.write(','.join([_dt_, _data_['PM1'], _data_['PM25'], _data_['PM10']])) | |
sleep(SAMPLE_INT) | |
LF.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi @dhhagan,
Here are some things (previously, mistakenly, posted here: ) that I noticed in the code, but I'm not sure about my interpretation.
Preamble
For me, when running the run_opcn2_usbiss.py example as-is resulted in these two error messages, respectively:
and
It appears that in the alpha.pm() code, the column is titled 'PM2.5' with a period e.g.,
data['PM2.5'] = self._calculate_float(resp[4:8])
but the the script uses 'PM25' e.g.,
LF.write(','.join([dt, data['PM1'], data['PM25'], data['PM10']]))
and the definition of caplocks COLUMNS but call using columns, e.g.,
COLUMNS = ['DATETIME', 'PM1', 'PM25', 'PM10']
and then later
LF.write(','.join(columns))
Question re:changes, and interpreting subsequent error message (re: expected string but got a float...)
So I've adapted your example with the tiny exception of changing 25 to 2.5 and columns to COLUMNS...
and run the whole things...
and run into the error
"TypeError: sequence item 1: expected str instance, float found"
The traceback:
The modified script that generated the above traceback: