|
| 1 | +# The MIT License (MIT) |
| 2 | +# |
| 3 | +# Copyright (c) 2020 ladyada for Adafruit Industries |
| 4 | +# |
| 5 | +# Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 | +# of this software and associated documentation files (the "Software"), to deal |
| 7 | +# in the Software without restriction, including without limitation the rights |
| 8 | +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 | +# copies of the Software, and to permit persons to whom the Software is |
| 10 | +# furnished to do so, subject to the following conditions: |
| 11 | +# |
| 12 | +# The above copyright notice and this permission notice shall be included in |
| 13 | +# all copies or substantial portions of the Software. |
| 14 | +# |
| 15 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 18 | +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 | +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 21 | +# THE SOFTWARE. |
| 22 | +""" |
| 23 | +`adafruit_pm25.i2c` |
| 24 | +================================================================================ |
| 25 | +
|
| 26 | +I2C module for CircuitPython library for PM2.5 Air Quality Sensors |
| 27 | +
|
| 28 | +
|
| 29 | +* Author(s): ladyada |
| 30 | +
|
| 31 | +Implementation Notes |
| 32 | +-------------------- |
| 33 | +
|
| 34 | +**Hardware:** |
| 35 | +
|
| 36 | +Works with most (any?) Plantower I2C interfaced PM2.5 sensor. |
| 37 | +
|
| 38 | +**Software and Dependencies:** |
| 39 | +
|
| 40 | +* Adafruit CircuitPython firmware for the supported boards: |
| 41 | + https://github.com/adafruit/circuitpython/releases |
| 42 | +* Adafruit's Bus Device library: https://github.com/adafruit/Adafruit_CircuitPython_BusDevice |
| 43 | +
|
| 44 | +""" |
| 45 | + |
| 46 | +# imports |
| 47 | +import time |
| 48 | +from digitalio import Direction |
| 49 | +from adafruit_bus_device.i2c_device import I2CDevice |
| 50 | +from . import PM25 |
| 51 | + |
| 52 | + |
| 53 | +class PM25_I2C(PM25): |
| 54 | + """ |
| 55 | + A module for using the PM2.5 Air quality sensor over I2C |
| 56 | + """ |
| 57 | + |
| 58 | + def __init__(self, i2c_bus, reset_pin=None, address=0x12): |
| 59 | + if reset_pin: |
| 60 | + # Reset device |
| 61 | + reset_pin.direction = Direction.OUTPUT |
| 62 | + reset_pin.value = False |
| 63 | + time.sleep(0.01) |
| 64 | + reset_pin.value = True |
| 65 | + # it takes at least a second to start up |
| 66 | + time.sleep(1) |
| 67 | + |
| 68 | + for _ in range(5): # try a few times, it can be sluggish |
| 69 | + try: |
| 70 | + self.i2c_device = I2CDevice(i2c_bus, address) |
| 71 | + break |
| 72 | + except ValueError: |
| 73 | + time.sleep(1) |
| 74 | + continue |
| 75 | + else: |
| 76 | + raise RuntimeError("Unable to find PM2.5 device") |
| 77 | + super().__init__() |
| 78 | + |
| 79 | + def _read_into_buffer(self): |
| 80 | + with self.i2c_device as i2c: |
| 81 | + try: |
| 82 | + i2c.readinto(self._buffer) |
| 83 | + except OSError as err: |
| 84 | + raise RuntimeError("Unable to read from PM2.5 over I2C") from err |
0 commit comments