#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); /* * IIC address default 0x5A, the address becomes 0x5B if the ADDR_SEL is soldered. */ //CCS811 sensor(&Wire, /*IIC_ADDRESS=*/0x5A); 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() != 0){ Serial.println("failed to init chip, please check if the chip connection is fine"); delay(1000); } /** * @brief Set measurement cycle * @param cycle:in typedef enum{ * eClosed, //Idle (Measurements are disabled in this mode) * eCycle_1s, //Constant power mode, IAQ measurement every second * Ecycle_10s, //Pulse heating mode IAQ measurement every 10 seconds * eCycle_60s, //Low power pulse heating mode IAQ measurement every 60 seconds * eCycle_250ms //Constant power mode, sensor measurement every 250ms * }eCycle_t; */ sensor.setMeasCycle(sensor.eCycle_250ms); } void log_reading ( float temp_c, float humidity, float heat_index_c, int photons, uint16_t co2, uint16_t tvoc ) { auto fmt = "T: %.2f H: %.2f HI: %.2f Light: %d CO2: %d TVOC: %d\r\n"; char buf[500]; snprintf(buf, sizeof(buf), fmt, temp_c, humidity, heat_index_c, photons, co2, tvoc); //Serial.printf(fmt, temp_c, humidity, heat_index_c); Serial.print(buf); if (wific->connected()) { wific->write(buf); } } void loop() { uint16_t co2, tvoc; delay(1000); if(sensor.checkDataReady() == true){ co2 = sensor.getCO2PPM(); tvoc = sensor.getTVOCPPB(); // Serial.print("CO2: "); // Serial.print(sensor.getCO2PPM()); // Serial.print("ppm, TVOC: "); // Serial.print(sensor.getTVOCPPB()); // Serial.println("ppb"); } else { Serial.println("Data is not ready!"); } /*! * @brief Set baseline * @param get from getBaseline.ino */ sensor.writeBaseLine(0x847B); //delay cannot be less than measurement cycle //delay(1000); ArduinoOTA.handle(); auto ip = IPAddress(192,168,1,2); auto port = 3141; if (!wific->connected()) { Serial.println("Attempting to connect"); wific->connect(ip, port); } // Wait a few seconds between measurements. float hum = dht.readHumidity(); float temp = dht.readTemperature(); // Check if any reads failed and exit early (to try again). if (isnan(hum) || isnan(temp) ) { Serial.println(F("Failed to read from DHT sensor!")); return; } float hic = dht.computeHeatIndex(temp, hum, false); int photons = analogRead(PHOTORESISTOR); log_reading(temp, hum, hic, photons, co2, tvoc); }