split project a bit
This commit is contained in:
parent
590d4958c2
commit
54285dd721
23
include/CCS811Sensor.h
Normal file
23
include/CCS811Sensor.h
Normal file
@ -0,0 +1,23 @@
|
||||
// CCS811Sensor.h
|
||||
|
||||
#ifndef CCS811SENSOR_H
|
||||
#define CCS811SENSOR_H
|
||||
#define CCS811_WAK 23
|
||||
|
||||
#include "ccs811.h" // Include CCS811 library
|
||||
|
||||
class CCS811Sensor {
|
||||
public:
|
||||
CCS811Sensor(uint8_t wakePin = CCS811_WAK); // Constructor
|
||||
void init(double* temperature, double* humidity); // temp and hum needed to correct env
|
||||
void init(); // Initialize without temperature and humidity
|
||||
void read_values(uint16_t* eco2, uint16_t* etvoc, uint16_t* errstat, uint16_t* raw);
|
||||
|
||||
private:
|
||||
uint8_t _wakePin; // CCS811 wake pin
|
||||
CCS811 ccs811; // CCS811 object
|
||||
double* _temperaturePtr; // Pointer to temperature value
|
||||
double* _humidityPtr; // Pointer to humidity value
|
||||
};
|
||||
|
||||
#endif // CCS811SENSOR_H
|
20
include/HDC1080Sensor.h
Normal file
20
include/HDC1080Sensor.h
Normal file
@ -0,0 +1,20 @@
|
||||
// HDC1080Sensor.h
|
||||
|
||||
#ifndef HDC1080SENSOR_H
|
||||
#define HDC1080SENSOR_H
|
||||
#define HDC1080_ADDR 0x40
|
||||
|
||||
#include "ClosedCube_HDC1080.h"
|
||||
|
||||
class HDC1080Sensor {
|
||||
public:
|
||||
HDC1080Sensor(uint8_t address = HDC1080_ADDR); // Constructor
|
||||
void init(); // Initialize HDC1080 sensor
|
||||
void read_values(double* temperature, double* humidity, bool* success); // Read temperature and humidity values
|
||||
|
||||
private:
|
||||
uint8_t _address; // HDC1080 I2C address
|
||||
ClosedCube_HDC1080 hdc1080; // HDC1080 object
|
||||
};
|
||||
|
||||
#endif // HDC1080SENSOR_H
|
112
src/main.cpp
112
src/main.cpp
@ -1,26 +1,8 @@
|
||||
/***************************************************
|
||||
|
||||
Wemos D1 mini or NodeMCU 1.0
|
||||
VCC - 3.3V
|
||||
GND - G
|
||||
SCL - D1 -- GPIO 5
|
||||
SDA - D2 -- GPIO 4
|
||||
WAK - D3 -- GPIO 0
|
||||
|
||||
ESP32
|
||||
VCC - 3.3V
|
||||
GND - G
|
||||
SCL - 19
|
||||
SDA - 18
|
||||
WAK - 23
|
||||
|
||||
****************************************************/
|
||||
|
||||
#include "ESPAsyncWebServer.h"
|
||||
#include <WiFi.h>
|
||||
#include <Wire.h>
|
||||
#include "ClosedCube_HDC1080.h" // HDC1080 library - https://github.com/closedcube/ClosedCube_HDC1080_Arduino // 14.04.2019
|
||||
#include "ccs811.h" // CCS811 library - https://github.com/maarten-pennings/CCS811 // 13.03.2020
|
||||
#include "HDC1080Sensor.h"
|
||||
#include "CCS811Sensor.h"
|
||||
#include <time.h>
|
||||
//#include "src/ESPinfluxdb.h" // https://github.com/hwwong/ESP_influxdb // 14.04.2019
|
||||
#include "http_static.h" // HTTP pages and JSON request templates
|
||||
@ -29,12 +11,6 @@
|
||||
// DeepSleep time – send data every 60 seconds
|
||||
const int sleepTimeS = 60;
|
||||
|
||||
//Global sensor objects
|
||||
#define CCS811_WAK 23
|
||||
|
||||
CCS811 ccs811(CCS811_WAK);
|
||||
ClosedCube_HDC1080 hdc1080;
|
||||
|
||||
// WiFi Config
|
||||
#define WiFi_SSID "Ischtar"
|
||||
#define WiFi_Password "highfive"
|
||||
@ -43,14 +19,19 @@ ClosedCube_HDC1080 hdc1080;
|
||||
const char* ntpServer = "pool.ntp.org";
|
||||
const long gmtOffset_sec = 0;
|
||||
const int daylightOffset_sec = 3600;
|
||||
struct tm timeinfo;
|
||||
|
||||
// Create AsyncWebServer object on port 80
|
||||
AsyncWebServer server(80);
|
||||
|
||||
// Globals for HDC1080
|
||||
HDC1080Sensor HDC1080_sensors;
|
||||
double hdc1080_temp, hdc1080_humidity;
|
||||
bool hdc1080_err;
|
||||
|
||||
// Globals for CCS811
|
||||
CCS811Sensor CCS811_sensors;
|
||||
uint16_t eco2, etvoc, errstat, raw;
|
||||
// Globals for timestamp
|
||||
struct tm timeinfo;
|
||||
|
||||
// loop cycle
|
||||
#define workCycle 60 //seconds
|
||||
@ -58,25 +39,23 @@ struct tm timeinfo;
|
||||
|
||||
String readHDC1080Temperature() {
|
||||
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
|
||||
float t = hdc1080.readTemperature();
|
||||
if (isnan(t)) {
|
||||
if (isnan(hdc1080_temp)) {
|
||||
Serial.println("Failed to read from HDC1080 sensor!");
|
||||
return "--";
|
||||
}
|
||||
else {
|
||||
return String(t);
|
||||
return String(hdc1080_temp);
|
||||
}
|
||||
}
|
||||
|
||||
String readHDC1080Humidity() {
|
||||
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
|
||||
float h = hdc1080.readHumidity();
|
||||
if (isnan(h)) {
|
||||
if (isnan(hdc1080_humidity)) {
|
||||
Serial.println("Failed to read from HDC1080 sensor!");
|
||||
return "--";
|
||||
}
|
||||
else {
|
||||
return String(h);
|
||||
return String(hdc1080_humidity);
|
||||
}
|
||||
}
|
||||
|
||||
@ -88,19 +67,25 @@ String processCCS811Error(err_t errstat) {
|
||||
Serial.println("CCS811: I2C error");
|
||||
return "i2c error";
|
||||
} else {
|
||||
Serial.print("CCS811: errstat="); Serial.print(errstat, HEX);
|
||||
Serial.print("="); Serial.println( ccs811.errstat_str(errstat) );
|
||||
return "<span color='red' title='" + String(ccs811.errstat_str(errstat)) + "'>CCS811 sensor error</span>";
|
||||
// Serial.print("CCS811: errstat="); Serial.print(errstat, HEX);
|
||||
// Serial.print("="); Serial.println( ccs811.errstat_str(errstat) );
|
||||
return "error";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
String readCCS811TVOC() {
|
||||
if( errstat==CCS811_ERRSTAT_OK ){
|
||||
return String(etvoc);
|
||||
} else {
|
||||
return processCCS811Error(errstat);
|
||||
}
|
||||
}
|
||||
String readCCS811ECO2() {
|
||||
if( errstat==CCS811_ERRSTAT_OK ){
|
||||
return String(eco2);
|
||||
} else {
|
||||
return processCCS811Error(errstat);
|
||||
}
|
||||
}
|
||||
|
||||
String formatISO8601() {
|
||||
@ -171,29 +156,14 @@ void setup()
|
||||
// Config NTP
|
||||
configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);
|
||||
|
||||
// hdc1080 info
|
||||
hdc1080.begin(0x40);
|
||||
Serial.print("Manufacturer ID=0x");
|
||||
Serial.println(hdc1080.readManufacturerId(), HEX); // 0x5449 ID of Texas Instruments
|
||||
Serial.print("Device ID=0x");
|
||||
Serial.println(hdc1080.readDeviceId(), HEX); // 0x1050 ID of the device
|
||||
|
||||
// i2c
|
||||
Wire.begin();
|
||||
|
||||
Serial.println("CCS811 test");
|
||||
// Enable CCS811
|
||||
bool ok = ccs811.begin();
|
||||
if ( !ok ) Serial.println("setup: CCS811 begin FAILED");
|
||||
// humidity and temperature
|
||||
HDC1080_sensors.init();
|
||||
// eCO2 and eTVOC, temp and humidity needed to adjust values
|
||||
CCS811_sensors.init(&hdc1080_temp, &hdc1080_humidity);
|
||||
|
||||
// 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("init: CCS811 start FAILED");
|
||||
|
||||
// Pages and JSONs
|
||||
server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){
|
||||
@ -232,27 +202,17 @@ void loop()
|
||||
Serial.println("Failed to obtain time");
|
||||
return;
|
||||
}
|
||||
|
||||
Serial.print(&timeinfo, "[%M-%d-%Y--%H:%M:%S]: ");
|
||||
|
||||
HDC1080_sensors.read_values(&hdc1080_temp, &hdc1080_humidity, &hdc1080_err);
|
||||
CCS811_sensors.read_values(&eco2, &etvoc, &errstat, &raw);
|
||||
|
||||
Serial.print("H="); Serial.print(readHDC1080Temperature()); Serial.print(" °C ");
|
||||
Serial.print("T="); Serial.print(readHDC1080Temperature()); Serial.print(" % ");
|
||||
// Read CCS811
|
||||
ccs811.read(&eco2,&etvoc,&errstat,&raw);
|
||||
|
||||
// Process CCS811
|
||||
if( errstat==CCS811_ERRSTAT_OK ) {
|
||||
Serial.print("eco2="); Serial.print(eco2); Serial.print(" ppm ");
|
||||
Serial.print("etvoc="); Serial.print(etvoc); Serial.print(" ppb ");
|
||||
} else if( errstat==CCS811_ERRSTAT_OK_NODATA ) {
|
||||
Serial.print("waiting for (new) data");
|
||||
} else if( errstat & CCS811_ERRSTAT_I2CFAIL ) {
|
||||
Serial.print("I2C error");
|
||||
} else {
|
||||
Serial.print( "error: " );
|
||||
Serial.print( ccs811.errstat_str(errstat) );
|
||||
}
|
||||
Serial.println();
|
||||
|
||||
Serial.print("T="); Serial.print(readHDC1080Humidity()); Serial.print(" % ");
|
||||
Serial.print("eco2="); Serial.print(readCCS811ECO2()); Serial.print(" ppm ");
|
||||
Serial.print("etvoc="); Serial.print(readCCS811TVOC()); Serial.print(" ppb ");
|
||||
Serial.println("");
|
||||
// Wait
|
||||
delay(workCycle*1000);
|
||||
}
|
||||
|
31
src/sensors/CCS811Sensor.cpp
Normal file
31
src/sensors/CCS811Sensor.cpp
Normal file
@ -0,0 +1,31 @@
|
||||
// CCS811Sensor.cpp
|
||||
|
||||
#include "CCS811Sensor.h"
|
||||
|
||||
CCS811Sensor::CCS811Sensor(uint8_t wakePin) : _wakePin(wakePin), ccs811(wakePin) {
|
||||
// Constructor sets the wake pin and initializes the ccs811 object
|
||||
}
|
||||
|
||||
void CCS811Sensor::init(double* temperature, double* humidity) {
|
||||
// Enable CCS811
|
||||
bool ok = ccs811.begin();
|
||||
// let's handle if not OK later
|
||||
// Lock pointers to temperature and humidity values
|
||||
ok = ccs811.start(CCS811_MODE_1SEC);
|
||||
// if( !ok ) Serial.println("setup: CCS811 start FAILED");
|
||||
_temperaturePtr = temperature;
|
||||
_humidityPtr = humidity;
|
||||
}
|
||||
|
||||
void CCS811Sensor::init() {
|
||||
bool ok = ccs811.begin();
|
||||
}
|
||||
|
||||
void CCS811Sensor::read_values(uint16_t* eco2, uint16_t* etvoc, uint16_t* errstat, uint16_t* raw) {
|
||||
if (_temperaturePtr && _humidityPtr) {
|
||||
// Set environmental data for CCS811 sensor using pointers
|
||||
ccs811.set_envdata(*_temperaturePtr, *_humidityPtr);
|
||||
}
|
||||
// Read eCO2, eTVOC, error status, and raw data values from CCS811 sensor
|
||||
ccs811.read(eco2, etvoc, errstat, raw);
|
||||
}
|
19
src/sensors/HDC1080Sensor.cpp
Normal file
19
src/sensors/HDC1080Sensor.cpp
Normal file
@ -0,0 +1,19 @@
|
||||
// HDC1080Sensor.cpp
|
||||
|
||||
#include "HDC1080Sensor.h"
|
||||
|
||||
HDC1080Sensor::HDC1080Sensor(uint8_t address) : _address(address) {
|
||||
// Constructor sets the address
|
||||
}
|
||||
|
||||
void HDC1080Sensor::init() {
|
||||
hdc1080.begin(_address); // Initialize HDC1080 sensor with the specified address
|
||||
}
|
||||
|
||||
void HDC1080Sensor::read_values(double* temperature, double* humidity, bool* success) {
|
||||
if (temperature) *temperature = hdc1080.readTemperature();
|
||||
if (humidity) *humidity = hdc1080.readHumidity();
|
||||
|
||||
// Set success flag based on whether readings were successful
|
||||
if (success) *success = !isnan(*temperature) && !isnan(*humidity);
|
||||
}
|
Loading…
Reference in New Issue
Block a user