def start(self): """Getting the bus""" if self.bus is None: raise KeyError("Bus missing for BME280Sensor") # Initialize the hardware driver. try: self.driver = BME280(address=self.address, i2c=self.bus.adapter) return True except Exception as ex: log.exc(ex, 'BME280 hardware driver failed')
def start(self): """ Setup the BME280 sensor driver. :return: """ # Ensure a bus object exists and is ready. self.ensure_bus() # Initialize the hardware driver. try: # MicroPython if platform_info.vendor in [platform_info.MICROPYTHON.Vanilla, platform_info.MICROPYTHON.Pycom]: from bme280_float import BME280 self.driver = BME280(address=self.address, i2c=self.bus.adapter) # Adafruit CircuitPython elif platform_info.vendor == platform_info.MICROPYTHON.RaspberryPi: import adafruit_bme280 self.driver = adafruit_bme280.Adafruit_BME280_I2C(i2c=self.bus.adapter, address=self.address) # RPi.bme280 elif platform_info.vendor == platform_info.MICROPYTHON.Odroid: import bme280 self.calibration_params = bme280.load_calibration_params(self.bus.adapter, self.address) self.driver = bme280.sample(self.bus.adapter, self.address) else: raise NotImplementedError('BME280 driver not implemented on this platform') return True except Exception as ex: log.exc(ex, 'BME280 hardware driver failed') return False
while True: try: volt = measure_battery_level() # After leaving deepsleep it may be necessary to un-hold the pin explicitly (e.g. if it is an output pin) # See https://docs.micropython.org/en/latest/esp32/quickref.html#deep-sleep-mode p1 = machine.Pin(4, machine.Pin.OUT, None) # enable power for temperatuer sensor p2 = machine.Pin(19, machine.Pin.OUT) p2.value(1) values = decode_memory() i2c = setup_i2c() bme = BME280(i2c=i2c) print(bme.values) # disable power for temperatuer sensor #p2.value(0) cleaned_values = (bme.values[0].replace("C", ""), bme.values[1].replace("hPa", ""), bme.values[2].replace("%", "")) values.append(cleaned_values) print("calc mean") value = calc_mean(values) print(len(values)) if len(values) == SENDING_BATCH: print("send batch") send_batch(value, volt)
# template for MQTT client import ubinascii from machine import unique_id, I2C, Pin import time from utime import ticks_ms from umqttsimple import MQTTClient from bme280_float import BME280, BME280_OSAMPLE_8 # BME280 - SPIs # D1 - 05 - SCL # D2 - 04 - SDA # init sensor i2c = I2C(scl=Pin(5), sda=Pin(4)) bme280 = BME280(i2c=i2c, mode=BME280_OSAMPLE_8) def read_data(bme280): data = bme280.read_compensated_data() dew = bme280.dew_point print("{:6.2}°C {:10}hPa {:5.2}% {:6.2}°C(dew)".format( data[0], data[1], data[2], dew)) return data[0], data[1], data[2], dew # MQTT configuration MQTT_SERVER = '192.168.42.1' #where your mosquitto server is running CLIENT_ID = ubinascii.hexlify(unique_id()).decode("utf-8") # callback to handle subscritions