Skip to content

Instantly share code, notes, and snippets.

@dhhagan
Created December 2, 2016 15:57
Show Gist options
  • Save dhhagan/93e8dcf4d7983ace9433fb0a4c7ff3b5 to your computer and use it in GitHub Desktop.
Save dhhagan/93e8dcf4d7983ace9433fb0a4c7ff3b5 to your computer and use it in GitHub Desktop.
run_opcn2_usbiss
"""
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()
@DonnaVakalis
Copy link

DonnaVakalis commented Jan 7, 2018

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:

LF.write(','.join(columns))
NameError: name 'columns' is not defined 

and

LF.write(','.join([dt, data['PM1'], data['PM25'], data['PM10']]))
KeyError: 'PM25' 

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:

LF.write(','.join([dt, data['PM1'],data['PM2.5'], data['PM10']]))
TypeError: sequence item 1: expected str instance, float found

The modified script that generated the above traceback:

import usbiss
import argparse
import sys
import os
from time import sleep
import datetime

SAMPLE_INT = 5.0
LOGFILE = os.path.join("/home/dmv/Public/Outputs", "TestRun.csv".format(datetime.datetime.utcnow().strftime("%Y%m%d")))

COLUMNS = ['DATETIME', 'PM1', 'PM2.5', '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
spi = usbiss.USBISS("/dev/ttyACM0", 'spi', spi_mode = 2, freq = 500000)

try:
    alpha = opc.OPCN2(spi, firmware=(18,2))
except Exception as e:
    print ("Startup Error: Could not set up an instance of the alpha".format(e))

alpha.on()

with open(LOGFILE, 'w') as LF:
    LF.write(','.join(COLUMNS))

    while True:
        _dt_    = datetime.datetime.utcnow().isoformat()
        _data_  = alpha.pm()

        LF.write(','.join([_dt_, _data_['PM1'],_data_['PM2.5'], _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