Skip to content

Instantly share code, notes, and snippets.

@sarasantos

sarasantos/bme680_moving_average2.py Secret

Last active Jan 24, 2021
Embed
What would you like to do?
from machine import Pin, I2C
from time import sleep
from bme680 import *
# ESP32 - Pin assignment
i2c = I2C(scl=Pin(22), sda=Pin(21), freq=10000)
bme = BME680_I2C(i2c=i2c)
#Number of readings to calculate the average
lenght = 5
temperatures = [0, 0, 0, 0, 0]
humidity = [0, 0, 0, 0, 0]
pressure = [0, 0, 0, 0, 0]
gas = [0, 0, 0, 0, 0]
#Calculates and returns average
def get_average(readings):
moving_average = sum(readings)/len(readings)
return moving_average
def get_BME680_average():
#Get New Readings
temperatures[n] = bme.temperature * (9 / 5) + 32
humidity[n] = bme.humidity
gas[n] = bme.gas
pressure[n] = bme.pressure
#Calculate Moving Average
average_p = get_average(pressure)
average_h = get_average(humidity)
average_t = get_average(temperatures)
average_g = get_average(gas)
#Returns Moving Average:
return average_t, average_h, average_p, average_h
while True:
n = 0
#Check if we exceeded the lenght
if n >= lenght:
n = 0
temperature_average, humidity_average, pressure_average, gas_average = get_BME680_average()
print(temperature_average)
print(humidity_average)
print(pressure_average)
print(gas_average)
n += 1
sleep(30)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment