bme680.py 2.99 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
import sys_bme680
import ucollections

# Import old module for compatibility
from sys_bme680 import *  # noqa

Bme680Data = ucollections.namedtuple(
    "Bme680Data", ["temperature", "humidity", "pressure", "gas_resistance"]
)


class Bme680:
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
    """
    BME680 4-in-1 environmental sensor.

    **Example**:

    .. code-block:: python

        import bme680

        environment = bme680.Bme680()
        print("Current temperature: {:4.1f} °C".format(environment.temperature()))

    This class can also be used as a context-manager which will automatically
    deactivate the sensor on exit:

    .. code-block:: python

        import bme680

        with bme680.Bme680() as environment:
            print("H: {:4.1f}%".format(environment.humidity()))

        # Sensor is off again, saving power

    .. versionadded:: 1.10
    """

40
41
42
43
44
45
46
47
48
49
    def __init__(self):
        sys_bme680.init()

    def __enter__(self):
        return self

    def __exit__(self, *args):
        self.close()

    def get_data(self):
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
        """
        Get all sensor data at once.

        :py:meth:`~bme680.Bme680.get_data` returns a namedtuple with the
        following fields:

        - ``temperature``: Temperature in *°C*
        - ``humidity``: Relative humidity
        - ``pressure``: Barometric pressure in *hPa*
        - ``gas_resistance``: Gas resistance in *Ω*

        **Example**:

        .. code-block:: python

            import bme680

            with bme680.Bme680() as environment:
                data = environment.get_data()

                print("T: {}".format(data.temperature))
                print("H: {}".format(data.humidity))
        """
73
74
75
        return Bme680Data(*sys_bme680.get_data())

    def close(self):
76
77
78
79
80
81
        """
        Stop/deinit the BME680.

        If you no longer need measurements, you should call this function to
        save power.
        """
82
83
84
        sys_bme680.deinit()

    def temperature(self):
85
86
87
88
89
90
91
92
93
94
        """
        Measure current temperature in *°C*.

        **Example**:

        .. code-block:: python

            environment = bme680.Bme680()
            print(str(environment.temperature()))
        """
95
96
97
        return self.get_data().temperature

    def humidity(self):
98
99
100
101
102
103
104
105
106
107
        """
        Measure current relative humidity.

        **Example**:

        .. code-block:: python

            environment = bme680.Bme680()
            print(str(environment.humidity()))
        """
108
109
110
        return self.get_data().humidity

    def pressure(self):
111
112
113
114
115
116
117
118
119
120
        """
        Measure current barometric pressure in *hPa*.

        **Example**:

        .. code-block:: python

            environment = bme680.Bme680()
            print(str(environment.pressure()))
        """
121
122
123
        return self.get_data().pressure

    def gas_resistance(self):
124
125
126
127
128
129
130
131
132
133
        """
        Measure current gas resistance in *Ω*.

        **Example**:

        .. code-block:: python

            environment = bme680.Bme680()
            print(str(environment.gas_resistance()))
        """
134
        return self.get_data().gas_resistance