1
0
mirror of https://github.com/cmur2/python-bme680.git synced 2024-11-10 16:56:14 +01:00
python-bme680/examples/read-all.py

75 lines
1.9 KiB
Python
Raw Normal View History

2017-10-17 17:45:50 +02:00
#!/usr/bin/env python
2017-10-15 12:29:51 +02:00
import bme680
2017-10-17 17:45:50 +02:00
import time
2017-10-15 12:29:51 +02:00
2018-09-02 12:26:04 +02:00
print("""Display Temperature, Pressure, Humidity and Gas
Press Ctrl+C to exit
""")
try:
sensor = bme680.BME680(bme680.I2C_ADDR_PRIMARY)
except IOError:
sensor = bme680.BME680(bme680.I2C_ADDR_SECONDARY)
2017-10-15 12:29:51 +02:00
2017-10-18 11:23:30 +02:00
# These calibration data can safely be commented
# out, if desired.
2018-09-02 12:26:04 +02:00
print('Calibration data:')
2017-10-15 12:29:51 +02:00
for name in dir(sensor.calibration_data):
2017-10-18 11:23:30 +02:00
2017-10-15 12:29:51 +02:00
if not name.startswith('_'):
value = getattr(sensor.calibration_data, name)
2017-10-18 11:23:30 +02:00
2017-10-15 12:29:51 +02:00
if isinstance(value, int):
2018-09-02 12:26:04 +02:00
print('{}: {}'.format(name, value))
2017-10-15 12:29:51 +02:00
2018-09-02 12:26:04 +02:00
# These oversampling settings can be tweaked to
2017-10-18 11:23:30 +02:00
# change the balance between accuracy and noise in
# the data.
2017-10-15 12:29:51 +02:00
2017-10-17 17:45:50 +02:00
sensor.set_humidity_oversample(bme680.OS_2X)
sensor.set_pressure_oversample(bme680.OS_4X)
sensor.set_temperature_oversample(bme680.OS_8X)
sensor.set_filter(bme680.FILTER_SIZE_3)
sensor.set_gas_status(bme680.ENABLE_GAS_MEAS)
2017-10-15 12:29:51 +02:00
2018-09-02 12:26:04 +02:00
print('\n\nInitial reading:')
2017-10-17 17:45:50 +02:00
for name in dir(sensor.data):
value = getattr(sensor.data, name)
2017-10-18 11:23:30 +02:00
2017-10-17 17:45:50 +02:00
if not name.startswith('_'):
2018-09-02 12:26:04 +02:00
print('{}: {}'.format(name, value))
2017-10-15 12:29:51 +02:00
2017-10-17 17:45:50 +02:00
sensor.set_gas_heater_temperature(320)
sensor.set_gas_heater_duration(150)
sensor.select_gas_heater_profile(0)
2017-10-15 12:29:51 +02:00
2017-10-17 17:45:50 +02:00
# Up to 10 heater profiles can be configured, each
# with their own temperature and duration.
# sensor.set_gas_heater_profile(200, 150, nb_profile=1)
# sensor.select_gas_heater_profile(1)
2017-10-15 12:29:51 +02:00
2018-09-02 12:26:04 +02:00
print('\n\nPolling:')
2017-10-17 17:45:50 +02:00
try:
while True:
if sensor.get_sensor_data():
2018-09-02 12:26:04 +02:00
output = '{0:.2f} C,{1:.2f} hPa,{2:.2f} %RH'.format(
sensor.data.temperature,
sensor.data.pressure,
sensor.data.humidity)
2017-10-15 12:29:51 +02:00
2017-10-17 17:45:50 +02:00
if sensor.data.heat_stable:
2018-09-02 12:26:04 +02:00
print('{0},{1} Ohms'.format(
output,
sensor.data.gas_resistance))
2017-10-18 11:23:30 +02:00
2017-10-17 17:45:50 +02:00
else:
print(output)
2017-10-15 12:29:51 +02:00
2017-10-17 17:45:50 +02:00
time.sleep(1)
2017-10-15 12:29:51 +02:00
2017-10-17 17:45:50 +02:00
except KeyboardInterrupt:
pass