Pimoroni Enviro+
Image credit: Pimoroni

f451 Labs RPI Enviro+ module

Overview

The f451 Labs Enviro+ module encapsulates the drivers for the Pimoroni Enviro+ HAT within a single class. This module provides a standard set of methods to read sensor data and display content to the onboard 0.96" LCD (160x80).

Core functions

This module includes the following main classes and functions:

  • Enviro — Main class for managing Enviro+ add-on.
  • EnviroError — Helper class for handling errors related to Enviro+.
  • prep_data — Helper function to prep/format data for display on Enviro+ 0.96" LCD.

In addition, the Enviro class has the following core methods and properties:

  • displayWidth — Property: width (pixels) of 0.96" LCD display
  • displayHeight — Property: height (pixels) of 0.96" LCD display
  • isFake — Property: ‘False’ if physical Enviro+.
  • get_CPU_temp() — Get CPU temp which we then can use to compensate temp reads.
  • get_proximity() — Get proximity value from sensor.
  • get_lux() — Get illumination value from sensor.
  • get_pressure() — Get barometric pressure from sensor.
  • get_humidity() — Get humidity from sensor.
  • get_temperature() — Get temperature from sensor.
  • get_gas_data() — Get gas data from sensor.
  • get_particles() — Get particle data from sensor.
  • add_display_modes() — Add one or more display modes to the list.
  • set_display_mode() — Switch display mode.
  • update_sleep_mode() — Switch to/from sleep mode.
  • display_init() — Initialize display so we can draw on it.
  • display_rotate() — Rotate display +/- 90 degrees.
  • display_on() — Turn ‘on’ LCD.
  • display_off() — Turn ‘off’ LCD.
  • display_blank() — Erase LCD.
  • display_reset() — Erase LCD.
  • display_sparkle() — Show random sparkles on LCD.
  • display_as_graph() — Display data as (sparkline) graph.
  • display_as_text() — Display data as text (in columns).
  • display_message() — Display text message.
  • display_progress() — Display progress bar.

Please refer to the GitHub repo for source code and documentation.

Dependencies

This module depends on the following libraries:

NOTE: Only install enviroplus-python library on a device that also has the physical Enviro+ HAT installed.

NOTE: You can run this app in demo mode in (almost) any device even without the Enviro+ HAT. It will then create random numbers and can send output to the logger when log level is DEBUG or when --debug flag is used.

How to use

Enviro+ Device

The Enviro object makes it easy to interact with the Enviro+ device. The methods of this object help read sensor data, display data to the 0.96" LCD, etc., and using the module is straightforward. Simply import it into your code and instantiate an Enviro object which you can then use throughout your code.

# Import f451 Labs Enviro+
from f451_enviro.enviro import Enviro

# Initialize device instance which includes all sensors
# and LCD display on Enviro+
myEnviro = Enviro({
    "ROTATION": 90,
    "DISPLAY": 0,
    "PROGRESS": 0,
    "SLEEP": 600    
})
myEnviro.display_init()

print(f"TEMP:     {round(myEnviro.get_temperature(), 1)} C")
print(f"PRESSURE: {round(myEnviro.get_pressure(), 1)} hPa")
print(f"HUMIDITY: {round(myEnviro.get_humidity(), 1)} %")

Enviro+ Data

The f451 Labs Enviro+ module also includes an EnviroData object and a few other helper objects. These objects are designed to simplify storing and managing sensor data. The EnviroData object implements so-called double-ended queues (‘deque’) which makes it easy to add and retrieve data. To use these objects in your code, simply import them into your code and instantiate an EnviroData object.

# Import f451 Labs SenseHat Data
from f451_enviro.enviro import EnviroData

maxLen = 10     # Max length of queue
defVal = 1      # Default value for initialization

myData = EnviroData(defVal, maxlen)

# Assuming we have instantiated the Enviro object as 'myEnviro' we
# can then read and store sensor data right into the data queues
myData.temperature.data.append(myEnviro.get_temperature())
myData.pressure.data.append(myEnviro.get_pressure())
myData.humidity.data.append(myEnviro.get_humidity())