def run(): """Main entry point to execute this program.""" if _isFirstRun(): _setRunsToCondition(CONDITIONING_RUNS) ccs = ccs811.CCS811(i2c, mode=ccs811.CCS811.DRIVE_MODE_60SEC) t, p, h = bme.read_data() # Full update of Waveshare on power on scr.update(t, h, None, None, bat.volts(), True) else: ccs = ccs811.CCS811(i2c, mode=None) _addRun() try: ccs.read() t, p, h = bme.read_data() ccs.put_envdata(t, h) if _isConditioned(): scr.update(t, h, ccs.eco2, ccs.tvoc, bat.volts()) else: scr.update(t, h, None, None, bat.volts()) print('eCO2: %dppm, TVOC: %dppb, %.1fC, %.1f%%RH' % (ccs.eco2, ccs.tvoc, t, h)) except OSError as e: print(e) _flashled() scr.sleep() esp32.wake_on_ext0(pin=config.int, level=0) machine.deepsleep()
def run(): """Main entry point to execute this program.""" try: bme = bme280.BME280(i2c=_i2c, mode=bme280.BME280_OSAMPLE_4) scr = screen.Screen(config) if _delete_ccs811_baseline_requested(): _baseline.delete() if _is_first_run(): # 20 runs (minutes), p9 of datasheet _set_runs_to_condition(20) ccs = ccs811.CCS811(_i2c, mode=ccs811.CCS811.DRIVE_MODE_60SEC) t, p, h = bme.read_data() # Full update of Waveshare on power on scr.update(t, h, None, None, _bat.volts(), False, True) else: ccs = ccs811.CCS811(_i2c, mode=None) _add_run() ccs.read() t, p, h = bme.read_data() ccs.put_envdata(t, h) if _ccs811_is_conditioned(): # Stored baseline should only be loaded after conditioning if not _ccs811_baseline_is_loaded() and _baseline.exists(): baseline = _baseline.retrieve() ccs.put_baseline(baseline) _set_ccs811_baseline_loaded() scr.update(t, h, None, None, _bat.volts()) print('ccs811 baseline %d loaded' % baseline) else: scr.update(t, h, ccs.eco2, ccs.tvoc, _bat.volts(), _ccs811_baseline_is_loaded()) if _new_ccs811_baseline_requested(): baseline = ccs.get_baseline() _baseline.store(baseline) print('ccs811 baseline %d stored' % baseline) else: scr.update(t, h, None, None, _bat.volts()) print('eCO2: %dppm, TVOC: %dppb, %.1fC, %.1f%%RH, baseline: %r' % (ccs.eco2, ccs.tvoc, t, h, _ccs811_baseline_is_loaded())) scr.sleep() _flash_led() except Exception as e: _flash_led(3) print(e) esp32.wake_on_ext0(pin=config.int, level=0) machine.deepsleep()
def __init__(self, i2cvayla=0, scl=22, sda=21, taajuus=400000, osoite=90): self.i2c = I2C(i2cvayla, scl=Pin(scl), sda=Pin(sda), freq=taajuus) self.laiteosoite = osoite self.sensori = ccs811.CCS811(self.i2c) self.eCO2 = 0 self.tVOC = 0 self.eCO2_keskiarvo = 0 self.eCO2_arvoja = 0 self.tVOC_keskiarvo = 0 self.tVOC_arvoja = 0 self.luettu_aika = utime.time()
import usocket as socket import utime as time import ubinascii as binascii import ccs811 import bme280 import esp8266_i2c_lcd from machine import I2C, Pin time.sleep(3) i2c = I2C(scl=Pin(2), sda=Pin(4), freq=100000) bme = bme280.BME280(i2c=i2c) ccs = ccs811.CCS811(i2c) lcd = esp8266_i2c_lcd.I2cLcd(i2c, 39, 2, 16) sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) address = ("0.0.0.0", 2003) delay_time = 5 def get_time(): lc = binascii.hexlify(i2c.readfrom_mem(104, 0x00, 7)) lt = (2000 + int(lc[12:14]), int(lc[10:12]), int(lc[8:10]), int(lc[4:6]), int(lc[2:4]), int(lc[0:2]), int(lc[6:8]), 0) return time.mktime(lt) + 946674000 while True: time.sleep(delay_time) lcd.clear() lcd.move_to(2, 0) lcd.putstr('Temperature') lcd.move_to(4, 1)
def main(): me = singleton.SingleInstance( ) # will sys.exit(-1) if other instance is running spinner = spinning_cursor() # Intitialize sensors try: print("Intializing BME280 Sensor") i2c = busio.I2C(board.SCL, board.SDA) bme280 = adafruit_bme280.Adafruit_BME280_I2C(i2c) bme280.sea_level_pressure = 1019.6 # 1013.25 is the standard value. Optimized by altitude, initial data from https://forecast.weather.gov/product.php?issuedby=BOU&product=OSO&site=bou except Exception as ex: print("Error intializing BME280 Sensor:", sys.exc_info()[0]) report_error(ex) exit() try: print("Intializing CCS811 Sensor") aqm = ccs.CCS811() aqm.setup() except Exception as ex: print("Error intializing CCS811 Sensor:", sys.exc_info()[0]) report_error(ex) exit() # configure Google Cloud settings credentials = GoogleCredentials.get_application_default() publisher = pubsub.PublisherClient() datetimeFormat = '%Y-%m-%d %H:%M:%S.%f' start_time = datetime.datetime.utcnow() while True: current_time = datetime.datetime.utcnow() sensorTime = current_time.strftime('%Y-%m-%d %H:%M:%S') #capture BME readings temp = '{0:0.2f}'.format(bme280.temperature) humidity = '{0:0.2f}'.format(bme280.humidity) dew_point = '{0:0.2f}'.format( dewpoint(bme280.temperature, bme280.humidity)) pressure = '{0:0.2f}'.format(bme280.pressure) altitude = '{0:0.2f}'.format( bme280.altitude ) # not capturing (assuming stationary!) but displaying # capture CCS811 readings try: if aqm.data_available(): aqm.read_logorithm_results() eco2 = aqm.CO2 tvoc = aqm.tVOC elif aqm.check_for_error(): aqm.print_error() except Exception as ex: print("Error condition detected reading CCS811 sensor: ", sys.exc_info()[0]) report_error(ex) print( "\033c" ) # an admitedly hack way to clear the screen on a linux terminal print("Date - Time:", sensorTime) print("Temperature: %s C" % temp) print(" Humidity: %s rh" % humidity) print(" Dewpoint: %s C" % dew_point) print(" Pressure: %s hPa" % pressure) print(" Altitude: %s m" % altitude) print(" eCO2: %d PPM " % eco2) print(" tVOC: %d PPB " % tvoc) run_time = current_time - start_time if run_time.seconds < warmup_delay: # elapsed_time = timedelta(run_time) print("Warm up mode (20 minutes) Elapsed time:", str(run_time).split('.')[0]) else: print("Saving data") sensorJSON = createJSON(sensorID, sensorTime, sensorZipCode, sensorLat, sensorLong, temp, humidity, dew_point, pressure, str(tvoc), str(eco2)).encode('utf-8') # print("Sensor Data JSON created") # print("sensorJSON:", sensorJSON) try: publisher.publish(topicName, sensorJSON, placeholder='') except Exception as ex: print("Error condition detected sending sensor data: ", sys.exc_info()[0]) report_error(ex) if run_time.seconds < warmup_delay: #read more while warming up! time.sleep(2) else: print("Pausing for", interval, "seconds") sys.stdout.write('\033[s') # save cursor location while ((datetime.datetime.utcnow() - current_time).seconds) < (interval - 1): sys.stdout.write( next(spinner) + " " + str((datetime.datetime.utcnow() - current_time).seconds)) sys.stdout.flush() sys.stdout.write( '\033[u') # move back to saved cursor location time.sleep(.5)
import smbus2 import sys import ccs811 address = int(sys.argv[1], 16) assert address in [0x5a, 0x5b] with smbus2.SMBus(1) as bus: ccs811.CCS811(bus, address).reset()