Skip to content
This repository has been archived by the owner on Apr 5, 2024. It is now read-only.

Commit

Permalink
Add Sense-HAT pressure sensor support
Browse files Browse the repository at this point in the history
Fixes #68

Change-type: patch
Signed-off-by: Chris Crocker-White <chriscw@balena.io>
  • Loading branch information
chrisys committed May 14, 2020
1 parent 2f81cf4 commit da91709
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 3 deletions.
42 changes: 42 additions & 0 deletions sensor/scripts/lps25h.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Credit: https://github.com/youkidearitai/rpi-sensors
import smbus
import time

class Lps25hsensor:
address = 0x5c

PRESS_OUT_XL = 0x28
PRESS_OUT_L = 0x29
PRESS_OUT_H = 0x2A

def __init__(self, smbus_addr = 1):
self.bus = smbus.SMBus(smbus_addr)

def setup(self):
try:
self.bus.write_i2c_block_data(self.address, 0x00, [])
except OSError as e:
print(e)

time.sleep(0.1)

def read(self):
try:
self.bus.write_i2c_block_data(self.address, 0x20, [0x90])
except OSError as e:
print(e)

time.sleep(0.05)

pressure = (
self.read_i2c_block(self.PRESS_OUT_XL) << 0 |
self.read_i2c_block(self.PRESS_OUT_L) << 8 |
self.read_i2c_block(self.PRESS_OUT_H) << 16
)
pressure = pressure / 4096

return pressure

def read_i2c_block(self, register):
blocks = self.bus.read_i2c_block_data(self.address, register, 1)
return blocks[0]
11 changes: 8 additions & 3 deletions sensor/scripts/sense_hat_air_quality.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
def get_readings(sensor):
# As we have a sense HAT we can give an indication of the air quality on the LED matrix
from ledmatrix import LedMatrix
from lps25h import Lps25hsensor

# The sense HAT does not include any way to obtain an air quality score via gas measurement,
# so we can create one using the temperature and humidity reading based on distance from ideal values
max_iaq = 500
Expand Down Expand Up @@ -32,8 +36,9 @@ def get_readings(sensor):
air_quality_score = (current_humidity_variance * humidity_weighting) + (current_temperature_variance * (1 - humidity_weighting))
air_quality_score = air_quality_score * 500

# As we have a sense HAT we can give an indication of the air quality on the LED matrix
from ledmatrix import LedMatrix
# Get a pressure reading from the LPS25H on the sense hat at 0x5c
pressure_sensor = Lps25hsensor()
current_pressure = pressure_sensor.read()

display = LedMatrix()
display.clear()
Expand Down Expand Up @@ -90,7 +95,7 @@ def get_readings(sensor):
'measurement': 'balena-sense',
'fields': {
'temperature': float(current_temperature),
# 'pressure': float(sensor.environ.pressure),
'pressure': float(current_pressure),
'humidity': float(current_humidity),
'air_quality_score': float(air_quality_score)
}
Expand Down

0 comments on commit da91709

Please sign in to comment.