diff options
author | Steven <steven.vasilogianis@gmail.com> | 2021-10-06 03:36:26 -0400 |
---|---|---|
committer | Steven <steven.vasilogianis@gmail.com> | 2021-10-06 03:36:26 -0400 |
commit | bc6f738e2d8db836b1ca0559701f18ffb425a106 (patch) | |
tree | b4ed7798eabc124715f048afe33f7aa334979cb1 | |
parent | 29b96288acbb58e66699f66f947e624afae55b0f (diff) |
run sensors more often plus big brain c++ lambda with explanatory comment for little brains
-rw-r--r-- | Makefile | 2 | ||||
-rw-r--r-- | mush.ino | 51 |
2 files changed, 31 insertions, 22 deletions
@@ -48,7 +48,7 @@ compile: $(ELF) | |||
48 | $(BIN) $(ELF): $(SRC) $(HDRS) | 48 | $(BIN) $(ELF): $(SRC) $(HDRS) |
49 | $(cli) compile --warnings all | 49 | $(cli) compile --warnings all |
50 | 50 | ||
51 | upload: | 51 | upload: $(ELF) |
52 | $(cli) upload -p "$(SERIAL_DEV)" | 52 | $(cli) upload -p "$(SERIAL_DEV)" |
53 | 53 | ||
54 | PLAT_PATH != $(cli) compile --show-properties | sed -ne 's/^runtime.platform.path=//p' | 54 | PLAT_PATH != $(cli) compile --show-properties | sed -ne 's/^runtime.platform.path=//p' |
@@ -24,10 +24,6 @@ WiFiClient *wific = 0; | |||
24 | #define DHTTYPE DHT11 | 24 | #define DHTTYPE DHT11 |
25 | DHT dht(DHTPIN, DHTTYPE); | 25 | DHT dht(DHTPIN, DHTTYPE); |
26 | 26 | ||
27 | /* | ||
28 | * IIC address default 0x5A, the address becomes 0x5B if the ADDR_SEL is soldered. | ||
29 | */ | ||
30 | //CCS811 sensor(&Wire, /*IIC_ADDRESS=*/0x5A); | ||
31 | CCS811 sensor; | 27 | CCS811 sensor; |
32 | 28 | ||
33 | void setup(void) | 29 | void setup(void) |
@@ -57,6 +53,10 @@ void writeBoth(char *buf) | |||
57 | } | 53 | } |
58 | } | 54 | } |
59 | 55 | ||
56 | const int readings_per_second = 3; | ||
57 | const int seconds_to_average = 5; | ||
58 | const size_t readings_size = seconds_to_average * readings_per_second; | ||
59 | |||
60 | struct SensorState | 60 | struct SensorState |
61 | { | 61 | { |
62 | float last_reading; | 62 | float last_reading; |
@@ -65,19 +65,27 @@ struct SensorState | |||
65 | void record(float r) { | 65 | void record(float r) { |
66 | last_reading = r; | 66 | last_reading = r; |
67 | readings.push_back(r); | 67 | readings.push_back(r); |
68 | if (readings.size() > 12) | 68 | if (readings.size() > readings_size) |
69 | { | 69 | { |
70 | readings.pop_front(); | 70 | readings.pop_front(); |
71 | } | 71 | } |
72 | }; | 72 | }; |
73 | float avg() | 73 | float avg() |
74 | { | 74 | { |
75 | return std::accumulate(readings.begin(), readings.end(), 0.0) / readings.size(); | 75 | int n = 0; |
76 | } | 76 | float total = std::accumulate(readings.begin(), // float total = 0; |
77 | 77 | readings.end(), // for (auto p = readings.begin(); p != readings.end(); ++p) | |
78 | 0.0, // { | ||
79 | [&n](float sum, int x) { // if (!isnan(*p)) { | ||
80 | if (isnan(x)) return sum; ++n; return sum + x; // total += *p; | ||
81 | }); // ++n; | ||
82 | // } | ||
83 | // } | ||
84 | return n ? total / n : NAN; | ||
85 | }; | ||
78 | }; | 86 | }; |
79 | 87 | ||
80 | struct TemperatureSensor : public SensorState { virtual void sense() { record(dht.readTemperature()); } }; | 88 | struct TemperatureSensor : public SensorState { virtual void sense() { record(dht.readTemperature(true)); } }; |
81 | struct HumiditySensor : public SensorState { virtual void sense() { record(dht.readHumidity()); } }; | 89 | struct HumiditySensor : public SensorState { virtual void sense() { record(dht.readHumidity()); } }; |
82 | struct PhotoSensor : public SensorState { virtual void sense() { record(analogRead(PHOTORESISTOR)); } }; | 90 | struct PhotoSensor : public SensorState { virtual void sense() { record(analogRead(PHOTORESISTOR)); } }; |
83 | struct CO2Sensor : public SensorState { virtual void sense() { record(sensor.getCO2PPM()); } }; | 91 | struct CO2Sensor : public SensorState { virtual void sense() { record(sensor.getCO2PPM()); } }; |
@@ -91,18 +99,17 @@ TVOCSensor tVOCSensor; | |||
91 | 99 | ||
92 | void log_reading() | 100 | void log_reading() |
93 | { | 101 | { |
94 | float temp_c = temperatureSensor.last_reading; | 102 | float temp = temperatureSensor.avg(); |
95 | float humidity = humiditySensor.last_reading; | 103 | float humidity = humiditySensor.avg(); |
96 | float heat_index_c = dht.computeHeatIndex(temp_c, humidity, false); | 104 | float heat_index = dht.computeHeatIndex(temp, humidity, true); |
97 | int photons = photoSensor.avg(); | 105 | int photons = photoSensor.avg(); |
98 | uint32_t co2 = cO2Sensor.last_reading; | 106 | uint32_t co2 = cO2Sensor.avg(); |
99 | uint32_t tvoc = tVOCSensor.last_reading; | 107 | uint32_t tvoc = tVOCSensor.avg(); |
100 | 108 | ||
101 | auto fmt = "T: %.2f H: %.2f HI: %.2f Light: %d CO2: %u TVOC: %u\r\n"; | 109 | auto fmt = "T: %.2f H: %.2f HI: %.2f Light: %d CO2: %u TVOC: %u\r\n"; |
102 | char buf[500]; | 110 | char buf[500]; |
103 | 111 | ||
104 | 112 | snprintf(buf, sizeof(buf), fmt, temp, humidity, heat_index, photons, co2, tvoc); | |
105 | snprintf(buf, sizeof(buf), fmt, temp_c, humidity, heat_index_c, photons, co2, tvoc); | ||
106 | writeBoth(buf); | 113 | writeBoth(buf); |
107 | } | 114 | } |
108 | 115 | ||
@@ -127,11 +134,13 @@ void loop() | |||
127 | wific->connect(ip, port); | 134 | wific->connect(ip, port); |
128 | } | 135 | } |
129 | 136 | ||
130 | sensor_loop(); | 137 | for (int i=0; i<readings_per_second; ++i) |
131 | 138 | { | |
132 | log_reading(); | 139 | sensor_loop(); |
133 | 140 | if (i == readings_per_second - 1) | |
134 | delay(1000); | 141 | log_reading(); |
142 | delay(1000 / readings_per_second); | ||
143 | } | ||
135 | } | 144 | } |
136 | 145 | ||
137 | 146 | ||