From bc6f738e2d8db836b1ca0559701f18ffb425a106 Mon Sep 17 00:00:00 2001 From: Steven Date: Wed, 6 Oct 2021 03:36:26 -0400 Subject: run sensors more often plus big brain c++ lambda with explanatory comment for little brains --- Makefile | 2 +- mush.ino | 51 ++++++++++++++++++++++++++++++--------------------- 2 files changed, 31 insertions(+), 22 deletions(-) diff --git a/Makefile b/Makefile index 28fb21d..faa38e0 100644 --- a/Makefile +++ b/Makefile @@ -48,7 +48,7 @@ compile: $(ELF) $(BIN) $(ELF): $(SRC) $(HDRS) $(cli) compile --warnings all -upload: +upload: $(ELF) $(cli) upload -p "$(SERIAL_DEV)" PLAT_PATH != $(cli) compile --show-properties | sed -ne 's/^runtime.platform.path=//p' diff --git a/mush.ino b/mush.ino index 4400420..4ce6ef9 100644 --- a/mush.ino +++ b/mush.ino @@ -24,10 +24,6 @@ WiFiClient *wific = 0; #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) @@ -57,6 +53,10 @@ void writeBoth(char *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; @@ -65,19 +65,27 @@ struct SensorState void record(float r) { last_reading = r; readings.push_back(r); - if (readings.size() > 12) + if (readings.size() > readings_size) { readings.pop_front(); } }; float avg() { - return std::accumulate(readings.begin(), readings.end(), 0.0) / readings.size(); - } - + 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()); } }; +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()); } }; @@ -91,18 +99,17 @@ TVOCSensor tVOCSensor; void log_reading() { - float temp_c = temperatureSensor.last_reading; - float humidity = humiditySensor.last_reading; - float heat_index_c = dht.computeHeatIndex(temp_c, humidity, false); + float temp = temperatureSensor.avg(); + float humidity = humiditySensor.avg(); + float heat_index = dht.computeHeatIndex(temp, humidity, true); int photons = photoSensor.avg(); - uint32_t co2 = cO2Sensor.last_reading; - uint32_t tvoc = tVOCSensor.last_reading; + 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_c, humidity, heat_index_c, photons, co2, tvoc); + snprintf(buf, sizeof(buf), fmt, temp, humidity, heat_index, photons, co2, tvoc); writeBoth(buf); } @@ -127,11 +134,13 @@ void loop() wific->connect(ip, port); } - sensor_loop(); - - log_reading(); - - delay(1000); + for (int i=0; i