Skip to content

Commit 7188e44

Browse files
authored
Support for CCS811 CO2/VOC sensor
1 parent 5bb2ce1 commit 7188e44

File tree

5 files changed

+110
-0
lines changed

5 files changed

+110
-0
lines changed

.travis.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -490,3 +490,10 @@ jobs:
490490
- sed -r -i 's/\/\/(#include <sensors\/SensorPN532.h>)/\1/' $SKETCH
491491
- eval $OTA_CONFIGURATION
492492
- eval $COMPILE
493+
- name: "SensorCCS811"
494+
script:
495+
- arduino --install-library "Adafruit CCS811 Library"
496+
- sed -r -i 's/\/\/(SensorCCS811 .+)/\1/' $SKETCH
497+
- sed -r -i 's/\/\/(#include <sensors\/SensorCCS811.h>)/\1/' $SKETCH
498+
- eval $OTA_CONFIGURATION
499+
- eval $COMPILE

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ SensorPca9685Rgb | 2 | Generic RGB-dimmer sensor (S_RGB_LIGHT) used
149149
SensorPca9685Rgbw | 2 | Generic RGBW-dimmer sensor (S_RGBW_LIGHT) used to drive RGBW resp. 4-channel pwm output of PCA9685| https://github.com/adafruit/Adafruit-PWM-Servo-Driver-Library
150150
SensorDSM501A | 1 | Dust sensor module DSM501A for PM1.0 and PM2.5 particles | -
151151
SensorPN532 | 1 | PN532 NFC RFID Module | https://github.com/elechouse/PN532
152+
SensorCCS811 | 1 | CCS811 gas/Air Quality sensor. Measure VOC and eCO2 | https://github.com/adafruit/Adafruit_CCS811
152153

153154
Those sensors requiring a pin to operate would take it as an argument in the constructor.
154155
NodeManager automatically creates all the child_ids, assigning an incremental counter. If you need to set your own child_id, pass it as the last argument to the constructor
@@ -955,6 +956,14 @@ Each sensor class may expose additional methods.
955956
bool getCardIsValid();
956957
~~~
957958

959+
* SensorCCS811
960+
~~~c
961+
// [101] set the temperature for calibrating the sensor
962+
void setTemperature(float value);
963+
// Set to true if the board has a temperature sensor embedded that can be used for calibration (default: false)
964+
void setTemperatureSensor(bool value);
965+
~~~
966+
958967
### OTA Configuration
959968
960969
When `NODEMANAGER_OTA_CONFIGURATION` is set to ON the API presented above can be also called remotely through `SensorConfiguration`, which is automatically added to NodeManager. SensorConfiguration exposes by default child id 200 that can be used to interact with the service by sending `V_CUSTOM` type of messages and commands within the payload. For each `REQ` message, the node will respond with a `SET` message if successful.

examples/Template/Template.ino

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,9 @@ NodeManager. Just uncomment the settings you need and the sensors you want to ad
361361
//#include <sensors/SensorPN532.h>
362362
//SensorPN532 pn532;
363363

364+
//#include <sensors/SensorCCS811.h>
365+
//SensorCCS811 ccs811;
366+
364367
/***********************************
365368
* Main Sketch
366369
*/

keywords.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,3 +217,4 @@ SensorPca9685Rgbw KEYWORD1
217217
SensorPca9685W KEYWORD1
218218
SensorDSM501A KEYWORD1
219219
SensorPN532 KEYWORD1
220+
SensorCCS811 KEYWORD1

sensors/SensorCCS811.h

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/*
2+
* The MySensors Arduino library handles the wireless radio link and protocol
3+
* between your home built sensors/actuators and HA controller of choice.
4+
* The sensors forms a self healing radio network with optional repeaters. Each
5+
* repeater and gateway builds a routing tables in EEPROM which keeps track of the
6+
* network topology allowing messages to be routed to nodes.
7+
*
8+
* Created by Henrik Ekblad <[email protected]>
9+
* Copyright (C) 2013-2017 Sensnology AB
10+
* Full contributor list: https://github.com/mysensors/Arduino/graphs/contributors
11+
*
12+
* Documentation: http://www.mysensors.org
13+
* Support Forum: http://forum.mysensors.org
14+
*
15+
* This program is free software; you can redistribute it and/or
16+
* modify it under the terms of the GNU General Public License
17+
* version 2 as published by the Free Software Foundation.
18+
*/
19+
#ifndef SensorCCS811_h
20+
#define SensorCCS811_h
21+
22+
/*
23+
SensorCCS811
24+
*/
25+
26+
#include "Adafruit_CCS811.h"
27+
28+
class SensorCCS811: public Sensor {
29+
protected:
30+
Adafruit_CCS811* _ccs = new Adafruit_CCS811();
31+
bool _temperature_sensor = false;
32+
33+
public:
34+
SensorCCS811(uint8_t child_id = 0): Sensor(-1) {
35+
_name = "CCS811";
36+
children.allocateBlocks(2);
37+
new Child(this,INT,nodeManager.getAvailableChildId(child_id),S_AIR_QUALITY,V_LEVEL,_name);
38+
new Child(this,INT,child_id > 0 ? nodeManager.getAvailableChildId(child_id+1) : nodeManager.getAvailableChildId(child_id),S_AIR_QUALITY,V_LEVEL,_name);
39+
};
40+
41+
// [101] set the temperature for calibrating the sensor
42+
void setTemperature(float value) {
43+
_ccs->setTempOffset(value - 25.0);
44+
};
45+
46+
// Set to true if the board has a temperature sensor embedded that can be used for calibration (default: false)
47+
void setTemperatureSensor(bool value) {
48+
_temperature_sensor = value;
49+
};
50+
51+
// what to do during setup
52+
void onSetup() {
53+
// initialize the library
54+
if(!_ccs->begin()) debug(PSTR(LOG_SENSOR "%s: KO\n"),_name);
55+
if (_temperature_sensor) {
56+
// use the on-board temperature sensor for calibration
57+
wait(500);
58+
setTemperature(_ccs->calculateTemperature());
59+
}
60+
};
61+
62+
// what to do during loop
63+
void onLoop(Child* child) {
64+
if (_ccs->available() && ! _ccs->readData()) {
65+
if (child == children.get(1)) {
66+
// eCO2
67+
child->setValue((int)_ccs->geteCO2());
68+
}
69+
if (child == children.get(2)) {
70+
// TVOC
71+
child->setValue((int)_ccs->getTVOC());
72+
}
73+
} else {
74+
debug(PSTR(LOG_SENSOR "%s: ERROR\n"),_name);
75+
}
76+
// we need to wait otherwise during loop, two consecutive readData() would result in an error
77+
wait(500);
78+
};
79+
80+
#if NODEMANAGER_OTA_CONFIGURATION == ON
81+
// define what to do when receiving an OTA configuration request
82+
void onOTAConfiguration(ConfigurationRequest* request) {
83+
switch(request->getFunction()) {
84+
case 101: setTemperature(request->getValueFloat()); break;
85+
default: return;
86+
}
87+
};
88+
#endif
89+
};
90+
#endif

0 commit comments

Comments
 (0)