Dateien nach "/" hochladen
check in
This commit is contained in:
40
sensors.py
Normal file
40
sensors.py
Normal file
@@ -0,0 +1,40 @@
|
||||
import smbus2
|
||||
import bme280
|
||||
import time
|
||||
|
||||
# I2C bus ini initialisieren
|
||||
bus = smbus2.SMBus(1)
|
||||
|
||||
class EnvironmentSensor:
|
||||
def __init__(self, addr):
|
||||
self.addr = addr
|
||||
|
||||
def temperature(self):
|
||||
return bme280.sample(bus, self.addr).temperature
|
||||
def humidity(self):
|
||||
return bme280.sample(bus, self.addr).humidity
|
||||
def pressure(self):
|
||||
return bm2280.sample(bus, self.addr).pressure
|
||||
|
||||
env=[EnvironmentSensor(0x76), EnvironmentSensor(0x77)]
|
||||
|
||||
lightMeasurementTime=0.2
|
||||
|
||||
def readBH1750(bus, addr = 0x23):
|
||||
bus.write_byte(addr, 0x01) # power on
|
||||
bus.write_byte(addr, 0x20) # set high res onetime measurement mode
|
||||
time.sleep(lightMeasurementTime) # 0.1 should already be enough time for measurement
|
||||
data = bus.read_i2c_block_data(addr, 0, 2) # get measurement result
|
||||
return ((data[0] << 8) + data[1]) / 1.2 # factor 1.2 is from sensor datasheet
|
||||
|
||||
|
||||
def illuminance():
|
||||
return readBH1750(bus)
|
||||
|
||||
def environment(sensor):
|
||||
if sensor == 0:
|
||||
return bme280.sample(bus, 0x76)
|
||||
elif sensor == 1:
|
||||
return bme280.sample(bus, 0x77)
|
||||
else:
|
||||
raise ValueError
|
||||
Reference in New Issue
Block a user