Created
April 1, 2023 04:50
-
-
Save collabnix/ae926a50dba3d6a30bf3a4ba2d271ecb to your computer and use it in GitHub Desktop.
A Python script to fetch sensor values
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import time | |
import smbus2 | |
import bme680 | |
# Set up BME680 sensor object | |
bus = smbus2.SMBus(1) # Use I2C bus 1 | |
bme = bme680.BME680(bme680.I2C_ADDR_PRIMARY, bus) | |
# Set up oversampling settings for temperature, pressure, and humidity readings | |
bme.set_humidity_oversample(bme680.OS_2X) | |
bme.set_pressure_oversample(bme680.OS_4X) | |
bme.set_temperature_oversample(bme680.OS_8X) | |
bme.set_filter(bme680.FILTER_SIZE_3) | |
# Continuously fetch and print sensor data | |
while True: | |
if bme.get_sensor_data(): | |
temperature = bme.data.temperature | |
pressure = bme.data.pressure | |
humidity = bme.data.humidity | |
gas_resistance = bme.data.gas_resistance | |
print("Temperature: {:.2f} C".format(temperature)) | |
print("Pressure: {:.2f} hPa".format(pressure)) | |
print("Humidity: {:.2f} %".format(humidity)) | |
print("Gas Resistance: {:.2f} Ohms".format(gas_resistance)) | |
time.sleep(1) # Wait for 1 second before fetching the next set of sensor data | |
import time | |
import smbus2 | |
import bme680 | |
# Set up BME680 sensor object | |
bus = smbus2.SMBus(1) # Use I2C bus 1 | |
bme = bme680.BME680(bme680.I2C_ADDR_PRIMARY, bus) | |
# Set up oversampling settings for temperature, pressure, and humidity readings | |
bme.set_humidity_oversample(bme680.OS_2X) | |
bme.set_pressure_oversample(bme680.OS_4X) | |
bme.set_temperature_oversample(bme680.OS_8X) | |
bme.set_filter(bme680.FILTER_SIZE_3) | |
# Continuously fetch and print sensor data | |
while True: | |
if bme.get_sensor_data(): | |
temperature = bme.data.temperature | |
pressure = bme.data.pressure | |
humidity = bme.data.humidity | |
gas_resistance = bme.data.gas_resistance | |
print("Temperature: {:.2f} C".format(temperature)) | |
print("Pressure: {:.2f} hPa".format(pressure)) | |
print("Humidity: {:.2f} %".format(humidity)) | |
print("Gas Resistance: {:.2f} Ohms".format(gas_resistance)) | |
time.sleep(1) # Wait for 1 second before fetching the next set of sensor data |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment