Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed implementation for BME680 sensor #482

Merged
merged 1 commit into from
Feb 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 59 additions & 47 deletions octoprint_enclosure/BME680.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,41 +2,20 @@
import time


try:
sensor = bme680.BME680(bme680.I2C_ADDR_PRIMARY)
except IOError:
sensor = bme680.BME680(bme680.I2C_ADDR_SECONDARY)

hum_weighting = float(0.25) # so hum effect is 25% of the total air quality score
gas_weighting = float(0.75) # so gas effect is 75% of the total air quality score

sensor.set_humidity_oversample(bme680.OS_2X)
sensor.set_pressure_oversample(bme680.OS_2X)
sensor.set_temperature_oversample(bme680.OS_2X)
sensor.set_filter(bme680.FILTER_SIZE_3)

sensor.set_gas_heater_temperature(320)
sensor.set_gas_heater_duration(150)
sensor.select_gas_heater_profile(0)
sensor.set_gas_status(bme680.ENABLE_GAS_MEAS)

gas_reference = float(250000)
hum_reference = float(40)
getgasreference_count = int(0)


def GetGasReference(gas_reference):
def GetGasReference():
# Now run the sensor for a burn-in period, then use combination of relative humidity and gas resistance to estimate indoor air quality as a percentage.
# print("Getting a new gas reference value")
readings = int(10)
gas_reference = 0
while True:
sensor.get_sensor_data()
if sensor.data.heat_stable:
for i in range(1, readings): # // read gas for 10 x 0.150mS = 1.5secs
sensor.get_sensor_data()
gas_reference = gas_reference + sensor.data.gas_resistance
gas_reference = gas_reference / readings
return
return gas_reference


def CalculateIAQ(score):
IAQ_text = "Air quality is "
Expand All @@ -55,32 +34,65 @@ def CalculateIAQ(score):
IAQ_text = IAQ_text + "Good"
return IAQ_text

#Calculate humidity contribution to IAQ index
current_humidity = float(sensor.data.humidity)
if (current_humidity >= 38 and current_humidity <= 42):
hum_score = float(0.25*100) # Humidity +/-5% around optimum
else:
#sub-optimal
if (current_humidity < 38):
hum_score = float(0.25/hum_reference*current_humidity*100)
else:
hum_score = ((-0.25/(100-hum_reference)*current_humidity)+0.416666)*100

#Calculate gas contribution to IAQ index
gas_lower_limit = float(5000) # Bad air quality limit
gas_upper_limit = float(50000) # Good air quality limit
if __name__ == "__main__":

try:
sensor = bme680.BME680(bme680.I2C_ADDR_PRIMARY)
except RuntimeError:
try:
sensor = bme680.BME680(bme680.I2C_ADDR_SECONDARY)
except Exception as ex:
print(ex)
quit(-1)

hum_weighting = float(0.25) # so hum effect is 25% of the total air quality score
gas_weighting = float(0.75) # so gas effect is 75% of the total air quality score

sensor.set_humidity_oversample(bme680.OS_2X)
sensor.set_pressure_oversample(bme680.OS_2X)
sensor.set_temperature_oversample(bme680.OS_2X)
sensor.set_filter(bme680.FILTER_SIZE_3)

sensor.get_sensor_data()
temperature = sensor.data.temperature
humidity = sensor.data.humidity

sensor.set_gas_heater_temperature(320)
sensor.set_gas_heater_duration(150)
sensor.select_gas_heater_profile(0)
sensor.set_gas_status(bme680.ENABLE_GAS_MEAS)

gas_reference = float(250000)
hum_reference = float(40)
getgasreference_count = int(0)

# Calculate humidity contribution to IAQ index
current_humidity = float(humidity)
if (current_humidity >= 38 and current_humidity <= 42):
hum_score = float(0.25 * 100) # Humidity +/-5% around optimum
else:
# sub-optimal
if (current_humidity < 38):
hum_score = float(0.25 / hum_reference * current_humidity * 100)
else:
hum_score = ((-0.25 / (100 - hum_reference) * current_humidity) + 0.416666) * 100

if gas_reference > gas_upper_limit:
gas_reference = gas_upper_limit
if gas_reference < gas_lower_limit:
gas_reference = gas_lower_limit
# Calculate gas contribution to IAQ index
gas_lower_limit = float(5000) # Bad air quality limit
gas_upper_limit = float(50000) # Good air quality limit

gas_score = float((0.75/(gas_upper_limit-gas_lower_limit)*gas_reference -(gas_lower_limit*(0.75/(gas_upper_limit-gas_lower_limit))))*100)
gas_reference = GetGasReference()

#Combine results for the final IAQ index value (0-100% where 100% is good quality air)
air_quality_score = float(hum_score + gas_score)
if gas_reference > gas_upper_limit:
gas_reference = gas_upper_limit
if gas_reference < gas_lower_limit:
gas_reference = gas_lower_limit

GetGasReference(gas_reference)
gas_score = float((0.75 / (gas_upper_limit - gas_lower_limit) * gas_reference - (
gas_lower_limit * (0.75 / (gas_upper_limit - gas_lower_limit)))) * 100)

print('{0:0.1f}'.format(air_quality_score))
# Combine results for the final IAQ index value (0-100% where 100% is good quality air)
air_quality_score = float(hum_score + gas_score)

print('{:0.1f}|{:0.1f}|{:0.1f}'.format(temperature, humidity, air_quality_score))
4 changes: 2 additions & 2 deletions octoprint_enclosure/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1183,7 +1183,7 @@ def read_bme280_temp(self, address):

def read_bme680_temp(self, address):
try:
script = os.path.dirname(os.path.realpath(__file__)) + "/BME680.py "
script = os.path.dirname(os.path.realpath(__file__)) + "/BME680.py"
cmd = [sys.executable, script, str(address)]
if self._settings.get(["use_sudo"]):
cmd.insert(0, "sudo")
Expand All @@ -1204,7 +1204,7 @@ def read_bme680_temp(self, address):
self._logger.info(
"Failed to execute python scripts, try disabling use SUDO on advanced section of the plugin.")
self.log_error(ex)
return (0, 0)
return (0, 0, 0)

def read_am2320_temp(self):
try:
Expand Down
2 changes: 1 addition & 1 deletion octoprint_enclosure/static/js/enclosure.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ $(function () {
self.notifications = ko.observableArray([]);

self.humidityCapableSensor = function(sensor){
if (['11', '22', '2302', 'bme280', 'am2320', 'aht10' , 'si7021', 'hum_raw_i2c', 'temp_raw_i2c'].indexOf(sensor) >= 0){
if (['11', '22', '2302', 'bme280', 'bme680', 'am2320', 'aht10' , 'si7021', 'hum_raw_i2c', 'temp_raw_i2c'].indexOf(sensor) >= 0){
return true;
}
return false;
Expand Down