Skip to content

Instantly share code, notes, and snippets.

@magik6k
Created April 15, 2019 22:02
Show Gist options
  • Save magik6k/d407705673793bf800a79a7e3cd9c222 to your computer and use it in GitHub Desktop.
Save magik6k/d407705673793bf800a79a7e3cd9c222 to your computer and use it in GitHub Desktop.
/*
ccs811basic.ino - Demo sketch printing results of the CCS811 digital gas sensor for monitoring indoor air quality from ams.
Created by Maarten Pennings 2017 Dec 11
*/
#include <Wire.h> // I2C library
#include <ESP8266WiFi.h>
#include <InfluxDb.h>
#include "ccs811.h" // CCS811 library
#define INFLUXDB_HOST "10.1.6.1"
#define INFLUXDB_PORT 8086
#define INFLUXDB_DATABASE "sensors_db"
//if used with authentication
#define INFLUXDB_USER "sersors"
#define INFLUXDB_PASS "uietdhbuitx"
// Wiring for ESP8266 NodeMCU boards: VDD to 3V3, GND to GND, SDA to D2, SCL to D1, nWAKE to D3 (or GND)
CCS811 ccs811(D3); // nWAKE on D3
// Wiring for Nano: VDD to 3v3, GND to GND, SDA to A4, SCL to A5, nWAKE to 13
//CCS811 ccs811(13);
// nWAKE not controlled via Arduino host, so connect CCS811.nWAKE to GND
//CCS811 ccs811;
// Wiring for ESP32 NodeMCU boards: VDD to 3V3, GND to GND, SDA to 21, SCL to 22, nWAKE to D3 (or GND)
//CCS811 ccs811(23); // nWAKE on 23
Influxdb *influx;
void setup() {
// Enable serial
Serial.begin(115200);
Serial.println("");
Serial.println("setup: Starting CCS811 basic demo");
Serial.print("setup: ccs811 lib version: "); Serial.println(CCS811_VERSION);
WiFi.begin("Alpha Base - Things", "thingsaremagic");
Serial.print("WiFi Connecting");
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.println();
Serial.print("Connected, IP address: ");
Serial.println(WiFi.localIP());
influx = new Influxdb(INFLUXDB_HOST, INFLUXDB_PORT);
influx->setDbAuth(INFLUXDB_DATABASE, INFLUXDB_USER, INFLUXDB_PASS);
// Enable I2C
Wire.begin();
// Enable CCS811
ccs811.set_i2cdelay(50); // Needed for ESP8266 because it doesn't handle I2C clock stretch correctly
bool ok= ccs811.begin();
if( !ok ) Serial.println("setup: CCS811 begin FAILED");
// Print CCS811 versions
Serial.print("setup: hardware version: "); Serial.println(ccs811.hardware_version(),HEX);
Serial.print("setup: bootloader version: "); Serial.println(ccs811.bootloader_version(),HEX);
Serial.print("setup: application version: "); Serial.println(ccs811.application_version(),HEX);
// Start measuring
ok= ccs811.start(CCS811_MODE_1SEC);
if( !ok ) Serial.println("setup: CCS811 start FAILED");
ccs811.set_baseline(0xFEC2);
}
void loop() {
// Read
uint16_t eco2, etvoc, errstat, raw;
ccs811.read(&eco2,&etvoc,&errstat,&raw);
// Print measurement results based on status
if( errstat==CCS811_ERRSTAT_OK ) {
Serial.print("CCS811: ");
Serial.print("eco2="); Serial.print(eco2); Serial.print(" ppm ");
Serial.print("etvoc="); Serial.print(etvoc); Serial.print(" ppb ");
//Serial.print("raw6="); Serial.print(raw/1024); Serial.print(" uA ");
//Serial.print("raw10="); Serial.print(raw%1024); Serial.print(" ADC ");
Serial.print("R="); Serial.print((1650*1000L/1023)*(raw%1024)/(raw/1024)); Serial.print(" ohm");
Serial.println();
InfluxData measurement ("ccs");
measurement.addValue("eco2", eco2);
measurement.addValue("etvoc", etvoc);
measurement.addValue("r", (int)(1650*1000L/1023)*(raw%1024)/(raw/1024));
measurement.addTag("sensor", String(ESP.getChipId(), HEX));
influx->write(measurement);
} else if( errstat==CCS811_ERRSTAT_OK_NODATA ) {
Serial.println("CCS811: waiting for (new) data");
} else if( errstat & CCS811_ERRSTAT_I2CFAIL ) {
Serial.println("CCS811: I2C error");
} else {
Serial.print("CCS811: errstat="); Serial.print(errstat,HEX);
Serial.print("="); Serial.println( ccs811.errstat_str(errstat) );
}
// Wait
delay(1000);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment