Created
February 2, 2022 19:53
-
-
Save hellvesper/3668ba5e58a0fe156bbfa2f8ddaab835 to your computer and use it in GitHub Desktop.
This file contains 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
#include <Arduino.h> | |
#include <Wire.h> // I2C library | |
#include "ccs811.h" // CCS811 library | |
#if defined(ESP32) | |
#include <WiFiMulti.h> | |
WiFiMulti wifiMulti; | |
#define DEVICE "ESP32" | |
#elif defined(ESP8266) | |
#include <ESP8266WiFiMulti.h> | |
ESP8266WiFiMulti wifiMulti; | |
#define DEVICE "ESP8266" | |
#endif | |
#include <InfluxDbClient.h> | |
#include <InfluxDbCloud.h> | |
// WiFi AP SSID | |
#define WIFI_SSID "MY SSID" | |
// WiFi password | |
#define WIFI_PASSWORD "wifi_passwd" | |
// InfluxDB v2 server url, e.g. https://eu-central-1-1.aws.cloud2.influxdata.com (Use: InfluxDB UI -> Load Data -> Client Libraries) | |
#define INFLUXDB_URL "https://influx.local:8086" | |
// InfluxDB v2 server or cloud API token (Use: InfluxDB UI -> Data -> API Tokens -> <select token>) | |
#define INFLUXDB_TOKEN "INs5123456-w==" | |
// InfluxDB v2 organization id (Use: InfluxDB UI -> User -> About -> Common Ids ) | |
#define INFLUXDB_ORG "home" | |
// InfluxDB v2 bucket name (Use: InfluxDB UI -> Data -> Buckets) | |
#define INFLUXDB_BUCKET "sensors" | |
// Set timezone string according to https://www.gnu.org/software/libc/manual/html_node/TZ-Variable.html | |
// Examples: | |
// Pacific Time: "PST8PDT" | |
// Eastern: "EST5EDT" | |
// Japanesse: "JST-9" | |
// Central Europe: "CET-1CEST,M3.5.0,M10.5.0/3" | |
#define TZ_INFO "EET-2EEST,M3.4.0/3,M10.4.0/4" | |
// InfluxDB client instance with preconfigured InfluxCloud certificate | |
InfluxDBClient client(INFLUXDB_URL, INFLUXDB_ORG, INFLUXDB_BUCKET, INFLUXDB_TOKEN, InfluxDbCloud2CACert); | |
// Data point | |
Point sensor("wifi_status"); | |
Point ccs_sensor("ccs811"); | |
// 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; | |
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); | |
// Enable I2C | |
// Wire.pins(D4, D5); | |
Wire.begin(D4, D5); | |
// 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"); | |
// Setup wifi | |
WiFi.mode(WIFI_STA); | |
wifiMulti.addAP(WIFI_SSID, WIFI_PASSWORD); | |
Serial.print("Connecting to wifi"); | |
while (wifiMulti.run() != WL_CONNECTED) | |
{ | |
Serial.print("."); | |
delay(100); | |
} | |
Serial.println(); | |
// Add tags | |
sensor.addTag("device", DEVICE); | |
sensor.addTag("SSID", WiFi.SSID()); | |
// Accurate time is necessary for certificate validation and writing in batches | |
// For the fastest time sync find NTP servers in your area: https://www.pool.ntp.org/zone/ | |
// Syncing progress and the time will be printed to Serial. | |
timeSync(TZ_INFO, "pool.ntp.org", "time.nis.gov"); | |
// Check server connection | |
if (client.validateConnection()) | |
{ | |
Serial.print("Connected to InfluxDB: "); | |
Serial.println(client.getServerUrl()); | |
} | |
else | |
{ | |
Serial.print("InfluxDB connection failed: "); | |
Serial.println(client.getLastErrorMessage()); | |
} | |
// =========== restore baseline from db =========== | |
// Construct a Flux query | |
// Query will list RSSI for last 24 hours for each connected WiFi network of this device type | |
String query = "from(bucket: \"" INFLUXDB_BUCKET "\") |> range(start: -1h) |> filter(fn: (r) => r._measurement == \"ccs811\" and r._field == \"baseline\")"; | |
query += " |> last()"; | |
// Send query to the server and get result | |
FluxQueryResult result = client.query(query); | |
// Iterate over rows. Even there is just one row, next() must be called at least once. | |
result.next(); | |
FluxValue val = result.getValueByName("_value"); | |
uint16_t baseline = (uint16_t)val.getLong(); | |
Serial.print("Previous baseline: "); | |
Serial.println(baseline); | |
// uint16_t baseline = 42428; | |
ccs811.set_baseline(baseline); | |
Serial.println("Baseline restored"); | |
} | |
void loop() | |
{ | |
// Read | |
uint16_t eco2, etvoc, errstat, raw, baseline; | |
float raw6, raw10, resist; | |
ccs811.read(&eco2, &etvoc, &errstat, &raw); | |
ccs811.get_baseline(&baseline); | |
// Print measurement results based on status | |
if (errstat == CCS811_ERRSTAT_OK) | |
{ | |
raw6 = raw / 1024; | |
raw10 = raw % 1024; | |
resist = (1650 * 1000L / 1023) * (raw % 1024) / (raw / 1024); | |
Serial.print("CCS811: "); | |
Serial.print("eco2="); | |
Serial.print(eco2); | |
Serial.print(" ppm "); | |
Serial.print("etvoc="); | |
Serial.print(etvoc); | |
Serial.print(" ppb "); | |
Serial.print(" raw="); | |
Serial.print(raw); | |
Serial.print("raw6="); Serial.print(raw6); Serial.print(" uA "); | |
Serial.print("raw10="); Serial.print(raw10); Serial.print(" ADC "); | |
Serial.print("R="); Serial.print(resist); Serial.print(" ohm"); | |
Serial.println(); | |
Serial.print("baseline: "); | |
Serial.print(baseline); | |
Serial.print(" | "); | |
Serial.println(baseline, HEX); | |
} | |
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)); | |
} | |
// Clear fields for reusing the point. Tags will remain untouched | |
sensor.clearFields(); | |
ccs_sensor.clearFields(); | |
// Store measured value into point | |
// Report RSSI of currently connected network | |
sensor.addField("rssi", WiFi.RSSI()); | |
ccs_sensor.addField("eco2", eco2); | |
ccs_sensor.addField("etvoc", etvoc); | |
ccs_sensor.addField("baseline", baseline); | |
ccs_sensor.addField("raw", raw); | |
ccs_sensor.addField("raw6", raw6); | |
ccs_sensor.addField("raw10", raw10); | |
ccs_sensor.addField("resist", resist); | |
ccs_sensor.addField("errstat", errstat); | |
// Print what are we exactly writing | |
Serial.print("Writing: "); | |
Serial.println(sensor.toLineProtocol()); | |
Serial.println(ccs_sensor.toLineProtocol()); | |
// Check WiFi connection and reconnect if needed | |
if (wifiMulti.run() != WL_CONNECTED) | |
{ | |
Serial.println("Wifi connection lost"); | |
} | |
// Write point | |
if (errstat == CCS811_ERRSTAT_OK) { | |
if (!client.writePoint(sensor) || !client.writePoint(ccs_sensor)) | |
{ | |
Serial.print("InfluxDB write failed: "); | |
Serial.println(client.getLastErrorMessage()); | |
} | |
} | |
Serial.println("Wait 10s"); | |
delay(1000); | |
} |
This file contains 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
; PlatformIO Project Configuration File | |
; | |
; Build options: build flags, source filter | |
; Upload options: custom upload port, speed and extra flags | |
; Library options: dependencies, extra library storages | |
; Advanced options: extra scripting | |
; | |
; Please visit documentation for the other options and examples | |
; https://docs.platformio.org/page/projectconf.html | |
[env:nodemcuv2] | |
platform = espressif8266 | |
board = nodemcuv2 | |
framework = arduino | |
monitor_speed = 115200 | |
lib_deps = | |
maarten-pennings/CCS811 @ ^12.0.0 | |
; https://github.com/tobiasschuerg/InfluxDB-Client-for-Arduino.git#v3.4.0 | |
tobiasschuerg/ESP8266 Influxdb @ ^3.10.0 | |
[ota] | |
esp_name = ccs811 | |
esp_pw = passwd | |
[env:ccs811_ota] | |
platform = espressif8266 | |
board = nodemcuv2 | |
framework = arduino | |
upload_port = /dev/cu.usbserial-1410 | |
monitor_speed = 115200 | |
; upload_port = /dev/cu.usbserial-A50285BI | |
build_flags = -DESP_NAME=\"${ota.esp_name}\" -DESP_PW=\"${ota.esp_pw}\" | |
; upload_flags = --auth=${ota.esp_pw} ;#uncomment after initial flash | |
; upload_protocol = espota | |
; upload_port = ${ota.esp_name}.local ;#uncomment after initial flash | |
; upload_port = 192.168.88.182 ;#uncomment after initial flash | |
; upload_port = 192.168.88.190 ;#uncomment after initial flash | |
lib_deps = | |
; # Using a library name | |
gitlab-regenbogencode/[email protected]+sha.59a1299a1f | |
maarten-pennings/CCS811 @ ^12.0.0 | |
tobiasschuerg/ESP8266 Influxdb @ ^3.10.0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment