diff options
author | Steven <steven.vasilogianis@gmail.com> | 2021-10-06 01:46:21 -0400 |
---|---|---|
committer | Steven <steven.vasilogianis@gmail.com> | 2021-10-06 01:46:21 -0400 |
commit | 29b96288acbb58e66699f66f947e624afae55b0f (patch) | |
tree | e5488fcd2f23f2e09a87a414d9a96bf015f02199 | |
parent | 9e5442ed88d34ab6a875ff9468decf9f9f126043 (diff) |
use average of last 12 readings for photosensor
-rw-r--r-- | mush.ino | 30 |
1 files changed, 23 insertions, 7 deletions
@@ -1,3 +1,5 @@ | |||
1 | #include <list> | ||
2 | #include <numeric> | ||
1 | #include <DHT.h> | 3 | #include <DHT.h> |
2 | #include "ota.h" | 4 | #include "ota.h" |
3 | #include <WiFiClient.h> | 5 | #include <WiFiClient.h> |
@@ -59,13 +61,27 @@ struct SensorState | |||
59 | { | 61 | { |
60 | float last_reading; | 62 | float last_reading; |
61 | virtual void sense() = 0; | 63 | virtual void sense() = 0; |
64 | std::list<float> readings; | ||
65 | void record(float r) { | ||
66 | last_reading = r; | ||
67 | readings.push_back(r); | ||
68 | if (readings.size() > 12) | ||
69 | { | ||
70 | readings.pop_front(); | ||
71 | } | ||
72 | }; | ||
73 | float avg() | ||
74 | { | ||
75 | return std::accumulate(readings.begin(), readings.end(), 0.0) / readings.size(); | ||
76 | } | ||
77 | |||
62 | }; | 78 | }; |
63 | 79 | ||
64 | struct TemperatureSensor : public SensorState { virtual void sense() { last_reading = dht.readTemperature(); } }; | 80 | struct TemperatureSensor : public SensorState { virtual void sense() { record(dht.readTemperature()); } }; |
65 | struct HumiditySensor : public SensorState { virtual void sense() { last_reading = dht.readHumidity(); } }; | 81 | struct HumiditySensor : public SensorState { virtual void sense() { record(dht.readHumidity()); } }; |
66 | struct PhotoSensor : public SensorState { virtual void sense() { last_reading = analogRead(PHOTORESISTOR); } }; | 82 | struct PhotoSensor : public SensorState { virtual void sense() { record(analogRead(PHOTORESISTOR)); } }; |
67 | struct CO2Sensor : public SensorState { virtual void sense() { last_reading = sensor.getCO2PPM(); } }; | 83 | struct CO2Sensor : public SensorState { virtual void sense() { record(sensor.getCO2PPM()); } }; |
68 | struct TVOCSensor : public SensorState { virtual void sense() { last_reading = sensor.getTVOCPPB(); } }; | 84 | struct TVOCSensor : public SensorState { virtual void sense() { record(sensor.getTVOCPPB()); } }; |
69 | 85 | ||
70 | TemperatureSensor temperatureSensor; | 86 | TemperatureSensor temperatureSensor; |
71 | HumiditySensor humiditySensor; | 87 | HumiditySensor humiditySensor; |
@@ -78,7 +94,7 @@ void log_reading() | |||
78 | float temp_c = temperatureSensor.last_reading; | 94 | float temp_c = temperatureSensor.last_reading; |
79 | float humidity = humiditySensor.last_reading; | 95 | float humidity = humiditySensor.last_reading; |
80 | float heat_index_c = dht.computeHeatIndex(temp_c, humidity, false); | 96 | float heat_index_c = dht.computeHeatIndex(temp_c, humidity, false); |
81 | int photons = photoSensor.last_reading; | 97 | int photons = photoSensor.avg(); |
82 | uint32_t co2 = cO2Sensor.last_reading; | 98 | uint32_t co2 = cO2Sensor.last_reading; |
83 | uint32_t tvoc = tVOCSensor.last_reading; | 99 | uint32_t tvoc = tVOCSensor.last_reading; |
84 | 100 | ||
@@ -107,7 +123,7 @@ void loop() | |||
107 | auto ip = IPAddress(192,168,1,2); | 123 | auto ip = IPAddress(192,168,1,2); |
108 | auto port = 3141; | 124 | auto port = 3141; |
109 | if (!wific->connected()) { | 125 | if (!wific->connected()) { |
110 | Serial.println("Attempting to connect"); | 126 | //Serial.println("Attempting to connect"); |
111 | wific->connect(ip, port); | 127 | wific->connect(ip, port); |
112 | } | 128 | } |
113 | 129 | ||