├── .gitignore ├── esp-ccs811-bme280-breadboard.jpg ├── esp-ccs811-bme280-mqtt ├── platformio.ini ├── config.example.h └── esp-ccs811-bme280-mqtt.ino ├── LICENSE └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | esp-ccs811-bme280-mqtt/config.h 2 | -------------------------------------------------------------------------------- /esp-ccs811-bme280-breadboard.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codmpm/esp-ccs811-bme280-mqtt/HEAD/esp-ccs811-bme280-breadboard.jpg -------------------------------------------------------------------------------- /esp-ccs811-bme280-mqtt/platformio.ini: -------------------------------------------------------------------------------- 1 | ; PlatformIO Project Configuration File 2 | ; 3 | ; Build options: build flags, source filter 4 | ; Upload options: custom upload port, speed and extra flags 5 | ; Library options: dependencies, extra library storages 6 | ; Advanced options: extra scripting 7 | ; 8 | ; Please visit documentation for the other options and examples 9 | ; http://docs.platformio.org/page/projectconf.html 10 | 11 | [platformio] 12 | src_dir = . 13 | 14 | [env:default] 15 | platform = espressif8266 16 | board = d1_mini 17 | framework = arduino 18 | monitor_speed = 115200 19 | upload_speed = 921600 20 | lib_deps = 21 | Tasker 22 | PubSubClient 23 | Adafruit CCS811 Library 24 | SparkFun BME280 25 | 26 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 Patrik Mayer 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /esp-ccs811-bme280-mqtt/config.example.h: -------------------------------------------------------------------------------- 1 | /* config file for esp ccs bme280 mqtt project */ 2 | 3 | /* 4 | * Main Settings 5 | * 6 | * measureDelay: defines how often data will be pulled from the sensors (in ms, means 60000 = 60s) 7 | */ 8 | const unsigned long measureDelay = 60000; 9 | 10 | /* 11 | * These are the current Wifi Settings 12 | * 13 | * ssid: wifi name 14 | * password: wifi access key 15 | * 16 | * hostname and OTA name are defined by mqttClientName below 17 | */ 18 | const char* ssid = ""; 19 | const char* password = ""; 20 | 21 | /* 22 | * Settings for the MQTT Broker, which shall be used 23 | * 24 | * mqttServer: IP or Hostname of mqttBroker 25 | * mqttUser: username on MQTT server 26 | * mqttPass: users password 27 | * mqttClientName: clientId on MQTT broker 28 | * mqttTopicPrefix: prefix for your topic (e.g. status will be shown in mqttTopicPrefix + "/status") 29 | */ 30 | const char* mqttServer = ""; 31 | const char* mqttUser = ""; 32 | const char* mqttPass = ""; 33 | const char* mqttClientName = ""; //will also be used hostname and OTA name 34 | const char* mqttTopicPrefix = ""; 35 | 36 | /* 37 | * GPIO Settings 38 | * 39 | * LED_BUILTIN: Digial Pin Number of your boards built-in LED or 40 | * the LED you wish to show status signals 41 | */ 42 | #define LED_BUILTIN 2 //ESP-12F has the builtin LED on GPIO2, comment for other boards 43 | 44 | /* 45 | * CCS811 46 | * 47 | * WAKE_PIN: Digial Pin Number, where your BME280s wake signal is connected 48 | */ 49 | #define WAKE_PIN 15 50 | 51 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # ESP8266 MQTT CCS811/BME280 air quality sensor 2 | 3 | This is a simple sketch to measure the air quality with the [CCS811 Air Quality Sensor](http://ams.com/eng/Products/Environmental-Sensors/Air-Quality-Sensors/CCS811). It get's compensated with an [Bosch BME280](https://www.bosch-sensortec.com/bst/products/all_products/bme280). 4 | 5 | I'm using the Adafruit CCS811 library which worked after I used the correct I2C address of 0x5A for my sensor. 6 | 7 | __Currently I'm testing if my setup works and the values are reliable - so please see this as a work in progress. Any Feedback and help is appreciated!__ 8 | 9 | --- 10 | 11 | ![image of breadboard ](esp-ccs811-bme280-breadboard.jpg) 12 | Used here are the [Sparkfun CCS811](https://www.sparkfun.com/products/14193) and the [Adaferuit BME280](https://www.adafruit.com/product/2652) breakout. 13 | 14 | ## Config 15 | Before compiling, you must implement your config by copying the file `config.example.h` to `config.h` in directory `esp-ccs811-bme280-mqtt`. After that you have to fill or change the values in `config.h`. 16 | 17 | The read data get's dropped to the following topics using ``. All values are retained, the online state is set to `offline` using mqtt's last-will. 18 | 19 | ``` 20 | status online/offline (last will) 21 | ip system ip 22 | temperature temperature in °C 23 | humidity relative humidity in % 24 | pressure pressure in hPa 25 | altitude altitude in m 26 | co2 CO2 concentration in ppm 27 | tvoc total volatile compound in ppb 28 | ``` 29 | 30 | ## Todo 31 | - Check Sparkfuns CCS811 lib 32 | - better configuration 33 | 34 | ## Created with 35 | - Arduino 1.8.1 (https://www.arduino.cc/) 36 | - ESP8266 board definition 2.4.0 (https://github.com/esp8266/Arduino) 37 | - Tasker (https://github.com/sticilface/Tasker) 38 | - PubSubClient 2.6.0 by Nick O'Leary (https://github.com/knolleary/pubsubclient) 39 | - Adafruit CCS811 Library (https://github.com/adafruit/Adafruit_CCS811) 40 | - Sparkfun BME280 Library (https://github.com/sparkfun/SparkFun_CCS811_Arduino_Library) 41 | 42 | ### Credits 43 | Patrik Mayer, 2018 44 | Tobias Schmitt, 2018 45 | 46 | ### License 47 | MIT 48 | -------------------------------------------------------------------------------- /esp-ccs811-bme280-mqtt/esp-ccs811-bme280-mqtt.ino: -------------------------------------------------------------------------------- 1 | /**************************************************** 2 | ESP8266 CSS811 compensated via BME280 with MQTT 3 | 2018, Patrik Mayer - patrik.mayer@codm.de 4 | 5 | mqtt client from https://github.com/knolleary/pubsubclient/ 6 | Arduino OTA from ESP8266 Arduino Core V2.4 7 | Adafruit CCS811 from https://github.com/adafruit/Adafruit_CCS811 8 | Sparkfun BME280 from https://github.com/sparkfun/SparkFun_CCS811_Arduino_Library 9 | Tasker from https://github.com/sticilface/Tasker 10 | 11 | Connect the sensors via I2C, (SDA -> GPIO4 / SCL -> GPIO5). Don't forget 12 | the I2C Pull-Ups. 13 | The used Adafruit CCS811 library expects I2C address 0x5A, so configure your 14 | CCS811 accordingly. 15 | !WAKE is currently not used. 16 | Serial is left open for debugging (115200). 17 | 18 | The following topics will be dropped to mqtt, all retained: 19 | 20 | status online/offline (last will) 21 | ip system ip 22 | temperature temperature in °C 23 | humidity relative humidity in % 24 | pressure pressure in hPa 25 | altitude altitude in m 26 | co2 CO2 concentration in ppm 27 | tvoc total volatile compound in ppb 28 | 29 | If you want to read Farenheit/Feet change readTempC() to readTempF() and 30 | readFloatAltitudeMeters() to readFloatAltitudeFeet(). 31 | ccs811.setEnvironmentalData() expects the temperature as °C 32 | 33 | ArduinoOTA is enabled, the mqttClientName is used as OTA name, see config. 34 | 35 | ****************************************************/ 36 | 37 | #include 38 | #include 39 | #include 40 | #include 41 | #include 42 | #include 43 | 44 | #include 45 | 46 | #include "Adafruit_CCS811.h" 47 | #include 48 | 49 | #include "config.h" 50 | 51 | // internal vars 52 | Adafruit_CCS811 ccs811; 53 | BME280 myBME280; 54 | 55 | long lastReconnectAttempt = 0; //For the non blocking mqtt reconnect (in millis) 56 | 57 | WiFiClient espClient; 58 | PubSubClient mqttClient(espClient); 59 | 60 | Tasker tasker; 61 | 62 | char mqttTopicStatus[64]; 63 | char mqttTopicIp[64]; 64 | char mqttTopicTemperatureC[64]; 65 | char mqttTopicHumidity[64]; 66 | char mqttTopicPressure[64]; 67 | char mqttTopicAltitude[64]; 68 | char mqttTopicCO2[64]; 69 | char mqttTopicTVOC[64]; 70 | 71 | float bme280TemperatureC; 72 | float bme280Humidity; 73 | float bme280Pressure; 74 | float bme280Altitude; 75 | 76 | uint16_t ccs811co2; 77 | uint16_t ccs811tvoc; 78 | 79 | void meassureEnvironment(void); 80 | 81 | void setup() { 82 | Serial.begin(115200); 83 | Serial.println("Starting up..."); 84 | 85 | pinMode(LED_BUILTIN, OUTPUT); 86 | 87 | //put in mqtt prefix 88 | sprintf(mqttTopicStatus, "%sstatus", mqttTopicPrefix); 89 | sprintf(mqttTopicIp, "%sip", mqttTopicPrefix); 90 | sprintf(mqttTopicTemperatureC, "%stemperature", mqttTopicPrefix); 91 | sprintf(mqttTopicHumidity, "%shumidity", mqttTopicPrefix); 92 | sprintf(mqttTopicPressure, "%spressure", mqttTopicPrefix); 93 | sprintf(mqttTopicAltitude, "%saltitude", mqttTopicPrefix); 94 | sprintf(mqttTopicCO2, "%sco2", mqttTopicPrefix); 95 | sprintf(mqttTopicTVOC, "%stvoc", mqttTopicPrefix); 96 | 97 | 98 | if (!ccs811.begin()) { 99 | Serial.println("CCS811 Initialization failed."); 100 | while (1); 101 | } 102 | 103 | //wait for ccs811 to be available 104 | while(!ccs811.available()); 105 | 106 | //Initialize BME280 107 | myBME280.settings.commInterface = I2C_MODE; 108 | myBME280.settings.I2CAddress = 0x77; 109 | myBME280.settings.runMode = 3; //Normal mode 110 | myBME280.settings.tStandby = 0; 111 | myBME280.settings.filter = 4; 112 | myBME280.settings.tempOverSample = 5; 113 | myBME280.settings.pressOverSample = 5; 114 | myBME280.settings.humidOverSample = 5; 115 | 116 | //Calling .begin() causes the settings to be loaded 117 | delay(10); //Make sure sensor had enough time to turn on. BME280 requires 2ms to start up. 118 | 119 | if (!myBME280.begin()) { 120 | Serial.println("Could not find a valid BME280 sensor, check wiring!"); 121 | while (1); 122 | } 123 | 124 | setup_wifi(); 125 | mqttClient.setServer(mqttServer, 1883); 126 | 127 | //----------- OTA 128 | ArduinoOTA.setHostname(mqttClientName); 129 | 130 | ArduinoOTA.onStart([]() { 131 | String type; 132 | if (ArduinoOTA.getCommand() == U_FLASH) { 133 | type = "sketch"; 134 | } else { // U_SPIFFS 135 | type = "filesystem"; 136 | } 137 | // NOTE: if updating SPIFFS this would be the place to unmount SPIFFS using SPIFFS.end() 138 | Serial.println("Start updating " + type); 139 | }); 140 | 141 | ArduinoOTA.onEnd([]() { 142 | Serial.println("\nEnd"); 143 | delay(1000); 144 | ESP.restart(); 145 | }); 146 | 147 | ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) { 148 | Serial.printf("Progress: %u%%\r", (progress / (total / 100))); 149 | }); 150 | 151 | ArduinoOTA.onError([](ota_error_t error) { 152 | Serial.printf("Error[%u]: ", error); 153 | if (error == OTA_AUTH_ERROR) Serial.println("Auth Failed"); 154 | else if (error == OTA_BEGIN_ERROR) Serial.println("Begin Failed"); 155 | else if (error == OTA_CONNECT_ERROR) Serial.println("Connect Failed"); 156 | else if (error == OTA_RECEIVE_ERROR) Serial.println("Receive Failed"); 157 | else if (error == OTA_END_ERROR) Serial.println("End Failed"); 158 | }); 159 | ArduinoOTA.begin(); 160 | 161 | 162 | 163 | Serial.println("ready..."); 164 | digitalWrite(LED_BUILTIN, LOW); 165 | delay(500); 166 | //ready, blink the led twice 167 | for (uint8_t i = 0; i < 5; i++ ) { 168 | digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN)); 169 | delay(200); 170 | } 171 | 172 | //measure every ms - GO! 173 | tasker.setInterval(meassureEnvironment, measureDelay); 174 | 175 | } 176 | 177 | void loop() { 178 | 179 | //handle mqtt connection, non-blocking 180 | if (!mqttClient.connected()) { 181 | long now = millis(); 182 | if (now - lastReconnectAttempt > 5000) { 183 | lastReconnectAttempt = now; 184 | // Attempt to reconnect 185 | if (MqttReconnect()) { 186 | lastReconnectAttempt = 0; 187 | } 188 | } 189 | } 190 | 191 | mqttClient.loop(); 192 | tasker.loop(); 193 | 194 | //handle OTA 195 | ArduinoOTA.handle(); 196 | 197 | 198 | } 199 | 200 | void meassureEnvironment(void) { 201 | bme280TemperatureC = myBME280.readTempC();; 202 | bme280Humidity = myBME280.readFloatHumidity(); 203 | bme280Pressure = myBME280.readFloatPressure(); 204 | bme280Altitude = myBME280.readFloatAltitudeMeters(); 205 | 206 | Serial.print("Temperature = "); 207 | Serial.print(bme280TemperatureC); 208 | Serial.println(" *C"); 209 | 210 | Serial.print("Pressure = "); 211 | Serial.print(bme280Pressure); 212 | Serial.println(" hPa"); 213 | 214 | Serial.print("Approx. Altitude = "); 215 | Serial.print(bme280Altitude); 216 | Serial.println(" m"); 217 | 218 | Serial.print("Humidity = "); 219 | Serial.print(bme280Humidity); 220 | Serial.println(" %"); 221 | 222 | 223 | ccs811.setEnvironmentalData(bme280Humidity, bme280TemperatureC); 224 | //wait for ccs811 reading 225 | while(ccs811.readData()); 226 | 227 | ccs811co2 = ccs811.geteCO2(); 228 | ccs811tvoc = ccs811.getTVOC(); 229 | 230 | Serial.print("CO2 concentration : "); 231 | Serial.print(ccs811co2); 232 | Serial.println(" ppm"); 233 | 234 | Serial.print("TVOC concentration : "); 235 | Serial.print(ccs811tvoc); 236 | Serial.println(" ppb"); 237 | 238 | 239 | char buf[8] = ""; 240 | sprintf(buf, "%f", bme280TemperatureC); 241 | mqttClient.publish(mqttTopicTemperatureC, buf, true); 242 | 243 | sprintf(buf, "%f", bme280Humidity); 244 | mqttClient.publish(mqttTopicHumidity, buf, true); 245 | 246 | sprintf(buf, "%f", bme280Pressure); 247 | mqttClient.publish(mqttTopicPressure, buf, true); 248 | 249 | sprintf(buf, "%f", bme280Altitude); 250 | mqttClient.publish(mqttTopicAltitude, buf, true); 251 | 252 | sprintf(buf, "%d", ccs811co2); 253 | mqttClient.publish(mqttTopicCO2, buf, true); 254 | 255 | sprintf(buf, "%d", ccs811tvoc); 256 | mqttClient.publish(mqttTopicTVOC, buf, true); 257 | 258 | 259 | Serial.println(); 260 | } 261 | 262 | 263 | void setup_wifi() { 264 | 265 | delay(10); 266 | 267 | Serial.println(); 268 | Serial.print("Connecting to "); 269 | Serial.println(ssid); 270 | 271 | WiFi.mode(WIFI_STA); //disable AP mode, only station 272 | WiFi.hostname(mqttClientName); 273 | WiFi.begin(ssid, password); 274 | 275 | while (WiFi.status() != WL_CONNECTED) { 276 | delay(500); 277 | Serial.print("."); 278 | } 279 | 280 | Serial.println(""); 281 | Serial.println("WiFi connected"); 282 | Serial.println("IP address: "); 283 | Serial.println(WiFi.localIP()); 284 | } 285 | 286 | 287 | bool MqttReconnect() { 288 | 289 | if (!mqttClient.connected()) { 290 | 291 | Serial.print("Attempting MQTT connection..."); 292 | 293 | // Attempt to connect with last will retained 294 | if (mqttClient.connect(mqttClientName, mqttUser, mqttPass, mqttTopicStatus, 1, true, "offline")) { 295 | 296 | Serial.println("connected"); 297 | 298 | // Once connected, publish an announcement... 299 | char curIp[16]; 300 | sprintf(curIp, "%d.%d.%d.%d", WiFi.localIP()[0], WiFi.localIP()[1], WiFi.localIP()[2], WiFi.localIP()[3]); 301 | 302 | mqttClient.publish(mqttTopicStatus, "online", true); 303 | mqttClient.publish(mqttTopicIp, curIp, true); 304 | 305 | } else { 306 | Serial.print("failed, rc="); 307 | Serial.print(mqttClient.state()); 308 | Serial.println(" try again in 5 seconds"); 309 | } 310 | } 311 | return mqttClient.connected(); 312 | } 313 | 314 | 315 | --------------------------------------------------------------------------------