From 8334d0165aaf8d9cdad403dfd5f90dfa75482933 Mon Sep 17 00:00:00 2001 From: Ted Timmons Date: Thu, 23 Mar 2023 11:31:49 -0700 Subject: [PATCH] add sleep/wake so the sensor (fan) can be shut off included a long-running example. tested with a branch install: sudo -H pip3 install 'git+https://github.com/tedder/pms5003-python.git@tedder/allow_shutdown#subdirectory=library' --- examples/hourly.py | 50 +++++++++++++++++++++++++++++++++++++ library/pms5003/__init__.py | 9 +++++++ 2 files changed, 59 insertions(+) create mode 100755 examples/hourly.py diff --git a/examples/hourly.py b/examples/hourly.py new file mode 100755 index 0000000..880b35b --- /dev/null +++ b/examples/hourly.py @@ -0,0 +1,50 @@ +#!/usr/bin/env python + +from pms5003 import PMS5003 +import time + +print("""hourly.py - Run (approximately) hourly. + +Press Ctrl+C to exit! + +""") + +# Configure the PMS5003 for Enviro+ +pms5003 = PMS5003( + device='/dev/ttyAMA0', + baudrate=9600, + pin_enable=22, + pin_reset=27 +) + +last_read_epoch = 0 + +try: + while True: + # has it been an hour since our last reading? + now = int(time.time()) + if (now - last_read_epoch) > 3600: + last_read_epoch = now + + print("waking") + pms5003.wake() + + # give sensor time to wake up and calibrate + print("sleeping") + time.sleep(60) + print("reading") + + # take several readings, good for a median + for _ in range(9): + data = pms5003.read() + print("PM2.5 ug/m3 (combustion particles, organic compounds, metals): {}".format(data.pm_ug_per_m3(2.5))) + + # done reading, shut down sensor (and its fan) + print("sleeping") + pms5003.sleep() + else: + time.sleep(10) + +except KeyboardInterrupt: + pass + diff --git a/library/pms5003/__init__.py b/library/pms5003/__init__.py index 4941016..1fa06ce 100644 --- a/library/pms5003/__init__.py +++ b/library/pms5003/__init__.py @@ -105,6 +105,15 @@ def setup(self): self.reset() + def sleep(self): + '''Turn off enable (SET). This stops the fan, aka sleeping + mode. Note this is different than active/passive.''' + GPIO.output(self._pin_enable, GPIO.LOW) + + def wake(self): + '''Enable (SET). This starts the fan and sensor. Wait 30sec for values.''' + GPIO.output(self._pin_enable, GPIO.HIGH) + def reset(self): time.sleep(0.1) GPIO.output(self._pin_reset, GPIO.LOW)