Created
March 23, 2019 23:10
-
-
Save Dvorson/6d6195b5fb54deb6c25b9f369fa26bad 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 <Wire.h> | |
#include <WiFi.h> | |
#include <SparkFunBME280.h> | |
#include <SparkFunCCS811.h> | |
#include "Adafruit_Si7021.h" | |
#include <MQTT.h> | |
#define CCS811_ADDR 0x5A | |
#define BMP280_ADDR 0x76 | |
#define SI2071_ADDR 0x40 | |
#define PUB_INTERVAL 30000 | |
CCS811 myCCS811(CCS811_ADDR); | |
BME280 myBMP280; | |
Adafruit_Si7021 mySi7021 = Adafruit_Si7021(); | |
WiFiClient net; | |
MQTTClient client; | |
float BMPtempC; | |
float BMPpressure; | |
float Si7021humidity; | |
float Si7021temperature; | |
int CO2; | |
int TVOC; | |
float altitude; | |
long lastPublishMillis = 0; | |
void setup() { | |
Serial.begin(115200); | |
Wire.begin(16,17); | |
CCS811Core::status returnCode = myCCS811.begin(); | |
printDriverError( returnCode ); | |
myBMP280.begin(); | |
delay(10); | |
if (!mySi7021.begin(16,17)) { | |
Serial.println("Did not find Si7021 sensor!"); | |
while (true); | |
} | |
WiFi.begin("*", "*"); | |
while (WiFi.status() != WL_CONNECTED) { | |
delay(500); | |
} | |
client.begin("192.168.1.125", net); | |
while (!client.connect("arduino")) { | |
delay(1000); | |
} | |
} | |
void loop() { | |
client.loop(); | |
delay(10); | |
if (myCCS811.dataAvailable()) { | |
BMPtempC = myBMP280.readTempC(); | |
Si7021humidity = mySi7021.readHumidity(); | |
Si7021temperature = mySi7021.readTemperature(); | |
myCCS811.setEnvironmentalData(Si7021humidity, Si7021temperature); | |
myCCS811.readAlgorithmResults(); | |
CO2 = myCCS811.getCO2(); | |
TVOC = myCCS811.getTVOC(); | |
BMPpressure = myBMP280.readFloatPressure() * 0.00750062; | |
altitude = myBMP280.readFloatAltitudeMeters(); | |
if (lastPublishMillis + PUB_INTERVAL < millis()) { | |
lastPublishMillis = millis(); | |
printInfo(); | |
// publish(); | |
} | |
} else if (myCCS811.checkForStatusError()) { | |
printSensorError(); | |
} | |
} | |
void printInfo() { | |
Serial.println("CO2: " + String(CO2) + "ppm "); | |
Serial.println("TVOC: " + String(TVOC) + "ppb "); | |
Serial.println("Tmp1: " + String(BMPtempC, 2) + "C"); | |
Serial.println("Tmp1: " + String(Si7021temperature, 2) + "C"); | |
Serial.println("Prsr: " + String(BMPpressure, 1) + "mmHg"); | |
Serial.println("Alt: " + String(altitude, 2) + "m"); | |
Serial.println("Hmd: " + String(Si7021humidity, 2) + " %"); | |
} | |
void printDriverError( CCS811Core::status errorCode ) { | |
switch ( errorCode ) { | |
case CCS811Core::SENSOR_SUCCESS: | |
Serial.println("SUCCESS"); | |
break; | |
case CCS811Core::SENSOR_ID_ERROR: | |
Serial.println("ID_ERROR"); | |
break; | |
case CCS811Core::SENSOR_I2C_ERROR: | |
Serial.println("I2C_ERROR"); | |
break; | |
case CCS811Core::SENSOR_INTERNAL_ERROR: | |
Serial.println("INTERNAL_ERROR"); | |
break; | |
case CCS811Core::SENSOR_GENERIC_ERROR: | |
Serial.println("GENERIC_ERROR"); | |
break; | |
default: | |
Serial.println("Unspecified error."); | |
} | |
} | |
void printSensorError() { | |
uint8_t error = myCCS811.getErrorRegister(); | |
if ( error == 0xFF ) { | |
Serial.println("Failed to get"); | |
Serial.println("ERROR_ID register."); | |
} else { | |
Serial.println("Error: "); | |
if (error & 1 << 5) Serial.println("HeaterSupply"); | |
if (error & 1 << 4) Serial.println("HeaterFault"); | |
if (error & 1 << 3) Serial.println("MaxResistance"); | |
if (error & 1 << 2) Serial.println("MeasModeInvalid"); | |
if (error & 1 << 1) Serial.println("ReadRegInvalid"); | |
if (error & 1 << 0) Serial.println("MsgInvalid"); | |
} | |
} | |
void publish() { | |
Serial.println("Publishing..."); | |
client.publish("home/temperature3", String(BMPtempC)); | |
client.publish("home/temperature4", String(Si7021temperature)); | |
client.publish("home/humidity3", String(Si7021humidity)); | |
client.publish("home/CO2_2", String(CO2)); | |
client.publish("home/TVOC2", String(TVOC)); | |
client.publish("home/pressure3", String(BMPpressure)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment