From e4393cc86c106096e85c8c6d1fa9f1be324cda22 Mon Sep 17 00:00:00 2001 From: Steven Date: Wed, 6 Oct 2021 00:30:47 -0400 Subject: andy was here --- src/main/main.ino | 109 +++++++++++++++++++++++++++++------------------------- 1 file changed, 59 insertions(+), 50 deletions(-) diff --git a/src/main/main.ino b/src/main/main.ino index e29a174..6c2688f 100644 --- a/src/main/main.ino +++ b/src/main/main.ino @@ -40,7 +40,7 @@ void setup(void) Serial.begin(115200); /*Wait for the chip to be initialized completely, and then exit*/ - while(sensor.begin() != 0){ + while (sensor.begin()) { Serial.println("failed to init chip, please check if the chip connection is fine"); delay(1000); } @@ -57,61 +57,70 @@ void setup(void) 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); +void writeBoth(char *buf) +{ Serial.print(buf); if (wific->connected()) { wific->write(buf); } } -void loop() { - uint16_t co2, tvoc; +void log_reading (float temp_c, float humidity, 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]; - 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); + float heat_index_c = dht.computeHeatIndex(temp_c, humidity, false); + snprintf(buf, sizeof(buf), fmt, temp_c, humidity, heat_index_c, photons, co2, tvoc); + writeBoth(buf); } + +struct SensorState +{ + float last_reading; + virtual void sense() = 0; +}; + +struct TemperatureSensor : public SensorState { virtual void sense() { last_reading = dht.readTemperature(); } }; +struct HumiditySensor : public SensorState { virtual void sense() { last_reading = dht.readHumidity(); } }; +struct PhotoSensor : public SensorState { virtual void sense() { last_reading = analogRead(PHOTORESISTOR); } }; +struct CO2Sensor : public SensorState { virtual void sense() { last_reading = sensor.getCO2PPM(); } }; +struct TVOCSensor : public SensorState { virtual void sense() { last_reading = sensor.getTVOCPPB(); } }; + +TemperatureSensor temperatureSensor; +HumiditySensor humiditySensor; +PhotoSensor photoSensor; +CO2Sensor cO2Sensor; +TVOCSensor tVOCSensor; + +SensorState *sensors[] = { &temperatureSensor, &humiditySensor, &photoSensor, &cO2Sensor, &tVOCSensor }; + +void sensor_loop() +{ + for (int 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); + } + + sensor_loop(); + + log_reading(temperatureSensor.last_reading, humiditySensor.last_reading, photoSensor.last_reading, cO2Sensor.last_reading, tVOCSensor.last_reading); + + delay(1000); +} + + -- cgit v1.2.3