13. API - Environment Sensors¶
The Sense HAT has two environment sensors: a humidity sensor and a pressure
sensor, which are exposed in the combined SenseEnviron
class. This
provides readings as EnvironReadings
tuples.
13.1. SenseEnviron¶
-
class
pisense.
SenseEnviron
(settings=None, temp_source=<function temp_humidity>, emulate=False)[source]¶ The
SenseEnviron
class represents the suite of environmental sensors on the Sense HAT. Users can either instantiate this class themselves, or can access an instance fromSenseHAT.environ
.The
temperature
,pressure
, andhumidity
attributes can be queried to read the current values from the sensors. Alternatively, the instance can be treated as an iterator in which case readings will be yielded as they are detected:hat = SenseHAT() for reading in hat.environ: print(reading.temperature)
Because both the pressure and humidity sensors contain a temperature sensor, a source must be selected for the temperature reading. By default this is from the pressure sensor only, but you can specify a function for
temperature_source
which, given the two temperature readings returns the reading you are interested in, or some combination there-of.The settings parameter can be used to point to alternate settings files. The temp_source parameter provides an initial value for the
temp_source
attribute (this defaults totemp_humidity()
). If the emulate parameter isTrue
, the instance will connect to the environment sensors in the desktop Sense HAT emulator instead of the “real” Sense HAT’s sensors.-
close
()[source]¶ Call the
close()
method to close the environmental sensor interface and free up any background resources. The method is idempotent (you can call it multiple times without error) and after it is called, any operations on the environmental sensors may return an error (but are not guaranteed to do so).
-
read
()[source]¶ Return the current state of all environmental sensors as an
EnvironReadings
tuple.Note
This method will wait until the next set of readings are available, and then return them. Hence it is suitable for use in a loop without additional waits, although it may be simpler to simply treat the instance as an iterator in that case.
This is in contrast to reading the
pressure
,humidity
, andtemperature
attributes which always return immediately.
-
humidity
¶ Return the current humidity reading from the environmental sensors. The humidity is measured as a % of relative humidity.
-
pressure
¶ Return the current pressure reading from the environmental sensors. The pressure is measured in millibars (aka hectopascals).
-
temp_source
¶ Specify the conversion function for the temperature sensors.
The Sense HAT contains two temperature sensors, one in the humidity sensor, and one in the pressure sensor. The
temp_source
property contains the function that is used to determine how the temperature is reported from the two sources. The function must take two parameters (the readings from the humidity and pressure sensors respectively) and can return whatever you wish to see as the value of thetemperature
property (including a tuple of both temperatures).The default value is
temp_humidity()
which simply returns the reading from the humidity sensor, discarding the the pressure sensor reading.Warning
You may be tempted to average the two readings under the assumption that this will provide more accuracy. This is almost certainly not the case!
-
13.2. EnvironReadings¶
-
class
pisense.
EnvironReadings
(pressure, humidity, temperature)[source]¶ A
namedtuple()
representing the readings from the environmental sensors as a named 3-tuple containing the fields pressure (in mbar or hPa), humidity (in %RH), and temperature (in °C) respectively.
13.3. Temperature Configuration¶
-
pisense.
temp_pressure
(p_temp, h_temp)[source]¶ Use this function as
temp_source
if you want to read temperature from the pressure sensor only.
-
pisense.
temp_humidity
(p_temp, h_temp)[source]¶ Use this function as
temp_source
if you want to read temperature from the humidity sensor only. This is the default.
-
pisense.
temp_average
(p_temp, h_temp)[source]¶ Use this function as
temp_source
if you wish to read the average of both the pressure and humidity sensor’s temperatures.
-
pisense.
temp_both
(p_temp, h_temp)[source]¶ Use this function as
temp_source
if you wish to return both the pressure and humidity sensor’s temperature readings as a tuple from thetemperature
attribute.