CODE: xxxx.bas
'#######################################################################
'Show AIR QUALITY with a CCS811 CO2-TVOC-Sensor at I2C-pins of an ESP32
'#######################################################################
' DB9JG 2021/02/13 V1.0
Version$ = "V1.0"
SILENT = 0 '1 => suppress all messages 0 => show some status-messages via wlog
SDA_PIN = 21 'SDA=21 at ESP32
SCL_PIN = 22 'SCL=22 at ESP32
CCS811temp = 0
CCS811_schreiben = &hb4 ' Die I2C-Adresse von CCS811 zum schreiben hB4 =1011 0100 7 Bit Plus das Bit 0 für lesen (0)
CCS811_lesen = &hb5 ' Die I2C-Adresse von CCS811 zum lesen hB5 = 10110101 7 Bit Plus das Bit 0 für schreiben (1)
STATUS_REG = &h00
MEAS_MODE_REG = &h01
ALG_RESULT_DATA = &h02
ENV_DATA = &h05
NTC_REG = &h06
THRESHOLDS = &h10
BASELINE = &h11
HW_ID_REG = &h20
ERROR_ID_REG = &hE0
APP_START_REG = &hF4
SW_RESET = &hFF
CCS811_I2C_ADR = &h5A
GPIO_WAKE = &h5
DRIVE_MODE_IDLE = &h0
DRIVE_MODE_1SEC = &h10
DRIVE_MODE_10SEC = &h20
DRIVE_MODE_60SEC = &h30
INTERRUPT_DRIVEN = &h8
THRESHOLDS_ENABLED = &h4
Dim BUFFER(10) 'I2C-Sende- / Empfangspuffer.
if not silent GOSUB I2C_SCANNER
I2C.SETUP SDA_PIN, SCL_PIN ' set I2C ports
GOSUB SHOW_CCS811_STATUS
GOSUB CCS811_GO_APP_MODE 'leave BOOT-Mode
GOSUB SHOW_CCS811_STATUS
GOSUB CCS811_SET_MODE 'CCS811 generates eCO2 and TVOC once per second
GOSUB SHOW_CCS811_STATUS
TIMER0 2000, CCS811_SHOW 'fetch eCO2 and TVOC from CCS811 sensor
WAIT
end '!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
'!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
'######################################################################
CCS811_SHOW:
GOSUB CCS811_READ
wlog "eCO2 = ";eCO2,"TVOC = ";TVOC
return
'######################################################################
CCS811_READ:
' Bit 3 = Data Ready 0:no new Data 1:new Data
' Bit 0 Error dedection 0:no Error 1: Error on i2c or Sensor
i2c.writeRegByte CCS811_I2C_ADR, STATUS_REG , 0
i2c.ReqFrom CCS811_I2C_ADR,1
CCS811temp = i2c.read
If CCS811temp and 8 then 'if Bit3 of status_register =1 then DATA_available
i2c.writeRegByte CCS811_I2C_ADR, ALG_RESULT_DATA ,0 'selects the Result mailbox
i2c.ReqFrom CCS811_I2C_ADR, 8
for i = 1 to 8 'The Result mailbox contains 8 Bytes
BUFFER(i) = i2c.read
next
eCO2 = 256*BUFFER(1) + BUFFER(2) 'eCO2 ppm
TVOC = 256*BUFFER(3) + BUFFER(4) 'eTVOC
else
eCO2 = 0
TVOC = 0
end if
return
'#########################################################################################################################################################
'######################################################################
SHOW_CCS811_STATUS:
'----------------------Status request-------------------------------------
' Bit 7 FW_Mode = 0:Boot Mode 1:Application Mode
' Bit 4 = Application Firmware loaded 0: no 1:Valid application loaded
' Bit 3 = Data Ready 0:no new Data 1:new Data
' Bit 0 Error dedection 0:no Error 1: Error on i2c or Sensor
if silent return
i2c.writeRegByte CCS811_I2C_ADR, STATUS_REG , 0
i2c.ReqFrom CCS811_I2C_ADR,1
CCS811temp = i2c.read
pause 25
wlog ""
wlog "-------------CCS811-Sensor_Status:-------------"
wlog "STATUSBYTE :",CCS811temp,"= &b"; bin$(CCS811temp)
If CCS811temp and 128 then
wlog " Application Mode"
else
wlog " Boot Mode"
end if
If CCS811temp and 16 then
wlog " valid firmware loaded"
else
wlog " NO valid firmware loaded!"
end if
If CCS811temp and 8 then
wlog " DATA available to read"
else
wlog " no DATA available to read"
end if
If CCS811temp and 1 then
wlog " Error on i2c or sensor!!"
'Read the ERROR-ID
i2c.writeRegByte CCS811_I2C_ADR, ERROR_ID_REG , 0
i2c.ReqFrom CCS811_I2C_ADR,1
CCS811temp = i2c.read
wlog " ERROR_ID_REG : dec",CCS811temp,"= &b";bin$(CCS811temp)
else
wlog " no Error"
end if
wlog "----------------------------------------"
wlog " "
RETURN
'######################################################################
CCS811_GO_APP_MODE:
'----------------------- Change to Application Mode -------------------
if not silent wlog "Change from BOOT-Mode to App Mode"
'i2c.writeRegByte CCS811_I2C_ADR, APP_START_REG,0
' geht nicht da value geschreiben wird, Register soll aber nur angewählt werden
'Aber DAS funktioniert:
i2c.begin CCS811_I2C_ADR
I2c.write APP_START_REG
' value hier NICHT schreiben!!!
' i2c.write value
i2c.end
pause 10
return
'######################################################################
CCS811_SET_MODE:
'------------------------Modus einstellen---------------------------
MODUS = &b00010000 'Bit7=0reserved, 6-4=001Mode 1 jede sekunde neue daten, 3=0 Interrupt für neue Daten aus, 2=0 Interrupt Mode Normal, 1-0 =00 Reserved
if not silent wlog "Set MEAS_MODE_REG to &b:"; bin$(MODUS); " = read new Data once per 1 second"
i2c.writeRegByte CCS811_I2C_ADR, MEAS_MODE_REG, MODUS
pause 10
return
'######################################################################
I2C_SCANNER:
'I2C Address Scanner
'print and wlog the address of the I2C-devices found
I2C.SETUP 21, 22 ' set I2C port on pins 21 and 22
print "---start-I2C-scan---"
wlog "---start-I2C-scan---"
for i = 0 to 120
i2c.begin i
if i2c.end = 0 then
print "found a device at I2C-Adr dec "; i ,", hex "; hex$(i)
wlog "found a device at I2C-Adr dec "; i ,", hex "; hex$(i)
pause 10
end if
next i
print "---end-I2C-scan---"
wlog "---end-I2C-scan---"
return