Created
January 23, 2021 17:55
-
-
Save JsBergbau/7c962dc3507e25e0145a5cd6c8bf372d to your computer and use it in GitHub Desktop.
CCS811Tester
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python3 | |
import os | |
import smbus | |
import time | |
#On Rasbperry PI please install smbus via sudo apt install python3-smbus | |
ccs811Address = 0x5A | |
ccs811StatusRegister = 0x00 | |
ccs811MeasurementMode = 0x01 | |
CCS811_ALG_RESULT_DATA = 0x02 | |
ccs811MeasurementModeRegister = 0x01 | |
CCS811_BOOTLOADER_APP_START = 0xF4 | |
ccs811BaselineRegister = 0x11 | |
i2cInterface = smbus.SMBus(1) | |
running = None | |
ressult = i2cInterface.read_byte_data(ccs811Address, ccs811StatusRegister) | |
if (ressult & 0b10000000) == 128: | |
running = True | |
else: | |
running = False | |
print("Sensor not running") | |
if not running: | |
print("Booting sensor") | |
i2cInterface.write_i2c_block_data(ccs811Address, CCS811_BOOTLOADER_APP_START,[]) | |
modus = i2cInterface.read_byte_data(ccs811Address,ccs811MeasurementModeRegister) | |
if modus != 0b00010000: | |
print("Sensor not in desired mode, changing") | |
i2cInterface.write_byte_data(ccs811Address,ccs811MeasurementMode,0b00010000) | |
time.sleep(0.1) | |
baseline= i2cInterface.read_i2c_block_data(ccs811Address, ccs811BaselineRegister, 2) | |
print("Baseline:", baseline) | |
ressult = i2cInterface.read_i2c_block_data(ccs811Address, CCS811_ALG_RESULT_DATA, 8) | |
eCO2 = ressult[0] * 256 + ressult [1] | |
TVOC = ressult [2] * 256 + ressult[3] | |
print ("eCO2: ", eCO2, " TVOC: ", TVOC) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment