def __init__(self, ccs811_address=0x5b, bme280_address=0x77, tmp117_address=0x48, ccs811_n_samples=1): self.lock = _thread.allocate_lock() self.ccs811_n_samples = ccs811_n_samples self.ccs811 = qwiic_ccs811.QwiicCcs811(address=ccs811_address) self.bme280 = qwiic_bme280.QwiicBme280(address=bme280_address) self.tmp117 = qwiic_tmp117.QwiicTmp117(address=tmp117_address) self.ccs811.begin() self.bme280.begin() self.tmp117.begin() # The conversion and averaging affect who long we will have to wait # for a conversion. See the TMP117 data sheet. self.tmp117.set_avg(qwiic_tmp117.QwiicTmp117.CONV_AVG_8) self.tmp117.set_cycle(qwiic_tmp117.QwiicTmp117.CONV_CYCLE_2) self.tvoc_q = [] self.eco2_q = [] self.pres_q = [] self.tdry_q = [] self.rh_q = [] _thread.start_new_thread(self.read_loop, (None, )) # Sleep so that the measure thread can start time.sleep(1)
def runExample(): print("\nSparkFun BME280 Sensor Example 4\n") mySensor = qwiic_bme280.QwiicBme280() if mySensor.isConnected() == False: print("The Qwiic BME280 device isn't connected to the system. Please check your connection", \ file=sys.stderr) return mySensor.begin() # setup the sensor mySensor.filter = 1 # 0 to 4 is valid. Filter coefficient. See 3.4.4 mySensor.standby_time = 0 # 0 to 7 valid. Time between readings. See table 27. mySensor.over_sample = 1 # 0 to 16 are valid. 0 disables temp sensing. See table 24. mySensor.pressure_oversample = 1 # 0 to 16 are valid. 0 disables pressure sensing. See table 23. mySensor.humidity_oversample = 1 # 0 to 16 are valid. 0 disables humidity sensing. See table 19. mySensor.mode = mySensor.MODE_NORMAL # MODE_SLEEP, MODE_FORCED, MODE_NORMAL is valid. See 3.3 while True: print("Humidity:\t%.3f" % mySensor.humidity) print("Pressure:\t%.3f" % mySensor.pressure) print("Altitude:\t%.3f" % mySensor.altitude_feet) print("Temperature:\t%.2f\n" % mySensor.temperature_fahrenheit) time.sleep(1)
def runSensors(): myBME280 = qwiic_bme280.QwiicBme280() myCCS811 = qwiic_ccs811.QwiicCcs811() if my BME280.is_connected() == False: print("The Qwiic BME280 device isn't connected to the system. Please check your connection", \ file=sys.stderr) return
def runExample(): print("\nSparkFun BME280 Sensor Example 1\n") mySensor = qwiic_bme280.QwiicBme280() if mySensor.connected == False: print("The Qwiic BME280 device isn't connected to the system. Please check your connection", \ file=sys.stderr) return mySensor.begin() while True: print("Humidity:\t%.3f" % mySensor.humidity) print("Pressure:\t%.3f" % mySensor.pressure) print("Altitude:\t%.3f" % mySensor.altitude_feet) print("Temperature:\t%.2f" % mySensor.temperature_fahrenheit) print("") time.sleep(1)
import qwiic_bme280 import time import sys print("\nSparkFun BME280 Sensor Example 1\n") mySensor = qwiic_bme280.QwiicBme280() if mySensor.connected == False: print("The Qwiic BME280 device isn't connected to the system. Please check your connection", \ file=sys.stderr) else: mySensor.begin() while True: print("Humidity:\t%.3f" % mySensor.humidity) print("Pressure:\t%.3f" % mySensor.pressure) print("Altitude:\t%.3f" % mySensor.altitude_feet) print("Temperature:\t%.2f" % mySensor.temperature_fahrenheit) print("") time.sleep(1)
def runExample(): mySensor = qwiic_bme280.QwiicBme280() mySensor.begin() tempC = "%.2f" % mySensor.temperature_celsius print("temp_ambient", tempC)
print("Scanning for Qwiic Devices...") print() devices = qwiic.list_devices() if len(devices) == 0: print("No Devices Found!") exit(1) print("Found the following device(s):") for device in devices: print(device[1]) myJoystick = qwiic_joystick.QwiicJoystick() bme280 = qwiic_bme280.QwiicBme280() ccs811 = qwiic_ccs811.QwiicCcs811() if myJoystick.connected == True: useJoystick = True myJoystick.begin() if bme280.connected == True: useBme280 = True bme280.begin() bme280.filter = 1 # 0 to 4 is valid. Filter coefficient. See 3.4.4 bme280.standby_time = 0 # 0 to 7 valid. Time between readings. See table 27. bme280.over_sample = 1 # 0 to 16 are valid. 0 disables temp sensing. See table 24. bme280.pressure_oversample = 1 # 0 to 16 are valid. 0 disables pressure sensing. See table 23.
def runExample(): print("\nSparkFun BME280 Sensor Example 5\n") mySensor = qwiic_bme280.QwiicBme280() if mySensor.connected == False: print("The Qwiic BME280 device isn't connected to the system. Please check your connection", \ file=sys.stderr) return mySensor.begin() print( "ID(0xD0): 0x%.2x" % mySensor._i2c.readByte(mySensor.address, mySensor.BME280_CHIP_ID_REG)) print("Reset register(0xE0): 0x%.2x" % mySensor._i2c.readByte(mySensor.address, mySensor.BME280_RST_REG)) print("ctrl_meas(0xF4): 0x%.2x" % mySensor._i2c.readByte( mySensor.address, mySensor.BME280_CTRL_MEAS_REG)) print("ctrl_hum(0xF2): 0x%.2x\n" % mySensor._i2c.readByte( mySensor.address, mySensor.BME280_CTRL_HUMIDITY_REG)) print("Displaying all regs:") memCounter = 0x80 for row in range(8, 16): print("0x%.2x 0:" % row, end='') for column in range(0, 16): tempReadData = mySensor._i2c.readByte(mySensor.address, memCounter) print("0x%.2x " % tempReadData, end='') memCounter += 1 print("") print("Displaying concatenated calibration words:") print("dig_T1, uint16: %d" % mySensor.calibration["dig_T1"]) print("dig_T2, int16: %d" % mySensor.calibration["dig_T2"]) print("dig_T3, int16: %d" % mySensor.calibration["dig_T3"]) print("dig_P1, uint16: %d" % mySensor.calibration["dig_P1"]) print("dig_P2, int16: %d" % mySensor.calibration["dig_P2"]) print("dig_P3, int16: %d" % mySensor.calibration["dig_P3"]) print("dig_P4, int16: %d" % mySensor.calibration["dig_P4"]) print("dig_P5, int16: %d" % mySensor.calibration["dig_P5"]) print("dig_P6, int16: %d" % mySensor.calibration["dig_P6"]) print("dig_P7, int16: %d" % mySensor.calibration["dig_P7"]) print("dig_P8, int16: %d" % mySensor.calibration["dig_P8"]) print("dig_P9, int16: %d" % mySensor.calibration["dig_P9"]) print("dig_H1, uint8: %d" % mySensor.calibration["dig_H1"]) print("dig_H2, int16: %d" % mySensor.calibration["dig_H2"]) print("dig_H3, uint8: %d" % mySensor.calibration["dig_H3"]) print("dig_H4, int16: %d" % mySensor.calibration["dig_H5"]) print("dig_H6, int8: %d" % mySensor.calibration["dig_H6"]) while True: print("Humidity:\t%.3f" % mySensor.humidity) print("Pressure:\t%.3f" % mySensor.pressure) print("Altitude:\t%.3f" % mySensor.altitude_feet) print("Temperature:\t%.2f\n" % mySensor.temperature_fahrenheit) time.sleep(1)
def main(): constants.hostname = socket.gethostname() if ((len(constants.hostname) < 1) or (constants.hostname == "undefined")): print("could not determine hostname") sys.exit() os.chdir("/home/pi/Projects/sensor-box") logger.logEvent("sensor-box.py started at " + str(datetime.datetime.now())) # using CCS811 mode 1 for internal measurement every second ccs811Sensor = qwiic_ccs811.QwiicCcs811() bme280Sensor = qwiic_bme280.QwiicBme280() if (HASPM25): pm25Sensor = smbus.SMBus(1) pm1count = -1 pm10count = -1 pm25count = -1 if (ccs811Sensor.connected == False) or (bme280Sensor.connected == False): logger.logEvent("failed to connect to sensors") sys.exit(-1) ccs811Sensor.begin() bme280Sensor.begin() yesterday = 0 today = 1 baseValues = [ 0, 0, 0, 0, 0, 0] baseValueSet = False baseValue = 0 baseIndex = 0 config = constants.hostname + ".conf" if (exists(config)): with open(config) as c: baseValue = int(c.read()) ccs811Sensor.set_baseline(baseValue) baseValueSet = True if (DEBUG): print("read baseline from " + config) while True: today = time.localtime().tm_mday if (yesterday != today): if (DEBUG): print("yesterday", yesterday, "not equal to today",today) logger.logEvent("yesterday " + str(yesterday) + " not equal to today " + str(today)) uptime = os.popen('uptime -s').read() [:-1] logger.logSensorHeader("#boot time " + uptime) logger.logSensorHeader("time,temp,humid,CO2,tVOC,PM1.0,PM2.5,PM10.0") logger.logEventHeader("#boot time " + uptime) logger.logEventHeader("#description of event") baseline = ccs811Sensor.get_baseline() logger.logEvent("#baseline at day rollover " + str(baseline)) yesterday = today if (baseline != baseValue) and (baseValueSet == True): if (DEBUG): logger.logEvent("#baseline " + str(baseline) + " does not match baseValue " + str(baseValue) + ", resetting") ccs811Sensor.set_baseline(baseValue) if (baseValueSet == False): interval = 10 if ((time.localtime().tm_min % interval) == 0): baseline = ccs811Sensor.get_baseline() if (baseline >= constants.maxBaseline): if (DEBUG): logger.logEvent("baseline too high, using 0: " + str(baseline)) baseline = 0 if (DEBUG): logger.logEvent("#baseline " + str(interval) + " min read " + str(baseline)) baseValues[baseIndex] = baseline baseIndex +=1 logger.logEvent("#basevalues " + str(baseValues)) if (baseIndex == 6): baseIndex = 0 if (checkBaseline(baseValues)): logger.logEvent("#baseline set to " + str(baseline)) baseValue = baseline ccs811Sensor.set_baseline(baseValue) with open(config, 'w') as c: c.write(str(baseValue)) baseValueSet = True else: interval = 10 if ((time.localtime().tm_min % interval) == 0): baseline = ccs811Sensor.get_baseline() if (DEBUG): logger.logEvent("#baseline " + str(interval) + " min read " + str(baseline)) if (baseline != baseValue) and (baseValueSet == True): if (DEBUG): logger.logEvent("#baseline " + str(baseline) + " does not match baseValue " + str(baseValue) + ", resetting") ccs811Sensor.set_baseline(baseValue) humidity = bme280Sensor.humidity if (HASPM25): pm25Data = pm25Sensor.read_i2c_block_data(0x12, 0x00, 32) pm1count = (pm25Data[4]<<8) + pm25Data[5] pm10count = (pm25Data[8]<<8) + pm25Data[9] pm25count = (pm25Data[6]<<8) + pm25Data[7] tempCelsius = bme280Sensor.temperature_celsius ccs811Sensor.set_environmental_data(humidity, tempCelsius) #TODO my stretto reads 4C lower than the reported temperature #tempCelsius -= 4 if ccs811Sensor.data_available(): ccs811Sensor.read_algorithm_results() if (ccs811Sensor.CO2 > 2**14): logString = '#error CO2 {:.2f}'.format(ccs811Sensor.CO2) logger.logEvent(logString) elif (ccs811Sensor.TVOC > 2**14): logString = '#error tVOC {:.2f}'.format(ccs811Sensor.TVOC) logger.logEvent(logString) else: logString = '{:.2f},{:.2f},{:d},{:d},{:d},{:d},{:d}'.format( tempCelsius, humidity, ccs811Sensor.CO2, ccs811Sensor.TVOC, pm1count, pm25count, pm10count) logger.logSensor(logString) if DEBUG: print(logString) time.sleep(60)