Ejemplo n.º 1
0
    def __init__(self):
        # First, check to see if there is a BME680 on the I2C bus
        try:
            self.bus.write_byte(0x76, 0)
        except IOError:
            print('BME680 not found on 0x76, trying 0x77')
        else:
            self.readfrom = 'bme680primary'

        # If we didn't find it on 0x76, look on 0x77
        if self.readfrom == 'unset':
            try:
                self.bus.write_byte(0x77, 0)
            except IOError:
                print('BME680 not found on 0x77')
            else:
                self.readfrom = 'bme680secondary'


        # If no BME680, is there a Sense HAT?
        if self.readfrom == 'unset':
            try:
                self.bus.write_byte(0x5F, 0)
            except:
                print('Sense HAT not found')
            else:
                self.readfrom = 'sense-hat'
                print('Using Sense HAT for readings (no gas measurements)')

                # Import the sense hat methods
                import sense_hat_air_quality
                from hts221 import HTS221
                self.sense_hat_reading = lambda: sense_hat_air_quality.get_readings(HTS221())
        else:
                print('Using BME680 for readings')

                # Import the BME680 methods
                self.sensor = BME680(self.readfrom)


        # Next, check if there is a 1-wire temperature sensor (e.g. DS18B20)
        if self.readfrom == 'unset':
            if os.environ.get('BALENASENSE_1WIRE_SENSOR_ID') != None:
                sensor_id = os.environ['BALENASENSE_1WIRE_SENSOR_ID']
            else:
                sensor_id = None

            try:
                self.sensor = W1THERM(sensor_id)
            except:
                print('1-wire sensor not found')
            else:
                self.readfrom = '1-wire'
                print('Using 1-wire for readings (temperature only)')

        # If this is still unset, no sensors were found; quit!
        if self.readfrom == 'unset':
            print('No suitable sensors found! Exiting.')
            sys.exit()
Ejemplo n.º 2
0
    def __init__(self):
        # First, check to see if there is a BME680 on the I2C bus
        try:
            self.bus.write_byte(0x76, 0)
        except IOError:
            print('BME680 not found on 0x76, trying 0x77')
        else:
            self.readfrom = 'bme680primary'

        # If we didn't find it on 0x76, look on 0x77
        if self.readfrom == 'unset':
            try:
                self.bus.write_byte(0x77, 0)
            except IOError:
                print('BME680 not found on 0x77')
            else:
                self.readfrom = 'bme680secondary'


        # If no BME680, is there a Sense HAT?
        if self.readfrom == 'unset':
            try:
                self.bus.write_byte(0x5F, 0)
            except:
                print('Sense HAT not found')
            else:
                self.readfrom = 'sense-hat'
                print('Using Sense HAT for readings (no gas measurements)')

                # Import the sense hat methods
                try:
                    import sense_hat_air_quality
                    print('Module sense_hat_air_quality was correctly imported')
                except:
                    print('Module sense_hat_air_quality could not be imported')

                self.sensor = HTS221()
        else:
                print('Using BME680 for readings')

                # Import the BME680 methods
                self.sensor = BME680(self.readfrom)


        # If this is still unset, no sensors were found; quit!
        if self.readfrom == 'unset':
            print('No suitable sensors found! Exiting.')
            sys.exit()
Ejemplo n.º 3
0
#!/usr/bin/python3

from tsl2561 import TSL2561
from bme680 import BME680
from ds18b20_therm import DS18B20
import bme680
import MySQLdb

tsl = TSL2561()
bme = BME680(i2c_addr=bme680.I2C_ADDR_SECONDARY)
ds = DS18B20()

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)
bme.set_gas_status(bme680.ENABLE_GAS_MEAS)

bme.set_gas_heater_temperature(320)
bme.set_gas_heater_duration(150)
bme.select_gas_heater_profile(0)

bme.get_sensor_data()

sample = {
    "air_temp": 0,
    "ground_temp": 0,
    "pressure": 0,
    "humidity": 0,
    "air_conductivity": 0,
    "light": 0
Ejemplo n.º 4
0
def init_sensors(kind):
    global bme680, si7021, sht31, pmsx003, anemo, vane, rain
    global cwop, display

    # ===== pin configuration, see also Projects/kicad/esp32-weather/README.md
    if kind == "lolin-d32":
        scl0, sda0 = 23, 22  # bme680, si7021, sht31
        scl1, sda1 = 18, 4  # oled
        scl2, sda2 = 13, 12  # expansion
        pm_tx, pm_rx = 25, 26  # pmsa003
        anemo_pin = 39  # anemometer pulse
        vane_pin = 36  # wind vane analog
        rain_pin = 34  # rain pulse
        pow_3v3 = 32  # active-low power for anemo/vane/rain/pmsa003
    else:
        raise ("Unknown board kind: " + kind)

    # ===== init devices

    # show splash screen on display
    from ssd1306 import SSD1306_I2C

    try:
        scl1_pin = machine.Pin(scl1)
        sda1_pin = machine.Pin(sda1, pull=machine.Pin.PULL_UP)  # pup: helps presence detection
        display = SSD1306_I2C(128, 64, machine.I2C(scl=scl1_pin, sda=sda1_pin, freq=1000000))
        display.fill(1)
        display.fill_rect(10, 10, 108, 44, 0)
        display.text("WCC Weather", 20, 20, 1)
        display.show()
        log.info("Found display")
    except Exception as e:
        display = None
        log.warning("No display: %s", e)

    # start power for anemo, vane, etc.
    pow_3v3_pin = machine.Pin(pow_3v3, machine.Pin.OUT)
    pow_3v3_pin(0)

    # I2C bus for primary sensors
    scl0_pin = machine.Pin(scl0)
    sda0_pin = machine.Pin(sda0)
    i2c0_dev = machine.I2C(scl=scl0_pin, sda=sda0_pin, freq=100000)

    # BME680 temperature/humidity/pressure/voc
    from bme680 import BME680

    try:
        bme680 = BME680(i2c0_dev)
        bme680.set_gas_heater_temperature(320)
        bme680.set_gas_heater_duration(100)
        log.info("Found BME680")
    except Exception as e:
        bme680 = None
        log.warning("No BME680 found: %s", e)

    # SI7021 temperature/humidity
    from si7021 import Si7021

    try:
        si7021 = Si7021(i2c0_dev)
        si7021.convert()
        log.info("Found Si7021")
    except Exception as e:
        si7021 = None
        log.warning("No Si7021 found: %s", e)

    # SHT31 temperature/humidity
    from sht31 import SHT31

    try:
        sht31 = SHT31(i2c0_dev)
        sht31.convert()
        log.info("Found SHT31")
    except Exception as e:
        sht31 = None
        log.warning("No SHT31 found: %s", e)

    # PMSx003 PM sensor
    from pms_x003 import PMSx003

    try:
        pmsx003 = PMSx003(tx=pm_tx, rx=pm_rx)
        log.info("Found PMSx003")
    except Exception as e:
        pmsx003 = None
        log.warning("No PMSx003 found: %s", e)

    # Anemometer and wind vane
    from wind import Anemo, Vane
    from counter import Counter

    try:
        # configure pin with pull-up
        machine.Pin(anemo_pin, mode=machine.Pin.IN)
        anemo_ctr = Counter(0, anemo_pin)
        anemo_ctr.filter(10)  # 10us filter
        anemo = Anemo(anemo_ctr, 2.5)  # 2.5 mph per Hz
        anemo.start()
    except Exception as e:
        anemo = None
        log.exc(e, "Anemometer failed to init")
    try:
        vane = Vane(vane_pin, 140, 1600, 15)
        vane.start()
    except Exception as e:
        vane = None
        log.exc(e, "Wind vane failed to init")

    # init rain gauge
    pass

    # init CWOP
    try:
        from cwop import send_wx

        cwop = send_wx
    except ImportError:
        log.warning("Cannot import CWOP, skipping")
Ejemplo n.º 5
0
import machine
import time
from bme680 import BME680
from io_pubsub import IOClient

adc = machine.ADC(machine.Pin(35))
adc.atten(machine.ADC.ATTN_11DB)  # provides full range of 0-4095

led = machine.Pin(22, machine.Pin.OUT)  # LED on the board
led.value(0)
bme680 = BME680()
io = IOClient()

while True:
    battery = (adc.read() * 2 * 3.3) / 4096
    print("Battery: %d mV", battery)

    io.update(bme680.temperature, bme680.humidity, bme680.pressure, bme680.gas,
              battery)
    io.publish()
    time.sleep(60)
Ejemplo n.º 6
0
    def __init__(self):
        # First, check for enviro plus hat (since it also has BME on 0x76)
        try:
            self.bus.write_byte(0x23, 0)  # test if we can connect to ADS1015
        except IOError:
            print('Enviro Plus hat not found')
            # Fix issue of LTR559 not found
            print('SUPSPECT USING CHEAP CHINESE BME680 :)')
        else:
            try:
                self.sensor = ENVIROPLUS()
                print('Found Enviro+ Hat')
                self.readfrom = 'enviroplus'
            except RuntimeError:
                # Fix issue of LTR559 not found
                print('SUPSPECT USING CHEAP CHINESE BME680 :)')

        # Next, check to see if there is a BME680 on the I2C bus
        if self.readfrom == 'unset':
            try:
                self.bus.write_byte(0x76, 0)
            except IOError:
                print('BME680 not found on 0x76, trying 0x77')
            else:
                print('BME680 found on 0x76')
                self.readfrom = 'bme680primary'
                self.sensor = BME680(self.readfrom)

        # If we didn't find it on 0x76, look on 0x77
        if self.readfrom == 'unset':
            try:
                self.bus.write_byte(0x77, 0)
            except IOError:
                print('BME680 not found on 0x77')
            else:
                print('BME680 found on 0x77')
                self.readfrom = 'bme680secondary'
                self.sensor = BME680(self.readfrom)

        # If no BME680, is there a Sense HAT?
        if self.readfrom == 'unset':
            try:
                self.bus.write_byte(0x5F, 0)
            except:
                print('Sense HAT not found')
            else:
                self.readfrom = 'sense-hat'
                print('Using Sense HAT for readings (no gas measurements)')

                # Import the sense hat methods
                import sense_hat_air_quality
                from hts221 import HTS221
                self.sense_hat_reading = lambda: sense_hat_air_quality.get_readings(
                    HTS221())

        # Next, check if there is a 1-wire temperature sensor (e.g. DS18B20)
        if self.readfrom == 'unset':
            if os.environ.get('BALENASENSE_1WIRE_SENSOR_ID') != None:
                sensor_id = os.environ['BALENASENSE_1WIRE_SENSOR_ID']
            else:
                sensor_id = None

            try:
                self.sensor = W1THERM(sensor_id)
            except:
                print('1-wire sensor not found')
            else:
                self.readfrom = '1-wire'
                print('Using 1-wire for readings (temperature only)')

        # If this is still unset, no sensors were found; quit!
        if self.readfrom == 'unset':
            print('No suitable sensors found! Exiting.')
            sys.exit()