#include #include #include #include "ota.h" #include #include "CCS811.h" #define TEMP_SENSOR_PIN 12 #define HUMIDITY_SENSOR_PIN 12 #define FAN_PIN 15 #define CO2_PIN 16 #define ULTRASONIC_PIN 12 #define PHOTORESISTOR 0 // pin 0 is analog #define CHECK_FREQUENCY 2 // check sensors every 2 seconds #define HUMIDITY_DESIRED 75 #define HUMIDITY_VARIATION 3 // ultrasonic turns on at (75 - 3 = 72) and off // at (75 + 3 = 78) WiFiClient *wific = 0; // Tempature + Humidity Sensor #define DHTPIN 12 #define DHTTYPE DHT11 DHT dht(DHTPIN, DHTTYPE); CCS811 sensor; void setup(void) { setupWifi((char *) hostname); setupOTA((char *) hostname); dht.begin(); // temp+humidity sensor pinMode(PHOTORESISTOR, INPUT); // photo resistor wific = new WiFiClient(); Serial.begin(115200); /*Wait for the chip to be initialized completely, and then exit*/ while (sensor.begin()) { Serial.println("failed to init chip, please check if the chip connection is fine"); delay(1000); } sensor.setMeasCycle(sensor.eCycle_250ms); } void writeBoth(char *buf) { Serial.print(buf); if (wific->connected()) { wific->write(buf); } } const int readings_per_second = 3; const int seconds_to_average = 5; const size_t readings_size = seconds_to_average * readings_per_second; struct SensorState { float last_reading; virtual void sense() = 0; std::list readings; void record(float r) { last_reading = r; readings.push_back(r); if (readings.size() > readings_size) { readings.pop_front(); } }; float avg() { int n = 0; float total = std::accumulate(readings.begin(), // float total = 0; readings.end(), // for (auto p = readings.begin(); p != readings.end(); ++p) 0.0, // { [&n](float sum, int x) { // if (!isnan(*p)) { if (isnan(x)) return sum; ++n; return sum + x; // total += *p; }); // ++n; // } // } return n ? total / n : NAN; }; }; struct TemperatureSensor : public SensorState { virtual void sense() { record(dht.readTemperature(true)); } }; struct HumiditySensor : public SensorState { virtual void sense() { record(dht.readHumidity()); } }; struct PhotoSensor : public SensorState { virtual void sense() { record(analogRead(PHOTORESISTOR)); } }; struct CO2Sensor : public SensorState { virtual void sense() { record(sensor.getCO2PPM()); } }; struct TVOCSensor : public SensorState { virtual void sense() { record(sensor.getTVOCPPB()); } }; TemperatureSensor temperatureSensor; HumiditySensor humiditySensor; PhotoSensor photoSensor; CO2Sensor cO2Sensor; TVOCSensor tVOCSensor; void log_reading() { float temp = temperatureSensor.avg(); float humidity = humiditySensor.avg(); float heat_index = dht.computeHeatIndex(temp, humidity, true); int photons = photoSensor.avg(); uint32_t co2 = cO2Sensor.avg(); uint32_t tvoc = tVOCSensor.avg(); auto fmt = "T: %.2f H: %.2f HI: %.2f Light: %d CO2: %u TVOC: %u\r\n"; char buf[500]; snprintf(buf, sizeof(buf), fmt, temp, humidity, heat_index, photons, co2, tvoc); writeBoth(buf); } void sensor_loop() { SensorState *sensors[] = { &temperatureSensor, &humiditySensor, &photoSensor, &cO2Sensor, &tVOCSensor }; for (size_t i=0; isense(); } sensor.writeBaseLine(0x847B); } void loop() { ArduinoOTA.handle(); auto ip = IPAddress(192,168,1,2); auto port = 3141; if (!wific->connected()) { //Serial.println("Attempting to connect"); wific->connect(ip, port); } for (int i=0; i